From 8495d11d4f0136b0d697f01766d7ffe9d8d3d982 Mon Sep 17 00:00:00 2001 From: cheerful_ricky Date: Thu, 27 Mar 2025 11:43:01 +0800 Subject: [PATCH] add notificationExtensionWrapper unit test Signed-off-by: cheerful_ricky --- services/ans/test/unittest/BUILD.gn | 47 +++ .../notification_extension_wrapper_test.cpp | 385 ++++++++++++++++++ 2 files changed, 432 insertions(+) create mode 100644 services/ans/test/unittest/notification_extension_wrapper_test.cpp diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index 19aad802a..098cd250c 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -1341,6 +1341,52 @@ ohos_unittest("advanced_datashare_observer_unit_test") { part_name = "${component_name}" } +ohos_unittest("notification_extension_wrapper_unit_test") { + module_out_path = module_output_path + include_dirs = [ + ".", + "include", + "/${services_path}/ans/include", + "${services_path}/ans/test/unittest/mock/include", + ] + defines = [] + + sources = [ "notification_extension_wrapper_test.cpp" ] + + deps = [ + "${frameworks_module_ans_path}:ans_innerkits", + "${services_path}/ans:libans", + ] + + external_deps = [ + "ability_runtime:dataobs_manager", + "c_utils:utils", + "googletest:gtest_main", + "hilog:libhilog", + ] + + if (distributed_notification_service_feature_summary || + distributed_notification_service_feature_additional_control || + distributed_notification_service_feature_privileged_message) { + defines += [ "ENABLE_ANS_EXT_WRAPPER" ] + } + + if (distributed_notification_service_feature_privileged_message) { + defines += [ "ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER" ] + } + + if (distributed_notification_service_feature_additional_control) { + defines += [ "ENABLE_ANS_ADDITIONAL_CONTROL" ] + } + + if (distributed_notification_service_feature_summary) { + defines += [ "ENABLE_ANS_AGGREGATION" ] + } + + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + ohos_unittest("advanced_notification_service_unit_test") { sanitize = { integer_overflow = true @@ -1407,6 +1453,7 @@ group("unittest") { ":disturb_manager_unit_test", ":notification_config_parse_test", ":notification_dialog_test", + ":notification_extension_wrapper_unit_test", ":notification_preferences_database_branch_test", ":notification_preferences_database_test", ":notification_preferences_test", diff --git a/services/ans/test/unittest/notification_extension_wrapper_test.cpp b/services/ans/test/unittest/notification_extension_wrapper_test.cpp new file mode 100644 index 000000000..9c6fe14e5 --- /dev/null +++ b/services/ans/test/unittest/notification_extension_wrapper_test.cpp @@ -0,0 +1,385 @@ +/* + * Copyright (c) 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "gtest/gtest.h" +#define private public +#include "notification_extension_wrapper.h" +#undef private + +using namespace testing; +using namespace testing::ext; +namespace OHOS { +namespace Notification { +const int32_t ACTIVE_DELETE = 0; +const int32_t PASSITIVE_DELETE = 1; + +static bool g_mockCalled = false; +static bool g_mockLocalSwitch = false; +static int g_mockUpdateByCancelReason = 0; + +static void MockSetLocalSwitch(bool status) +{ + g_mockCalled = true; + g_mockLocalSwitch = status; +} + +static void MockUpdateByCancel(const std::vector>& notifications, int deleteType) +{ + g_mockCalled = true; + g_mockUpdateByCancelReason = deleteType; +} + +class NotificationExtensionWrapperTest : public ::testing::Test { +protected: + void SetUp() override {} + void TearDown() override {} + static void SetUpTestCas() + { + g_mockCalled = false; + g_mockLocalSwitch = false; + g_mockUpdateByCancelReason = 0; + } + static void TearDownTestCase() {}; +}; + +HWTEST_F(NotificationExtensionWrapperTest, InitExtentionWrapper_Test, TestSize.Level0) +{ + OHOS::Notification::ExtensionWrapper extensionWrapper; + extensionWrapper.InitExtentionWrapper(); +#ifdef ENABLE_ANS_EXT_WRAPPER + // 验证extensionWrapperHandle_是否被正确初始化 + EXPECT_NE(extensionWrapper.extensionWrapperHandle_, nullptr); + + // 验证syncAdditionConfig_是否被正确初始化 + EXPECT_NE(extensionWrapper.syncAdditionConfig_, nullptr); +#else + EXPECT_EQ(extensionWrapper.extensionWrapperHandle_, nullptr); + EXPECT_EQ(extensionWrapper.syncAdditionConfig_, nullptr); +#endif + + // 验证localControl_、reminderControl_、bannerControl_是否被正确初始化 +#ifdef ENABLE_ANS_ADDITIONAL_CONTROL + EXPECT_NE(extensionWrapper.localControl_, nullptr); + EXPECT_NE(extensionWrapper.reminderControl_, nullptr); + EXPECT_NE(extensionWrapper.bannerControl_, nullptr); +#endif + + // 验证modifyReminderFlags_是否被正确初始化 +#ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER + EXPECT_NE(extensionWrapper.modifyReminderFlags_, nullptr); +#endif + + // 验证initSummary_是否被正确初始化 +#ifdef ENABLE_ANS_AGGREGATION + EXPECT_NE(extensionWrapper.initSummary_, nullptr); +#endif +} + + +HWTEST_F(NotificationExtensionWrapperTest, CheckIfSetlocalSwitch_001, TestSize.Level0) +{ + // 创建ExtensionWrapper对象 + ExtensionWrapper extensionWrapper; + // 设置extensionWrapperHandle_为nullptr + extensionWrapper.extensionWrapperHandle_ = nullptr; + // 调用待测函数 + extensionWrapper.CheckIfSetlocalSwitch(); + // 验证extensionWrapperHandle_仍然为nullptr + EXPECT_EQ(extensionWrapper.extensionWrapperHandle_, nullptr); +} + +HWTEST_F(NotificationExtensionWrapperTest, CheckIfSetlocalSwitch_002, TestSize.Level0) +{ + // 创建ExtensionWrapper对象 + ExtensionWrapper extensionWrapper; + // 设置extensionWrapperHandle_不为nullptr + extensionWrapper.extensionWrapperHandle_ = new int; + // 设置isRegisterDataSettingObserver为false + extensionWrapper.isRegisterDataSettingObserver = false; + // 调用待测函数 + extensionWrapper.CheckIfSetlocalSwitch(); + // 验证isRegisterDataSettingObserver为true + EXPECT_EQ(extensionWrapper.isRegisterDataSettingObserver, true); +} + +HWTEST_F(NotificationExtensionWrapperTest, CheckIfSetlocalSwitch_003, TestSize.Level0) +{ + // 创建ExtensionWrapper对象 + ExtensionWrapper extensionWrapper; + // 设置extensionWrapperHandle_不为nullptr + extensionWrapper.extensionWrapperHandle_ = new int; + // 设置isRegisterDataSettingObserver为true + extensionWrapper.isRegisterDataSettingObserver = true; + // 调用待测函数 + extensionWrapper.CheckIfSetlocalSwitch(); + // 验证isRegisterDataSettingObserver仍然为true + EXPECT_EQ(extensionWrapper.isRegisterDataSettingObserver, true); +} + +HWTEST_F(NotificationExtensionWrapperTest, SetlocalSwitch_False_Test, TestSize.Level0) +{ + OHOS::Notification::ExtensionWrapper extensionWrapper; + std::string enable = "false"; + extensionWrapper.setLocalSwitch_ = reinterpret_cast(&MockSetLocalSwitch); + + extensionWrapper.SetlocalSwitch(enable); + + EXPECT_TRUE(g_mockCalled); + EXPECT_FALSE(g_mockLocalSwitch); +} + +HWTEST_F(NotificationExtensionWrapperTest, SetlocalSwitch_True_Test, TestSize.Level0) +{ + OHOS::Notification::ExtensionWrapper extensionWrapper; + std::string enable = "true"; + extensionWrapper.setLocalSwitch_ = reinterpret_cast(&MockSetLocalSwitch); + + extensionWrapper.SetlocalSwitch(enable); + + EXPECT_TRUE(g_mockCalled); + EXPECT_TRUE(g_mockLocalSwitch); +} + +HWTEST_F(NotificationExtensionWrapperTest, SyncAdditionConfig_NullSyncAdditionConfig, TestSize.Level0) +{ + ExtensionWrapper extensionWrapper; + ErrCode result = extensionWrapper.SyncAdditionConfig("key", "value"); + EXPECT_EQ(result, 0); +} + +HWTEST_F(NotificationExtensionWrapperTest, SyncAdditionConfig_ValidSyncAdditionConfig, TestSize.Level0) +{ + ExtensionWrapper extensionWrapper; + extensionWrapper.syncAdditionConfig_ = [](const std::string& key, const std::string& value) { + return 1; + }; + ErrCode result = extensionWrapper.SyncAdditionConfig("key", "value"); + EXPECT_EQ(result, 1); +} + +HWTEST_F(NotificationExtensionWrapperTest, UpdateByCancel_NullUpdateByCancel, TestSize.Level0) +{ + // Arrange + ExtensionWrapper wrapper; + std::vector> notifications; + int deleteReason = 1; + int expectedReason = 0; + + wrapper.UpdateByCancel(notifications, deleteReason); + + EXPECT_NE(expectedReason, deleteReason); +} + +HWTEST_F(NotificationExtensionWrapperTest, UpdateByCancel_Normal_Test, TestSize.Level0) { + ExtensionWrapper wrapper; + wrapper.updateByCancel_ = reinterpret_cast(&MockUpdateByCancel); + + std::vector> notifications; + int deleteReason = 5; + + wrapper.UpdateByCancel(notifications, deleteReason); + + EXPECT_TRUE(g_mockCalled); + EXPECT_EQ(g_mockUpdateByCancelReason, 1); +} + +HWTEST_F(NotificationExtensionWrapperTest, GetUnifiedGroupInfo_NullFunction, TestSize.Level0) +{ + OHOS::Notification::ExtensionWrapper extensionWrapper; + OHOS::sptr request = nullptr; + EXPECT_EQ(extensionWrapper.GetUnifiedGroupInfo(request), 0); +} + +HWTEST_F(NotificationExtensionWrapperTest, GetUnifiedGroupInfo_ValidFunction, TestSize.Level0) +{ + OHOS::Notification::ExtensionWrapper extensionWrapper; + OHOS::sptr request = new OHOS::Notification::NotificationRequest(); + extensionWrapper.getUnifiedGroupInfo_ = [](const OHOS::sptr &request) { + return 1; + }; + EXPECT_EQ(extensionWrapper.GetUnifiedGroupInfo(request), 1); +} + + +HWTEST_F(NotificationExtensionWrapperTest, ReminderControl_NullReminderControl, TestSize.Level0) +{ + OHOS::Notification::ExtensionWrapper extensionWrapper; + std::string bundleName = "testBundle"; + int32_t result = extensionWrapper.ReminderControl(bundleName); + EXPECT_EQ(result, 0); +} + +HWTEST_F(NotificationExtensionWrapperTest, ReminderControl_ValidReminderControl, TestSize.Level0) +{ + OHOS::Notification::ExtensionWrapper extensionWrapper; + std::string bundleName = "testBundle"; + extensionWrapper.reminderControl_ = [](const std::string &bundleName) { return 1; }; + int32_t result = extensionWrapper.ReminderControl(bundleName); + EXPECT_EQ(result, 1); +} + +HWTEST_F(NotificationExtensionWrapperTest, BannerControl_NullBannerControl, TestSize.Level0) +{ + // Arrange + ExtensionWrapper wrapper; + std::string bundleName = "testBundle"; + + // Act + int32_t result = wrapper.BannerControl(bundleName); + + // Assert + EXPECT_EQ(-1, result); +} + +HWTEST_F(NotificationExtensionWrapperTest, BannerControl_ValidBannerControl, TestSize.Level0) +{ + // Arrange + ExtensionWrapper wrapper; + std::string bundleName = "testBundle"; + auto mockBannerControl = [](const std::string &bundleName) { return 0; }; + wrapper.bannerControl_ = mockBannerControl; + + // Act + int32_t result = wrapper.BannerControl(bundleName); + + // Assert + EXPECT_EQ(0, result); +} + +#ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER +HWTEST_F(NotificationExtensionWrapperTest, ModifyReminderFlags_NullCase, TestSize.Level0) { + // Arrange + OHOS::Notification::ExtensionWrapper wrapper; + wrapper.modifyReminderFlags_ = nullptr; + auto request = new NotificationRequest(); + + // Act + bool result = wrapper.ModifyReminderFlags(request); + + // Assert + ASSERT_FALSE(result); +} + +HWTEST_F(NotificationExtensionWrapperTest, ModifyReminderFlags_SuccessCase, TestSize.Level0) { + // Arrange + OHOS::Notification::ExtensionWrapper wrapper; + bool (*mockFunc)(const sptr &) = [](const sptr &req) { + return true; + }; + wrapper.modifyReminderFlags_ = mockFunc; + auto request = new NotificationRequest(); + + // Act + bool result = wrapper.ModifyReminderFlags(request); + + // Assert + ASSERT_TRUE(result); +} +#endif +HWTEST_F(NotificationExtensionWrapperTest, LocalControl_NullCase, TestSize.Level0) { + // Arrange + OHOS::Notification::ExtensionWrapper wrapper; + wrapper.localControl_ = nullptr; + auto request = new NotificationRequest(); + + // Act + int32_t result = wrapper.LocalControl(request); + + // Assert + ASSERT_EQ(0, result); // 预期返回 0 +} + +HWTEST_F(NotificationExtensionWrapperTest, LocalControl_SuccessCase, TestSize.Level0) { + // Arrange + OHOS::Notification::ExtensionWrapper wrapper; + int32_t (*mockFunc)(const sptr &) = [](const sptr &req) { + return 1; + }; + wrapper.localControl_ = mockFunc; + auto request = new NotificationRequest(); + + // Act + int32_t result = wrapper.LocalControl(request); + + // Assert + ASSERT_EQ(1, result); +} + +HWTEST_F(NotificationExtensionWrapperTest, convertToDelType_ActiveDelete, TestSize.Level0) +{ + // Arrange + int32_t deleteReason = NotificationConstant::PACKAGE_CHANGED_REASON_DELETE + 1; + int32_t expectedDelType = ACTIVE_DELETE; + + // Act + int32_t actualDelType = OHOS::Notification::ExtensionWrapper::convertToDelType(deleteReason); + + // Assert + ASSERT_EQ(expectedDelType, actualDelType); +} + +HWTEST_F(NotificationExtensionWrapperTest, convertToDelType_PassiveDelete, TestSize.Level0) +{ + // Arrange + int32_t deleteReason = NotificationConstant::PACKAGE_CHANGED_REASON_DELETE; + int32_t expectedDelType = PASSITIVE_DELETE; + + // Act + int32_t actualDelType = OHOS::Notification::ExtensionWrapper::convertToDelType(deleteReason); + + // Assert + ASSERT_EQ(expectedDelType, actualDelType); +} + +HWTEST_F(NotificationExtensionWrapperTest, convertToDelType_UserRemovedReasonDelete, TestSize.Level0) +{ + // Arrange + int32_t deleteReason = NotificationConstant::USER_REMOVED_REASON_DELETE; + int32_t expectedDelType = PASSITIVE_DELETE; + + // Act + int32_t actualDelType = OHOS::Notification::ExtensionWrapper::convertToDelType(deleteReason); + + // Assert + ASSERT_EQ(expectedDelType, actualDelType); +} + +HWTEST_F(NotificationExtensionWrapperTest, convertToDelType_DisableSlotReasonDelete, TestSize.Level0) +{ + // Arrange + int32_t deleteReason = NotificationConstant::DISABLE_SLOT_REASON_DELETE; + int32_t expectedDelType = PASSITIVE_DELETE; + + // Act + int32_t actualDelType = OHOS::Notification::ExtensionWrapper::convertToDelType(deleteReason); + + // Assert + ASSERT_EQ(expectedDelType, actualDelType); +} + +HWTEST_F(NotificationExtensionWrapperTest, convertToDelType_DisableNotificationReasonDelete, TestSize.Level0) +{ + // Arrange + int32_t deleteReason = NotificationConstant::DISABLE_NOTIFICATION_REASON_DELETE; + int32_t expectedDelType = PASSITIVE_DELETE; + + // Act + int32_t actualDelType = OHOS::Notification::ExtensionWrapper::convertToDelType(deleteReason); + + // Assert + ASSERT_EQ(expectedDelType, actualDelType); +} +} //namespace Notification +} //namespace OHOS \ No newline at end of file -- Gitee