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