diff --git a/frameworks/ans/core/include/ans_notification.h b/frameworks/ans/core/include/ans_notification.h index 0f879f05f11cb20acd38116c68d82bc04b691d24..54d4811052892156826e737f91910dca76a3484d 100644 --- a/frameworks/ans/core/include/ans_notification.h +++ b/frameworks/ans/core/include/ans_notification.h @@ -619,19 +619,19 @@ public: ErrCode DoesSupportDoNotDisturbMode(bool &doesSupport); /** - * Publishes a long task notification. + * Publishes a continuous task notification. * @param request Indicates the NotificationRequest object for setting the notification content. * This parameter must be specified. - * @return Returns publish long task notification result. + * @return Returns publish continuous task notification result. */ ErrCode PublishContinuousTaskNotification(const NotificationRequest &request); /** - * Cancels a published long task notification matching the specified label and notificationId. + * Cancels a published continuous task notification matching the specified label and notificationId. * - * @param label Indicates the label of the long task notification to cancel. - * @param notificationId Indicates the ID of the long task notification to cancel. - * @return Returns cancel long task notification result. + * @param label Indicates the label of the continuous task notification to cancel. + * @param notificationId Indicates the ID of the continuous task notification to cancel. + * @return Returns cancel continuous task notification result. */ ErrCode CancelContinuousTaskNotification(const std::string &label, int32_t notificationId); diff --git a/frameworks/ans/test/moduletest/ans_fw_module_test.cpp b/frameworks/ans/test/moduletest/ans_fw_module_test.cpp index ef727d9dafdb2faeba7fbd12f35c7ad91822196e..aeed6929a0200ac9c2604f48358bafb19c29c360 100644 --- a/frameworks/ans/test/moduletest/ans_fw_module_test.cpp +++ b/frameworks/ans/test/moduletest/ans_fw_module_test.cpp @@ -88,15 +88,13 @@ public: ~OnSubscribeResultEvent() override {} - OnSubscribeResultEvent() - : SubscriberEvent(SubscriberEventType::ON_SUBSCRIBERESULT) + OnSubscribeResultEvent() : SubscriberEvent(SubscriberEventType::ON_SUBSCRIBERESULT) {} }; class OnUnSubscribeResultEvent : public SubscriberEvent { public: - OnUnSubscribeResultEvent() - : SubscriberEvent(SubscriberEventType::ON_UNSUBSCRIBERESULT) + OnUnSubscribeResultEvent() : SubscriberEvent(SubscriberEventType::ON_UNSUBSCRIBERESULT) {} ~OnUnSubscribeResultEvent() override @@ -132,8 +130,7 @@ private: class OnDoNotDisturbDateChangedEvent : public SubscriberEvent { public: - explicit OnDoNotDisturbDateChangedEvent( - const std::shared_ptr &date) + explicit OnDoNotDisturbDateChangedEvent(const std::shared_ptr &date) : SubscriberEvent(SubscriberEventType::ON_DND_CHANGED), date_(date) {} @@ -1352,10 +1349,10 @@ static NotificationDoNotDisturbDate GetDoNotDisturbDateInstance( std::chrono::time_point beginTp = std::chrono::system_clock::now(); auto beginDur = std::chrono::duration_cast(beginTp.time_since_epoch()); - auto beginMs = beginDur.count(); + auto beginMs = beginDur.count(); auto endDur = beginDur + std::chrono::hours(intervalHours); - auto endMs = endDur.count(); + auto endMs = endDur.count(); return {type, beginMs, endMs}; } @@ -1603,7 +1600,7 @@ HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PulbishLongTask_07200, Function | Med EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK); EXPECT_NE((int)notifications.size(), (int)0); std::string key = notifications[0]->GetKey().c_str(); - EXPECT_EQ(NotificationHelper::RemoveNotification(key), (int)ERR_ANS_NOTIFICATION_NOT_EXISTS); + EXPECT_EQ(NotificationHelper::RemoveNotification(key), (int)ERR_OK); int32_t id = notifications[0]->GetId(); IPCSkeleton::SetCallingUid(SYSTEM_SERVICE_UID); EXPECT_EQ(NotificationHelper::CancelContinuousTaskNotification(NOTIFICATION_LABEL_0, id), ERR_OK); @@ -1726,5 +1723,81 @@ HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PulbishLongTask_07600, Function | Med subscriber.ClearEvents(); IPCSkeleton::SetCallingUid(1); } + +HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PulbishLongTask_07700, Function | MediumTest | Level1) +{ + IPCSkeleton::SetCallingUid(SYSTEM_SERVICE_UID); + TestAnsSubscriber subscriber; + EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); + NotificationRequest req(0); + req.SetLabel(NOTIFICATION_LABEL_0); + EXPECT_EQ(NotificationHelper::PublishContinuousTaskNotification(req), ERR_OK); + SleepForFC(); + EventParser eventParser; + std::list> events = subscriber.GetEvents(); + eventParser.Parse(events); + EXPECT_TRUE(eventParser.GetWaitOnConsumed()); + eventParser.SetWaitOnConsumed(false); + + std::vector> notifications; + EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK); + EXPECT_NE((int)notifications.size(), (int)0); + int32_t id = notifications[0]->GetId(); + EXPECT_EQ(NotificationHelper::CancelAllNotifications(), (int)ERR_OK); + IPCSkeleton::SetCallingUid(SYSTEM_SERVICE_UID); + EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK); + EXPECT_NE((int)notifications.size(), (int)0); + EXPECT_EQ(NotificationHelper::CancelContinuousTaskNotification(NOTIFICATION_LABEL_0, id), ERR_OK); + EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK); + EXPECT_EQ((int)notifications.size(), (int)0); + SleepForFC(); + + EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK); + events = subscriber.GetEvents(); + eventParser.Parse(events); + EXPECT_TRUE(eventParser.GetWaitOnCanceled()); + EXPECT_TRUE(eventParser.GetWaitOnCanceledWithSortingMapAndDeleteReason()); + subscriber.ClearEvents(); + SleepForFC(); + IPCSkeleton::SetCallingUid(1); +} + +HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PulbishLongTask_07800, Function | MediumTest | Level1) +{ + IPCSkeleton::SetCallingUid(SYSTEM_SERVICE_UID); + TestAnsSubscriber subscriber; + EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); + NotificationRequest req(0); + req.SetLabel(NOTIFICATION_LABEL_0); + EXPECT_EQ(NotificationHelper::PublishContinuousTaskNotification(req), ERR_OK); + SleepForFC(); + EventParser eventParser; + std::list> events = subscriber.GetEvents(); + eventParser.Parse(events); + EXPECT_TRUE(eventParser.GetWaitOnConsumed()); + eventParser.SetWaitOnConsumed(false); + + std::vector> notifications; + EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK); + EXPECT_NE((int)notifications.size(), (int)0); + int32_t id = notifications[0]->GetId(); + EXPECT_EQ(NotificationHelper::RemoveNotifications(), (int)ERR_OK); + EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK); + EXPECT_NE((int)notifications.size(), (int)0); + IPCSkeleton::SetCallingUid(SYSTEM_SERVICE_UID); + EXPECT_EQ(NotificationHelper::CancelContinuousTaskNotification(NOTIFICATION_LABEL_0, id), ERR_OK); + EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK); + EXPECT_EQ((int)notifications.size(), (int)0); + SleepForFC(); + + EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK); + events = subscriber.GetEvents(); + eventParser.Parse(events); + EXPECT_TRUE(eventParser.GetWaitOnCanceled()); + EXPECT_TRUE(eventParser.GetWaitOnCanceledWithSortingMapAndDeleteReason()); + subscriber.ClearEvents(); + SleepForFC(); + IPCSkeleton::SetCallingUid(1); +} } // namespace Notification } // namespace OHOS diff --git a/interfaces/innerkits/ans/native/include/notification_helper.h b/interfaces/innerkits/ans/native/include/notification_helper.h index cbc3d46c1f2b183bef20b299cdd7584b7438355a..0395b638cd181e2199c31fc42e9dc7236b858852 100644 --- a/interfaces/innerkits/ans/native/include/notification_helper.h +++ b/interfaces/innerkits/ans/native/include/notification_helper.h @@ -614,19 +614,19 @@ public: static ErrCode DoesSupportDoNotDisturbMode(bool &doesSupport); /** - * Publishes a long task notification. + * Publishes a continuous task notification. * @param request Indicates the NotificationRequest object for setting the notification content. * This parameter must be specified. - * @return Returns publish long task notification result. + * @return Returns publish continuous task notification result. */ static ErrCode PublishContinuousTaskNotification(const NotificationRequest &request); /** - * Cancels a published long task notification matching the specified label and notificationId. + * Cancels a published continuous task notification matching the specified label and notificationId. * - * @param label Indicates the label of the long task notification to cancel. - * @param notificationId Indicates the ID of the long task notification to cancel. - * @return Returns cancel long task notification result. + * @param label Indicates the label of the continuous task notification to cancel. + * @param notificationId Indicates the ID of the continuous task notification to cancel. + * @return Returns cancel continuous task notification result. */ static ErrCode CancelContinuousTaskNotification(const std::string &label, int32_t notificationId); }; diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 921ad9d9ec47dcd05a3d31a9816513c7e36ec052..176224efaf837d9831416eacb123012bf52ba8ec 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -129,6 +129,7 @@ private: ErrCode RemoveFromNotificationList(const sptr &bundleOption, const std::string &label, int notificationId, sptr ¬ification, bool isCancel = false); ErrCode RemoveFromNotificationList(const std::string &key, sptr ¬ification, bool isCancel = false); + ErrCode RemoveFromNotificationListForDeleteAll(const std::string &key, sptr ¬ification); std::vector GetNotificationKeys(const sptr &bundleOption); bool IsNotificationExists(const std::string &key); void SortNotificationList(); @@ -149,7 +150,7 @@ private: void AdjustDateForDndTypeOnce(int64_t &beginDate, int64_t &endDate); bool CheckPermission(const std::string &bundleName); - ErrCode PrepereLongTaskNotificationRequest(const sptr &request, const int &uid); + ErrCode PrepereContinuousTaskNotificationRequest(const sptr &request, const int &uid); private: static sptr instance_; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 7cc52336980741f600fd755d238f6d0e121b73bf..3deb150f6fdf3692455eb2926a97c59e657cb3bf 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -781,7 +781,7 @@ ErrCode AdvancedNotificationService::DeleteAll() for (auto key : keys) { sptr notification = nullptr; - result = RemoveFromNotificationList(key, notification); + result = RemoveFromNotificationListForDeleteAll(key, notification); if (result != ERR_OK) { continue; } @@ -993,7 +993,7 @@ ErrCode AdvancedNotificationService::RemoveFromNotificationList( const std::string &key, sptr ¬ification, bool isCancel) { for (auto record : notificationList_) { - if (!record->notification->IsRemoveAllowed()) { + if (!record->notification->IsRemoveAllowed() && isCancel) { continue; } if (record->notification->GetKey() == key) { @@ -1009,6 +1009,26 @@ ErrCode AdvancedNotificationService::RemoveFromNotificationList( return ERR_ANS_NOTIFICATION_NOT_EXISTS; } +ErrCode AdvancedNotificationService::RemoveFromNotificationListForDeleteAll( + const std::string &key, sptr ¬ification) +{ + for (auto record : notificationList_) { + if (!record->notification->IsRemoveAllowed()) { + continue; + } + if (record->notification->GetKey() == key) { + if (record->request->IsUnremovable()) { + return ERR_ANS_NOTIFICATION_IS_UNREMOVABLE; + } + notification = record->notification; + notificationList_.remove(record); + return ERR_OK; + } + } + + return ERR_ANS_NOTIFICATION_NOT_EXISTS; +} + ErrCode AdvancedNotificationService::Subscribe( const sptr &subscriber, const sptr &info) { @@ -1293,7 +1313,7 @@ ErrCode AdvancedNotificationService::PublishContinuousTaskNotification(const spt return ERR_ANS_INVALID_BUNDLE; } - ErrCode result = PrepereLongTaskNotificationRequest(request, uid); + ErrCode result = PrepereContinuousTaskNotificationRequest(request, uid); if (result != ERR_OK) { return result; } @@ -1634,9 +1654,6 @@ ErrCode AdvancedNotificationService::RemoveNotification( sptr notification = nullptr; for (auto record : notificationList_) { - if (!record->notification->IsRemoveAllowed()) { - continue; - } if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) && (record->bundleOption->GetUid() == bundleOption->GetUid()) && (record->notification->GetId() == notificationId) && (record->notification->GetLabel() == label)) { @@ -1694,7 +1711,6 @@ ErrCode AdvancedNotificationService::RemoveAllNotifications(const sptrnotification != nullptr) { int reason = NotificationConstant::CANCEL_REASON_DELETE; UpdateRecentNotification(record->notification, true, reason); @@ -1995,7 +2011,7 @@ bool AdvancedNotificationService::CheckPermission(const std::string &bundleName) return true; } -ErrCode AdvancedNotificationService::PrepereLongTaskNotificationRequest( +ErrCode AdvancedNotificationService::PrepereContinuousTaskNotificationRequest( const sptr &request, const int &uid) { int pid = IPCSkeleton::GetCallingPid(); diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 1b51ab08ddc62075d3a0c752815ff85f20f2fa32..f4693c9fd3d1882d55fbdac263e631d80283c6d4 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -1562,7 +1562,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10900, /** * @tc.number : ANS_Publish_01500 * @tc.name : ANSPublish01500 - * @tc.desc : publish a long task notification + * @tc.desc : publish a continuous task notification */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_11000, Function | SmallTest | Level1) { @@ -1578,7 +1578,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_11000, /** * @tc.number : ANS_Publish_01600 * @tc.name : ANSPublish01600 - * @tc.desc : publish a long task notification + * @tc.desc : publish a continuous task notification */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_11100, Function | SmallTest | Level1) {