From aa06c6c59e02fa7af9fd316725c7c860bb58e486 Mon Sep 17 00:00:00 2001 From: songbao1 Date: Mon, 31 Mar 2025 16:49:28 +0800 Subject: [PATCH 1/5] =?UTF-8?q?tdd=E8=A6=86=E7=9B=96=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: songbao1 --- ...nced_notification_publish_service_test.cpp | 23 ++ ...nced_notification_service_ability_test.cpp | 1 + ...anced_notification_service_branch_test.cpp | 3 + .../advanced_notification_service_test.cpp | 216 +++++++++++++++ .../advanced_notification_utils_test.cpp | 258 +++++++++++++++++- .../unittest/mock/include/mock_bundle_mgr.h | 2 + .../test/unittest/mock/mock_bundle_mgr.cpp | 9 + .../mock_os_account_manager_annex.cpp | 4 + 8 files changed, 509 insertions(+), 7 deletions(-) diff --git a/services/ans/test/unittest/advanced_notification_publish_service_test.cpp b/services/ans/test/unittest/advanced_notification_publish_service_test.cpp index 2c19f8d34..6d4db5fe0 100644 --- a/services/ans/test/unittest/advanced_notification_publish_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_publish_service_test.cpp @@ -300,6 +300,29 @@ HWTEST_F(AnsPublishServiceTest, Publish_00007, Function | SmallTest | Level1) ASSERT_EQ(ret, (int)ERR_OK); } +/** + * @tc.name: Publish_00008 + * @tc.desc: Test Publish + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsPublishServiceTest, Publish_00008, Function | SmallTest | Level1) +{ + sptr request = new (std::nothrow) NotificationRequest(); + std::string label = ""; + request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); + request->SetOwnerUid(1); + request->SetCreatorUid(1); + request->SetCreatorUserId(100); + request->SetIsAgentNotification(true); + MockIsOsAccountExists(true); + + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE); + MockIsSystemApp(false); + auto ret = advancedNotificationService_->Publish(label, request); + ASSERT_EQ(ret, (int)ERR_OK); +} + /** * @tc.name: DeleteByBundle_00001 * @tc.desc: Test DeleteByBundle diff --git a/services/ans/test/unittest/advanced_notification_service_ability_test.cpp b/services/ans/test/unittest/advanced_notification_service_ability_test.cpp index 562598b62..dc0da64c6 100644 --- a/services/ans/test/unittest/advanced_notification_service_ability_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_ability_test.cpp @@ -141,6 +141,7 @@ HWTEST_F( bool runOnCreate = true; std::string deviceId = "deviceId"; AdvancedNotificationServiceAbility test(systemAbilityId, runOnCreate); + test.OnAddSystemAbility(COMMON_EVENT_SERVICE_ID, deviceId); test.OnRemoveSystemAbility(COMMON_EVENT_SERVICE_ID, deviceId); EXPECT_EQ(test.isDatashaReready_, false); 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 33d6cc57c..68da9d131 100644 --- a/services/ans/test/unittest/advanced_notification_service_branch_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_branch_test.cpp @@ -1059,6 +1059,9 @@ HWTEST_F(AnsBranchTest, DoDistributedPublish_4000, Function | SmallTest | Level1 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundleOption); ASSERT_EQ(advancedNotificationService_->DoDistributedPublish(bundleOption, record), (int)ERR_ANS_DISTRIBUTED_OPERATION_FAILED); + auto ret = advancedNotificationService_->DoDistributedDelete( + "1", "DoDistributedPublish_4000", record->notification); + ASSERT_EQ(ret, (int)ERR_ANS_DISTRIBUTED_OPERATION_FAILED); } /** diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 58704630f..25333950b 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -56,6 +56,8 @@ extern void MockIsOsAccountExists(bool mockRet); using namespace testing::ext; using namespace OHOS::Media; +extern void MockQueryForgroundOsAccountId(bool mockRet, uint8_t mockCase); + namespace OHOS { namespace Notification { extern void MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum mockRet); @@ -431,6 +433,21 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_04300, EXPECT_TRUE(advancedNotificationService_ != nullptr); } +/** + * @tc.number : SetAgentNotification_0200 + * @tc.name : SetAgentNotification_0200 + * @tc.desc : Test AddDoNotDisturbProfiles function + */ +HWTEST_F(AdvancedNotificationServiceTest, SetAgentNotification_0200, Function | SmallTest | Level1) +{ + MockQueryForgroundOsAccountId(false, 0); + sptr request(new NotificationRequest()); + std::string bundleName = "bundleName"; + advancedNotificationService_->SetAgentNotification(request, bundleName); + ASSERT_NE(request->GetOwnerBundleName(), bundleName); + MockQueryForgroundOsAccountId(true, 0); +} + /** * @tc.number : AdvancedNotificationServiceTest_04400 * @tc.name : ANS_AddDoNotDisturbProfiles_0100 @@ -1444,6 +1461,26 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17700, * @tc.require : #I60KYN */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17800, Function | SmallTest | Level1) +{ + GTEST_LOG_(INFO) << "GetAppTargetBundle_3000 test start"; + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); + MockIsSystemApp(false); + sptr bundleOption = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID); + sptr targetBundle = nullptr; + bundleOption->SetBundleName("test"); + ASSERT_EQ(advancedNotificationService_->GetAppTargetBundle(bundleOption, targetBundle), ERR_ANS_NON_SYSTEM_APP); + + GTEST_LOG_(INFO) << "GetAppTargetBundle_3000 test end"; +} + +/** + * @tc.number : GetAppTargetBundle_4000 + * @tc.name : GetAppTargetBundle + * @tc.desc : Test GetAppTargetBundle function. + * @tc.require : #I60KYN + */ +HWTEST_F(AdvancedNotificationServiceTest, GetAppTargetBundle_4000, Function | SmallTest | Level1) + { GTEST_LOG_(INFO) << "GetAppTargetBundle_3000 test start"; @@ -1815,6 +1852,113 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_19900, GTEST_LOG_(INFO) << "CheckDistributedNotificationType_0100 test end"; } +/** + * @tc.number : OnDistributedPublish_0200 + * @tc.name : OnDistributedPublish_0200 + * @tc.desc : Test OnDistributedPublish function + * @tc.require : #I61RF2 + */ +HWTEST_F(AdvancedNotificationServiceTest, OnDistributedPublish_0200, Function | SmallTest | Level1) +{ + AdvancedNotificationService advancedNotificationService; + std::string deviceId = "DeviceId"; + std::string bundleName = "BundleName"; + auto slotType = NotificationConstant::SlotType::SOCIAL_COMMUNICATION; + sptr request = new NotificationRequest(19800); + request->SetSlotType(slotType); + request->SetOwnerBundleName("test"); + std::vector devices; + devices.push_back("a"); + request->SetDevicesSupportDisplay(devices); + advancedNotificationService.OnDistributedPublish(deviceId, bundleName, request); + SleepForFC(); + advancedNotificationService.OnDistributedUpdate(deviceId, bundleName, request); + SleepForFC(); + ASSERT_EQ(advancedNotificationService.notificationList_.size(), 0); +} + +/** + * @tc.number : OnDistributedPublish_0300 + * @tc.name : OnDistributedPublish_0300 + * @tc.desc : Test OnDistributedPublish function + * @tc.require : #I61RF2 + */ +HWTEST_F(AdvancedNotificationServiceTest, OnDistributedPublish_0300, Function | SmallTest | Level1) +{ + AdvancedNotificationService advancedNotificationService; + std::string deviceId = "DeviceId"; + std::string bundleName = "BundleName"; + auto slotType = NotificationConstant::SlotType::SOCIAL_COMMUNICATION; + sptr request = new NotificationRequest(19800); + request->SetSlotType(slotType); + request->SetOwnerBundleName("test"); + std::vector devices; + devices.push_back("a"); + request->SetDevicesSupportDisplay(devices); + advancedNotificationService.OnDistributedPublish(deviceId, bundleName, request); + advancedNotificationService.OnDistributedUpdate(deviceId, bundleName, request); + ASSERT_EQ(advancedNotificationService.notificationList_.size(), 0); +} + +/** + * @tc.number : OnDistributedPublish_0300 + * @tc.name : OnDistributedPublish_0300 + * @tc.desc : Test OnDistributedPublish function + * @tc.require : #I61RF2 + */ +HWTEST_F(AdvancedNotificationServiceTest, OnDistributedPublish_0400, Function | SmallTest | Level1) +{ + AdvancedNotificationService advancedNotificationService; + std::string deviceId = "DeviceId"; + std::string bundleName = "BundleName"; + auto slotType = NotificationConstant::SlotType::SOCIAL_COMMUNICATION; + sptr request = new NotificationRequest(19800); + request->SetSlotType(slotType); + request->SetOwnerBundleName("test"); + std::vector devices; + devices.push_back("a"); + request->SetDevicesSupportDisplay(devices); + advancedNotificationService.OnDistributedPublish(deviceId, bundleName, request); + advancedNotificationService.OnDistributedUpdate(deviceId, bundleName, request); + ASSERT_EQ(advancedNotificationService.notificationList_.size(), 0); +} + +/** + * @tc.number : OnDistributedPublish_0500 + * @tc.name : OnDistributedPublish_0500 + * @tc.desc : Test OnDistributedPublish function + * @tc.require : #I61RF2 + */ +HWTEST_F(AdvancedNotificationServiceTest, OnDistributedPublish_0500, Function | SmallTest | Level1) +{ + std::string deviceId = "DeviceId"; + std::string bundleName = "BundleName"; + sptr request = new NotificationRequest(); + ASSERT_NE(nullptr, advancedNotificationService_); + advancedNotificationService_->OnDistributedPublish(deviceId, bundleName, request); + ASSERT_EQ(advancedNotificationService_->notificationList_.size(), 0); +} + +/** + * @tc.number : OnDistributedPublish_0300 + * @tc.name : OnDistributedPublish_0300 + * @tc.desc : Test OnDistributedPublish function + * @tc.require : #I61RF2 + */ +HWTEST_F(AdvancedNotificationServiceTest, OnDistributedPublish_0600, Function | SmallTest | Level1) +{ + AdvancedNotificationService advancedNotificationService; + std::string deviceId = "DeviceId"; + std::string bundleName = "BundleName"; + auto slotType = NotificationConstant::SlotType::SOCIAL_COMMUNICATION; + sptr request = new NotificationRequest(19800); + request->SetSlotType(slotType); + request->SetOwnerBundleName("test"); + request->SetUpdateOnly(true); + advancedNotificationService.OnDistributedPublish(deviceId, bundleName, request); + ASSERT_EQ(advancedNotificationService.notificationList_.size(), 0); +} + /** * @tc.number : AdvancedNotificationServiceTest_20000 * @tc.name : OnDistributedUpdate_0100 @@ -1949,6 +2093,10 @@ HWTEST_F(AdvancedNotificationServiceTest, OnDistributedDelete_0200, Function | S SleepForFC(); int32_t id = 101; + MockQueryForgroundOsAccountId(false, 0); + advancedNotificationService.OnDistributedDelete(deviceId, bundleName, label, id); + SleepForFC(); + MockQueryForgroundOsAccountId(true, 100); advancedNotificationService.notificationSvrQueue_ = nullptr; advancedNotificationService.OnDistributedDelete(deviceId, bundleName, label, id); @@ -3840,5 +3988,73 @@ HWTEST_F(AdvancedNotificationServiceTest, Dialog_00006, Function | SmallTest | L NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(bundleOption, enable); ASSERT_EQ(enable, false); } + +/** + * @tc.name : SetNotificationRemindType_00001 + * @tc.number : + * @tc.desc : test SetNotificationRemindType + */ +HWTEST_F(AdvancedNotificationServiceTest, SetNotificationRemindType_00001, Function | SmallTest | Level1) +{ + advancedNotificationService_->distributedReminderPolicy_ = NotificationConstant::DistributedReminderPolicy::DEFAULT; + advancedNotificationService_->localScreenOn_ = false; + auto reminderType = advancedNotificationService_->GetRemindType(); + ASSERT_EQ(reminderType, NotificationConstant::RemindType::DEVICE_IDLE_REMIND); +} + +/** + * @tc.name : SetNotificationRemindType_00002 + * @tc.number : + * @tc.desc : test SetNotificationRemindType + */ +HWTEST_F(AdvancedNotificationServiceTest, SetNotificationRemindType_00002, Function | SmallTest | Level1) +{ + advancedNotificationService_->localScreenOn_ = true; + advancedNotificationService_->distributedReminderPolicy_ = NotificationConstant::DistributedReminderPolicy::DEFAULT; + auto reminderType = advancedNotificationService_->GetRemindType(); + ASSERT_EQ(reminderType, NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND); +} + +/** + * @tc.name : SetNotificationRemindType_00003 + * @tc.number : + * @tc.desc : test SetNotificationRemindType + */ +HWTEST_F(AdvancedNotificationServiceTest, SetNotificationRemindType_00003, Function | SmallTest | Level1) +{ + advancedNotificationService_->localScreenOn_ = true; + advancedNotificationService_->distributedReminderPolicy_ = + NotificationConstant::DistributedReminderPolicy::ALWAYS_REMIND; + auto reminderType = advancedNotificationService_->GetRemindType(); + ASSERT_EQ(reminderType, NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND); +} + +/** + * @tc.name : SetNotificationRemindType_00004 + * @tc.number : + * @tc.desc : test SetNotificationRemindType + */ +HWTEST_F(AdvancedNotificationServiceTest, SetNotificationRemindType_00004, Function | SmallTest | Level1) +{ + advancedNotificationService_->localScreenOn_ = false; + advancedNotificationService_->distributedReminderPolicy_ = + NotificationConstant::DistributedReminderPolicy::DO_NOT_REMIND; + auto reminderType = advancedNotificationService_->GetRemindType(); + ASSERT_EQ(reminderType, NotificationConstant::RemindType::DEVICE_IDLE_DONOT_REMIND); +} + +/** + * @tc.name : SetNotificationRemindType_00005 + * @tc.number : + * @tc.desc : test SetNotificationRemindType + */ +HWTEST_F(AdvancedNotificationServiceTest, SetNotificationRemindType_00005, Function | SmallTest | Level1) +{ + advancedNotificationService_->localScreenOn_ = true; + advancedNotificationService_->distributedReminderPolicy_ = + NotificationConstant::DistributedReminderPolicy::DO_NOT_REMIND; + auto reminderType = advancedNotificationService_->GetRemindType(); + ASSERT_EQ(reminderType, NotificationConstant::RemindType::DEVICE_ACTIVE_DONOT_REMIND); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/test/unittest/advanced_notification_utils_test.cpp b/services/ans/test/unittest/advanced_notification_utils_test.cpp index 7b7184e78..1ba076753 100644 --- a/services/ans/test/unittest/advanced_notification_utils_test.cpp +++ b/services/ans/test/unittest/advanced_notification_utils_test.cpp @@ -45,6 +45,8 @@ using namespace testing::ext; using namespace OHOS::Media; using namespace OHOS::Security::AccessToken; +extern void MockQueryForgroundOsAccountId(bool mockRet, uint8_t mockCase); + namespace OHOS { namespace Notification { extern void MockIsVerfyPermisson(bool isVerify); @@ -349,6 +351,41 @@ HWTEST_F(AnsUtilsTest, RecentNotificationDump_00002, Function | SmallTest | Leve ASSERT_EQ(dumpInfo.size(), 1); } +/** + * @tc.name: RecentNotificationDump_00003 + * @tc.desc: Test RecentNotificationDump + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsUtilsTest, RecentNotificationDump_00003, Function | SmallTest | Level1) +{ + sptr notification = nullptr; + auto recentNotification = std::make_shared(); + recentNotification->isActive = true; + recentNotification->notification = notification; + advancedNotificationService_->recentInfo_->list.emplace_front(recentNotification); + + auto slotType = NotificationConstant::SlotType::LIVE_VIEW; + sptr request = new (std::nothrow) NotificationRequest(); + request->SetSlotType(slotType); + request->SetOwnerUserId(1); + request->SetCreatorUserId(1); + request->SetOwnerBundleName("test"); + request->SetOwnerUid(0); + request->SetNotificationId(1); + auto notification1 = new (std::nothrow) Notification(request); + + auto recentNotification1 = std::make_shared(); + recentNotification1->isActive = false; + recentNotification1->notification = notification1; + advancedNotificationService_->recentInfo_->list.emplace_front(recentNotification1); + + std::vector dumpInfo; + int ret = advancedNotificationService_->RecentNotificationDump("test", 1, 1, dumpInfo); + ASSERT_EQ(ret, (int)ERR_OK); + ASSERT_EQ(dumpInfo.size(), 1); +} + /** * @tc.name: GetLocalNotificationKeys_00001 * @tc.desc: Test GetLocalNotificationKeys @@ -436,13 +473,15 @@ HWTEST_F(AnsUtilsTest, InitNotificationEnableList_00001, Function | SmallTest | * @tc.type: FUNC * @tc.require: issue */ -// HWTEST_F(AnsUtilsTest, GetBundleInfoByNotificationBundleOption_00001, Function | SmallTest | Level1) -// { -// sptr bundle = new NotificationBundleOption("test", 1); -// AppExecFwk::BundleInfo bundleInfo; -// bool res = advancedNotificationService_->GetBundleInfoByNotificationBundleOption(bundle, bundleInfo); -// ASSERT_EQ(res, true); -// } +HWTEST_F(AnsUtilsTest, GetBundleInfoByNotificationBundleOption_00001, Function | SmallTest | Level1) +{ + MockSetBundleInfoFailed(true); + sptr bundle = new NotificationBundleOption("test", 1); + AppExecFwk::BundleInfo bundleInfo; + bool res = advancedNotificationService_->GetBundleInfoByNotificationBundleOption(bundle, bundleInfo); + ASSERT_EQ(res, false); + MockSetBundleInfoFailed(false); +} /** * @tc.name: OnBundleRemoved_00001 @@ -518,6 +557,26 @@ HWTEST_F(AnsUtilsTest, OnBundleDataAdd_00002, Function | SmallTest | Level1) ASSERT_EQ(enable, true); } +/** + * @tc.name: OnBundleDataAdd_00003 + * @tc.desc: Test OnBundleDataAdd + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsUtilsTest, OnBundleDataAdd_00003, Function | SmallTest | Level1) +{ + int notificationId = 1; + sptr bundle = new NotificationBundleOption("test", 1); + TestAddNotification(notificationId, bundle); + MockSetBundleInfoFailed(true); + advancedNotificationService_->OnBundleDataAdd(bundle); + SleepForFC(); + bool enable = false; + NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(bundle, enable); + MockSetBundleInfoFailed(false); + ASSERT_EQ(enable, false); +} + /** * @tc.name: OnBundleDataUpdate_00001 * @tc.desc: Test OnBundleDataUpdate @@ -554,6 +613,27 @@ HWTEST_F(AnsUtilsTest, OnBundleDataUpdate_00002, Function | SmallTest | Level1) ASSERT_EQ(enable, true); } +/** + * @tc.name: OnBundleDataUpdate_00003 + * @tc.desc: Test OnBundleDataUpdate + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsUtilsTest, OnBundleDataUpdate_00003, Function | SmallTest | Level1) +{ + AdvancedNotificationService ans; + int notificationId = 1; + sptr bundle = new NotificationBundleOption("test", 1); + TestAddNotification(notificationId, bundle); + MockSetBundleInfoFailed(true); + advancedNotificationService_->OnBundleDataUpdate(bundle); + SleepForFC(); + MockSetBundleInfoFailed(false); + bool enable = false; + NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(bundle, enable); + ASSERT_EQ(enable, false); +} + /** * @tc.name: GetBundlesOfActiveUser_00001 * @tc.desc: Test GetBundlesOfActiveUser @@ -574,6 +654,25 @@ HWTEST_F(AnsUtilsTest, GetBundlesOfActiveUser_00001, Function | SmallTest | Leve * @tc.require: issue */ HWTEST_F(AnsUtilsTest, ResetDistributedEnabled_00001, Function | SmallTest | Level1) +{ + AdvancedNotificationService ans; + std::string oldKey = "enabledNotificationDistributed-test-88-aaa"; + NotificationPreferences::GetInstance()->SetKvToDb(oldKey, "1", 0); + ans.notificationSvrQueue_ = nullptr; + ans.ResetDistributedEnabled(); + SleepForFC(); + std::string value; + NotificationPreferences::GetInstance()->GetKvFromDb("tableVersion", value, 0); + ASSERT_NE(value, "1"); +} + +/** + * @tc.name: ResetDistributedEnabled_00002 + * @tc.desc: Test ResetDistributedEnabled + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsUtilsTest, ResetDistributedEnabled_00002, Function | SmallTest | Level1) { std::string oldKey = "enabledNotificationDistributed-test-88-aaa"; std::string oldKey1 = "enabledNotificationDistributed-test-88"; @@ -612,6 +711,34 @@ HWTEST_F(AnsUtilsTest, UpdateCloneBundleInfo_00001, Function | SmallTest | Level ASSERT_EQ(enable, true); } +/** + * @tc.name: UpdateCloneBundleInfo_00002 + * @tc.desc: Test UpdateCloneBundleInfo + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsUtilsTest, UpdateCloneBundleInfo_00002, Function | SmallTest | Level1) +{ + AdvancedNotificationService ans; + NotificationCloneBundleInfo cloneBundleInfo; + cloneBundleInfo.SetBundleName("UpdateCloneBundleInfo_00002"); + cloneBundleInfo.SetUid(1); + cloneBundleInfo.SetIsShowBadge(true); + cloneBundleInfo.SetEnableNotification(true); + cloneBundleInfo.SetSlotFlags(63); + NotificationCloneBundleInfo::SlotInfo info; + info.slotType_ = NotificationConstant::SlotType::SOCIAL_COMMUNICATION; + info.enable_ = true; + cloneBundleInfo.AddSlotInfo(info); + ans.notificationSvrQueue_ = nullptr; + ans.UpdateCloneBundleInfo(cloneBundleInfo); + SleepForFC(); + sptr bundle = new NotificationBundleOption("UpdateCloneBundleInfo_00002", 1); + bool enable = false; + NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(bundle, enable); + ASSERT_EQ(enable, false); +} + /** * @tc.name: ExecBatchCancel_00001 * @tc.desc: Test ExecBatchCancel @@ -799,5 +926,122 @@ HWTEST_F(AnsUtilsTest, IsSupportTemplate_00001, Function | SmallTest | Level1) bool support = false; ASSERT_EQ(ans.IsSupportTemplate(templateName, support), (int)ERR_ANS_INVALID_PARAM); } + +/** + * @tc.name: CheckCommonParams_00001 + * @tc.desc: Test CheckCommonParams + * @tc.name: CheckCommonParams_00001 + * @tc.desc: Test CheckCommonParams + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsUtilsTest, CheckCommonParams_00001, Function | SmallTest | Level1) +{ + AdvancedNotificationService ans; + ans.notificationSvrQueue_ = nullptr; + ASSERT_EQ(ans.CheckCommonParams(), (int)ERR_ANS_INVALID_PARAM); +} + +/** + * @tc.name: CheckCommonParams_00002 + * @tc.desc: Test CheckCommonParams + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsUtilsTest, CheckCommonParams_00002, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); + MockIsSystemApp(false); + ASSERT_EQ(advancedNotificationService_->CheckCommonParams(), (int)ERR_ANS_NON_SYSTEM_APP); +} + +/** + * @tc.name: DeleteAllByUser_0001 + * @tc.desc: Test DeleteAllByUser_0001 + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsUtilsTest, DeleteAllByUser_0001, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); + MockIsSystemApp(false); + ASSERT_EQ(advancedNotificationService_->DeleteAllByUser(0), (int)ERR_ANS_NON_SYSTEM_APP); +} + +/** + * @tc.name: DeleteAllByUser_0001 + * @tc.desc: Test DeleteAllByUser_0001 + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsUtilsTest, DeleteAllByUser_0002, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); + MockIsSystemApp(true); + MockIsVerfyPermisson(false); + ASSERT_EQ(advancedNotificationService_->DeleteAllByUser(0), (int)ERR_ANS_PERMISSION_DENIED); +} + +/** + * @tc.name: DeleteAllByUserInner_0001 + * @tc.desc: Test OnUserRemoved_0001 + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsUtilsTest, DeleteAllByUserInner_0001, Function | SmallTest | Level1) +{ + AdvancedNotificationService ans; + ans.notificationSvrQueue_ = nullptr; + ASSERT_EQ(ans.DeleteAllByUserInner(0, 0 ,true), (int)ERR_ANS_INVALID_PARAM); +} + +/** + * @tc.name: CheckBundleOptionValid_0001 + * @tc.desc: Test CheckBundleOptionValid_0001 + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsUtilsTest, CheckBundleOptionValid_0001, Function | SmallTest | Level1) +{ + sptr bundle = nullptr; + ASSERT_EQ(advancedNotificationService_->CheckBundleOptionValid(bundle), (int)ERR_ANS_INVALID_PARAM); + + bundle = new (std::nothrow) NotificationBundleOption("", 1); + ASSERT_EQ(advancedNotificationService_->CheckBundleOptionValid(bundle), (int)ERR_ANS_INVALID_PARAM); + + MockQueryForgroundOsAccountId(false, 0); + bundle = new (std::nothrow) NotificationBundleOption("test", 1); + ASSERT_EQ(advancedNotificationService_->CheckBundleOptionValid(bundle), (int)ERR_ANS_INVALID_BUNDLE); + + MockQueryForgroundOsAccountId(true, 0); + ASSERT_EQ(advancedNotificationService_->CheckBundleOptionValid(bundle), (int)ERR_OK); + + sptr bundle1 = new (std::nothrow) NotificationBundleOption("test", 1); + MockQueryForgroundOsAccountId(true, 2); + ASSERT_EQ(advancedNotificationService_->CheckBundleOptionValid(bundle1), (int)ERR_OK); + + sptr bundle2 = new (std::nothrow) NotificationBundleOption("test", 0); + ASSERT_EQ(advancedNotificationService_->CheckBundleOptionValid(bundle2), (int)ERR_ANS_INVALID_BUNDLE); + + sptr bundle3 = new (std::nothrow) NotificationBundleOption("test", 0); + MockQueryForgroundOsAccountId(true, 0); + ASSERT_EQ(advancedNotificationService_->CheckBundleOptionValid(bundle3), (int)ERR_OK); +} + +/** + * @tc.name: SubmitAsyncTask_0001 + * @tc.desc: Test SubmitAsyncTask_0001 + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsUtilsTest, SubmitAsyncTask_0001, Function | SmallTest | Level1) +{ + ErrCode result = ERR_OK; + advancedNotificationService_->SubmitAsyncTask(std::bind([&]() { + result = ERR_ANS_INVALID_PARAM; + })); + SleepForFC(); + ASSERT_EQ(result, (int)ERR_ANS_INVALID_PARAM); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/test/unittest/mock/include/mock_bundle_mgr.h b/services/ans/test/unittest/mock/include/mock_bundle_mgr.h index 1eebaa2bc..a2e4e12ee 100644 --- a/services/ans/test/unittest/mock/include/mock_bundle_mgr.h +++ b/services/ans/test/unittest/mock/include/mock_bundle_mgr.h @@ -46,6 +46,8 @@ namespace Notification { void MockIsNonBundleName(bool isNonBundleName); void MockDistributedNotificationEnabled(bool isEnable); + +void MockSetBundleInfoFailed(bool getFail); } // namespace Notification } // namespace OHOS diff --git a/services/ans/test/unittest/mock/mock_bundle_mgr.cpp b/services/ans/test/unittest/mock/mock_bundle_mgr.cpp index 2765549cd..009aed45f 100644 --- a/services/ans/test/unittest/mock/mock_bundle_mgr.cpp +++ b/services/ans/test/unittest/mock/mock_bundle_mgr.cpp @@ -25,6 +25,12 @@ namespace { bool g_isNonBundleName = false; bool g_isEnable = true; bool g_setBundleInfoEnabled = false; +bool g_getBundleInfoFailed = false; +} + +void MockSetBundleInfoFailed(bool getFail) +{ + g_getBundleInfoFailed = getFail; } void MockSetBundleInfoEnabled(bool enabled) @@ -55,6 +61,9 @@ ErrCode BundleMgrProxy::GetNameForUid(const int uid, std::string &name) bool BundleMgrProxy::GetBundleInfo(const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, int32_t userId) { + if (Notification::g_getBundleInfoFailed) { + return false; + } if (Notification::g_setBundleInfoEnabled) { bundleInfo.applicationInfo.allowEnableNotification = true; } diff --git a/services/ans/test/unittest/notification_dialog_test/mock_os_account_manager_annex.cpp b/services/ans/test/unittest/notification_dialog_test/mock_os_account_manager_annex.cpp index 8be5780b3..11400fc0c 100644 --- a/services/ans/test/unittest/notification_dialog_test/mock_os_account_manager_annex.cpp +++ b/services/ans/test/unittest/notification_dialog_test/mock_os_account_manager_annex.cpp @@ -31,6 +31,10 @@ void MockQueryForgroundOsAccountId(bool mockRet, uint8_t mockCase) g_mockId = 101; // 101 mockcase1 break; } + case 2: { + g_mockId = 0; // 0 mockcase1 + break; + } default: { g_mockId = 100; // 100 mockdefault break; -- Gitee From 5fcfd2f03f80f173f0800881f8ae0553d37499f4 Mon Sep 17 00:00:00 2001 From: songbao1 Date: Mon, 31 Mar 2025 16:53:01 +0800 Subject: [PATCH 2/5] =?UTF-8?q?tdd=E8=A6=86=E7=9B=96=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: songbao1 --- .../unittest/advanced_notification_service_ability_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/ans/test/unittest/advanced_notification_service_ability_test.cpp b/services/ans/test/unittest/advanced_notification_service_ability_test.cpp index dc0da64c6..b4d9d3577 100644 --- a/services/ans/test/unittest/advanced_notification_service_ability_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_ability_test.cpp @@ -141,10 +141,10 @@ HWTEST_F( bool runOnCreate = true; std::string deviceId = "deviceId"; AdvancedNotificationServiceAbility test(systemAbilityId, runOnCreate); - + test.OnAddSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID, deviceId); test.OnAddSystemAbility(COMMON_EVENT_SERVICE_ID, deviceId); test.OnRemoveSystemAbility(COMMON_EVENT_SERVICE_ID, deviceId); - EXPECT_EQ(test.isDatashaReready_, false); + EXPECT_EQ(test.isDatashaReready_, true); } /** -- Gitee From 372df391bb91ca6628cf736cce48ae01865ddcb6 Mon Sep 17 00:00:00 2001 From: songbao1 Date: Mon, 31 Mar 2025 17:13:39 +0800 Subject: [PATCH 3/5] =?UTF-8?q?tdd=E8=A6=86=E7=9B=96=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: songbao1 --- .../ans/test/unittest/advanced_notification_service_test.cpp | 2 +- services/ans/test/unittest/advanced_notification_utils_test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 25333950b..773c71db6 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -4023,7 +4023,7 @@ HWTEST_F(AdvancedNotificationServiceTest, SetNotificationRemindType_00002, Funct HWTEST_F(AdvancedNotificationServiceTest, SetNotificationRemindType_00003, Function | SmallTest | Level1) { advancedNotificationService_->localScreenOn_ = true; - advancedNotificationService_->distributedReminderPolicy_ = + advancedNotificationService_->distributedReminderPolicy_ = NotificationConstant::DistributedReminderPolicy::ALWAYS_REMIND; auto reminderType = advancedNotificationService_->GetRemindType(); ASSERT_EQ(reminderType, NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND); diff --git a/services/ans/test/unittest/advanced_notification_utils_test.cpp b/services/ans/test/unittest/advanced_notification_utils_test.cpp index 1ba076753..510ba51ca 100644 --- a/services/ans/test/unittest/advanced_notification_utils_test.cpp +++ b/services/ans/test/unittest/advanced_notification_utils_test.cpp @@ -992,7 +992,7 @@ HWTEST_F(AnsUtilsTest, DeleteAllByUserInner_0001, Function | SmallTest | Level1) { AdvancedNotificationService ans; ans.notificationSvrQueue_ = nullptr; - ASSERT_EQ(ans.DeleteAllByUserInner(0, 0 ,true), (int)ERR_ANS_INVALID_PARAM); + ASSERT_EQ(ans.DeleteAllByUserInner(0, 0 , true), (int)ERR_ANS_INVALID_PARAM); } /** -- Gitee From 3413164894d089aed841f610821db0118267a1b8 Mon Sep 17 00:00:00 2001 From: songbao1 Date: Mon, 31 Mar 2025 17:21:39 +0800 Subject: [PATCH 4/5] =?UTF-8?q?tdd=E8=A6=86=E7=9B=96=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: songbao1 --- .../ans/test/unittest/advanced_notification_service_test.cpp | 2 +- services/ans/test/unittest/advanced_notification_utils_test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 773c71db6..ea8a1b59d 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -4037,7 +4037,7 @@ HWTEST_F(AdvancedNotificationServiceTest, SetNotificationRemindType_00003, Funct HWTEST_F(AdvancedNotificationServiceTest, SetNotificationRemindType_00004, Function | SmallTest | Level1) { advancedNotificationService_->localScreenOn_ = false; - advancedNotificationService_->distributedReminderPolicy_ = + advancedNotificationService_->distributedReminderPolicy_ = NotificationConstant::DistributedReminderPolicy::DO_NOT_REMIND; auto reminderType = advancedNotificationService_->GetRemindType(); ASSERT_EQ(reminderType, NotificationConstant::RemindType::DEVICE_IDLE_DONOT_REMIND); diff --git a/services/ans/test/unittest/advanced_notification_utils_test.cpp b/services/ans/test/unittest/advanced_notification_utils_test.cpp index 510ba51ca..745f91f7c 100644 --- a/services/ans/test/unittest/advanced_notification_utils_test.cpp +++ b/services/ans/test/unittest/advanced_notification_utils_test.cpp @@ -992,7 +992,7 @@ HWTEST_F(AnsUtilsTest, DeleteAllByUserInner_0001, Function | SmallTest | Level1) { AdvancedNotificationService ans; ans.notificationSvrQueue_ = nullptr; - ASSERT_EQ(ans.DeleteAllByUserInner(0, 0 , true), (int)ERR_ANS_INVALID_PARAM); + ASSERT_EQ(ans.DeleteAllByUserInner(0, 0, true), (int)ERR_ANS_INVALID_PARAM); } /** -- Gitee From 876c24c3cbed882437cb911b22d6d16ed6253d1f Mon Sep 17 00:00:00 2001 From: songbao1 Date: Mon, 31 Mar 2025 17:32:16 +0800 Subject: [PATCH 5/5] =?UTF-8?q?tdd=E8=A6=86=E7=9B=96=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: songbao1 --- .../ans/test/unittest/advanced_notification_service_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index ea8a1b59d..2cd9abed3 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -4051,7 +4051,7 @@ HWTEST_F(AdvancedNotificationServiceTest, SetNotificationRemindType_00004, Funct HWTEST_F(AdvancedNotificationServiceTest, SetNotificationRemindType_00005, Function | SmallTest | Level1) { advancedNotificationService_->localScreenOn_ = true; - advancedNotificationService_->distributedReminderPolicy_ = + advancedNotificationService_->distributedReminderPolicy_ = NotificationConstant::DistributedReminderPolicy::DO_NOT_REMIND; auto reminderType = advancedNotificationService_->GetRemindType(); ASSERT_EQ(reminderType, NotificationConstant::RemindType::DEVICE_ACTIVE_DONOT_REMIND); -- Gitee