From 5192e24ca16d9f36e45a5ba76755568c3fa3b087 Mon Sep 17 00:00:00 2001 From: cheerful_ricky Date: Wed, 9 Apr 2025 11:37:46 +0800 Subject: [PATCH] revert delete service method Signed-off-by: cheerful_ricky --- .../include/advanced_notification_service.h | 69 ++++ .../ans/src/advanced_notification_service.cpp | 44 +++ .../ans/src/advanced_notification_utils.cpp | 299 ++++++++++++++++++ ...anced_notification_service_branch_test.cpp | 132 ++++++++ .../advanced_notification_service_test.cpp | 223 +++++++++++++ 5 files changed, 767 insertions(+) diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 31340968d..32ba2af4d 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -597,6 +597,46 @@ public: */ ErrCode IsSpecialBundleAllowedNotify(const sptr &bundleOption, bool &allowed) override; + /** + * @brief Set do not disturb date. + * + * @param date Indicates the NotificationDoNotDisturbDate object. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode SetDoNotDisturbDate(const sptr &date) override; + + /** + * @brief Get do not disturb date. + * + * @param date Indicates the NotificationDoNotDisturbDate object. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode GetDoNotDisturbDate(sptr &date) override; + + /** + * @brief Add Do Not Disturb profiles. + * + * @param profiles Indicates the list of NotificationDoNotDisturbProfile objects to add. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode AddDoNotDisturbProfiles(const std::vector> &profiles) override; + + /** + * @brief Remove Do Not Disturb profiles. + * + * @param profiles Indicates the list of NotificationDoNotDisturbProfile objects to remove. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode RemoveDoNotDisturbProfiles(const std::vector> &profiles) override; + + /** + * @brief Get whether Do Not Disturb mode is supported. + * + * @param doesSupport Indicates the flag that supports DND mode. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode DoesSupportDoNotDisturbMode(bool &doesSupport) override; + /** * @brief Is coming call need silent in do not disturb mode. * @@ -735,6 +775,24 @@ public: */ ErrCode DeleteAllByUser(const int32_t &userId) override; + /** + * @brief Set do not disturb date by user. + * + * @param userId Indicates the user id. + * @param date Indicates NotificationDoNotDisturbDate object. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr &date) override; + + /** + * @brief Get the do not disturb date by user. + * + * @param userId Indicates the user id. + * @param date Indicates the NotificationDoNotDisturbDate object. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode GetDoNotDisturbDate(const int32_t &userId, sptr &date) override; + ErrCode SetEnabledForBundleSlot(const sptr &bundleOption, const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl) override; ErrCode GetEnabledForBundleSlot(const sptr &bundleOption, @@ -1133,6 +1191,15 @@ public: */ ErrCode AllowUseReminder(const std::string& bundleName, bool& isAllowUseReminder) override; + /** + * @brief Get do not disturb profile by id. + * + * @param id Profile id. + * @param status Indicates the NotificationDoNotDisturbProfile object. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode GetDoNotDisturbProfile(int64_t id, sptr &profile) override; + /** * @brief Distribution operation based on hashCode. * @@ -1318,6 +1385,8 @@ private: void InitDistributeCallBack(); #endif + ErrCode SetDoNotDisturbDateByUser(const int32_t &userId, const sptr &date); + ErrCode GetDoNotDisturbDateByUser(const int32_t &userId, sptr &date); ErrCode GetHasPoppedDialog(const sptr bundleOption, bool &hasPopped); static ErrCode GetAppTargetBundle(const sptr &bundleOption, sptr &targetBundle); diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index d0d60fad5..0e9a198a4 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1936,6 +1936,50 @@ ErrCode AdvancedNotificationService::IsDistributedEnableByBundle( #endif } +ErrCode AdvancedNotificationService::SetDoNotDisturbDate(const int32_t &userId, + const sptr &date) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + if (userId <= SUBSCRIBE_USER_INIT) { + ANS_LOGE("Input userId is invalidity."); + return ERR_ANS_INVALID_PARAM; + } + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + + return SetDoNotDisturbDateByUser(userId, date); +} + +ErrCode AdvancedNotificationService::GetDoNotDisturbDate(const int32_t &userId, + sptr &date) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + if (userId <= SUBSCRIBE_USER_INIT) { + ANS_LOGE("Input userId is invalid."); + return ERR_ANS_INVALID_PARAM; + } + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + + return GetDoNotDisturbDateByUser(userId, date); +} + ErrCode AdvancedNotificationService::GetHasPoppedDialog( const sptr bundleOption, bool &hasPopped) { diff --git a/services/ans/src/advanced_notification_utils.cpp b/services/ans/src/advanced_notification_utils.cpp index 6eb9a6f85..14b20eaf1 100644 --- a/services/ans/src/advanced_notification_utils.cpp +++ b/services/ans/src/advanced_notification_utils.cpp @@ -746,6 +746,204 @@ ErrCode AdvancedNotificationService::GetCommonTargetRecordList(const int32_t uid } return ERR_OK; } +void AdvancedNotificationService::AdjustDateForDndTypeOnce(int64_t &beginDate, int64_t &endDate) +{ + std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); + time_t nowT = std::chrono::system_clock::to_time_t(now); + tm nowTm = GetLocalTime(nowT); + + auto beginDateMilliseconds = std::chrono::milliseconds(beginDate); + auto beginDateTimePoint = + std::chrono::time_point(beginDateMilliseconds); + time_t beginDateT = std::chrono::system_clock::to_time_t(beginDateTimePoint); + tm beginDateTm = GetLocalTime(beginDateT); + + auto endDateMilliseconds = std::chrono::milliseconds(endDate); + auto endDateTimePoint = + std::chrono::time_point(endDateMilliseconds); + time_t endDateT = std::chrono::system_clock::to_time_t(endDateTimePoint); + tm endDateTm = GetLocalTime(endDateT); + + tm todayBeginTm = nowTm; + todayBeginTm.tm_sec = 0; + todayBeginTm.tm_min = beginDateTm.tm_min; + todayBeginTm.tm_hour = beginDateTm.tm_hour; + + tm todayEndTm = nowTm; + todayEndTm.tm_sec = 0; + todayEndTm.tm_min = endDateTm.tm_min; + todayEndTm.tm_hour = endDateTm.tm_hour; + + time_t todayBeginT = mktime(&todayBeginTm); + if (todayBeginT == -1) { + return; + } + time_t todayEndT = mktime(&todayEndTm); + if (todayEndT == -1) { + return; + } + + auto newBeginTimePoint = std::chrono::system_clock::from_time_t(todayBeginT); + auto newEndTimePoint = std::chrono::system_clock::from_time_t(todayEndT); + if (newBeginTimePoint >= newEndTimePoint) { + newEndTimePoint += std::chrono::hours(HOURS_IN_ONE_DAY); + } + + if (newEndTimePoint < now) { + newBeginTimePoint += std::chrono::hours(HOURS_IN_ONE_DAY); + newEndTimePoint += std::chrono::hours(HOURS_IN_ONE_DAY); + } + + auto newBeginDuration = std::chrono::duration_cast(newBeginTimePoint.time_since_epoch()); + beginDate = newBeginDuration.count(); + + auto newEndDuration = std::chrono::duration_cast(newEndTimePoint.time_since_epoch()); + endDate = newEndDuration.count(); +} + +ErrCode AdvancedNotificationService::SetDoNotDisturbDate(const sptr &date) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGW("Not system app!"); + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + ANS_LOGW("Check permission denied!"); + return ERR_ANS_PERMISSION_DENIED; + } + + int32_t userId = SUBSCRIBE_USER_INIT; + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { + ANS_LOGW("No active user found!"); + return ERR_ANS_GET_ACTIVE_USER_FAILED; + } + + return SetDoNotDisturbDateByUser(userId, date); +} + +ErrCode AdvancedNotificationService::GetDoNotDisturbDate(sptr &date) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + + int32_t userId = SUBSCRIBE_USER_INIT; + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { + return ERR_ANS_GET_ACTIVE_USER_FAILED; + } + + return GetDoNotDisturbDateByUser(userId, date); +} + +ErrCode AdvancedNotificationService::AddDoNotDisturbProfiles( + const std::vector> &profiles) +{ + ANS_LOGD("Called."); + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalid."); + return ERR_ANS_INVALID_PARAM; + } + int32_t userId = SUBSCRIBE_USER_INIT; + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { + ANS_LOGW("No active user found."); + return ERR_ANS_GET_ACTIVE_USER_FAILED; + } + ffrt::task_handle handler = + notificationSvrQueue_->submit_h(std::bind([copyUserId = userId, copyProfiles = profiles]() { + ANS_LOGD("The ffrt enter."); + NotificationPreferences::GetInstance()->AddDoNotDisturbProfiles(copyUserId, copyProfiles); + })); + notificationSvrQueue_->wait(handler); + return ERR_OK; +} + +ErrCode AdvancedNotificationService::RemoveDoNotDisturbProfiles( + const std::vector> &profiles) +{ + ANS_LOGD("Called."); + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalid."); + return ERR_ANS_INVALID_PARAM; + } + int32_t userId = SUBSCRIBE_USER_INIT; + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { + ANS_LOGW("No active user found."); + return ERR_ANS_GET_ACTIVE_USER_FAILED; + } + ffrt::task_handle handler = + notificationSvrQueue_->submit_h(std::bind([copyUserId = userId, copyProfiles = profiles]() { + ANS_LOGD("The ffrt enter."); + NotificationPreferences::GetInstance()->RemoveDoNotDisturbProfiles(copyUserId, copyProfiles); + })); + notificationSvrQueue_->wait(handler); + return ERR_OK; +} + +ErrCode AdvancedNotificationService::GetDoNotDisturbProfile(int64_t id, sptr &profile) +{ + ANS_LOGD("Called."); + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + int32_t userId = SUBSCRIBE_USER_INIT; + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { + ANS_LOGW("No active user found."); + return ERR_ANS_GET_ACTIVE_USER_FAILED; + } + + profile = new (std::nothrow) NotificationDoNotDisturbProfile(); + ErrCode result = NotificationPreferences::GetInstance()->GetDoNotDisturbProfile(id, userId, profile); + if (result != ERR_OK) { + ANS_LOGE("profile failed id: %{public}s, userid: %{public}d", std::to_string(id).c_str(), userId); + } + return result; +} + +ErrCode AdvancedNotificationService::DoesSupportDoNotDisturbMode(bool &doesSupport) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + + doesSupport = SUPPORT_DO_NOT_DISTRUB; + return ERR_OK; +} #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED void AdvancedNotificationService::OnDistributedPublish( @@ -1406,6 +1604,107 @@ void AdvancedNotificationService::GetDumpInfo(const std::vector } } +ErrCode AdvancedNotificationService::SetDoNotDisturbDateByUser(const int32_t &userId, + const sptr &date) +{ + ANS_LOGD("%{public}s enter, userId = %{public}d", __FUNCTION__, userId); + if (date == nullptr) { + ANS_LOGE("Invalid date param"); + return ERR_ANS_INVALID_PARAM; + } + + ErrCode result = ERR_OK; + + int64_t beginDate = ResetSeconds(date->GetBeginDate()); + int64_t endDate = ResetSeconds(date->GetEndDate()); + switch (date->GetDoNotDisturbType()) { + case NotificationConstant::DoNotDisturbType::NONE: + beginDate = 0; + endDate = 0; + break; + case NotificationConstant::DoNotDisturbType::ONCE: + AdjustDateForDndTypeOnce(beginDate, endDate); + break; + case NotificationConstant::DoNotDisturbType::CLEARLY: + if (beginDate >= endDate) { + return ERR_ANS_INVALID_PARAM; + } + break; + default: + break; + } + ANS_LOGD("Before set SetDoNotDisturbDate beginDate = %{public}" PRId64 ", endDate = %{public}" PRId64, + beginDate, endDate); + const sptr newConfig = new (std::nothrow) NotificationDoNotDisturbDate( + date->GetDoNotDisturbType(), + beginDate, + endDate + ); + if (newConfig == nullptr) { + ANS_LOGE("Failed to create NotificationDoNotDisturbDate instance"); + return ERR_NO_MEMORY; + } + + sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + ANS_LOGE("Generate invalid bundle option!"); + return ERR_ANS_INVALID_BUNDLE; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalid."); + return ERR_ANS_INVALID_PARAM; + } + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + result = NotificationPreferences::GetInstance()->SetDoNotDisturbDate(userId, newConfig); + if (result == ERR_OK) { + NotificationSubscriberManager::GetInstance()->NotifyDoNotDisturbDateChanged(userId, newConfig); + } + })); + notificationSvrQueue_->wait(handler); + + return ERR_OK; +} + +ErrCode AdvancedNotificationService::GetDoNotDisturbDateByUser(const int32_t &userId, + sptr &date) +{ + ErrCode result = ERR_OK; + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalid."); + return ERR_ANS_INVALID_PARAM; + } + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + sptr currentConfig = nullptr; + result = NotificationPreferences::GetInstance()->GetDoNotDisturbDate(userId, currentConfig); + if (result != ERR_OK) { + return; + } + int64_t now = GetCurrentTime(); + switch (currentConfig->GetDoNotDisturbType()) { + case NotificationConstant::DoNotDisturbType::CLEARLY: + case NotificationConstant::DoNotDisturbType::ONCE: + if (now >= currentConfig->GetEndDate()) { + date = new (std::nothrow) NotificationDoNotDisturbDate( + NotificationConstant::DoNotDisturbType::NONE, 0, 0); + if (date == nullptr) { + ANS_LOGE("Failed to create NotificationDoNotDisturbDate instance"); + return; + } + date = currentConfig; + } + break; + default: + date = currentConfig; + break; + } + })); + notificationSvrQueue_->wait(handler); + return ERR_OK; +} + ErrCode AdvancedNotificationService::SetRequestBundleInfo(const sptr &request, int32_t uid, std::string &bundle) { diff --git a/services/ans/test/unittest/advanced_notification_service_branch_test.cpp b/services/ans/test/unittest/advanced_notification_service_branch_test.cpp index 68cfb192c..f95e50d93 100644 --- a/services/ans/test/unittest/advanced_notification_service_branch_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_branch_test.cpp @@ -780,6 +780,72 @@ HWTEST_F(AnsBranchTest, AnsBranchTest_259000, Function | SmallTest | Level1) ASSERT_EQ(advancedNotificationService_->RemoveGroupByBundle(bundleOption, groupName), ERR_ANS_PERMISSION_DENIED); } +/** + * @tc.number : AnsBranchTest_260000 + * @tc.name : SetDoNotDisturbDate_1000 + * @tc.desc : Test SetDoNotDisturbDate function return ERR_ANS_NON_SYSTEM_APP. + * @tc.require : #I6P8UI + */ +HWTEST_F(AnsBranchTest, AnsBranchTest_260000, Function | SmallTest | Level1) +{ + MockVerifyNativeToken(false); + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + MockIsSystemApp(false); + + sptr date = + new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); + ASSERT_EQ(advancedNotificationService_->SetDoNotDisturbDate(date), ERR_ANS_NON_SYSTEM_APP); + ASSERT_EQ(advancedNotificationService_->GetDoNotDisturbDate(date), ERR_ANS_NON_SYSTEM_APP); +} + +/** + * @tc.number : AnsBranchTest_261000 + * @tc.name : SetDoNotDisturbDate_2000 + * @tc.desc : Test SetDoNotDisturbDate function return ERR_ANS_PERMISSION_DENIED. + * @tc.require : #I6P8UI + */ +HWTEST_F(AnsBranchTest, AnsBranchTest_261000, Function | SmallTest | Level1) +{ + MockVerifyNativeToken(false); + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + MockIsVerfyPermisson(false); + + sptr date = + new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); + ASSERT_EQ(advancedNotificationService_->SetDoNotDisturbDate(date), ERR_ANS_PERMISSION_DENIED); + ASSERT_EQ(advancedNotificationService_->GetDoNotDisturbDate(date), ERR_ANS_PERMISSION_DENIED); +} + +/** + * @tc.number : AnsBranchTest_262000 + * @tc.name : DoesSupportDoNotDisturbMode_1000 + * @tc.desc : Test DoesSupportDoNotDisturbMode function return ERR_ANS_NON_SYSTEM_APP. + * @tc.require : #I6P8UI + */ +HWTEST_F(AnsBranchTest, AnsBranchTest_262000, Function | SmallTest | Level1) +{ + MockIsSystemApp(false); + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + + bool doesSupport = true; + ASSERT_EQ(advancedNotificationService_->DoesSupportDoNotDisturbMode(doesSupport), ERR_ANS_NON_SYSTEM_APP); +} + +/** + * @tc.number : AnsBranchTest_263000 + * @tc.name : DoesSupportDoNotDisturbMode_2000 + * @tc.desc : Test DoesSupportDoNotDisturbMode function return ERR_ANS_PERMISSION_DENIED. + * @tc.require : #I6P8UI + */ +HWTEST_F(AnsBranchTest, AnsBranchTest_263000, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + MockIsVerfyPermisson(false); + + bool doesSupport = true; + ASSERT_EQ(advancedNotificationService_->DoesSupportDoNotDisturbMode(doesSupport), ERR_ANS_PERMISSION_DENIED); +} + /** * @tc.number : AnsBranchTest_264000 * @tc.name : EnableDistributed_1000 @@ -890,6 +956,40 @@ HWTEST_F(AnsBranchTest, AnsBranchTest_267100, Function | SmallTest | Level1) userId, enable), (int)ERR_ANS_NON_SYSTEM_APP); } +/** + * @tc.number : AnsBranchTest_268000 + * @tc.name : SetDoNotDisturbDate_1000 + * @tc.desc : Test SetDoNotDisturbDate function return ERR_ANS_NON_SYSTEM_APP. + * @tc.require : #I6P8UI + */ +HWTEST_F(AnsBranchTest, AnsBranchTest_268000, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + MockIsSystemApp(false); + + int32_t userId = 3; + sptr date = nullptr; + ASSERT_EQ(advancedNotificationService_->SetDoNotDisturbDate(userId, date), ERR_ANS_NON_SYSTEM_APP); + ASSERT_EQ(advancedNotificationService_->GetDoNotDisturbDate(userId, date), ERR_ANS_NON_SYSTEM_APP); +} + +/** + * @tc.number : AnsBranchTest_269000 + * @tc.name : SetDoNotDisturbDate_2000 + * @tc.desc : Test SetDoNotDisturbDate function return ERR_ANS_PERMISSION_DENIED. + * @tc.require : #I6P8UI + */ +HWTEST_F(AnsBranchTest, AnsBranchTest_269000, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + MockIsVerfyPermisson(false); + + int32_t userId = 3; + sptr date = nullptr; + ASSERT_EQ(advancedNotificationService_->SetDoNotDisturbDate(userId, date), ERR_ANS_PERMISSION_DENIED); + ASSERT_EQ(advancedNotificationService_->GetDoNotDisturbDate(userId, date), ERR_ANS_PERMISSION_DENIED); +} + /** * @tc.number : AnsBranchTest_270000 * @tc.name : SetEnabledForBundleSlot_1000 @@ -1172,5 +1272,37 @@ HWTEST_F(AnsBranchTest, GetDeviceRemindType_3000, Function | SmallTest | Level1) NotificationConstant::RemindType remindType = NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND; ASSERT_EQ(ans.GetDeviceRemindType(remindType), ERR_ANS_INVALID_PARAM); } + +/** + * @tc.number : AnsBranchTest_285000 + * @tc.name : IsNeedSilentInDoNotDisturbMode_1000 + * @tc.desc : Test IsNeedSilentInDoNotDisturbMode function return ERR_ANS_NON_SYSTEM_APP. + */ +HWTEST_F(AnsBranchTest, AnsBranchTest_285000, Function | SmallTest | Level1) +{ + MockIsSystemApp(false); + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + + std::string phoneNumber = "11111111111"; + int32_t callerType = 0; + ASSERT_EQ(advancedNotificationService_->IsNeedSilentInDoNotDisturbMode( + phoneNumber, callerType), -1); +} + +/** + * @tc.number : AnsBranchTest_286000 + * @tc.name : IsNeedSilentInDoNotDisturbMode_2000 + * @tc.desc : Test IsNeedSilentInDoNotDisturbMode function return ERR_ANS_PERMISSION_DENIED. + */ +HWTEST_F(AnsBranchTest, AnsBranchTest_286000, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + MockIsVerfyPermisson(false); + + std::string phoneNumber = "11111111111"; + int32_t callerType = 0; + ASSERT_EQ(advancedNotificationService_->IsNeedSilentInDoNotDisturbMode( + phoneNumber, callerType), ERR_ANS_PERMISSION_DENIED); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 6e75f45b0..8b1dd0c80 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -371,6 +371,19 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_03500, ASSERT_EQ(ret, (int)ERR_ANS_NOTIFICATION_NOT_EXISTS); } +/** + * @tc.number : AdvancedNotificationServiceTest_03600 + * @tc.name : ANS_AddDoNotDisturbProfiles_0100 + * @tc.desc : Test AddDoNotDisturbProfiles function + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_03600, Function | SmallTest | Level1) +{ + sptr date = nullptr; + std::vector> profiles = { date }; + auto ret = advancedNotificationService_->AddDoNotDisturbProfiles(profiles); + ASSERT_EQ(ret, (int)ERR_OK); +} + /** * @tc.number : AdvancedNotificationServiceTest_03700 * @tc.name : ANS_Delete_0100 @@ -420,6 +433,19 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_04000, ASSERT_EQ(slots.size(), (size_t)1); } +/** + * @tc.number : AdvancedNotificationServiceTest_04200 + * @tc.name : ANS_AddDoNotDisturbProfiles_0100 + * @tc.desc : Test AddDoNotDisturbProfiles function + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_04200, Function | SmallTest | Level1) +{ + sptr date = nullptr; + std::vector> profiles = { date }; + auto ret = advancedNotificationService_->RemoveDoNotDisturbProfiles(profiles); + ASSERT_EQ(ret, (int)ERR_OK); +} + /** * @tc.number : AdvancedNotificationServiceTest_04300 * @tc.name : ANS_AddDoNotDisturbProfiles_0100 @@ -801,6 +827,149 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_09900, (int)ERR_OK); } +/** + * @tc.number : AdvancedNotificationServiceTest_10500 + * @tc.name : ANS_SetDisturbMode_10500 + * @tc.desc : Test SetDisturbMode function + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10500, Function | SmallTest | Level1) +{ + sptr date = + new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); + ASSERT_EQ((int)advancedNotificationService_->SetDoNotDisturbDate(date), (int)ERR_OK); + + std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now(); + auto beginDuration = std::chrono::duration_cast(timePoint.time_since_epoch()); + int64_t beginDate = beginDuration.count(); + timePoint += std::chrono::hours(1); + auto endDuration = std::chrono::duration_cast(timePoint.time_since_epoch()); + int64_t endDate = endDuration.count(); + date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::ONCE, beginDate, endDate); + ASSERT_EQ((int)advancedNotificationService_->SetDoNotDisturbDate(date), (int)ERR_OK); + + timePoint = std::chrono::system_clock::now(); + beginDuration = std::chrono::duration_cast(timePoint.time_since_epoch()); + beginDate = beginDuration.count(); + timePoint += std::chrono::hours(1); + endDuration = std::chrono::duration_cast(timePoint.time_since_epoch()); + endDate = endDuration.count(); + date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::DAILY, beginDate, endDate); + ASSERT_EQ((int)advancedNotificationService_->SetDoNotDisturbDate(date), (int)ERR_OK); + + timePoint = std::chrono::system_clock::now(); + beginDuration = std::chrono::duration_cast(timePoint.time_since_epoch()); + beginDate = beginDuration.count(); + timePoint += std::chrono::hours(1); + endDuration = std::chrono::duration_cast(timePoint.time_since_epoch()); + endDate = endDuration.count(); + date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::CLEARLY, beginDate, endDate); + ASSERT_EQ((int)advancedNotificationService_->SetDoNotDisturbDate(date), (int)ERR_OK); +} + +/** + * @tc.number : AdvancedNotificationServiceTest_10600 + * @tc.name : ANS_GetDisturbMode_10600 + * @tc.desc : Test GetDisturbMode function + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10600, Function | SmallTest | Level1) +{ + sptr date = + new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); + + ASSERT_EQ((int)advancedNotificationService_->SetDoNotDisturbDate(date), (int)ERR_OK); + + sptr result = nullptr; + ASSERT_EQ((int)advancedNotificationService_->GetDoNotDisturbDate(result), (int)ERR_OK); + ASSERT_NE(result, nullptr); + ASSERT_EQ(result->GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::NONE); + ASSERT_EQ(result->GetBeginDate(), 0); + ASSERT_EQ(result->GetEndDate(), 0); +} + +/** + * @tc.number : AdvancedNotificationServiceTest_10700 + * @tc.name : ANS_GetDisturbMode_10700 + * @tc.desc : Test GetDisturbMode function + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10700, Function | SmallTest | Level1) +{ + std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now(); + timePoint = std::chrono::time_point_cast(timePoint); + timePoint += std::chrono::hours(1); + auto beginDuration = std::chrono::duration_cast(timePoint.time_since_epoch()); + int64_t beginDate = beginDuration.count(); + timePoint += std::chrono::hours(1); + auto endDuration = std::chrono::duration_cast(timePoint.time_since_epoch()); + int64_t endDate = endDuration.count(); + + sptr date = + new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::ONCE, beginDate, endDate); + ASSERT_EQ((int)advancedNotificationService_->SetDoNotDisturbDate(date), (int)ERR_OK); + + sptr result = nullptr; + ASSERT_EQ((int)advancedNotificationService_->GetDoNotDisturbDate(result), (int)ERR_OK); + ASSERT_NE(result, nullptr); + ASSERT_EQ(result->GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::ONCE); + ASSERT_EQ(result->GetBeginDate(), beginDate); + ASSERT_EQ(result->GetEndDate(), endDate); +} + +/** + * @tc.number : AdvancedNotificationServiceTest_10800 + * @tc.name : ANS_GetDisturbMode_10800 + * @tc.desc : Test GetDisturbMode function + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10800, Function | SmallTest | Level1) +{ + std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now(); + timePoint = std::chrono::time_point_cast(timePoint); + timePoint += std::chrono::hours(1); + auto beginDuration = std::chrono::duration_cast(timePoint.time_since_epoch()); + int64_t beginDate = beginDuration.count(); + timePoint += std::chrono::hours(1); + auto endDuration = std::chrono::duration_cast(timePoint.time_since_epoch()); + int64_t endDate = endDuration.count(); + + sptr date = + new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::DAILY, beginDate, endDate); + + ASSERT_EQ((int)advancedNotificationService_->SetDoNotDisturbDate(date), (int)ERR_OK); + sptr result = nullptr; + ASSERT_EQ((int)advancedNotificationService_->GetDoNotDisturbDate(result), (int)ERR_OK); + ASSERT_NE(result, nullptr); + ASSERT_EQ(result->GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::DAILY); + ASSERT_EQ(result->GetBeginDate(), beginDate); + ASSERT_EQ(result->GetEndDate(), endDate); +} + +/** + * @tc.number : AdvancedNotificationServiceTest_10900 + * @tc.name : ANS_GetDisturbMode_10900 + * @tc.desc : Test GetDisturbMode function + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10900, Function | SmallTest | Level1) +{ + std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now(); + timePoint = std::chrono::time_point_cast(timePoint); + timePoint += std::chrono::hours(1); + auto beginDuration = std::chrono::duration_cast(timePoint.time_since_epoch()); + int64_t beginDate = beginDuration.count(); + timePoint += std::chrono::hours(1); + auto endDuration = std::chrono::duration_cast(timePoint.time_since_epoch()); + int64_t endDate = endDuration.count(); + + sptr date = + new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::CLEARLY, beginDate, endDate); + ASSERT_EQ((int)advancedNotificationService_->SetDoNotDisturbDate(date), (int)ERR_OK); + + sptr result = nullptr; + ASSERT_EQ((int)advancedNotificationService_->GetDoNotDisturbDate(result), (int)ERR_OK); + ASSERT_NE(result, nullptr); + ASSERT_EQ(result->GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::CLEARLY); + ASSERT_EQ(result->GetBeginDate(), beginDate); + ASSERT_EQ(result->GetEndDate(), endDate); +} + /** * @tc.number : AdvancedNotificationServiceTest_12900 * @tc.name : ANS_HasNotificationPolicyAccessPermission_0100 @@ -900,6 +1069,20 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_13900, ASSERT_EQ(advancedNotificationService_->RemoveAllSlots(), (int)ERR_OK); } +/** + * @tc.number : AdvancedNotificationServiceTest_14200 + * @tc.name : ANS_DoesSupportDoNotDisturbMode_0100 + * @tc.desc : Test DoesSupportDoNotDisturbMode function when the result is ERR_OK + * @tc.require : issueI5S4VP + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_14200, Function | SmallTest | Level1) +{ + TestAddSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); + sptr req = new NotificationRequest(); + EXPECT_NE(req, nullptr); + bool doesSupport = true; + ASSERT_EQ(advancedNotificationService_->DoesSupportDoNotDisturbMode(doesSupport), (int)ERR_OK); +} /** * @tc.number : AdvancedNotificationServiceTest_14300 @@ -972,6 +1155,18 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_14900, ASSERT_EQ(advancedNotificationService_->SetNotificationsEnabledByUser(userId, enabled), (int)ERR_OK); } +/** + * @tc.number : AdvancedNotificationServiceTest_15000 + * @tc.name : ANS_GetDoNotDisturbDate_0100 + * @tc.desc : Test GetDoNotDisturbDate function when the result is ERR_OK + * @tc.require : issueI5S4VP + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15000, Function | SmallTest | Level1) +{ + int32_t userId = 3; + sptr date = nullptr; + ASSERT_EQ(advancedNotificationService_->GetDoNotDisturbDate(userId, date), (int)ERR_OK); +} /** * @tc.number : AdvancedNotificationServiceTest_15200 @@ -1330,6 +1525,12 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17100, std::string groupName = "group"; ASSERT_EQ(advancedNotificationService_->RemoveGroupByBundle(bundleOption, groupName), ERR_OK); + sptr date = nullptr; + ASSERT_EQ(advancedNotificationService_->SetDoNotDisturbDate(date), ERR_ANS_INVALID_PARAM); + ASSERT_EQ(advancedNotificationService_->GetDoNotDisturbDate(date), ERR_OK); + + ASSERT_EQ(advancedNotificationService_->DoesSupportDoNotDisturbMode(enable), ERR_OK); + ASSERT_EQ(advancedNotificationService_->EnableDistributed(enable), ERR_OK); ASSERT_EQ(advancedNotificationService_->EnableDistributedByBundle(bundleOption, enable), ERR_OK); @@ -1345,6 +1546,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17100, ASSERT_EQ(advancedNotificationService_->SetNotificationsEnabledByUser(userId, enable), ERR_OK); ASSERT_EQ(advancedNotificationService_->DeleteAllByUser(userId), ERR_OK); + ASSERT_EQ(advancedNotificationService_->SetDoNotDisturbDate(userId, date), ERR_ANS_INVALID_PARAM); + ASSERT_EQ(advancedNotificationService_->GetDoNotDisturbDate(userId, date), ERR_OK); ASSERT_EQ(advancedNotificationService_->SetEnabledForBundleSlot(bundleOption, NotificationConstant::SlotType::OTHER, enable, false), ERR_OK); @@ -2453,6 +2656,26 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_20400, GTEST_LOG_(INFO) << "TriggerRemoveWantAgent_0100 test end"; } +/** + * @tc.number : AdvancedNotificationServiceTest_20500 + * @tc.name : DeleteAllByUser_0100 + * @tc.desc : Test DeleteAllByUser function + * @tc.require : #I61RF2 + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_20500, Function | SmallTest | Level1) +{ + GTEST_LOG_(INFO) << "DeleteAllByUser_0100 test start"; + + int32_t userId = -2; + ASSERT_EQ(advancedNotificationService_->DeleteAllByUser(userId), ERR_ANS_INVALID_PARAM); + + sptr date = nullptr; + ASSERT_EQ(advancedNotificationService_->SetDoNotDisturbDate(userId, date), ERR_ANS_INVALID_PARAM); + ASSERT_EQ(advancedNotificationService_->GetDoNotDisturbDate(userId, date), ERR_ANS_INVALID_PARAM); + ASSERT_EQ(advancedNotificationService_->SetDoNotDisturbDateByUser(userId, date), ERR_ANS_INVALID_PARAM); + GTEST_LOG_(INFO) << "DeleteAllByUser_0100 test end"; +} + /** * @tc.number : AdvancedNotificationServiceTest_20600 * @tc.name : OnResourceRemove_0100 -- Gitee