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 <chrono>
16 #include <condition_variable>
17 #include <functional>
18 #include <gtest/gtest.h>
19 #include <list>
20 #include <mutex>
21 #include <string>
22 #include <thread>
23 #include <vector>
24 
25 #include "advanced_notification_service.h"
26 #include "ans_const_define.h"
27 #include "ans_inner_errors.h"
28 #include "ans_manager_proxy.h"
29 #include "common_event_manager.h"
30 #include "common_event_support.h"
31 #include "datetime_ex.h"
32 #include "if_system_ability_manager.h"
33 #include "iservice_registry.h"
34 #include "mock_single_kv_store.h"
35 #include "notification_content.h"
36 #include "notification_helper.h"
37 #include "notification_long_text_content.h"
38 #include "notification_subscriber.h"
39 #include "system_ability_definition.h"
40 
41 namespace OHOS {
42 namespace AppExecFwk {
43 void MockSetDistributedNotificationEnabled(bool enable);
44 }  // AppExecFwk
45 }  // namespace OHOS
46 
47 using namespace testing::ext;
48 using namespace OHOS::Media;
49 
50 namespace OHOS {
51 namespace Notification {
52 namespace {
53 const std::string APP_NAME = "bundleName";
54 const std::string NOTIFICATION_LABEL_0 = "Label0";
55 const std::string NOTIFICATION_LABEL_1 = "Label1";
56 const std::string NOTIFICATION_LABEL_2 = "Label2";
57 const std::string AN_NOT_EXIST_KEY = "AN_NOT_EXIST_KEY";
58 const std::string KEY_SPLITER = "_";
59 
60 const std::string KVSTORE_APP_ID = "advanced_notification_service";
61 const std::string KVSTORE_NOTIFICATION_STORE_ID = "distributed_notification";
62 const std::string KVSTORE_PREFERENCES_STORE_ID = "distributed_preferences";
63 const std::string KVSTORE_SCREEN_STATUS_STORE_ID = "distributed_screen_status";
64 
65 constexpr int UID = 1;
66 constexpr int USER_ID = 0;
67 constexpr int CANCEL_REASON_DELETE = 2;
68 constexpr int APP_CANCEL_REASON_DELETE = 8;
69 constexpr int APP_CANCEL_ALL_REASON_DELETE = 9;
70 }  // namespace
71 
72 enum class SubscriberEventType {
73     ON_SUBSCRIBERESULT,
74     ON_UNSUBSCRIBERESULT,
75     ON_DIED,
76     ON_UPDATE,
77     ON_DND_CHANGED,
78     ON_ENABLED_NOTIFICATION_CHANGED,
79     ON_CANCELED,
80     ON_CANCELED_WITH_SORTINGMAP_AND_DELETEREASON,
81     ON_CONSUMED,
82     ON_CONSUMED_WITH_SORTINGMAP,
83     UNKNOWN,
84 };
85 
86 class SubscriberEvent {
87 public:
~SubscriberEvent()88     virtual ~SubscriberEvent()
89     {}
GetType()90     SubscriberEventType GetType()
91     {
92         return type_;
93     }
94 
95 protected:
SubscriberEvent(SubscriberEventType type)96     explicit SubscriberEvent(SubscriberEventType type) : type_(type)
97     {}
98 
99     SubscriberEventType type_;
100 };
101 
102 class OnSubscribeResultEvent : public SubscriberEvent {
103 public:
~OnSubscribeResultEvent()104     ~OnSubscribeResultEvent() override
105     {}
106 
OnSubscribeResultEvent()107     OnSubscribeResultEvent() : SubscriberEvent(SubscriberEventType::ON_SUBSCRIBERESULT)
108     {}
109 };
110 
111 class OnUnSubscribeResultEvent : public SubscriberEvent {
112 public:
OnUnSubscribeResultEvent()113     OnUnSubscribeResultEvent() : SubscriberEvent(SubscriberEventType::ON_UNSUBSCRIBERESULT)
114     {}
115 
~OnUnSubscribeResultEvent()116     ~OnUnSubscribeResultEvent() override
117     {}
118 };
119 
120 class OnDiedEvent : public SubscriberEvent {
121 public:
OnDiedEvent()122     OnDiedEvent() : SubscriberEvent(SubscriberEventType::ON_DIED)
123     {}
124 
~OnDiedEvent()125     ~OnDiedEvent() override
126     {}
127 };
128 
129 class OnUpdatedEvent : public SubscriberEvent {
130 public:
OnUpdatedEvent(const std::shared_ptr<NotificationSortingMap> & sortingMap)131     explicit OnUpdatedEvent(const std::shared_ptr<NotificationSortingMap> &sortingMap)
132         : SubscriberEvent(SubscriberEventType::ON_UPDATE), sortingMap_(sortingMap)
133     {}
134 
~OnUpdatedEvent()135     ~OnUpdatedEvent() override
136     {}
137 
GetNotificationSortingMap()138     std::shared_ptr<NotificationSortingMap> GetNotificationSortingMap()
139     {
140         return sortingMap_;
141     }
142 
143 private:
144     std::shared_ptr<NotificationSortingMap> sortingMap_;
145 };
146 
147 class OnDoNotDisturbDateChangedEvent : public SubscriberEvent {
148 public:
OnDoNotDisturbDateChangedEvent(const std::shared_ptr<NotificationDoNotDisturbDate> & date)149     explicit OnDoNotDisturbDateChangedEvent(const std::shared_ptr<NotificationDoNotDisturbDate> &date)
150         : SubscriberEvent(SubscriberEventType::ON_DND_CHANGED), date_(date)
151     {}
152 
~OnDoNotDisturbDateChangedEvent()153     ~OnDoNotDisturbDateChangedEvent() override
154     {}
155 
GetDoNotDisturbDate() const156     const std::shared_ptr<NotificationDoNotDisturbDate> &GetDoNotDisturbDate() const
157     {
158         return date_;
159     }
160 
161 private:
162     std::shared_ptr<NotificationDoNotDisturbDate> date_;
163 };
164 
165 class OnEnabledNotificationChangedEvent : public SubscriberEvent {
166 public:
OnEnabledNotificationChangedEvent(const std::shared_ptr<EnabledNotificationCallbackData> & callbackData)167     explicit OnEnabledNotificationChangedEvent(const std::shared_ptr<EnabledNotificationCallbackData> &callbackData)
168         : SubscriberEvent(SubscriberEventType::ON_ENABLED_NOTIFICATION_CHANGED), callbackData_(callbackData)
169     {}
170 
~OnEnabledNotificationChangedEvent()171     ~OnEnabledNotificationChangedEvent() override
172     {}
173 
GetEnabledNotificationCallbackData() const174     const std::shared_ptr<EnabledNotificationCallbackData> &GetEnabledNotificationCallbackData() const
175     {
176         return callbackData_;
177     }
178 
179 private:
180     std::shared_ptr<EnabledNotificationCallbackData> callbackData_;
181 };
182 
183 class OnOnCanceledEvent : public SubscriberEvent {
184 public:
OnOnCanceledEvent(const std::shared_ptr<Notification> & request)185     explicit OnOnCanceledEvent(const std::shared_ptr<Notification> &request)
186         : SubscriberEvent(SubscriberEventType::ON_CANCELED), request_(request)
187     {}
188 
~OnOnCanceledEvent()189     ~OnOnCanceledEvent() override
190     {}
191 
GetRequest()192     std::shared_ptr<Notification> GetRequest()
193     {
194         return request_;
195     }
196 
197 private:
198     std::shared_ptr<Notification> request_;
199 };
200 
201 class OnOnCanceledWithSortingMapAndDeleteReasonEvent : public SubscriberEvent {
202 public:
OnOnCanceledWithSortingMapAndDeleteReasonEvent(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap,int deleteReason)203     explicit OnOnCanceledWithSortingMapAndDeleteReasonEvent(const std::shared_ptr<Notification> &request,
204         const std::shared_ptr<NotificationSortingMap> &sortingMap, int deleteReason)
205         : SubscriberEvent(SubscriberEventType::ON_CANCELED_WITH_SORTINGMAP_AND_DELETEREASON),
206           request_(request),
207           sortingMap_(sortingMap),
208           deleteReason_(deleteReason)
209     {}
210 
~OnOnCanceledWithSortingMapAndDeleteReasonEvent()211     ~OnOnCanceledWithSortingMapAndDeleteReasonEvent() override
212     {}
213 
GetRequest()214     std::shared_ptr<Notification> GetRequest()
215     {
216         return request_;
217     }
GetSortingMap()218     std::shared_ptr<NotificationSortingMap> GetSortingMap()
219     {
220         return sortingMap_;
221     }
GetDeleteReason()222     int GetDeleteReason()
223     {
224         return deleteReason_;
225     }
226 
227 private:
228     std::shared_ptr<Notification> request_;
229     std::shared_ptr<NotificationSortingMap> sortingMap_;
230     int deleteReason_;
231 };
232 
233 class OnConsumedEvent : public SubscriberEvent {
234 public:
OnConsumedEvent(const std::shared_ptr<Notification> & request)235     explicit OnConsumedEvent(const std::shared_ptr<Notification> &request)
236         : SubscriberEvent(SubscriberEventType::ON_CONSUMED), request_(request)
237     {}
238 
~OnConsumedEvent()239     ~OnConsumedEvent() override
240     {}
241 
GetRequest()242     std::shared_ptr<Notification> GetRequest()
243     {
244         return request_;
245     }
246 
247 private:
248     std::shared_ptr<Notification> request_;
249 };
250 
251 class OnConsumedWithSortingMapEvent : public SubscriberEvent {
252 public:
OnConsumedWithSortingMapEvent(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap)253     explicit OnConsumedWithSortingMapEvent(
254         const std::shared_ptr<Notification> &request, const std::shared_ptr<NotificationSortingMap> &sortingMap)
255         : SubscriberEvent(SubscriberEventType::ON_CONSUMED_WITH_SORTINGMAP), request_(request), sortingMap_(sortingMap)
256     {
257         type_ = SubscriberEventType::ON_CONSUMED_WITH_SORTINGMAP;
258     }
259 
~OnConsumedWithSortingMapEvent()260     ~OnConsumedWithSortingMapEvent() override
261     {}
262 
GetRequest()263     std::shared_ptr<Notification> GetRequest()
264     {
265         return request_;
266     }
267 
GetSortingMap()268     std::shared_ptr<NotificationSortingMap> GetSortingMap()
269     {
270         return sortingMap_;
271     }
272 
273 private:
274     std::shared_ptr<Notification> request_;
275     std::shared_ptr<NotificationSortingMap> sortingMap_;
276 };
277 
278 class TestAnsSubscriber : public NotificationSubscriber {
279 public:
OnConnected()280     void OnConnected() override
281     {
282         std::shared_ptr<OnSubscribeResultEvent> event = std::make_shared<OnSubscribeResultEvent>();
283         std::unique_lock<std::mutex> lck(mtx_);
284         events_.push_back(event);
285     }
OnDisconnected()286     void OnDisconnected() override
287     {
288         std::shared_ptr<OnUnSubscribeResultEvent> event = std::make_shared<OnUnSubscribeResultEvent>();
289         std::unique_lock<std::mutex> lck(mtx_);
290         events_.push_back(event);
291     }
OnDied()292     void OnDied() override
293     {}
OnUpdate(const std::shared_ptr<NotificationSortingMap> & sortingMap)294     void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) override
295     {
296         std::shared_ptr<OnUpdatedEvent> event = std::make_shared<OnUpdatedEvent>(sortingMap);
297         std::unique_lock<std::mutex> lck(mtx_);
298         events_.push_back(event);
299     }
OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> & date)300     void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) override
301     {
302         std::shared_ptr<OnDoNotDisturbDateChangedEvent> event = std::make_shared<OnDoNotDisturbDateChangedEvent>(date);
303         std::unique_lock<std::mutex> lck(mtx_);
304         events_.push_back(event);
305     }
OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> & callbackData)306     void OnEnabledNotificationChanged(
307         const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) override
308     {
309         std::shared_ptr<OnEnabledNotificationChangedEvent> event =
310             std::make_shared<OnEnabledNotificationChangedEvent>(callbackData);
311         std::unique_lock<std::mutex> lck(mtx_);
312         events_.push_back(event);
313     }
OnCanceled(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap,int deleteReason)314     void OnCanceled(const std::shared_ptr<Notification> &request,
315         const std::shared_ptr<NotificationSortingMap> &sortingMap, int deleteReason) override
316     {
317         std::shared_ptr<OnOnCanceledWithSortingMapAndDeleteReasonEvent> event =
318             std::make_shared<OnOnCanceledWithSortingMapAndDeleteReasonEvent>(request, sortingMap, deleteReason);
319         std::unique_lock<std::mutex> lck(mtx_);
320         events_.push_back(event);
321     }
322 
OnConsumed(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap)323     void OnConsumed(const std::shared_ptr<Notification> &request,
324         const std::shared_ptr<NotificationSortingMap> &sortingMap) override
325     {
326         std::shared_ptr<OnConsumedWithSortingMapEvent> event =
327             std::make_shared<OnConsumedWithSortingMapEvent>(request, sortingMap);
328         std::unique_lock<std::mutex> lck(mtx_);
329         events_.push_back(event);
330     }
331 
OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> & badgeData)332     void SubscriberInstance::OnBadgeChanged(
333         const std::shared_ptr<BadgeNumberCallbackData> &badgeData) override
334     {}
335 
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)336     void OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override
337     {}
338 
OnBatchCanceled(const std::vector<std::shared_ptr<Notification>> & requestList,const std::shared_ptr<NotificationSortingMap> & sortingMap,int32_t deleteReason)339     void OnBatchCanceled(const std::vector<std::shared_ptr<Notification>>
340         &requestList, const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override
341     {}
342 
GetEvents()343     std::list<std::shared_ptr<SubscriberEvent>> GetEvents()
344     {
345         std::unique_lock<std::mutex> lck(mtx_);
346         return events_;
347     }
348 
ClearEvents()349     void ClearEvents()
350     {
351         std::unique_lock<std::mutex> lck(mtx_);
352         events_.clear();
353     }
354 
355 private:
356     std::mutex mtx_;
357     std::list<std::shared_ptr<SubscriberEvent>> events_;
358 };
359 
360 class AnsFWModuleTest : public testing::Test {
361 public:
362     static void SetUpTestCase();
363     static void TearDownTestCase();
364     void SetUp();
365     void TearDown();
SleepForFC()366     inline void SleepForFC()
367     {
368         std::this_thread::sleep_for(std::chrono::seconds(1));
369     }
370 
371     const std::string DELIMITER = "|";
372     const std::string LOCAL_DEVICE_ID = "<localDeviceId>";
373     const std::string REMOTE_DEVICE_ID = "<remoteDeviceId>";
GenerateDistributedKey(const NotificationRequest & req,const std::string & deviceId)374     inline std::string GenerateDistributedKey(const NotificationRequest &req, const std::string &deviceId)
375     {
376         return std::string()
377             .append(deviceId)
378             .append(DELIMITER)
379             .append(APP_NAME)
380             .append(DELIMITER)
381             .append(req.GetLabel())
382             .append(DELIMITER)
383             .append(ToString(req.GetNotificationId()));
384     }
385 
GetRequestInDistributedEntryList(NotificationRequest & req,std::vector<DistributedKv::Entry> & entries,DistributedKv::Entry & outEntry)386     bool GetRequestInDistributedEntryList(
387         NotificationRequest &req, std::vector<DistributedKv::Entry> &entries, DistributedKv::Entry &outEntry)
388     {
389         std::string localDistributedKey = GenerateDistributedKey(req, LOCAL_DEVICE_ID);
390         for (auto entry : entries) {
391             if (entry.key.ToString() == localDistributedKey) {
392                 outEntry = entry;
393                 return true;
394             }
395         }
396         return false;
397     }
398 
GetRequestInNotificationList(NotificationRequest & req,std::vector<std::shared_ptr<Notification>> & notificationList,std::shared_ptr<Notification> & outNotification)399     bool GetRequestInNotificationList(NotificationRequest &req,
400         std::vector<std::shared_ptr<Notification>> &notificationList, std::shared_ptr<Notification> &outNotification)
401     {
402         for (auto notification : notificationList) {
403             if (notification->GetNotificationRequest().GetNotificationId() == req.GetNotificationId() &&
404                 notification->GetNotificationRequest().GetLabel() == req.GetLabel()) {
405                 outNotification = notification;
406                 return true;
407             }
408         }
409         return false;
410     }
411 
CreateDistributedRequest(std::string label)412     NotificationRequest CreateDistributedRequest(std::string label)
413     {
414         int32_t notificationId = 1;
415         auto normalContent = std::make_shared<NotificationNormalContent>();
416         auto content = std::make_shared<NotificationContent>(normalContent);
417         NotificationRequest request(notificationId);
418         request.SetLabel(label);
419         request.SetContent(content);
420         request.SetDistributed(true);
421         std::vector<std::string> devices = {"<localDeviceType>"};
422         request.SetDevicesSupportDisplay(devices);
423         return request;
424     }
425 
PublishCommonEventScreenStatus(bool isScreenOn)426     void PublishCommonEventScreenStatus(bool isScreenOn)
427     {
428         EventFwk::Want want;
429         EventFwk::CommonEventData data;
430         if (isScreenOn) {
431             data.SetWant(want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON));
432         } else {
433             data.SetWant(want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF));
434         }
435 
436         EventFwk::CommonEventManager::PublishCommonEvent(data);
437     }
438 
SetDistributedScreenStatus(bool isScreenOn)439     void SetDistributedScreenStatus(bool isScreenOn)
440     {
441         DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID};
442         DistributedKv::StoreId storeId = {.storeId = KVSTORE_SCREEN_STATUS_STORE_ID};
443         std::shared_ptr<DistributedKv::MockSingleKvStore> pointer =
444             DistributedKv::MockSingleKvStore::GetMockKvStorePointer(appId, storeId);
445         if (pointer) {
446             DistributedKv::Key key("<remoteDeviceId>" + DELIMITER + "screen_status");
447             DistributedKv::Value value(isScreenOn ? "on" : "off");
448             pointer->Put(key, value);
449         }
450     }
451 };
452 
453 class EventParser {
454 public:
EventParser()455     EventParser()
456     {
457         waitOnSubscriber_ = 0;
458         waitOnUnSubscriber_ = 0;
459         waitOnConsumed_ = false;
460         onConsumedReq_.clear();
461         onConsumedWithSortingMapReq_.clear();
462         onConsumedWithSortingMapSor_.clear();
463         waitOnConsumedWithSortingMap_ = false;
464         waitOnCanceled_ = false;
465         onCanceledReq_.clear();
466         onCanceledWithSortingMapReq_.clear();
467         onCanceledWithSortingMapSor_.clear();
468         onCanceledWithSortingMapDelRea_.clear();
469         waitOnCanceledWithSortingMapAndDeleteReason_ = false;
470     }
471 
~EventParser()472     ~EventParser()
473     {}
474 
Parse(std::list<std::shared_ptr<SubscriberEvent>> events)475     void Parse(std::list<std::shared_ptr<SubscriberEvent>> events)
476     {
477         GTEST_LOG_(INFO) << "TestAnsSubscriber::Parse event size=" << events.size();
478         for (auto event : events) {
479             GTEST_LOG_(INFO) << "TestAnsSubscriber::Parse event type=" << static_cast<int>(event->GetType());
480             if (event->GetType() == SubscriberEventType::ON_SUBSCRIBERESULT) {
481                 waitOnSubscriber_ = true;
482             } else if (event->GetType() == SubscriberEventType::ON_CONSUMED) {
483                 std::shared_ptr<OnConsumedEvent> ev = std::static_pointer_cast<OnConsumedEvent>(event);
484                 waitOnConsumed_ = true;
485                 onConsumedReq_.push_back(ev->GetRequest());
486             } else if (event->GetType() == SubscriberEventType::ON_CONSUMED_WITH_SORTINGMAP) {
487                 std::shared_ptr<OnConsumedWithSortingMapEvent> ev =
488                     std::static_pointer_cast<OnConsumedWithSortingMapEvent>(event);
489                 waitOnConsumedWithSortingMap_ = true;
490                 onConsumedWithSortingMapReq_.push_back(ev->GetRequest());
491                 onConsumedWithSortingMapSor_.push_back(ev->GetSortingMap());
492             } else if (event->GetType() == SubscriberEventType::ON_CANCELED) {
493                 std::shared_ptr<OnOnCanceledEvent> ev = std::static_pointer_cast<OnOnCanceledEvent>(event);
494                 waitOnCanceled_ = true;
495                 onCanceledReq_.push_back(ev->GetRequest());
496             } else if (event->GetType() == SubscriberEventType::ON_CANCELED_WITH_SORTINGMAP_AND_DELETEREASON) {
497                 std::shared_ptr<OnOnCanceledWithSortingMapAndDeleteReasonEvent> ev =
498                     std::static_pointer_cast<OnOnCanceledWithSortingMapAndDeleteReasonEvent>(event);
499                 waitOnCanceledWithSortingMapAndDeleteReason_ = true;
500                 onCanceledWithSortingMapReq_.push_back(ev->GetRequest());
501                 onCanceledWithSortingMapSor_.push_back(ev->GetSortingMap());
502                 onCanceledWithSortingMapDelRea_.push_back(ev->GetDeleteReason());
503             } else if (event->GetType() == SubscriberEventType::ON_UNSUBSCRIBERESULT) {
504                 waitOnUnSubscriber_ = true;
505             }
506         }
507     }
508 
SetWaitOnConsumed(bool bl)509     void SetWaitOnConsumed(bool bl)
510     {
511         waitOnConsumed_ = bl;
512     }
513 
SetWaitOnCanceled(bool bl)514     void SetWaitOnCanceled(bool bl)
515     {
516         waitOnCanceled_ = bl;
517     }
518 
SetWaitOnCanceledWithSortingMapAndDeleteReason(bool bl)519     void SetWaitOnCanceledWithSortingMapAndDeleteReason(bool bl)
520     {
521         waitOnCanceledWithSortingMapAndDeleteReason_ = bl;
522     }
523 
SetWaitOnUnSubscriber()524     void SetWaitOnUnSubscriber()
525     {
526         waitOnUnSubscriber_ = NotificationConstant::SubscribeResult::RESOURCES_FAIL;
527     }
528 
GetWaitOnSubscriber() const529     bool GetWaitOnSubscriber() const
530     {
531         return waitOnSubscriber_;
532     }
533 
GetWaitOnUnSubscriber() const534     bool GetWaitOnUnSubscriber() const
535     {
536         return waitOnUnSubscriber_;
537     }
538 
GetWaitOnConsumed() const539     bool GetWaitOnConsumed() const
540     {
541         return waitOnConsumed_;
542     }
543 
GetOnConsumedReq() const544     std::vector<std::shared_ptr<Notification>> GetOnConsumedReq() const
545     {
546         return onConsumedReq_;
547     }
548 
GetOnConsumedWithSortingMapReq() const549     std::vector<std::shared_ptr<Notification>> GetOnConsumedWithSortingMapReq() const
550     {
551         return onConsumedWithSortingMapReq_;
552     }
553 
GetOnConsumedWithSortingMapSor() const554     std::vector<std::shared_ptr<NotificationSortingMap>> GetOnConsumedWithSortingMapSor() const
555     {
556         return onConsumedWithSortingMapSor_;
557     }
558 
GetWaitOnConsumedWithSortingMap() const559     bool GetWaitOnConsumedWithSortingMap() const
560     {
561         return waitOnConsumedWithSortingMap_;
562     }
563 
GetWaitOnCanceled() const564     bool GetWaitOnCanceled() const
565     {
566         return waitOnCanceled_;
567     }
568 
GetWaitOnCanceledWithSortingMapAndDeleteReason() const569     bool GetWaitOnCanceledWithSortingMapAndDeleteReason() const
570     {
571         return waitOnCanceledWithSortingMapAndDeleteReason_;
572     }
573 
GetOnCanceledReq() const574     std::vector<std::shared_ptr<Notification>> GetOnCanceledReq() const
575     {
576         return onCanceledReq_;
577     }
578 
GetOnCanceledWithSortingMapReq() const579     std::vector<std::shared_ptr<Notification>> GetOnCanceledWithSortingMapReq() const
580     {
581         return onCanceledWithSortingMapReq_;
582     }
583 
GetOnCanceledWithSortingMapSor() const584     std::vector<std::shared_ptr<NotificationSortingMap>> GetOnCanceledWithSortingMapSor() const
585     {
586         return onCanceledWithSortingMapSor_;
587     }
588 
GetOnCanceledWithSortingMapDelRea() const589     std::vector<int> GetOnCanceledWithSortingMapDelRea() const
590     {
591         return onCanceledWithSortingMapDelRea_;
592     }
593 
594 private:
595     bool waitOnSubscriber_ = false;
596     bool waitOnUnSubscriber_ = false;
597     bool waitOnConsumed_ = false;
598     std::vector<std::shared_ptr<Notification>> onConsumedReq_;
599     std::vector<std::shared_ptr<Notification>> onConsumedWithSortingMapReq_;
600     std::vector<std::shared_ptr<NotificationSortingMap>> onConsumedWithSortingMapSor_;
601     bool waitOnConsumedWithSortingMap_ = false;
602     bool waitOnCanceled_ = false;
603     std::vector<std::shared_ptr<Notification>> onCanceledReq_;
604     std::vector<std::shared_ptr<Notification>> onCanceledWithSortingMapReq_;
605     std::vector<std::shared_ptr<NotificationSortingMap>> onCanceledWithSortingMapSor_;
606     std::vector<int> onCanceledWithSortingMapDelRea_;
607     bool waitOnCanceledWithSortingMapAndDeleteReason_ = false;
608 };
609 
610 sptr<ISystemAbilityManager> g_systemAbilityManager =
611     SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
SetUpTestCase()612 void AnsFWModuleTest::SetUpTestCase()
613 {
614     sptr<AdvancedNotificationService> service = OHOS::Notification::AdvancedNotificationService::GetInstance();
615     OHOS::ISystemAbilityManager::SAExtraProp saExtraProp;
616     g_systemAbilityManager->AddSystemAbility(OHOS::ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID, service, saExtraProp);
617 }
618 
TearDownTestCase()619 void AnsFWModuleTest::TearDownTestCase()
620 {}
621 
SetUp()622 void AnsFWModuleTest::SetUp()
623 {}
624 
TearDown()625 void AnsFWModuleTest::TearDown()
626 {
627     NotificationHelper::CancelAllNotifications();
628     NotificationHelper::RemoveAllSlots();
629     NotificationHelper::RemoveNotifications();
630     SleepForFC();
631 }
632 
633 /**
634  *
635  * @tc.number    : ANS_FW_MT_FlowControl_00100
636  * @tc.name      : FlowControl_00100
637  * @tc.desc      : Test notification's flow control.
638  */
639 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_FlowControl_00100, Function | MediumTest | Level1)
640 {
641     TestAnsSubscriber subscriber;
642     NotificationSubscribeInfo info;
643     info.AddAppName(APP_NAME);
644     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber, info), ERR_OK);
645     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
646     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
647     for (uint32_t i = 0; i <= MAX_ACTIVE_NUM_PERSECOND; i++) {
648         std::string label = std::to_string(i);
649         NotificationRequest req(i);
650         req.SetLabel(label);
651         req.SetContent(content);
652         if (i < MAX_ACTIVE_NUM_PERSECOND) {
653             EXPECT_EQ(NotificationHelper::PublishNotification(req), ERR_OK);
654         } else {
655             EXPECT_EQ(NotificationHelper::PublishNotification(req), (int)ERR_ANS_OVER_MAX_ACTIVE_PERSECOND);
656         }
657     }
658     SleepForFC();
659     EXPECT_EQ(NotificationHelper::RemoveNotifications(), ERR_OK);
660     SleepForFC();
661     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), ERR_OK);
662     SleepForFC();
663     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
664     EventParser eventParser;
665     eventParser.Parse(events);
666     for (uint32_t i = 0; i <= MAX_ACTIVE_NUM_PERSECOND; i++) {
667         std::string notificationLabel = std::to_string(i);
668         std::string notificationIdStr = std::to_string(i);
669         int32_t notificationIdInt = i;
670         if (i < MAX_ACTIVE_NUM_PERSECOND) {
671             std::stringstream stream;
672             stream << KEY_SPLITER << USER_ID << KEY_SPLITER << UID << KEY_SPLITER
673                 << notificationLabel << KEY_SPLITER << notificationIdInt;
674             std::string notificationKey = stream.str();
675             NotificationSorting sorting;
676             EXPECT_EQ(eventParser.GetOnConsumedReq()[i]->GetLabel().c_str(), notificationLabel);
677             EXPECT_EQ(eventParser.GetOnConsumedReq()[i]->GetId(), notificationIdInt);
678             EXPECT_EQ(eventParser.GetOnConsumedReq()[i]->GetKey(), notificationKey);
679             EXPECT_EQ(eventParser.GetOnConsumedWithSortingMapReq()[i]->GetLabel().c_str(), notificationLabel);
680             EXPECT_EQ(eventParser.GetOnConsumedWithSortingMapReq()[i]->GetId(), notificationIdInt);
681             EXPECT_EQ(eventParser.GetOnConsumedWithSortingMapReq()[i]->GetKey(), notificationKey);
682             EXPECT_TRUE(
683                 eventParser.GetOnConsumedWithSortingMapSor()[i]->GetNotificationSorting(notificationKey, sorting));
684             EXPECT_EQ(sorting.GetKey().c_str(), notificationKey);
685         }
686     }
687     EXPECT_EQ((uint32_t)eventParser.GetOnConsumedReq().size(), MAX_ACTIVE_NUM_PERSECOND);
688     EXPECT_EQ((uint32_t)eventParser.GetOnConsumedWithSortingMapReq().size(), MAX_ACTIVE_NUM_PERSECOND);
689     EXPECT_TRUE(eventParser.GetWaitOnSubscriber());
690     EXPECT_TRUE(eventParser.GetWaitOnUnSubscriber());
691     subscriber.ClearEvents();
692     SleepForFC();
693 }
694 
695 /**
696  *
697  * @tc.number    : ANS_FW_MT_RemoveNotificationsByKey_00100
698  * @tc.name      : RemoveNotificationsByKey_00100
699  * @tc.desc      : Remove Notification by key.
700  */
701 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_RemoveNotificationsByKey_00100, Function | MediumTest | Level1)
702 {
703     TestAnsSubscriber subscriber;
704     NotificationSubscribeInfo info;
705     info.AddAppName("bundleName");
706     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber, info), ERR_OK);
707     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
708     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
709     NotificationRequest req(0);
710     req.SetLabel(NOTIFICATION_LABEL_0);
711     req.SetContent(content);
712     EXPECT_EQ(NotificationHelper::PublishNotification(req), ERR_OK);
713     std::vector<sptr<Notification>> notifications;
714     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
715     std::string key = notifications[0]->GetKey().c_str();
716     EXPECT_EQ(NotificationHelper::RemoveNotification(key), ERR_OK);
717     SleepForFC();
718     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), ERR_OK);
719     SleepForFC();
720     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
721     EventParser eventParser;
722     eventParser.Parse(events);
723     EXPECT_TRUE(eventParser.GetWaitOnConsumed());
724     EXPECT_TRUE(eventParser.GetWaitOnConsumedWithSortingMap());
725     EXPECT_TRUE(eventParser.GetWaitOnCanceled());
726     EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetLabel().c_str(), NOTIFICATION_LABEL_0);
727     EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetId(), 0);
728     std::stringstream stream;
729     stream << KEY_SPLITER << USER_ID << KEY_SPLITER << UID << KEY_SPLITER << NOTIFICATION_LABEL_0 << KEY_SPLITER << 0;
730     std::string notificationKey = stream.str();
731     NotificationSorting sorting;
732     EXPECT_EQ(eventParser.GetOnCanceledReq()[0]->GetKey(), notificationKey);
733     EXPECT_EQ(eventParser.GetOnCanceledWithSortingMapReq()[0]->GetKey(), notificationKey);
734     EXPECT_TRUE(eventParser.GetOnConsumedWithSortingMapSor()[0]->GetNotificationSorting(notificationKey, sorting));
735     EXPECT_EQ(sorting.GetKey().c_str(), notificationKey);
736     EXPECT_EQ(eventParser.GetOnCanceledWithSortingMapDelRea()[0], CANCEL_REASON_DELETE);
737     subscriber.ClearEvents();
738     SleepForFC();
739 }
740 
741 /**
742  *
743  * @tc.number    : ANS_FW_MT_RemoveNotificationsByKey_00200
744  * @tc.name      : RemoveNotificationsByKey_00200
745  * @tc.desc      : Remove Notification by a nonexistent key.
746  */
747 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_RemoveNotificationsByKey_00200, Function | MediumTest | Level1)
748 {
749     TestAnsSubscriber subscriber;
750     NotificationSubscribeInfo info;
751     info.AddAppName("bundleName");
752     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber, info), ERR_OK);
753     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
754     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
755     NotificationRequest req(0);
756     req.SetLabel(NOTIFICATION_LABEL_0);
757     req.SetContent(content);
758     EXPECT_EQ(NotificationHelper::PublishNotification(req), ERR_OK);
759     std::vector<sptr<Notification>> notifications;
760     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
761     EXPECT_EQ(NotificationHelper::RemoveNotification(AN_NOT_EXIST_KEY), (int)ERR_ANS_NOTIFICATION_NOT_EXISTS);
762     std::string key = notifications[0]->GetKey().c_str();
763     EXPECT_EQ(NotificationHelper::RemoveNotification(key), (int)ERR_OK);
764     SleepForFC();
765     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
766     EventParser eventParser;
767     eventParser.Parse(events);
768     EXPECT_TRUE(eventParser.GetWaitOnCanceled());
769     eventParser.SetWaitOnCanceled(false);
770     EXPECT_EQ(NotificationHelper::RemoveNotification(key), (int)ERR_ANS_NOTIFICATION_NOT_EXISTS);
771     events = subscriber.GetEvents();
772     eventParser.Parse(events);
773     EXPECT_TRUE(eventParser.GetWaitOnCanceled());
774     SleepForFC();
775     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), ERR_OK);
776     SleepForFC();
777     events = subscriber.GetEvents();
778     eventParser.Parse(events);
779     EXPECT_TRUE(eventParser.GetWaitOnConsumedWithSortingMap());
780     subscriber.ClearEvents();
781     SleepForFC();
782 }
783 
784 /**
785  *
786  * @tc.number    : ANS_FW_MT_RemoveNotifications_00100
787  * @tc.name      : RemoveNotifications_00100
788  * @tc.desc      : Remove all Notifications.
789  */
790 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_RemoveNotifications_00100, Function | MediumTest | Level1)
791 {
792     TestAnsSubscriber subscriber;
793     NotificationSubscribeInfo info;
794     info.AddAppName("bundleName");
795     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber, info), ERR_OK);
796 
797     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
798     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
799     NotificationRequest req(0);
800     req.SetLabel(NOTIFICATION_LABEL_0);
801     req.SetContent(content);
802     EXPECT_EQ(NotificationHelper::PublishNotification(req), ERR_OK);
803     SleepForFC();
804     EventParser eventParser;
805     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
806     eventParser.Parse(events);
807     EXPECT_TRUE(eventParser.GetWaitOnConsumed());
808     eventParser.SetWaitOnConsumed(false);
809 
810     NotificationRequest req1(1);
811     req1.SetLabel(NOTIFICATION_LABEL_0);
812     req1.SetContent(content);
813     events = subscriber.GetEvents();
814     EXPECT_EQ(NotificationHelper::PublishNotification(req1), ERR_OK);
815     SleepForFC();
816     eventParser.Parse(events);
817     EXPECT_TRUE(eventParser.GetWaitOnConsumed());
818     SleepForFC();
819     EXPECT_EQ(NotificationHelper::RemoveNotifications(USER_ID), ERR_OK);
820     std::vector<sptr<Notification>> notifications;
821     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
822     EXPECT_EQ((int)notifications.size(), (int)0);
823 
824     SleepForFC();
825     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), ERR_OK);
826     events = subscriber.GetEvents();
827     eventParser.Parse(events);
828     EXPECT_TRUE(eventParser.GetWaitOnCanceled());
829     EXPECT_TRUE(eventParser.GetWaitOnCanceledWithSortingMapAndDeleteReason());
830     subscriber.ClearEvents();
831     SleepForFC();
832 }
833 
834 /**
835  *
836  * @tc.number    : ANS_FW_MT_RemoveNotifications_00200
837  * @tc.name      : RemoveNotifications_00200
838  * @tc.desc      : Remove Notifications when no Notification.
839  */
840 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_RemoveNotifications_00200, Function | MediumTest | Level1)
841 {
842     EventParser eventParser;
843     TestAnsSubscriber subscriber;
844     NotificationSubscribeInfo info;
845     info.AddAppName("bundleName");
846     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber, info), ERR_OK);
847     EXPECT_EQ(NotificationHelper::RemoveNotifications(), ERR_OK);
848     SleepForFC();
849     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), ERR_OK);
850     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
851     eventParser.Parse(events);
852     EXPECT_FALSE(eventParser.GetWaitOnConsumed());
853     EXPECT_FALSE(eventParser.GetWaitOnConsumedWithSortingMap());
854     EXPECT_FALSE(eventParser.GetWaitOnCanceled());
855     EXPECT_FALSE(eventParser.GetWaitOnCanceledWithSortingMapAndDeleteReason());
856     subscriber.ClearEvents();
857     SleepForFC();
858 }
859 
860 /**
861  *
862  * @tc.number    : ANS_FW_MT_UnSubscriber_00100
863  * @tc.name      : UnSubscriber_00100
864  * @tc.desc      : Remove Subscriber.
865  */
866 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_UnSubscriber_00100, Function | MediumTest | Level1)
867 {
868     TestAnsSubscriber subscriber;
869     NotificationSubscribeInfo info;
870     info.AddAppName("ANS_FW_MT_UnSubscriber_00100");
871     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber, info), ERR_OK);
872     SleepForFC();
873     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), ERR_OK);
874     SleepForFC();
875     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
876     bool waitOnSubscriber = false;
877     bool waitOnUnSubscriber = false;
878 
879     for (auto event : events) {
880         if (event->GetType() == SubscriberEventType::ON_SUBSCRIBERESULT) {
881             waitOnSubscriber = true;
882         } else if (event->GetType() == SubscriberEventType::ON_UNSUBSCRIBERESULT) {
883             waitOnUnSubscriber = true;
884         }
885     }
886     EXPECT_TRUE(waitOnSubscriber);
887 
888     subscriber.ClearEvents();
889     SleepForFC();
890 }
891 
892 /**
893  *
894  * @tc.number    : ANS_FW_MT_UnSubscriber_00200
895  * @tc.name      : UnSubscriber_00200
896  * @tc.desc      : Remove Subscriber.
897  */
898 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_UnSubscriber_00200, Function | MediumTest | Level1)
899 {
900     EventParser eventParser;
901     TestAnsSubscriber subscriber;
902     NotificationSubscribeInfo info;
903     info.AddAppName("ANS_FW_MT_UnSubscriber_00100");
904     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber, info), ERR_OK);
905     SleepForFC();
906     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), (int)ERR_OK);
907     SleepForFC();
908     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
909     bool waitOnSubscriber = false;
910     bool waitOnUnSubscriber = false;
911     for (auto event : events) {
912         if (event->GetType() == SubscriberEventType::ON_SUBSCRIBERESULT) {
913             waitOnSubscriber = true;
914         } else if (event->GetType() == SubscriberEventType::ON_UNSUBSCRIBERESULT) {
915             waitOnUnSubscriber = true;
916         }
917     }
918     EXPECT_TRUE(waitOnSubscriber);
919     EXPECT_TRUE(waitOnUnSubscriber);
920 
921     waitOnSubscriber = false;
922     waitOnUnSubscriber = false;
923     subscriber.ClearEvents();
924     EXPECT_NE(NotificationHelper::UnSubscribeNotification(subscriber, info), (int)ERR_OK);
925     events = subscriber.GetEvents();
926     for (auto event : events) {
927         if (event->GetType() == SubscriberEventType::ON_UNSUBSCRIBERESULT) {
928             waitOnUnSubscriber = true;
929         }
930     }
931     EXPECT_FALSE(waitOnUnSubscriber);
932     subscriber.ClearEvents();
933     SleepForFC();
934 }
935 /**
936  *
937  * @tc.number    : ANS_FW_MT_Subscriber_00100
938  * @tc.name      : Subscriber_00100
939  * @tc.desc      : Subscriber Notifications.
940  */
941 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_Subscriber_00100, Function | MediumTest | Level1)
942 {
943     TestAnsSubscriber subscriber;
944     NotificationSubscribeInfo info;
945     info.AddAppName("ANS_FW_MT_UnSubscriber_00100");
946     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber, info), ERR_OK);
947     SleepForFC();
948     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
949     bool waitOnSubscriber = false;
950     bool waitOnUnSubscriber = false;
951     for (auto event : events) {
952         if (event->GetType() == SubscriberEventType::ON_SUBSCRIBERESULT) {
953             waitOnSubscriber = true;
954         } else if (event->GetType() == SubscriberEventType::ON_UNSUBSCRIBERESULT) {
955             waitOnUnSubscriber = true;
956         }
957     }
958     EXPECT_TRUE(waitOnSubscriber);
959     subscriber.ClearEvents();
960     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), ERR_OK);
961     SleepForFC();
962 }
963 
964 /**
965  *
966  * @tc.number    : ANS_FW_MT_CancelNotificationById_00100
967  * @tc.name      : CancelNotificationById_00100
968  * @tc.desc      : Cancel Notification By Id.
969  */
970 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_CancelNotificationById_00100, Function | MediumTest | Level1)
971 {
972     EventParser eventParser;
973     TestAnsSubscriber subscriber;
974     NotificationSubscribeInfo info;
975     info.AddAppName("bundleName");
976     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber, info), ERR_OK);
977     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
978     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
979     NotificationRequest req(1);
980     req.SetLabel(NOTIFICATION_LABEL_0);
981     req.SetContent(content);
982     EXPECT_EQ(NotificationHelper::PublishNotification(req), ERR_OK);
983     SleepForFC();
984     std::vector<sptr<Notification>> notifications;
985     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
986     int32_t id = notifications[0]->GetId();
987     SleepForFC();
988     EXPECT_EQ(NotificationHelper::CancelNotification(NOTIFICATION_LABEL_0, id), ERR_OK);
989     SleepForFC();
990     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), ERR_OK);
991 
992     SleepForFC();
993     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
994 
995     eventParser.Parse(events);
996     EXPECT_TRUE(eventParser.GetWaitOnConsumed());
997     EXPECT_TRUE(eventParser.GetWaitOnConsumedWithSortingMap());
998     EXPECT_TRUE(eventParser.GetWaitOnCanceled());
999     EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetLabel().c_str(), NOTIFICATION_LABEL_0);
1000     EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetId(), 1);
1001     std::stringstream stream;
1002     stream << KEY_SPLITER << USER_ID << KEY_SPLITER << UID << KEY_SPLITER << NOTIFICATION_LABEL_0 << KEY_SPLITER << 1;
1003     std::string notificationKey = stream.str();
1004     NotificationSorting sorting;
1005     EXPECT_EQ(eventParser.GetOnCanceledReq()[0]->GetKey(), notificationKey);
1006     EXPECT_EQ(eventParser.GetOnCanceledWithSortingMapReq()[0]->GetKey(), notificationKey);
1007     EXPECT_TRUE(eventParser.GetOnConsumedWithSortingMapSor()[0]->GetNotificationSorting(notificationKey, sorting));
1008     EXPECT_EQ(sorting.GetKey().c_str(), notificationKey);
1009     EXPECT_EQ(eventParser.GetOnCanceledWithSortingMapDelRea()[0], APP_CANCEL_REASON_DELETE);
1010     subscriber.ClearEvents();
1011     SleepForFC();
1012 }
1013 
1014 /**
1015  *
1016  * @tc.number    : ANS_FW_MT_CancelNotificationById_00200
1017  * @tc.name      : CancelNotificationById_00200
1018  * @tc.desc      : Cancel Notification By Id when Id is not exist.
1019  */
1020 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_CancelNotificationById_00200, Function | MediumTest | Level1)
1021 {
1022     EventParser eventParser;
1023     TestAnsSubscriber subscriber;
1024     NotificationSubscribeInfo info;
1025     info.AddAppName("bundleName");
1026     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber, info), ERR_OK);
1027 
1028     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
1029     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
1030     NotificationRequest req(1);
1031     req.SetLabel(NOTIFICATION_LABEL_0);
1032     req.SetContent(content);
1033     EXPECT_EQ(NotificationHelper::PublishNotification(req), ERR_OK);
1034     SleepForFC();
1035     std::vector<sptr<Notification>> notifications;
1036     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
1037     int32_t id = 0;
1038     SleepForFC();
1039 
1040     eventParser.SetWaitOnCanceled(false);
1041     eventParser.SetWaitOnCanceledWithSortingMapAndDeleteReason(false);
1042     EXPECT_EQ(NotificationHelper::CancelNotification(NOTIFICATION_LABEL_0, id), (int)ERR_ANS_NOTIFICATION_NOT_EXISTS);
1043     SleepForFC();
1044     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), ERR_OK);
1045 
1046     SleepForFC();
1047     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
1048     eventParser.Parse(events);
1049     EXPECT_TRUE(eventParser.GetWaitOnConsumed());
1050     EXPECT_TRUE(eventParser.GetWaitOnConsumedWithSortingMap());
1051     EXPECT_FALSE(eventParser.GetWaitOnCanceled());
1052     EXPECT_FALSE(eventParser.GetWaitOnCanceledWithSortingMapAndDeleteReason());
1053     subscriber.ClearEvents();
1054     SleepForFC();
1055 }
1056 
1057 /**
1058  *
1059  * @tc.number    : ANS_FW_MT_CancelAllNotifications_00100
1060  * @tc.name      :
1061  * @tc.desc      : Cancel all notifications.
1062  */
1063 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_CancelAllNotifications_00100, Function | MediumTest | Level1)
1064 {
1065     TestAnsSubscriber subscriber;
1066     NotificationSubscribeInfo info;
1067     info.AddAppName("bundleName");
1068     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber, info), ERR_OK);
1069 
1070     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
1071     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
1072     NotificationRequest req0(0);
1073     req0.SetLabel(NOTIFICATION_LABEL_0);
1074     req0.SetContent(content);
1075     EXPECT_EQ(NotificationHelper::PublishNotification(req0), ERR_OK);
1076 
1077     NotificationRequest req1(1);
1078     req1.SetLabel(NOTIFICATION_LABEL_1);
1079     req1.SetContent(content);
1080     EXPECT_EQ(NotificationHelper::PublishNotification(req1), ERR_OK);
1081     SleepForFC();
1082     EXPECT_EQ(NotificationHelper::CancelAllNotifications(), ERR_OK);
1083     SleepForFC();
1084     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), ERR_OK);
1085 
1086     SleepForFC();
1087     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
1088 
1089     EventParser eventParser;
1090     eventParser.Parse(events);
1091     EXPECT_TRUE(eventParser.GetWaitOnConsumed());
1092     EXPECT_TRUE(eventParser.GetWaitOnConsumedWithSortingMap());
1093     EXPECT_TRUE(eventParser.GetWaitOnCanceled());
1094     EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetLabel().c_str(), NOTIFICATION_LABEL_0);
1095     EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetId(), 0);
1096     std::stringstream stream0;
1097     stream0 << KEY_SPLITER << USER_ID << KEY_SPLITER << UID << KEY_SPLITER << NOTIFICATION_LABEL_0 << KEY_SPLITER << 0;
1098     std::string notificationKey0 = stream0.str();
1099     NotificationSorting sorting0;
1100     EXPECT_EQ(eventParser.GetOnCanceledReq()[0]->GetKey(), notificationKey0);
1101     EXPECT_EQ(eventParser.GetOnCanceledWithSortingMapReq()[0]->GetKey(), notificationKey0);
1102     EXPECT_TRUE(eventParser.GetOnConsumedWithSortingMapSor()[0]->GetNotificationSorting(notificationKey0, sorting0));
1103     EXPECT_EQ(sorting0.GetKey().c_str(), notificationKey0);
1104     EXPECT_EQ(eventParser.GetOnCanceledWithSortingMapDelRea()[0], APP_CANCEL_ALL_REASON_DELETE);
1105 
1106     EXPECT_EQ(eventParser.GetOnConsumedReq()[1]->GetLabel().c_str(), NOTIFICATION_LABEL_1);
1107     EXPECT_EQ(eventParser.GetOnConsumedReq()[1]->GetId(), 1);
1108     std::stringstream stream1;
1109     stream1 << KEY_SPLITER << USER_ID << KEY_SPLITER << UID << KEY_SPLITER << NOTIFICATION_LABEL_1 << KEY_SPLITER << 1;
1110     std::string notificationKey1 = stream1.str();
1111     NotificationSorting sorting1;
1112     EXPECT_EQ(eventParser.GetOnCanceledReq()[1]->GetKey(), notificationKey1);
1113     EXPECT_EQ(eventParser.GetOnCanceledWithSortingMapReq()[1]->GetKey(), notificationKey1);
1114     EXPECT_TRUE(eventParser.GetOnConsumedWithSortingMapSor()[1]->GetNotificationSorting(notificationKey1, sorting1));
1115     EXPECT_EQ(sorting1.GetKey().c_str(), notificationKey1);
1116     EXPECT_EQ(eventParser.GetOnCanceledWithSortingMapDelRea()[1], APP_CANCEL_ALL_REASON_DELETE);
1117 
1118     subscriber.ClearEvents();
1119     SleepForFC();
1120 }
1121 
1122 /**
1123  *
1124  * @tc.number    : ANS_FW_MT_PublishSoundNotification_00100
1125  * @tc.name      :
1126  * @tc.desc      : Publish a sound notification.
1127  */
1128 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_PublishSoundNotification_00100, Function | MediumTest | Level1)
1129 {
1130     std::vector<sptr<NotificationSlot>> slots;
1131     NotificationSlot slot1;
1132     slot1.SetType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1133     slot1.SetSound(Uri("."));
1134     slot1.SetVibrationStyle(std::vector<int64_t>(1, 1));
1135     EXPECT_EQ(NotificationHelper::AddNotificationSlot(slot1), (int)ERR_OK);
1136 
1137     TestAnsSubscriber subscriber;
1138     NotificationSubscribeInfo info;
1139     info.AddAppName("bundleName");
1140     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber, info), ERR_OK);
1141 
1142     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
1143     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
1144     NotificationRequest req(0);
1145     req.SetLabel(NOTIFICATION_LABEL_0);
1146     req.SetContent(content);
1147     req.SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1148     EXPECT_EQ(NotificationHelper::PublishNotification(req), ERR_OK);
1149     SleepForFC();
1150     EXPECT_EQ(NotificationHelper::CancelAllNotifications(), ERR_OK);
1151     SleepForFC();
1152     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), ERR_OK);
1153     SleepForFC();
1154     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
1155 
1156     EventParser eventParser;
1157     eventParser.Parse(events);
1158     EXPECT_TRUE(eventParser.GetWaitOnConsumed());
1159     EXPECT_TRUE(eventParser.GetWaitOnConsumedWithSortingMap());
1160     EXPECT_TRUE(eventParser.GetWaitOnCanceled());
1161     EXPECT_TRUE(eventParser.GetWaitOnCanceledWithSortingMapAndDeleteReason());
1162     subscriber.ClearEvents();
1163     SleepForFC();
1164 }
1165 
1166 /**
1167  *
1168  * @tc.number    : ANS_FW_MT_PublishVibrationNotification_00100
1169  * @tc.name      :
1170  * @tc.desc      : Publish a vibration notification.
1171  */
1172 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_PublishVibrationNotification_00100, Function | MediumTest | Level1)
1173 {
1174     std::vector<sptr<NotificationSlot>> slots;
1175     NotificationSlot slot1;
1176     slot1.SetType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1177     slot1.SetEnableVibration(true);
1178     slot1.SetVibrationStyle(std::vector<int64_t>(1, 1));
1179     EXPECT_EQ(NotificationHelper::AddNotificationSlot(slot1), (int)ERR_OK);
1180 
1181     TestAnsSubscriber subscriber;
1182     NotificationSubscribeInfo info;
1183     info.AddAppName("bundleName");
1184     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber, info), ERR_OK);
1185 
1186     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
1187     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
1188     NotificationRequest req(0);
1189     req.SetLabel(NOTIFICATION_LABEL_0);
1190     req.SetContent(content);
1191     req.SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1192     EXPECT_EQ(NotificationHelper::PublishNotification(req), ERR_OK);
1193     SleepForFC();
1194     EXPECT_EQ(NotificationHelper::CancelAllNotifications(), ERR_OK);
1195     SleepForFC();
1196     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), ERR_OK);
1197     SleepForFC();
1198     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
1199     EventParser eventParser;
1200     eventParser.Parse(events);
1201     EXPECT_TRUE(eventParser.GetWaitOnConsumed());
1202     EXPECT_TRUE(eventParser.GetWaitOnConsumedWithSortingMap());
1203     EXPECT_TRUE(eventParser.GetWaitOnCanceled());
1204     EXPECT_TRUE(eventParser.GetWaitOnCanceledWithSortingMapAndDeleteReason());
1205     subscriber.ClearEvents();
1206     SleepForFC();
1207 }
1208 
MakePixelMap(int32_t width,int32_t height)1209 inline std::shared_ptr<PixelMap> MakePixelMap(int32_t width, int32_t height)
1210 {
1211     const int32_t PIXEL_BYTES = 4;
1212     std::shared_ptr<PixelMap> pixelMap = std::make_shared<PixelMap>();
1213     if (pixelMap == nullptr) {
1214         return pixelMap;
1215     }
1216     ImageInfo info;
1217     info.size.width = width;
1218     info.size.height = height;
1219     info.pixelFormat = PixelFormat::ARGB_8888;
1220     info.colorSpace = ColorSpace::SRGB;
1221     pixelMap->SetImageInfo(info);
1222     int32_t rowDataSize = width * PIXEL_BYTES;
1223     uint32_t bufferSize = rowDataSize * height;
1224     void *buffer = malloc(bufferSize);
1225     if (buffer != nullptr) {
1226         pixelMap->SetPixelsAddr(buffer, nullptr, bufferSize, AllocatorType::HEAP_ALLOC, nullptr);
1227     }
1228     EXPECT_NE(buffer, nullptr);
1229     return pixelMap;
1230 }
1231 
1232 /**
1233  *
1234  * @tc.number    : ANS_FW_MT_PublishNotificationWithPixelMap_00100
1235  * @tc.name      :
1236  * @tc.desc      : Publish a notification with pixelMap.
1237  */
1238 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_PublishNotificationWithPixelMap_00100, Function | MediumTest | Level1)
1239 {
1240     const int BIG_PICTURE_WIDTH = 400;
1241     const int BIG_PICTURE_HEIGHT = 300;
1242     const int ICON_SIZE = 36;
1243 
1244     NotificationRequest req;
1245     req.SetSlotType(NotificationConstant::SlotType::OTHER);
1246     req.SetLabel("label");
1247     std::shared_ptr<NotificationPictureContent> pictureContent = std::make_shared<NotificationPictureContent>();
1248     EXPECT_NE(pictureContent, nullptr);
1249     pictureContent->SetText("notification text");
1250     pictureContent->SetTitle("notification title");
1251     std::shared_ptr<PixelMap> bigPicture = MakePixelMap(BIG_PICTURE_WIDTH, BIG_PICTURE_HEIGHT);
1252     EXPECT_NE(bigPicture, nullptr);
1253     pictureContent->SetBigPicture(bigPicture);
1254     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(pictureContent);
1255     EXPECT_NE(content, nullptr);
1256     req.SetContent(content);
1257     std::shared_ptr<PixelMap> littleIcon = MakePixelMap(ICON_SIZE, ICON_SIZE);
1258     req.SetLittleIcon(littleIcon);
1259     std::shared_ptr<PixelMap> bigIcon = MakePixelMap(ICON_SIZE, ICON_SIZE);
1260     req.SetBigIcon(bigIcon);
1261     EXPECT_EQ(NotificationHelper::PublishNotification(req), ERR_OK);
1262 }
1263 
1264 /**
1265  *
1266  * @tc.number    : ANS_FW_MT_PublishNotificationWithPixelMap_00200
1267  * @tc.name      :
1268  * @tc.desc      : Publish a notification with oversize pixelMap.
1269  */
1270 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_PublishNotificationWithPixelMap_00200, Function | MediumTest | Level1)
1271 {
1272     const int BIG_PICTURE_WIDTH = 1024;
1273     const int BIG_PICTURE_HEIGHT = 1024;
1274     const int ICON_SIZE = 36;
1275 
1276     NotificationRequest req;
1277     req.SetSlotType(NotificationConstant::SlotType::OTHER);
1278     req.SetLabel("label");
1279     std::shared_ptr<NotificationPictureContent> pictureContent = std::make_shared<NotificationPictureContent>();
1280     EXPECT_NE(pictureContent, nullptr);
1281     pictureContent->SetText("notification text");
1282     pictureContent->SetTitle("notification title");
1283     std::shared_ptr<PixelMap> bigPicture = MakePixelMap(BIG_PICTURE_WIDTH, BIG_PICTURE_HEIGHT);
1284     EXPECT_NE(bigPicture, nullptr);
1285     pictureContent->SetBigPicture(bigPicture);
1286     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(pictureContent);
1287     EXPECT_NE(content, nullptr);
1288     req.SetContent(content);
1289     std::shared_ptr<PixelMap> littleIcon = MakePixelMap(ICON_SIZE, ICON_SIZE);
1290     req.SetLittleIcon(littleIcon);
1291     std::shared_ptr<PixelMap> bigIcon = MakePixelMap(ICON_SIZE, ICON_SIZE);
1292     req.SetBigIcon(bigIcon);
1293     EXPECT_EQ(NotificationHelper::PublishNotification(req), (int)ERR_ANS_PICTURE_OVER_SIZE);
1294 }
1295 
1296 /**
1297  *
1298  * @tc.number    : ANS_FW_MT_PublishNotificationWithPixelMap_00300
1299  * @tc.name      :
1300  * @tc.desc      : Publish a notification with oversize pixelMap.
1301  */
1302 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_PublishNotificationWithPixelMap_00300, Function | MediumTest | Level1)
1303 {
1304     const int BIG_PICTURE_WIDTH = 400;
1305     const int BIG_PICTURE_HEIGHT = 300;
1306     const int ICON_SIZE = 256;
1307 
1308     NotificationRequest req;
1309     req.SetSlotType(NotificationConstant::SlotType::OTHER);
1310     req.SetLabel("label");
1311     std::shared_ptr<NotificationPictureContent> pictureContent = std::make_shared<NotificationPictureContent>();
1312     EXPECT_NE(pictureContent, nullptr);
1313     pictureContent->SetText("notification text");
1314     pictureContent->SetTitle("notification title");
1315     std::shared_ptr<PixelMap> bigPicture = MakePixelMap(BIG_PICTURE_WIDTH, BIG_PICTURE_HEIGHT);
1316     EXPECT_NE(bigPicture, nullptr);
1317     pictureContent->SetBigPicture(bigPicture);
1318     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(pictureContent);
1319     EXPECT_NE(content, nullptr);
1320     req.SetContent(content);
1321     std::shared_ptr<PixelMap> littleIcon = MakePixelMap(ICON_SIZE, ICON_SIZE);
1322     req.SetLittleIcon(littleIcon);
1323     std::shared_ptr<PixelMap> bigIcon = MakePixelMap(ICON_SIZE, ICON_SIZE);
1324     req.SetBigIcon(bigIcon);
1325     EXPECT_EQ(NotificationHelper::PublishNotification(req), (int)ERR_ANS_ICON_OVER_SIZE);
1326 }
1327 
1328 /**
1329  *
1330  * @tc.number    : ANS_FW_MT_OnDoNotDisturbDateChange_00100
1331  * @tc.name      :
1332  * @tc.desc      : OnDoNotDisturbDateChange callback.
1333  */
1334 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_OnDoNotDisturbDateChange_00100, Function | MediumTest | Level1)
1335 {
1336     TestAnsSubscriber subscriber;
1337     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
1338 
1339     NotificationDoNotDisturbDate date(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1340     EXPECT_EQ(NotificationHelper::SetDoNotDisturbDate(date), ERR_OK);
1341 
1342     std::this_thread::sleep_for(std::chrono::seconds(1));
1343 
1344     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
1345     for (auto event : events) {
1346         if (event->GetType() == SubscriberEventType::ON_DND_CHANGED) {
1347             std::shared_ptr<OnDoNotDisturbDateChangedEvent> ev =
1348                 std::static_pointer_cast<OnDoNotDisturbDateChangedEvent>(event);
1349             auto date = ev->GetDoNotDisturbDate();
1350             ASSERT_NE(date, nullptr);
1351             EXPECT_EQ(date->GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::NONE);
1352         }
1353     }
1354 
1355     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
1356 }
1357 
1358 /**
1359  *
1360  * @tc.number    : ANS_FW_MT_OnDoNotDisturbDateChange_00200
1361  * @tc.name      :
1362  * @tc.desc      : OnDoNotDisturbDateChange callback.
1363  */
1364 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_OnDoNotDisturbDateChange_00200, Function | MediumTest | Level1)
1365 {
1366     TestAnsSubscriber subscriber;
1367     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
1368 
1369     std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now();
1370     auto beginDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
1371     int64_t beginDate = beginDuration.count();
1372     timePoint += std::chrono::hours(1);
1373     auto endDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
1374     int64_t endDate = endDuration.count();
1375     NotificationDoNotDisturbDate date(NotificationConstant::DoNotDisturbType::ONCE, beginDate, endDate);
1376     EXPECT_EQ(NotificationHelper::SetDoNotDisturbDate(date), ERR_OK);
1377 
1378     std::this_thread::sleep_for(std::chrono::seconds(1));
1379 
1380     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
1381     for (auto event : events) {
1382         if (event->GetType() == SubscriberEventType::ON_DND_CHANGED) {
1383             std::shared_ptr<OnDoNotDisturbDateChangedEvent> ev =
1384                 std::static_pointer_cast<OnDoNotDisturbDateChangedEvent>(event);
1385             auto date = ev->GetDoNotDisturbDate();
1386             ASSERT_NE(date, nullptr);
1387             EXPECT_EQ(date->GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::ONCE);
1388         }
1389     }
1390 
1391     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
1392 }
1393 
1394 /**
1395  *
1396  * @tc.number    : ANS_FW_MT_OnDoNotDisturbDateChange_00300
1397  * @tc.name      :
1398  * @tc.desc      : OnDoNotDisturbDateChange callback.
1399  */
1400 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_OnDoNotDisturbDateChange_00300, Function | MediumTest | Level1)
1401 {
1402     TestAnsSubscriber subscriber;
1403     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
1404 
1405     std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now();
1406     auto beginDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
1407     int64_t beginDate = beginDuration.count();
1408     timePoint += std::chrono::hours(1);
1409     auto endDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
1410     int64_t endDate = endDuration.count();
1411     NotificationDoNotDisturbDate date(NotificationConstant::DoNotDisturbType::DAILY, beginDate, endDate);
1412     EXPECT_EQ(NotificationHelper::SetDoNotDisturbDate(date), ERR_OK);
1413 
1414     std::this_thread::sleep_for(std::chrono::seconds(1));
1415 
1416     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
1417     for (auto event : events) {
1418         if (event->GetType() == SubscriberEventType::ON_DND_CHANGED) {
1419             std::shared_ptr<OnDoNotDisturbDateChangedEvent> ev =
1420                 std::static_pointer_cast<OnDoNotDisturbDateChangedEvent>(event);
1421             auto date = ev->GetDoNotDisturbDate();
1422             ASSERT_NE(date, nullptr);
1423             EXPECT_EQ(date->GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::DAILY);
1424         }
1425     }
1426 
1427     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
1428 }
1429 
1430 /**
1431  *
1432  * @tc.number    : ANS_FW_MT_OnDoNotDisturbDateChange_00400
1433  * @tc.name      :
1434  * @tc.desc      : OnDoNotDisturbDateChange callback.
1435  */
1436 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_OnDoNotDisturbDateChange_00400, Function | MediumTest | Level1)
1437 {
1438     TestAnsSubscriber subscriber;
1439     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
1440 
1441     std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now();
1442     auto beginDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
1443     int64_t beginDate = beginDuration.count();
1444     timePoint += std::chrono::hours(1);
1445     auto endDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
1446     int64_t endDate = endDuration.count();
1447     NotificationDoNotDisturbDate date(NotificationConstant::DoNotDisturbType::CLEARLY, beginDate, endDate);
1448     EXPECT_EQ(NotificationHelper::SetDoNotDisturbDate(date), ERR_OK);
1449 
1450     std::this_thread::sleep_for(std::chrono::seconds(1));
1451 
1452     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
1453     for (auto event : events) {
1454         if (event->GetType() == SubscriberEventType::ON_DND_CHANGED) {
1455             std::shared_ptr<OnDoNotDisturbDateChangedEvent> ev =
1456                 std::static_pointer_cast<OnDoNotDisturbDateChangedEvent>(event);
1457             auto date = ev->GetDoNotDisturbDate();
1458             ASSERT_NE(date, nullptr);
1459             EXPECT_EQ(date->GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::CLEARLY);
1460         }
1461     }
1462 
1463     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
1464 }
1465 
GetDoNotDisturbDateInstance(NotificationConstant::DoNotDisturbType type,int64_t intervalHours)1466 static NotificationDoNotDisturbDate GetDoNotDisturbDateInstance(
1467     NotificationConstant::DoNotDisturbType type, int64_t intervalHours)
1468 {
1469     std::chrono::time_point<std::chrono::system_clock> beginTp = std::chrono::system_clock::now();
1470 
1471     auto beginDur = std::chrono::duration_cast<std::chrono::milliseconds>(beginTp.time_since_epoch());
1472     auto beginMs = beginDur.count();
1473 
1474     auto endDur = beginDur + std::chrono::hours(intervalHours);
1475     auto endMs = endDur.count();
1476 
1477     return {type, beginMs, endMs};
1478 }
1479 
1480 /**
1481  *
1482  * @tc.number    : ANS_FW_MT_GetDoNotDisturbDate_00100
1483  * @tc.name      :
1484  * @tc.desc      : GetDoNotDisturbDate.
1485  */
1486 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_GetDoNotDisturbDate_00100, Function | MediumTest | Level1)
1487 {
1488     NotificationDoNotDisturbDate setDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1489     EXPECT_EQ(NotificationHelper::SetDoNotDisturbDate(setDate), ERR_OK);
1490 
1491     NotificationDoNotDisturbDate getDate;
1492     EXPECT_EQ(NotificationHelper::GetDoNotDisturbDate(getDate), ERR_OK);
1493     EXPECT_EQ(getDate.GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::NONE);
1494     EXPECT_EQ(getDate.GetBeginDate(), 0);
1495     EXPECT_EQ(getDate.GetEndDate(), 0);
1496 }
1497 
1498 /**
1499  *
1500  * @tc.number    : ANS_FW_MT_DoesSupportDoNotDisturbMode_00100
1501  * @tc.name      :
1502  * @tc.desc      : DoesSupportDoNotDisturbMode.
1503  */
1504 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_DoesSupportDoNotDisturbMode_00100, Function | MediumTest | Level1)
1505 {
1506     bool isSupport = false;
1507     EXPECT_EQ(NotificationHelper::DoesSupportDoNotDisturbMode(isSupport), ERR_OK);
1508     EXPECT_EQ(isSupport, SUPPORT_DO_NOT_DISTRUB);
1509 }
1510 
1511 /**
1512  * @tc.number    : ANS_Interface_MT_DoNotDisturb_01000
1513  * @tc.name      : DoNotDisturb_01000
1514  * @tc.desc      : Set and get DoNotDisturbDate. E.g. 01:40 ~ 02:40
1515  * @tc.expected  : Set and get DoNotDisturbDate successfully.
1516  */
1517 HWTEST_F(AnsFWModuleTest, ANS_FW_MT_DoNotDisturb_01000, Function | MediumTest | Level1)
1518 {
1519     auto srcDate = GetDoNotDisturbDateInstance(NotificationConstant::DoNotDisturbType::ONCE, 1);
1520     EXPECT_EQ(NotificationHelper::SetDoNotDisturbDate(srcDate), ERR_OK);
1521 
1522     NotificationDoNotDisturbDate disDate;
1523     EXPECT_EQ(NotificationHelper::GetDoNotDisturbDate(disDate), ERR_OK);
1524 
1525     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_01000:: srcDate : beginMs : " << srcDate.GetBeginDate();
1526     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_01000:: srcDate : endMs   : " << srcDate.GetEndDate();
1527     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_01000:: disDate : beginMs : " << disDate.GetBeginDate();
1528     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_01000:: disDate : endMs   : " << disDate.GetEndDate();
1529 
1530     EXPECT_EQ(srcDate.GetDoNotDisturbType(), disDate.GetDoNotDisturbType());
1531 }
1532 
1533 /**
1534  * @tc.number    : ANS_Interface_MT_DoNotDisturb_02000
1535  * @tc.name      : DoNotDisturb_02000
1536  * @tc.desc      : Set and get DoNotDisturbDate. E.g. 1970-01-01 01:40 ~ 1970-01-02 01:40
1537  * @tc.expected  : Set and get DoNotDisturbDate successfully.
1538  */
1539 HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_DoNotDisturb_02000, Function | MediumTest | Level1)
1540 {
1541     auto srcDate = GetDoNotDisturbDateInstance(NotificationConstant::DoNotDisturbType::ONCE, 24);
1542     EXPECT_EQ(NotificationHelper::SetDoNotDisturbDate(srcDate), ERR_OK);
1543 
1544     NotificationDoNotDisturbDate disDate;
1545     EXPECT_EQ(NotificationHelper::GetDoNotDisturbDate(disDate), ERR_OK);
1546 
1547     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_02000:: srcDate : beginMs : " << srcDate.GetBeginDate();
1548     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_02000:: srcDate : endMs   : " << srcDate.GetEndDate();
1549     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_02000:: disDate : beginMs : " << disDate.GetBeginDate();
1550     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_02000:: disDate : endMs   : " << disDate.GetEndDate();
1551 
1552     EXPECT_EQ(srcDate.GetDoNotDisturbType(), disDate.GetDoNotDisturbType());
1553 
1554     EXPECT_NE(disDate.GetBeginDate(), disDate.GetEndDate());
1555 }
1556 
1557 /**
1558  * @tc.number    : ANS_Interface_MT_DoNotDisturb_03000
1559  * @tc.name      : DoNotDisturb_03000
1560  * @tc.desc      : Set and get DoNotDisturbDate. E.g. 1970-01-01 01:40 ~ 1970-01-02 02:40
1561  * @tc.expected  : Set and get DoNotDisturbDate successfully.
1562  */
1563 HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_DoNotDisturb_03000, Function | MediumTest | Level1)
1564 {
1565     auto srcDate = GetDoNotDisturbDateInstance(NotificationConstant::DoNotDisturbType::ONCE, 25);
1566     EXPECT_EQ(NotificationHelper::SetDoNotDisturbDate(srcDate), ERR_OK);
1567 
1568     NotificationDoNotDisturbDate disDate;
1569     EXPECT_EQ(NotificationHelper::GetDoNotDisturbDate(disDate), ERR_OK);
1570 
1571     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_03000:: srcDate : beginMs : " << srcDate.GetBeginDate();
1572     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_03000:: srcDate : endMs   : " << srcDate.GetEndDate();
1573     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_03000:: disDate : beginMs : " << disDate.GetBeginDate();
1574     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_03000:: disDate : endMs   : " << disDate.GetEndDate();
1575 
1576     EXPECT_EQ(srcDate.GetDoNotDisturbType(), disDate.GetDoNotDisturbType());
1577 }
1578 
1579 /**
1580  * @tc.number    : ANS_Interface_MT_DoNotDisturb_04000
1581  * @tc.name      : DoNotDisturb_04000
1582  * @tc.desc      : Set and get DoNotDisturbDate.  E.g. 01:40 ~ 07:40
1583  * @tc.expected  : Set and get DoNotDisturbDate successfully.
1584  */
1585 HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_DoNotDisturb_04000, Function | MediumTest | Level1)
1586 {
1587     auto srcDate = GetDoNotDisturbDateInstance(NotificationConstant::DoNotDisturbType::DAILY, 6);
1588     EXPECT_EQ(NotificationHelper::SetDoNotDisturbDate(srcDate), ERR_OK);
1589 
1590     NotificationDoNotDisturbDate disDate;
1591     EXPECT_EQ(NotificationHelper::GetDoNotDisturbDate(disDate), ERR_OK);
1592 
1593     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_04000:: srcDate : beginMs : " << srcDate.GetBeginDate();
1594     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_04000:: srcDate : endMs   : " << srcDate.GetEndDate();
1595     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_04000:: disDate : beginMs : " << disDate.GetBeginDate();
1596     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_04000:: disDate : endMs   : " << disDate.GetEndDate();
1597 
1598     EXPECT_EQ(srcDate.GetDoNotDisturbType(), disDate.GetDoNotDisturbType());
1599 }
1600 
1601 /**
1602  * @tc.number    : ANS_Interface_MT_DoNotDisturb_05000
1603  * @tc.name      : DoNotDisturb_05000
1604  * @tc.desc      : Set and get DoNotDisturbDate.  E.g. 1970-01-01 01:40 ~ 1970-01-02 01:40
1605  * @tc.expected  : Set and get DoNotDisturbDate successfully.
1606  */
1607 HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_DoNotDisturb_05000, Function | MediumTest | Level1)
1608 {
1609     auto srcDate = GetDoNotDisturbDateInstance(NotificationConstant::DoNotDisturbType::DAILY, 24);
1610     EXPECT_EQ(NotificationHelper::SetDoNotDisturbDate(srcDate), ERR_OK);
1611 
1612     NotificationDoNotDisturbDate disDate;
1613     EXPECT_EQ(NotificationHelper::GetDoNotDisturbDate(disDate), ERR_OK);
1614 
1615     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_05000:: srcDate : beginMs : " << srcDate.GetBeginDate();
1616     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_05000:: srcDate : endMs   : " << srcDate.GetEndDate();
1617     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_05000:: disDate : beginMs : " << disDate.GetBeginDate();
1618     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_05000:: disDate : endMs   : " << disDate.GetEndDate();
1619 
1620     EXPECT_EQ(srcDate.GetDoNotDisturbType(), disDate.GetDoNotDisturbType());
1621 
1622     EXPECT_NE(disDate.GetBeginDate(), disDate.GetEndDate());
1623 }
1624 
1625 /**
1626  * @tc.number    : ANS_Interface_MT_DoNotDisturb_06000
1627  * @tc.name      : DoNotDisturb_06000
1628  * @tc.desc      : Set and get DoNotDisturbDate.  E.g. 1970-01-01 01:40 ~ 1970-01-02 02:40
1629  * @tc.expected  : Set and get DoNotDisturbDate successfully.
1630  */
1631 HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_DoNotDisturb_06000, Function | MediumTest | Level1)
1632 {
1633     auto srcDate = GetDoNotDisturbDateInstance(NotificationConstant::DoNotDisturbType::DAILY, 25);
1634     EXPECT_EQ(NotificationHelper::SetDoNotDisturbDate(srcDate), ERR_OK);
1635 
1636     NotificationDoNotDisturbDate disDate;
1637     EXPECT_EQ(NotificationHelper::GetDoNotDisturbDate(disDate), ERR_OK);
1638 
1639     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_06000:: srcDate : beginMs : " << srcDate.GetBeginDate();
1640     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_06000:: srcDate : endMs   : " << srcDate.GetEndDate();
1641     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_06000:: disDate : beginMs : " << disDate.GetBeginDate();
1642     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_06000:: disDate : endMs   : " << disDate.GetEndDate();
1643 
1644     EXPECT_EQ(srcDate.GetDoNotDisturbType(), disDate.GetDoNotDisturbType());
1645 }
1646 
1647 /**
1648  * @tc.number    : ANS_Interface_MT_DoNotDisturb_07000
1649  * @tc.name      : DoNotDisturb_07000
1650  * @tc.desc      : Set and get DoNotDisturbDate.  E.g. 1970-01-01 01:40 ~ 1970-01-03 01:40
1651  * @tc.expected  : Set and get DoNotDisturbDate successfully.
1652  */
1653 HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_DoNotDisturb_07000, Function | MediumTest | Level1)
1654 {
1655     auto srcDate = GetDoNotDisturbDateInstance(NotificationConstant::DoNotDisturbType::CLEARLY, 48);
1656     EXPECT_EQ(NotificationHelper::SetDoNotDisturbDate(srcDate), ERR_OK);
1657 
1658     NotificationDoNotDisturbDate disDate;
1659     EXPECT_EQ(NotificationHelper::GetDoNotDisturbDate(disDate), ERR_OK);
1660 
1661     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_07000:: srcDate : beginMs : " << srcDate.GetBeginDate();
1662     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_07000:: srcDate : endMs   : " << srcDate.GetEndDate();
1663     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_07000:: disDate : beginMs : " << disDate.GetBeginDate();
1664     GTEST_LOG_(INFO) << "ANS_Interface_MT_DoNotDisturb_07000:: disDate : endMs   : " << disDate.GetEndDate();
1665 
1666     EXPECT_EQ(srcDate.GetDoNotDisturbType(), disDate.GetDoNotDisturbType());
1667 }
1668 
1669 /**
1670  *
1671  * @tc.number    : ANS_FW_MT_DistributedNotification_SetEnable_00100
1672  * @tc.name      : DistributedNotification_SetEnable_00100
1673  * @tc.desc      : Set distributed notification enable.
1674  */
1675 HWTEST_F(AnsFWModuleTest, DistributedNotification_SetEnable_00100, Function | MediumTest | Level1)
1676 {
1677     ANS_LOGI("%{public}s", test_info_->name());
1678     bool enable;
1679 
1680     ASSERT_EQ(NotificationHelper::EnableDistributed(false), ERR_OK);
1681     ASSERT_EQ(NotificationHelper::IsDistributedEnabled(enable), ERR_OK);
1682     ASSERT_EQ(enable, false);
1683 
1684     ASSERT_EQ(NotificationHelper::EnableDistributed(true), ERR_OK);
1685     ASSERT_EQ(NotificationHelper::IsDistributedEnabled(enable), ERR_OK);
1686     ASSERT_EQ(enable, true);
1687 }
1688 
1689 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1690 /**
1691  *
1692  * @tc.number    : ANS_FW_MT_DistributedNotification_SetEnableByBundle_00100
1693  * @tc.name      : DistributedNotification_SetEnableByBundle_00100
1694  * @tc.desc      : Set distributed notification enable by bundle.
1695  */
1696 HWTEST_F(AnsFWModuleTest, DistributedNotification_SetEnableByBundle_00100, Function | MediumTest | Level1)
1697 {
1698     ANS_LOGI("%{public}s", test_info_->name());
1699     bool enable;
1700     NotificationBundleOption bundleOption(APP_NAME, UID);
1701 
1702     ASSERT_EQ(NotificationHelper::EnableDistributedByBundle(bundleOption, false), ERR_OK);
1703     ASSERT_EQ(NotificationHelper::IsDistributedEnableByBundle(bundleOption, enable), ERR_OK);
1704     ASSERT_EQ(enable, false);
1705 
1706     ASSERT_EQ(NotificationHelper::EnableDistributedByBundle(bundleOption, true), ERR_OK);
1707     ASSERT_EQ(NotificationHelper::IsDistributedEnableByBundle(bundleOption, enable), ERR_OK);
1708     ASSERT_EQ(enable, true);
1709 
1710     ASSERT_EQ(NotificationHelper::EnableDistributedSelf(false), ERR_OK);
1711     ASSERT_EQ(NotificationHelper::IsDistributedEnableByBundle(bundleOption, enable), ERR_OK);
1712     ASSERT_EQ(enable, false);
1713 
1714     ASSERT_EQ(NotificationHelper::EnableDistributedSelf(true), ERR_OK);
1715     ASSERT_EQ(NotificationHelper::IsDistributedEnableByBundle(bundleOption, enable), ERR_OK);
1716     ASSERT_EQ(enable, true);
1717 }
1718 
1719 /**
1720  *
1721  * @tc.number    : ANS_FW_MT_DistributedNotification_Publish_00100
1722  * @tc.name      : DistributedNotification_Publish_00100
1723  * @tc.desc      : publish a notification to distributed kvstore.
1724  */
1725 HWTEST_F(AnsFWModuleTest, DistributedNotification_Publish_00100, Function | MediumTest | Level1)
1726 {
1727     ANS_LOGI("%{public}s", test_info_->name());
1728     NotificationRequest request = CreateDistributedRequest(test_info_->name());
1729 
1730     DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID};
1731     DistributedKv::StoreId storeId = {.storeId = KVSTORE_NOTIFICATION_STORE_ID};
1732     std::shared_ptr<DistributedKv::MockSingleKvStore> pointer =
1733         DistributedKv::MockSingleKvStore::GetMockKvStorePointer(appId, storeId);
1734     std::vector<DistributedKv::Entry> entries;
1735 
1736     ASSERT_EQ(NotificationHelper::PublishNotification(request), ERR_OK);
1737     ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
1738     DistributedKv::Entry outEntry;
1739     ASSERT_EQ(GetRequestInDistributedEntryList(request, entries, outEntry), true);
1740     SleepForFC();
1741 }
1742 
1743 /**
1744  *
1745  * @tc.number    : ANS_FW_MT_DistributedNotification_Publish_00200
1746  * @tc.name      : DistributedNotification_Publish_00200
1747  * @tc.desc      : publish a local notification not use distributed kvstore.
1748  */
1749 HWTEST_F(AnsFWModuleTest, DistributedNotification_Publish_00200, Function | MediumTest | Level1)
1750 {
1751     ANS_LOGI("%{public}s", test_info_->name());
1752     NotificationRequest request = CreateDistributedRequest(test_info_->name());
1753     request.SetDistributed(false);
1754 
1755     DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID};
1756     DistributedKv::StoreId storeId = {.storeId = KVSTORE_NOTIFICATION_STORE_ID};
1757     std::shared_ptr<DistributedKv::MockSingleKvStore> pointer =
1758         DistributedKv::MockSingleKvStore::GetMockKvStorePointer(appId, storeId);
1759     std::vector<DistributedKv::Entry> entries;
1760 
1761     ASSERT_EQ(NotificationHelper::PublishNotification(request), ERR_OK);
1762     ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
1763     DistributedKv::Entry outEntry;
1764     ASSERT_EQ(GetRequestInDistributedEntryList(request, entries, outEntry), false);
1765     SleepForFC();
1766 }
1767 
1768 /**
1769  *
1770  * @tc.number    : ANS_FW_MT_DistributedNotification_Publish_00300
1771  * @tc.name      : DistributedNotification_Publish_00300 MockSetDistributedNotificationEnabled
1772  * @tc.desc      : publish a distributed notification when DistributedNotificationEnabled is false in application info.
1773  */
1774 HWTEST_F(AnsFWModuleTest, DistributedNotification_Publish_00300, Function | MediumTest | Level1)
1775 {
1776     ANS_LOGI("%{public}s", test_info_->name());
1777     NotificationRequest request = CreateDistributedRequest(test_info_->name());
1778     request.SetDistributed(true);
1779 
1780     DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID};
1781     DistributedKv::StoreId storeId = {.storeId = KVSTORE_NOTIFICATION_STORE_ID};
1782     std::shared_ptr<DistributedKv::MockSingleKvStore> pointer =
1783         DistributedKv::MockSingleKvStore::GetMockKvStorePointer(appId, storeId);
1784     std::vector<DistributedKv::Entry> entries;
1785 
1786     AppExecFwk::MockSetDistributedNotificationEnabled(false);
1787     ASSERT_EQ(NotificationHelper::PublishNotification(request), ERR_OK);
1788     ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
1789     DistributedKv::Entry outEntry;
1790     ASSERT_EQ(GetRequestInDistributedEntryList(request, entries, outEntry), true);
1791     AppExecFwk::MockSetDistributedNotificationEnabled(true);
1792     SleepForFC();
1793 }
1794 
1795 /**
1796  *
1797  * @tc.number    : ANS_FW_MT_DistributedNotification_Cancel_00100
1798  * @tc.name      : DistributedNotification_Cancel_00100
1799  * @tc.desc      : cancel a notification to distributed kvstore ,use CancelNotification(label, id).
1800  */
1801 HWTEST_F(AnsFWModuleTest, DistributedNotification_Cancel_00100, Function | MediumTest | Level1)
1802 {
1803     ANS_LOGI("%{public}s", test_info_->name());
1804     NotificationRequest request = CreateDistributedRequest(test_info_->name());
1805 
1806     DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID};
1807     DistributedKv::StoreId storeId = {.storeId = KVSTORE_NOTIFICATION_STORE_ID};
1808     std::shared_ptr<DistributedKv::MockSingleKvStore> pointer =
1809         DistributedKv::MockSingleKvStore::GetMockKvStorePointer(appId, storeId);
1810     std::vector<DistributedKv::Entry> entries;
1811 
1812     ASSERT_EQ(NotificationHelper::PublishNotification(request), ERR_OK);
1813     ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
1814     DistributedKv::Entry outEntry;
1815     ASSERT_EQ(GetRequestInDistributedEntryList(request, entries, outEntry), true);
1816 
1817     ASSERT_EQ(NotificationHelper::CancelNotification(request.GetLabel(), request.GetNotificationId()), ERR_OK);
1818     ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
1819     ASSERT_EQ(GetRequestInDistributedEntryList(request, entries, outEntry), false);
1820     SleepForFC();
1821 }
1822 
1823 /**
1824  *
1825  * @tc.number    : ANS_FW_MT_DistributedNotification_Cancel_00200
1826  * @tc.name      : DistributedNotification_Cancel_00200
1827  * @tc.desc      : cancel a notification to distributed kvstore ,use CancelAllNotifications().
1828  */
1829 HWTEST_F(AnsFWModuleTest, DistributedNotification_Cancel_00200, Function | MediumTest | Level1)
1830 {
1831     ANS_LOGI("%{public}s", test_info_->name());
1832     NotificationRequest request = CreateDistributedRequest(test_info_->name());
1833 
1834     DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID};
1835     DistributedKv::StoreId storeId = {.storeId = KVSTORE_NOTIFICATION_STORE_ID};
1836     std::shared_ptr<DistributedKv::MockSingleKvStore> pointer =
1837         DistributedKv::MockSingleKvStore::GetMockKvStorePointer(appId, storeId);
1838     std::vector<DistributedKv::Entry> entries;
1839 
1840     ASSERT_EQ(NotificationHelper::PublishNotification(request), ERR_OK);
1841     ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
1842     DistributedKv::Entry outEntry;
1843     ASSERT_EQ(GetRequestInDistributedEntryList(request, entries, outEntry), true);
1844 
1845     request.SetNotificationId(request.GetNotificationId() + 1);
1846     ASSERT_EQ(NotificationHelper::PublishNotification(request), ERR_OK);
1847     ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
1848     ASSERT_EQ(GetRequestInDistributedEntryList(request, entries, outEntry), true);
1849 
1850     ASSERT_EQ(NotificationHelper::CancelAllNotifications(), ERR_OK);
1851     ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
1852     ASSERT_EQ(entries.size(), std::size_t(0));
1853     SleepForFC();
1854 }
1855 
1856 /**
1857  *
1858  * @tc.number    : ANS_FW_MT_DistributedNotification_Remove_00100
1859  * @tc.name      : DistributedNotification_Remove_00100
1860  * @tc.desc      : remove a notification to distributed kvstore ,use RemoveNotification(bundleOption, id, label).
1861  */
1862 HWTEST_F(AnsFWModuleTest, DistributedNotification_Remove_00100, Function | MediumTest | Level1)
1863 {
1864     ANS_LOGI("%{public}s", test_info_->name());
1865     NotificationRequest request = CreateDistributedRequest(test_info_->name());
1866 
1867     DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID};
1868     DistributedKv::StoreId storeId = {.storeId = KVSTORE_NOTIFICATION_STORE_ID};
1869     std::shared_ptr<DistributedKv::MockSingleKvStore> pointer =
1870         DistributedKv::MockSingleKvStore::GetMockKvStorePointer(appId, storeId);
1871     std::vector<DistributedKv::Entry> entries;
1872 
1873     ASSERT_EQ(NotificationHelper::PublishNotification(request), ERR_OK);
1874     ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
1875     DistributedKv::Entry outEntry;
1876     ASSERT_EQ(GetRequestInDistributedEntryList(request, entries, outEntry), true);
1877 
1878     NotificationBundleOption bundleOption(APP_NAME, UID);
1879     ASSERT_EQ(
1880         NotificationHelper::RemoveNotification(bundleOption, request.GetNotificationId(), request.GetLabel()), ERR_OK);
1881     ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
1882     ASSERT_EQ(GetRequestInDistributedEntryList(request, entries, outEntry), false);
1883     SleepForFC();
1884 }
1885 
1886 /**
1887  *
1888  * @tc.number    : ANS_FW_MT_DistributedNotification_Remove_00200
1889  * @tc.name      : DistributedNotification_Remove_00200
1890  * @tc.desc      : remove a notification to distributed kvstore ,use RemoveNotifications().
1891  */
1892 HWTEST_F(AnsFWModuleTest, DistributedNotification_Remove_00200, Function | MediumTest | Level1)
1893 {
1894     ANS_LOGI("%{public}s", test_info_->name());
1895     NotificationRequest request = CreateDistributedRequest(test_info_->name());
1896 
1897     DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID};
1898     DistributedKv::StoreId storeId = {.storeId = KVSTORE_NOTIFICATION_STORE_ID};
1899     std::shared_ptr<DistributedKv::MockSingleKvStore> pointer =
1900         DistributedKv::MockSingleKvStore::GetMockKvStorePointer(appId, storeId);
1901     std::vector<DistributedKv::Entry> entries;
1902 
1903     ASSERT_EQ(NotificationHelper::PublishNotification(request), ERR_OK);
1904     ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
1905     DistributedKv::Entry outEntry;
1906     ASSERT_EQ(GetRequestInDistributedEntryList(request, entries, outEntry), true);
1907 
1908     request.SetNotificationId(request.GetNotificationId() + 1);
1909     ASSERT_EQ(NotificationHelper::PublishNotification(request), ERR_OK);
1910     ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
1911     ASSERT_EQ(GetRequestInDistributedEntryList(request, entries, outEntry), true);
1912 
1913     ASSERT_EQ(NotificationHelper::RemoveNotifications(USER_ID), ERR_OK);
1914     ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
1915     ASSERT_EQ(entries.size(), std::size_t(0));
1916     SleepForFC();
1917 }
1918 
1919 /**
1920  *
1921  * @tc.number    : ANS_FW_MT_DistributedNotification_Remove_00300
1922  * @tc.name      : DistributedNotification_Remove_00300
1923  * @tc.desc      : remove a notification to distributed kvstore ,use RemoveNotificationsByBundle(bundleOption).
1924  */
1925 HWTEST_F(AnsFWModuleTest, DistributedNotification_Remove_00300, Function | MediumTest | Level1)
1926 {
1927     ANS_LOGI("%{public}s", test_info_->name());
1928     NotificationRequest request = CreateDistributedRequest(test_info_->name());
1929 
1930     DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID};
1931     DistributedKv::StoreId storeId = {.storeId = KVSTORE_NOTIFICATION_STORE_ID};
1932     std::shared_ptr<DistributedKv::MockSingleKvStore> pointer =
1933         DistributedKv::MockSingleKvStore::GetMockKvStorePointer(appId, storeId);
1934     std::vector<DistributedKv::Entry> entries;
1935 
1936     ASSERT_EQ(NotificationHelper::PublishNotification(request), ERR_OK);
1937     ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
1938     DistributedKv::Entry outEntry;
1939     ASSERT_EQ(GetRequestInDistributedEntryList(request, entries, outEntry), true);
1940 
1941     request.SetNotificationId(request.GetNotificationId() + 1);
1942     ASSERT_EQ(NotificationHelper::PublishNotification(request), ERR_OK);
1943     ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
1944     ASSERT_EQ(GetRequestInDistributedEntryList(request, entries, outEntry), true);
1945 
1946     NotificationBundleOption bundleOption(APP_NAME, UID);
1947     ASSERT_EQ(NotificationHelper::RemoveNotificationsByBundle(bundleOption), ERR_OK);
1948     ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
1949     ASSERT_EQ(entries.size(), std::size_t(0));
1950     SleepForFC();
1951 }
1952 
1953 /**
1954  *
1955  * @tc.number    : ANS_FW_MT_DistributedNotification_Subscribe_00100
1956  * @tc.name      : DistributedNotification_Subscribe_00100
1957  * @tc.desc      : distributed kvstore callback data insert/update/delete.
1958  */
1959 HWTEST_F(AnsFWModuleTest, DistributedNotification_Subscribe_00100, Function | MediumTest | Level1)
1960 {
1961     ANS_LOGI("%{public}s", test_info_->name());
1962     NotificationRequest request = CreateDistributedRequest(test_info_->name());
1963     std::string jsonString;
1964     NotificationJsonConverter::ConvertToJsonString(&request, jsonString);
1965 
1966     DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID};
1967     DistributedKv::StoreId storeId = {.storeId = KVSTORE_NOTIFICATION_STORE_ID};
1968     std::shared_ptr<DistributedKv::MockSingleKvStore> pointer =
1969         DistributedKv::MockSingleKvStore::GetMockKvStorePointer(appId, storeId);
1970     std::vector<DistributedKv::Entry> entries;
1971 
1972     TestAnsSubscriber subscriber;
1973     ASSERT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
1974 
1975     DistributedKv::Key key(GenerateDistributedKey(request, REMOTE_DEVICE_ID));
1976     DistributedKv::Value value(jsonString);
1977     pointer->InsertDataToDoCallback(key, value);
1978     SleepForFC();
1979 
1980     EventParser parser1;
1981     parser1.Parse(subscriber.GetEvents());
1982     auto notificationList = parser1.GetOnConsumedWithSortingMapReq();
1983     EXPECT_NE(notificationList.size(), std::size_t(0));
1984     std::shared_ptr<Notification> outNotification;
1985     EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true);
1986     subscriber.ClearEvents();
1987 
1988     pointer->UpdateDataToDoCallback(key, value);
1989     SleepForFC();
1990 
1991     EventParser parser2;
1992     parser2.Parse(subscriber.GetEvents());
1993     notificationList = parser2.GetOnConsumedWithSortingMapReq();
1994     EXPECT_NE(notificationList.size(), std::size_t(0));
1995     EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true);
1996     subscriber.ClearEvents();
1997 
1998     pointer->DeleteDataToDoCallback(key);
1999     SleepForFC();
2000 
2001     EventParser parser3;
2002     parser3.Parse(subscriber.GetEvents());
2003     notificationList = parser3.GetOnCanceledReq();
2004     EXPECT_NE(notificationList.size(), std::size_t(0));
2005     EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true);
2006     subscriber.ClearEvents();
2007     ASSERT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
2008 
2009     SleepForFC();
2010 }
2011 
2012 /**
2013  *
2014  * @tc.number    : ANS_FW_MT_DistributedNotification_Subscribe_00200
2015  * @tc.name      : DistributedNotification_Subscribe_00200
2016  * @tc.desc      : distributed kvstore callback data, delete notification after OnConsumed.
2017  */
2018 HWTEST_F(AnsFWModuleTest, DistributedNotification_Subscribe_00200, Function | MediumTest | Level1)
2019 {
2020     ANS_LOGI("%{public}s", test_info_->name());
2021     NotificationRequest request = CreateDistributedRequest(test_info_->name());
2022     request.SetOwnerBundleName(APP_NAME);
2023     request.SetCreatorBundleName(APP_NAME);
2024     std::string jsonString;
2025     NotificationJsonConverter::ConvertToJsonString(&request, jsonString);
2026 
2027     DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID};
2028     DistributedKv::StoreId storeId = {.storeId = KVSTORE_NOTIFICATION_STORE_ID};
2029     std::shared_ptr<DistributedKv::MockSingleKvStore> pointer =
2030         DistributedKv::MockSingleKvStore::GetMockKvStorePointer(appId, storeId);
2031     std::vector<DistributedKv::Entry> entries;
2032 
2033     TestAnsSubscriber subscriber;
2034     ASSERT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
2035 
2036     DistributedKv::Key key(GenerateDistributedKey(request, REMOTE_DEVICE_ID));
2037     DistributedKv::Value value(jsonString);
2038     pointer->InsertDataToDoCallback(key, value);
2039     SleepForFC();
2040 
2041     EventParser parser1;
2042     parser1.Parse(subscriber.GetEvents());
2043     auto notificationList = parser1.GetOnConsumedWithSortingMapReq();
2044     EXPECT_NE(notificationList.size(), std::size_t(0));
2045     std::shared_ptr<Notification> outNotification;
2046     EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true);
2047 
2048     EXPECT_EQ(NotificationHelper::RemoveNotifications(), ERR_OK);
2049 
2050     EXPECT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS);
2051     EXPECT_EQ(entries.size(), std::size_t(0));
2052     subscriber.ClearEvents();
2053     ASSERT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
2054     SleepForFC();
2055 }
2056 
2057 /**
2058  *
2059  * @tc.number    : ANS_Interface_MT_GetDeviceRemindType_00100
2060  * @tc.name      : GetDeviceRemindType_00100
2061  * @tc.desc      : Get device remind type.
2062  */
2063 HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_GetDeviceRemindType_00100, Function | MediumTest | Level1)
2064 {
2065     auto rType = NotificationConstant::RemindType::NONE;
2066     EXPECT_EQ(NotificationHelper::GetDeviceRemindType(rType), ERR_OK);
2067     ANS_LOGI("ANS_Interface_MT_GetDeviceRemindType_00100:: rType : %{public}d", static_cast<int32_t>(rType));
2068 
2069     EXPECT_NE(rType, NotificationConstant::RemindType::NONE);
2070 }
2071 
2072 /**
2073  *
2074  * @tc.number    : ANS_FW_MT_DistributedNotification_ScreenStatusChange_00100
2075  * @tc.name      : ScreenStatusChange_00100
2076  * @tc.desc      : Receive local screen status from common event to kvstore.
2077  */
2078 HWTEST_F(AnsFWModuleTest, ScreenStatusChange_00100, Function | MediumTest | Level1)
2079 {
2080     ANS_LOGI("%{public}s", test_info_->name());
2081     DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID};
2082     DistributedKv::StoreId storeId = {.storeId = KVSTORE_SCREEN_STATUS_STORE_ID};
2083     std::shared_ptr<DistributedKv::MockSingleKvStore> pointer =
2084         DistributedKv::MockSingleKvStore::GetMockKvStorePointer(appId, storeId);
2085     DistributedKv::Key key("<localDeviceId>");
2086     std::vector<DistributedKv::Entry> entries;
2087 
2088     PublishCommonEventScreenStatus(false);
2089     EXPECT_EQ(pointer->GetEntries(key, entries), DistributedKv::Status::SUCCESS);
2090     EXPECT_EQ(entries.size(), std::size_t(1));
2091     EXPECT_EQ(entries[0].value.ToString(), "off");
2092     entries.clear();
2093 
2094     PublishCommonEventScreenStatus(true);
2095     EXPECT_EQ(pointer->GetEntries(key, entries), DistributedKv::Status::SUCCESS);
2096     EXPECT_EQ(entries.size(), std::size_t(1));
2097     EXPECT_EQ(entries[0].value.ToString(), "on");
2098     entries.clear();
2099 }
2100 
2101 /**
2102  *
2103  * @tc.number    : ANS_FW_MT_DistributedNotification_DefaultRemindPolicy_00100
2104  * @tc.name      : DefaultRemindPolicy_00100
2105  * @tc.desc      : Publish a notification when local screen on and remote screen off.
2106  */
2107 HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00100, Function | MediumTest | Level1)
2108 {
2109     ANS_LOGI("%{public}s", test_info_->name());
2110     NotificationRequest request = CreateDistributedRequest(test_info_->name());
2111 
2112     PublishCommonEventScreenStatus(true);
2113     SetDistributedScreenStatus(false);
2114 
2115     TestAnsSubscriber subscriber;
2116     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
2117     EXPECT_EQ(NotificationHelper::PublishNotification(request), ERR_OK);
2118     SleepForFC();
2119 
2120     EventParser parser;
2121     parser.Parse(subscriber.GetEvents());
2122     auto notificationList = parser.GetOnConsumedWithSortingMapReq();
2123     std::shared_ptr<Notification> outNotification;
2124     EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true);
2125     EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND);
2126 
2127     subscriber.ClearEvents();
2128     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
2129     SleepForFC();
2130 }
2131 
2132 /**
2133  *
2134  * @tc.number    : ANS_FW_MT_DistributedNotification_DefaultRemindPolicy_00200
2135  * @tc.name      : DefaultRemindPolicy_00200
2136  * @tc.desc      : Publish a notification when local screen on and remote screen on.
2137  */
2138 HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00200, Function | MediumTest | Level1)
2139 {
2140     ANS_LOGI("%{public}s", test_info_->name());
2141     NotificationRequest request = CreateDistributedRequest(test_info_->name());
2142 
2143     PublishCommonEventScreenStatus(true);
2144     SetDistributedScreenStatus(true);
2145 
2146     TestAnsSubscriber subscriber;
2147     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
2148     EXPECT_EQ(NotificationHelper::PublishNotification(request), ERR_OK);
2149     SleepForFC();
2150 
2151     EventParser parser;
2152     parser.Parse(subscriber.GetEvents());
2153     auto notificationList = parser.GetOnConsumedWithSortingMapReq();
2154     std::shared_ptr<Notification> outNotification;
2155     EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true);
2156     EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND);
2157 
2158     subscriber.ClearEvents();
2159     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
2160     SleepForFC();
2161 }
2162 
2163 /**
2164  *
2165  * @tc.number    : ANS_FW_MT_DistributedNotification_DefaultRemindPolicy_00300
2166  * @tc.name      : DefaultRemindPolicy_00300
2167  * @tc.desc      : Publish a notification when local screen off and remote screen off.
2168  */
2169 HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00300, Function | MediumTest | Level1)
2170 {
2171     ANS_LOGI("%{public}s", test_info_->name());
2172     NotificationRequest request = CreateDistributedRequest(test_info_->name());
2173 
2174     PublishCommonEventScreenStatus(false);
2175     SetDistributedScreenStatus(false);
2176 
2177     TestAnsSubscriber subscriber;
2178     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
2179     EXPECT_EQ(NotificationHelper::PublishNotification(request), ERR_OK);
2180     SleepForFC();
2181 
2182     EventParser parser;
2183     parser.Parse(subscriber.GetEvents());
2184     auto notificationList = parser.GetOnConsumedWithSortingMapReq();
2185     std::shared_ptr<Notification> outNotification;
2186     EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true);
2187     EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_IDLE_REMIND);
2188 
2189     subscriber.ClearEvents();
2190     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
2191     SleepForFC();
2192 }
2193 
2194 /**
2195  *
2196  * @tc.number    : ANS_FW_MT_DistributedNotification_DefaultRemindPolicy_00400
2197  * @tc.name      : DefaultRemindPolicy_00400
2198  * @tc.desc      : Publish a notification when local screen off and remote screen on.
2199  */
2200 HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00400, Function | MediumTest | Level1)
2201 {
2202     ANS_LOGI("%{public}s", test_info_->name());
2203     NotificationRequest request = CreateDistributedRequest(test_info_->name());
2204 
2205     PublishCommonEventScreenStatus(false);
2206     SetDistributedScreenStatus(true);
2207 
2208     TestAnsSubscriber subscriber;
2209     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
2210     EXPECT_EQ(NotificationHelper::PublishNotification(request), ERR_OK);
2211     SleepForFC();
2212 
2213     EventParser parser;
2214     parser.Parse(subscriber.GetEvents());
2215     auto notificationList = parser.GetOnConsumedWithSortingMapReq();
2216     std::shared_ptr<Notification> outNotification;
2217     EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true);
2218     EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_IDLE_DONOT_REMIND);
2219 
2220     subscriber.ClearEvents();
2221     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
2222     SleepForFC();
2223 }
2224 
2225 /**
2226  *
2227  * @tc.number    : ANS_FW_MT_DistributedNotification_DefaultRemindPolicy_00500
2228  * @tc.name      : DefaultRemindPolicy_00500
2229  * @tc.desc      : Receive distributed notification when screen is on.
2230  */
2231 HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00500, Function | MediumTest | Level1)
2232 {
2233     ANS_LOGI("%{public}s", test_info_->name());
2234     std::vector<std::string> devices = {"<localDeviceType>"};
2235     NotificationRequest request = CreateDistributedRequest(test_info_->name());
2236     request.SetOwnerBundleName(APP_NAME);
2237     request.SetCreatorBundleName(APP_NAME);
2238     request.SetDevicesSupportDisplay(devices);
2239     std::string jsonString;
2240     NotificationJsonConverter::ConvertToJsonString(&request, jsonString);
2241 
2242     PublishCommonEventScreenStatus(true);
2243 
2244     DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID};
2245     DistributedKv::StoreId storeId = {.storeId = KVSTORE_NOTIFICATION_STORE_ID};
2246     std::shared_ptr<DistributedKv::MockSingleKvStore> pointer =
2247         DistributedKv::MockSingleKvStore::GetMockKvStorePointer(appId, storeId);
2248 
2249     TestAnsSubscriber subscriber;
2250     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
2251 
2252     DistributedKv::Key key(GenerateDistributedKey(request, REMOTE_DEVICE_ID));
2253     DistributedKv::Value value(jsonString);
2254     pointer->InsertDataToDoCallback(key, value);
2255     SleepForFC();
2256     SleepForFC();
2257 
2258     EventParser parser;
2259     parser.Parse(subscriber.GetEvents());
2260     auto notificationList = parser.GetOnConsumedWithSortingMapReq();
2261     std::shared_ptr<Notification> outNotification;
2262     EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true);
2263     EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND);
2264 
2265     subscriber.ClearEvents();
2266     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
2267     SleepForFC();
2268 }
2269 
2270 /**
2271  *
2272  * @tc.number    : ANS_FW_MT_DistributedNotification_DefaultRemindPolicy_00600
2273  * @tc.name      : DefaultRemindPolicy_00600
2274  * @tc.desc      : Receive distributed notification when screen is off.
2275  */
2276 HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00600, Function | MediumTest | Level1)
2277 {
2278     ANS_LOGI("%{public}s", test_info_->name());
2279     std::vector<std::string> devices = {"<localDeviceType>"};
2280     NotificationRequest request = CreateDistributedRequest(test_info_->name());
2281     request.SetOwnerBundleName(APP_NAME);
2282     request.SetCreatorBundleName(APP_NAME);
2283     request.SetDevicesSupportDisplay(devices);
2284     std::string jsonString;
2285     NotificationJsonConverter::ConvertToJsonString(&request, jsonString);
2286 
2287     PublishCommonEventScreenStatus(false);
2288 
2289     DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID};
2290     DistributedKv::StoreId storeId = {.storeId = KVSTORE_NOTIFICATION_STORE_ID};
2291     std::shared_ptr<DistributedKv::MockSingleKvStore> pointer =
2292         DistributedKv::MockSingleKvStore::GetMockKvStorePointer(appId, storeId);
2293 
2294     TestAnsSubscriber subscriber;
2295     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
2296 
2297     DistributedKv::Key key(GenerateDistributedKey(request, REMOTE_DEVICE_ID));
2298     DistributedKv::Value value(jsonString);
2299     pointer->InsertDataToDoCallback(key, value);
2300     SleepForFC();
2301 
2302     EventParser parser;
2303     parser.Parse(subscriber.GetEvents());
2304     auto notificationList = parser.GetOnConsumedWithSortingMapReq();
2305     std::shared_ptr<Notification> outNotification;
2306     EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true);
2307     EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_IDLE_DONOT_REMIND);
2308 
2309     subscriber.ClearEvents();
2310     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
2311     SleepForFC();
2312 }
2313 #endif
2314 
2315 HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PublishContinuousTask_07100, Function | MediumTest | Level1)
2316 {
2317     TestAnsSubscriber subscriber;
2318     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
2319 
2320     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
2321     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
2322     NotificationRequest req(0);
2323     req.SetContent(content);
2324     req.SetLabel(NOTIFICATION_LABEL_0);
2325     EXPECT_EQ(NotificationHelper::PublishContinuousTaskNotification(req), ERR_OK);
2326     SleepForFC();
2327     EventParser eventParser;
2328     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
2329     eventParser.Parse(events);
2330     EXPECT_TRUE(eventParser.GetWaitOnConsumed());
2331     eventParser.SetWaitOnConsumed(false);
2332 
2333     std::vector<sptr<Notification>> notifications;
2334     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
2335     EXPECT_NE((int)notifications.size(), (int)0);
2336     int32_t id = notifications[0]->GetId();
2337     EXPECT_EQ(NotificationHelper::CancelContinuousTaskNotification(NOTIFICATION_LABEL_0, id), ERR_OK);
2338     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
2339     EXPECT_EQ((int)notifications.size(), (int)0);
2340     SleepForFC();
2341 
2342     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
2343     events = subscriber.GetEvents();
2344     eventParser.Parse(events);
2345     EXPECT_TRUE(eventParser.GetWaitOnCanceled());
2346     EXPECT_TRUE(eventParser.GetWaitOnCanceledWithSortingMapAndDeleteReason());
2347     subscriber.ClearEvents();
2348     SleepForFC();
2349 }
2350 
2351 HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PublishContinuousTask_07200, Function | MediumTest | Level1)
2352 {
2353     TestAnsSubscriber subscriber;
2354     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
2355 
2356     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
2357     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
2358     NotificationRequest req(0);
2359     req.SetContent(content);
2360     req.SetLabel(NOTIFICATION_LABEL_0);
2361     EXPECT_EQ(NotificationHelper::PublishContinuousTaskNotification(req), ERR_OK);
2362     SleepForFC();
2363     EventParser eventParser;
2364     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
2365     eventParser.Parse(events);
2366     EXPECT_TRUE(eventParser.GetWaitOnConsumed());
2367     eventParser.SetWaitOnConsumed(false);
2368 
2369     std::vector<sptr<Notification>> notifications;
2370     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
2371     EXPECT_NE((int)notifications.size(), (int)0);
2372     std::string key = notifications[0]->GetKey().c_str();
2373     EXPECT_EQ(NotificationHelper::RemoveNotification(key), (int)ERR_OK);
2374     int32_t id = notifications[0]->GetId();
2375     EXPECT_EQ(NotificationHelper::CancelContinuousTaskNotification(NOTIFICATION_LABEL_0, id), ERR_OK);
2376     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
2377     EXPECT_EQ((int)notifications.size(), (int)0);
2378     SleepForFC();
2379 
2380     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
2381     events = subscriber.GetEvents();
2382     eventParser.Parse(events);
2383     EXPECT_TRUE(eventParser.GetWaitOnCanceled());
2384     EXPECT_TRUE(eventParser.GetWaitOnCanceledWithSortingMapAndDeleteReason());
2385     subscriber.ClearEvents();
2386     SleepForFC();
2387 }
2388 
2389 HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PublishContinuousTask_07300, Function | MediumTest | Level1)
2390 {
2391     TestAnsSubscriber subscriber;
2392     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
2393 
2394     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
2395     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
2396     NotificationRequest req(0);
2397     req.SetContent(content);
2398     req.SetLabel(NOTIFICATION_LABEL_0);
2399     EXPECT_EQ(NotificationHelper::PublishContinuousTaskNotification(req), ERR_OK);
2400     SleepForFC();
2401     EventParser eventParser;
2402     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
2403     eventParser.Parse(events);
2404     EXPECT_TRUE(eventParser.GetWaitOnConsumed());
2405     eventParser.SetWaitOnConsumed(false);
2406 
2407     std::vector<sptr<Notification>> notifications;
2408     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
2409     EXPECT_NE((int)notifications.size(), (int)0);
2410     int32_t id = notifications[0]->GetId();
2411     EXPECT_EQ(NotificationHelper::CancelNotification(id), (int)ERR_ANS_NOTIFICATION_NOT_EXISTS);
2412     EXPECT_EQ(NotificationHelper::CancelContinuousTaskNotification(NOTIFICATION_LABEL_0, id), ERR_OK);
2413     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
2414     EXPECT_EQ((int)notifications.size(), (int)0);
2415     SleepForFC();
2416 
2417     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
2418     events = subscriber.GetEvents();
2419     eventParser.Parse(events);
2420     EXPECT_TRUE(eventParser.GetWaitOnCanceled());
2421     EXPECT_TRUE(eventParser.GetWaitOnCanceledWithSortingMapAndDeleteReason());
2422     subscriber.ClearEvents();
2423     SleepForFC();
2424 }
2425 
2426 HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PublishContinuousTask_07400, Function | MediumTest | Level1)
2427 {
2428     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
2429     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
2430     NotificationRequest req(0);
2431     req.SetContent(content);
2432     req.SetLabel(NOTIFICATION_LABEL_0);
2433     EXPECT_EQ(NotificationHelper::PublishContinuousTaskNotification(req), (int)ERR_ANS_NOT_SYSTEM_SERVICE);
2434 }
2435 
2436 HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PublishContinuousTask_07500, Function | MediumTest | Level1)
2437 {
2438     TestAnsSubscriber subscriber;
2439     NotificationSubscribeInfo info;
2440     info.AddAppName("bundleName");
2441     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber, info), ERR_OK);
2442 
2443     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
2444     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
2445     NotificationRequest req0(0);
2446     req0.SetLabel(NOTIFICATION_LABEL_0);
2447     req0.SetContent(content);
2448     EXPECT_EQ(NotificationHelper::PublishNotification(req0), ERR_OK);
2449 
2450     NotificationRequest req1(1);
2451     req1.SetLabel(NOTIFICATION_LABEL_1);
2452     req1.SetContent(content);
2453     EXPECT_EQ(NotificationHelper::PublishNotification(req1), ERR_OK);
2454     EXPECT_EQ(
2455         NotificationHelper::CancelContinuousTaskNotification(NOTIFICATION_LABEL_1, 1), (int)ERR_ANS_NOT_SYSTEM_SERVICE);
2456     EXPECT_EQ(NotificationHelper::CancelAllNotifications(), ERR_OK);
2457     SleepForFC();
2458     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), ERR_OK);
2459     SleepForFC();
2460     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
2461 
2462     EventParser eventParser;
2463     eventParser.Parse(events);
2464     EXPECT_TRUE(eventParser.GetWaitOnConsumed());
2465     EXPECT_TRUE(eventParser.GetWaitOnConsumedWithSortingMap());
2466     EXPECT_TRUE(eventParser.GetWaitOnCanceled());
2467     EXPECT_TRUE(eventParser.GetWaitOnCanceledWithSortingMapAndDeleteReason());
2468     subscriber.ClearEvents();
2469     SleepForFC();
2470 }
2471 
2472 HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PublishContinuousTask_07600, Function | MediumTest | Level1)
2473 {
2474     TestAnsSubscriber subscriber;
2475     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
2476 
2477     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
2478     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
2479     NotificationRequest req(0);
2480     req.SetContent(content);
2481     req.SetLabel(NOTIFICATION_LABEL_0);
2482     EXPECT_EQ(NotificationHelper::PublishContinuousTaskNotification(req), ERR_OK);
2483 
2484     SleepForFC();
2485     EventParser eventParser;
2486     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
2487     eventParser.Parse(events);
2488     EXPECT_TRUE(eventParser.GetWaitOnConsumed());
2489 
2490     EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetLabel().c_str(), NOTIFICATION_LABEL_0);
2491     EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetId(), 0);
2492     EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->IsUnremovable(), true);
2493     EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetSourceType(), NotificationConstant::SourceType::TYPE_CONTINUOUS);
2494 
2495     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
2496     SleepForFC();
2497     subscriber.ClearEvents();
2498 }
2499 
2500 HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PublishContinuousTask_07700, Function | MediumTest | Level1)
2501 {
2502     TestAnsSubscriber subscriber;
2503     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
2504 
2505     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
2506     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
2507     NotificationRequest req(0);
2508     req.SetContent(content);
2509     req.SetLabel(NOTIFICATION_LABEL_0);
2510     EXPECT_EQ(NotificationHelper::PublishContinuousTaskNotification(req), ERR_OK);
2511     SleepForFC();
2512     EventParser eventParser;
2513     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
2514     eventParser.Parse(events);
2515     EXPECT_TRUE(eventParser.GetWaitOnConsumed());
2516     eventParser.SetWaitOnConsumed(false);
2517 
2518     std::vector<sptr<Notification>> notifications;
2519     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
2520     EXPECT_NE((int)notifications.size(), (int)0);
2521     int32_t id = notifications[0]->GetId();
2522     EXPECT_EQ(NotificationHelper::CancelAllNotifications(), (int)ERR_OK);
2523     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
2524     EXPECT_NE((int)notifications.size(), (int)0);
2525     EXPECT_EQ(NotificationHelper::CancelContinuousTaskNotification(NOTIFICATION_LABEL_0, id), ERR_OK);
2526     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
2527     EXPECT_EQ((int)notifications.size(), (int)0);
2528     SleepForFC();
2529 
2530     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
2531     events = subscriber.GetEvents();
2532     eventParser.Parse(events);
2533     EXPECT_TRUE(eventParser.GetWaitOnCanceled());
2534     EXPECT_TRUE(eventParser.GetWaitOnCanceledWithSortingMapAndDeleteReason());
2535     subscriber.ClearEvents();
2536     SleepForFC();
2537 }
2538 
2539 HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PublishContinuousTask_07800, Function | MediumTest | Level1)
2540 {
2541     TestAnsSubscriber subscriber;
2542     EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK);
2543 
2544     std::shared_ptr<NotificationNormalContent> implContent = std::make_shared<NotificationNormalContent>();
2545     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(implContent);
2546     NotificationRequest req(0);
2547     req.SetContent(content);
2548     req.SetLabel(NOTIFICATION_LABEL_0);
2549     EXPECT_EQ(NotificationHelper::PublishContinuousTaskNotification(req), ERR_OK);
2550     SleepForFC();
2551     EventParser eventParser;
2552     std::list<std::shared_ptr<SubscriberEvent>> events = subscriber.GetEvents();
2553     eventParser.Parse(events);
2554     EXPECT_TRUE(eventParser.GetWaitOnConsumed());
2555     eventParser.SetWaitOnConsumed(false);
2556 
2557     std::vector<sptr<Notification>> notifications;
2558     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
2559     EXPECT_NE((int)notifications.size(), (int)0);
2560     int32_t id = notifications[0]->GetId();
2561     EXPECT_EQ(NotificationHelper::RemoveNotifications(), (int)ERR_OK);
2562     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
2563     EXPECT_NE((int)notifications.size(), (int)0);
2564     EXPECT_EQ(NotificationHelper::CancelContinuousTaskNotification(NOTIFICATION_LABEL_0, id), ERR_OK);
2565     EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK);
2566     EXPECT_EQ((int)notifications.size(), (int)0);
2567     SleepForFC();
2568 
2569     EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK);
2570     events = subscriber.GetEvents();
2571     eventParser.Parse(events);
2572     EXPECT_TRUE(eventParser.GetWaitOnCanceled());
2573     EXPECT_TRUE(eventParser.GetWaitOnCanceledWithSortingMapAndDeleteReason());
2574     subscriber.ClearEvents();
2575     SleepForFC();
2576 }
2577 }  // namespace Notification
2578 }  // namespace OHOS
2579