From 7fbf577b2ad8d990396910827b546b56cc947890 Mon Sep 17 00:00:00 2001 From: zhengzhuolan Date: Tue, 8 Jul 2025 16:31:16 +0800 Subject: [PATCH] fix bug Signed-off-by: zhengzhuolan --- .../js/napi/include/disable_notification.h | 2 +- .../js/napi/src/disable_notification.cpp | 2 +- .../ans/include/notification_preferences.h | 2 +- .../notification_preferences_database.h | 2 +- .../include/notification_preferences_info.h | 2 +- .../ans/src/advanced_notification_service.cpp | 11 ++++++++ services/ans/src/notification_preferences.cpp | 2 +- .../src/notification_preferences_database.cpp | 2 +- .../ans/src/notification_preferences_info.cpp | 2 +- ...nced_notification_publish_service_test.cpp | 24 ++++++++++++------ .../advanced_notification_service_test.cpp | 22 ++++++++++++++++ .../notification_preferences_info_test.cpp | 25 ++++++++++++++++++- .../notification_preferences_test.cpp | 19 +++++++++++++- 13 files changed, 99 insertions(+), 18 deletions(-) diff --git a/frameworks/js/napi/include/disable_notification.h b/frameworks/js/napi/include/disable_notification.h index 63a64c409..0a314cb37 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 ea7585138..d123c24f0 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 71081debe..fd9c38dc9 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 1c1db201c..df6a250c5 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 634438f05..038a43a7f 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 97ff7934d..fdd2648dc 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 85fff5b98..34c2705e8 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 93fea2628..d57653161 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 5609b6558..1a9fd99af 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 651d3ed5a..8070356d0 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 4f6b14e20..8a23961ce 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 fdfbc2595..b0bf82303 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 7bb4c203b..8ef9a8664 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 -- Gitee