diff --git a/frameworks/js/napi/include/disable_notification.h b/frameworks/js/napi/include/disable_notification.h index 63a64c4097f8fad5eb7015c1cf551bcc4a4deefb..0a314cb379ef96b82e722d873a0f3cbfc5ffc62f 100644 --- a/frameworks/js/napi/include/disable_notification.h +++ b/frameworks/js/napi/include/disable_notification.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/js/napi/src/disable_notification.cpp b/frameworks/js/napi/src/disable_notification.cpp index ea7585138927756eeeb2b2472fa060e7812781d4..d123c24f00dfc09e67f24ba29f7fe26cafa54d19 100644 --- a/frameworks/js/napi/src/disable_notification.cpp +++ b/frameworks/js/napi/src/disable_notification.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 71081debe27c3b5875bce1fb75dae6d3dc7f6f8c..fd9c38dc9cabaf7fc557b1c2e4be1f985459ca2d 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/services/ans/include/notification_preferences_database.h b/services/ans/include/notification_preferences_database.h index 1c1db201c9544400c6ddd458dcff23291a9b184d..df6a250c5b9dbd01b9455451da39735f82c47265 100644 --- a/services/ans/include/notification_preferences_database.h +++ b/services/ans/include/notification_preferences_database.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/services/ans/include/notification_preferences_info.h b/services/ans/include/notification_preferences_info.h index 634438f055f0a95c3abea799f63af1f8cc15844a..038a43a7feb6ca95cd56479e7518a030ff3626ad 100644 --- a/services/ans/include/notification_preferences_info.h +++ b/services/ans/include/notification_preferences_info.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 97ff7934dbd8dd470a44ca6a8972ab85a552f0e5..fdd2648dcc0dd319b1afe67bf6591a82eba37136 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -2096,6 +2096,17 @@ ErrCode AdvancedNotificationService::DisableNotificationFeature(const sptrwait(handler); if (notificationDisable->GetDisabled()) { + int32_t userId = notificationDisable->GetUserId(); + if (userId != SUBSCRIBE_USER_INIT) { + int32_t currentUserId = SUBSCRIBE_USER_INIT; + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(currentUserId) != ERR_OK) { + ANS_LOGD("GetCurrentActiveUserId failed"); + return ERR_OK; + } + if (currentUserId != userId) { + return ERR_OK; + } + } std::vector bundleList = notificationDisable->GetBundleList(); for (auto bundle : bundleList) { RemoveAllNotificationsByBundleName( diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index 85fff5b988618f314b12868c52bf76387cfd6fd3..34c2705e80e506c7f1491f7914dd3b438c5d00c2 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index 93fea262810b7002846e37129506dbc25940fbe8..d57653161879a8de9876865bc338e3e442f3a5ea 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -1,5 +1,5 @@ /*os_account_manager - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/services/ans/src/notification_preferences_info.cpp b/services/ans/src/notification_preferences_info.cpp index 5609b65586f8f3a96fa2f41d45a31d5e033a1bd2..1a9fd99af1ed740e526f8ad0ea12639e878e7683 100644 --- a/services/ans/src/notification_preferences_info.cpp +++ b/services/ans/src/notification_preferences_info.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at 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 651d3ed5a0893dec3189b2f06dc8d1c170b26239..8070356d0b5627af9a16b525bde8b45f43b07377 100644 --- a/services/ans/test/unittest/advanced_notification_publish_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_publish_service_test.cpp @@ -38,6 +38,7 @@ #include "string_wrapper.h" #include "want_params.h" #include "int_wrapper.h" +#include "os_account_manager_helper.h" extern void MockIsOsAccountExists(bool exists); extern void MockGetOsAccountLocalIdFromUid(bool mockRet, uint8_t mockCase); @@ -2655,16 +2656,19 @@ HWTEST_F(AnsPublishServiceTest, IsDisableNotification_003, Function | SmallTest if (defaultPolicy) { system::SetBoolParameter("persist.edm.notification_disable", false); } + int32_t userId = -1; + EXPECT_EQ(OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId), ERR_OK); std::string bundleName = "com.testDisableNotification.example"; NotificationDisable notificationDisable; std::vector bundleList = {bundleName}; notificationDisable.SetDisabled(true); notificationDisable.SetBundleList(bundleList); - notificationDisable.SetUserId(100); + notificationDisable.SetUserId(userId); sptr notificationDisablePtr = new (std::nothrow) NotificationDisable(notificationDisable); NotificationPreferences::GetInstance()->preferencesInfo_.SetDisableNotificationInfo(notificationDisablePtr); bool result = advancedNotificationService_->IsDisableNotification(bundleName); - ASSERT_FALSE(result); + EXPECT_TRUE(result); + NotificationPreferences::GetInstance()->preferencesInfo_.userDisableNotificationInfo_.clear(); system::SetBoolParameter("persist.edm.notification_disable", defaultPolicy); } @@ -2680,16 +2684,19 @@ HWTEST_F(AnsPublishServiceTest, IsDisableNotification_004, Function | SmallTest if (defaultPolicy) { system::SetBoolParameter("persist.edm.notification_disable", false); } + int32_t userId = -1; + EXPECT_EQ(OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId), ERR_OK); std::string bundleName = "com.testDisableNotification.example"; NotificationDisable notificationDisable; std::vector bundleList = {bundleName}; notificationDisable.SetDisabled(false); notificationDisable.SetBundleList(bundleList); - notificationDisable.SetUserId(101); + notificationDisable.SetUserId(userId); sptr notificationDisablePtr = new (std::nothrow) NotificationDisable(notificationDisable); NotificationPreferences::GetInstance()->preferencesInfo_.SetDisableNotificationInfo(notificationDisablePtr); bool result = advancedNotificationService_->IsDisableNotification(bundleName); - ASSERT_FALSE(result); + EXPECT_FALSE(result); + NotificationPreferences::GetInstance()->preferencesInfo_.userDisableNotificationInfo_.clear(); system::SetBoolParameter("persist.edm.notification_disable", defaultPolicy); } @@ -2705,16 +2712,17 @@ HWTEST_F(AnsPublishServiceTest, IsDisableNotification_005, Function | SmallTest if (defaultPolicy) { system::SetBoolParameter("persist.edm.notification_disable", false); } + int32_t userId = -1; + EXPECT_EQ(OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId), ERR_OK); std::string bundleName = "com.testDisableNotification.example"; NotificationDisable notificationDisable; - std::vector bundleList = {bundleName}; notificationDisable.SetDisabled(true); - notificationDisable.SetBundleList(bundleList); - notificationDisable.SetUserId(101); + notificationDisable.SetUserId(userId); sptr notificationDisablePtr = new (std::nothrow) NotificationDisable(notificationDisable); NotificationPreferences::GetInstance()->preferencesInfo_.SetDisableNotificationInfo(notificationDisablePtr); bool result = advancedNotificationService_->IsDisableNotification(bundleName); - ASSERT_TRUE(result); + EXPECT_FALSE(result); + NotificationPreferences::GetInstance()->preferencesInfo_.userDisableNotificationInfo_.clear(); system::SetBoolParameter("persist.edm.notification_disable", defaultPolicy); } diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 4f6b14e20aa94a3f086eb20c5ee6ac6a78eb110d..8a23961ce10f756d09c1c1a12a78f08507a82ce0 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -41,6 +41,7 @@ #include "notification_subscriber.h" #include "notification_subscriber_manager.h" #include "mock_push_callback_stub.h" +#include "os_account_manager_helper.h" #include "system_event_observer.h" #include "notification_constant.h" #include "want_agent_info.h" @@ -4633,5 +4634,26 @@ HWTEST_F(AdvancedNotificationServiceTest, IsSilentReminderEnabled_00001, Functio ret = advancedNotificationService.IsSilentReminderEnabled(bo, enableStatusInt); ASSERT_EQ(ret, ERR_ANS_INVALID_PARAM); } + +/** + * @tc.number : DisableNotificationFeature_00002 + * @tc.name : Test DisableNotificationFeature + * @tc.desc : Test DisableNotificationFeature + * @tc.require : issueI5S4VP + */ +HWTEST_F(AdvancedNotificationServiceTest, DisableNotificationFeature_00002, Function | SmallTest | Level1) +{ + sptr notificationDisable = new (std::nothrow) NotificationDisable(); + int32_t userId = -1; + EXPECT_EQ(OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId), ERR_OK); + notificationDisable->SetDisabled(true); + notificationDisable->SetUserId(userId); + auto ret = advancedNotificationService_->DisableNotificationFeature(notificationDisable); + EXPECT_EQ(ret, ERR_OK); + userId++; + notificationDisable->SetUserId(userId); + ret = advancedNotificationService_->DisableNotificationFeature(notificationDisable); + EXPECT_EQ(ret, ERR_OK); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/test/unittest/notification_preferences_info_test.cpp b/services/ans/test/unittest/notification_preferences_info_test.cpp index fdfbc2595fae40ae665c79146fdca3f6f1c55842..b0bf823031a9fe5b3341d243b97ce61128cf5b87 100644 --- a/services/ans/test/unittest/notification_preferences_info_test.cpp +++ b/services/ans/test/unittest/notification_preferences_info_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -462,5 +462,28 @@ HWTEST_F(NotificationPreferencesInfoTest, GetDisableNotificationInfo_0600, TestS bool ret = preferencesInfo->GetUserDisableNotificationInfo(101, disable); EXPECT_TRUE(ret); } + +/** + * @tc.name: GetDisableNotificationInfo_0700 + * @tc.desc: test GetDisableNotificationInfo. + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesInfoTest, GetDisableNotificationInfo_0700, TestSize.Level1) +{ + std::shared_ptr preferencesInfo = std::make_shared(); + NotificationPreferencesInfo::DisableNotificationInfo disableNotificationInfo; + disableNotificationInfo.disabled = -1; + preferencesInfo->userDisableNotificationInfo_.insert_or_assign(101, disableNotificationInfo); + NotificationDisable disable; + bool ret = preferencesInfo->GetUserDisableNotificationInfo(101, disable); + EXPECT_FALSE(ret); + disableNotificationInfo.disabled = 1; + preferencesInfo->userDisableNotificationInfo_.insert_or_assign(101, disableNotificationInfo); + ret = preferencesInfo->GetUserDisableNotificationInfo(101, disable); + EXPECT_FALSE(ret); + ret = preferencesInfo->GetUserDisableNotificationInfo(102, disable); + EXPECT_FALSE(ret); + preferencesInfo->userDisableNotificationInfo_.clear(); +} } } diff --git a/services/ans/test/unittest/notification_preferences_test.cpp b/services/ans/test/unittest/notification_preferences_test.cpp index 7bb4c203bbaf831be569c1a9282de78cba15cf87..8ef9a86640ff41609b1547a2aaf02db738dbbf74 100644 --- a/services/ans/test/unittest/notification_preferences_test.cpp +++ b/services/ans/test/unittest/notification_preferences_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -2589,5 +2589,22 @@ HWTEST_F(NotificationPreferencesTest, GetkioskAppTrustList_008, Function | Small auto ret = notificationPreferences.GetkioskAppTrustList(resultList); ASSERT_EQ(ret, true); } + +/** + * @tc.name: GetUserDisableNotificationInfo_001 + * @tc.desc: test GetUserDisableNotificationInfo. + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetUserDisableNotificationInfo_001, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + notificationPreferences.preferencesInfo_ = NotificationPreferencesInfo(); + NotificationDisable disable; + bool ret = NotificationPreferences::GetInstance()->GetUserDisableNotificationInfo(105, disable); + EXPECT_FALSE(ret); + notificationPreferences.preferncesDB_ = nullptr; + ret = notificationPreferences.GetUserDisableNotificationInfo(105, disable); + EXPECT_FALSE(ret); +} } // namespace Notification } // namespace OHOS