From aece4ff239a9e6de9f74bcc7444a6d8b55bd5461 Mon Sep 17 00:00:00 2001 From: z30053788 Date: Sat, 14 Jun 2025 10:20:45 +0800 Subject: [PATCH 1/2] update Signed-off-by: z30053788 Change-Id: I0f32214efcd9eec64c0d90cb0ebfc96e17ef34d1 --- services/ans/test/unittest/BUILD.gn | 6 + ...notification_clone_bundle_service_test.cpp | 166 +++++++++ .../mock/mock_notification_clone_util.h | 31 ++ ...notification_clone_bundle_service_test.cpp | 177 +++++++++ .../notification_clone_disturb_service.cpp | 345 ++++++++++++++++++ .../notification_clone_manager_test.cpp | 157 ++++++++ .../notification_clone_util_test.cpp | 82 +++++ 7 files changed, 964 insertions(+) create mode 100644 services/ans/test/unittest/clone_test/dh_notification_clone_bundle_service_test.cpp create mode 100644 services/ans/test/unittest/clone_test/mock/mock_notification_clone_util.h create mode 100644 services/ans/test/unittest/clone_test/notification_clone_bundle_service_test.cpp create mode 100644 services/ans/test/unittest/clone_test/notification_clone_disturb_service.cpp create mode 100644 services/ans/test/unittest/clone_test/notification_clone_manager_test.cpp create mode 100644 services/ans/test/unittest/clone_test/notification_clone_util_test.cpp diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index 9494539d4..5ca5308fe 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -318,6 +318,11 @@ ohos_unittest("notification_service_test") { "advanced_notification_slot_service_test.cpp", "advanced_notification_utils_test.cpp", "clone_test/notification_clone_bundle_info_test.cpp", + "clone_test/notification_clone_disturb_service.cpp", + "clone_test/notification_clone_bundle_service_test.cpp", + "clone_test/notification_clone_manager_test.cpp", + "clone_test/notification_clone_util_test.cpp", + "clone_test/dh_notification_clone_bundle_service_test.cpp", "mock/blob.cpp", "mock/distributed_kv_data_manager.cpp", "mock/mock_access_token_helper.cpp", @@ -353,6 +358,7 @@ ohos_unittest("notification_service_test") { "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", + "googletest:gmock_main", "common_event_service:cesfwk_innerkits", "device_manager:devicemanagersdk", "eventhandler:libeventhandler", diff --git a/services/ans/test/unittest/clone_test/dh_notification_clone_bundle_service_test.cpp b/services/ans/test/unittest/clone_test/dh_notification_clone_bundle_service_test.cpp new file mode 100644 index 000000000..a0f1e12d9 --- /dev/null +++ b/services/ans/test/unittest/clone_test/dh_notification_clone_bundle_service_test.cpp @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2023 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 +#include "gmock/gmock.h" +#define private public +#define protected public +#include "ans_inner_errors.h" +#include "dh_notification_clone_bundle_service.h" +#include "advanced_notification_service.h" +#undef private +#undef protected + +using namespace testing::ext; +using namespace testing; +using namespace OHOS; +using namespace Notification; + +// Test suite class +class DhNotificationCloneBundleTest : public ::testing::Test { +protected: + void SetUp() override + { + // Initialize objects and dependencies + dhNotificationCloneBundle = DhNotificationCloneBundle::GetInstance(); + } + + void TearDown() override + {} + + std::shared_ptr dhNotificationCloneBundle = nullptr; +}; + +/** + * @tc.name: OnRestore_Test_001 + * @tc.desc: Test that error is reported when appIndex is -1 + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DhNotificationCloneBundleTest, OnRestore_Test_001, Function | SmallTest | Level1) +{ + nlohmann::json jsonObject; + int32_t userId = 100; + auto advancedNotificationService_ = new (std::nothrow) AdvancedNotificationService(); + + sptr date = nullptr; + std::vector> profiles = { date }; + auto ret = advancedNotificationService_->AddDoNotDisturbProfiles(profiles); + + ErrCode result = dhNotificationCloneBundle->OnBackup(jsonObject); + dhNotificationCloneBundle->OnRestore(jsonObject); + EXPECT_EQ(result, ERR_OK); +} + +/** + * @tc.name: OnRestoreStart_Test_001 + * @tc.desc: Test that error is reported when bundlesInfo_ is null. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DhNotificationCloneBundleTest, OnRestoreStart_Test_001, Function | SmallTest | Level1) +{ + // Arrange + std::string bundleName = "testBundle"; + int32_t appIndex = 0; + int32_t userId = 0; + int32_t uid = 12345; + + // Ensure bundlesInfo_ is empty + dhNotificationCloneBundle->bundlesInfo_.clear(); + + // Act + dhNotificationCloneBundle->OnRestoreStart(bundleName, appIndex, userId, uid); + + EXPECT_EQ(dhNotificationCloneBundle->bundlesInfo_.size(), 0); +} + +/** + * @tc.name: OnRestoreStart_Test_002 + * @tc.desc: Test that error is reported when bundlesInfo_ is not null. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DhNotificationCloneBundleTest, OnRestoreStart_Test_002, Function | SmallTest | Level1) +{ + // Arrange + std::string bundleName = "testBundle"; + int32_t appIndex = 0; + int32_t userId = 0; + int32_t uid = 12345; + + // Create a bundle with the same name + NotificationCloneBundleInfo bundleInfo; + bundleInfo.SetBundleName(bundleName); + bundleInfo.SetUid(54321); // Original UID + + // Add the bundle to bundlesInfo_ + dhNotificationCloneBundle->bundlesInfo_.push_back(bundleInfo); + + // Act + dhNotificationCloneBundle->OnRestoreStart(bundleName, appIndex, userId, uid); + + // Assert + // Check if the bundle was updated and deleted + EXPECT_EQ(dhNotificationCloneBundle->bundlesInfo_.size(), 0); +} + +/** + * @tc.name: OnRestoreStart_Test_003 + * @tc.desc: Test that error is reported when bundlesInfo_ is not null. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DhNotificationCloneBundleTest, OnRestoreStart_Test_003, Function | SmallTest | Level1) +{ + // Arrange + std::string bundleName = "testBundle"; + int32_t appIndex = 0; + int32_t userId = 0; + int32_t uid = 12345; + + // Create a bundle with a different name + NotificationCloneBundleInfo bundleInfo; + bundleInfo.SetBundleName("differentBundle"); + bundleInfo.SetUid(54321); + + // Add the bundle to bundlesInfo_ + dhNotificationCloneBundle->bundlesInfo_.push_back(bundleInfo); + + // Act + dhNotificationCloneBundle->OnRestoreStart(bundleName, appIndex, userId, uid); + + // Assert + // Check if the bundle was not modified + EXPECT_EQ(dhNotificationCloneBundle->bundlesInfo_.size(), 1); + EXPECT_EQ(dhNotificationCloneBundle->bundlesInfo_.front().GetBundleName(), "differentBundle"); + EXPECT_EQ(dhNotificationCloneBundle->bundlesInfo_.front().GetUid(), 54321); +} + +/** + * @tc.name: OnUserSwitch_Test_001 + * @tc.desc: Test that error is reported when dhCloneBundleQueue_ is null. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DhNotificationCloneBundleTest, OnUserSwitch_Test_001, Function | SmallTest | Level1) +{ + dhNotificationCloneBundle->dhCloneBundleQueue_ = nullptr; + + // Call the function under test + dhNotificationCloneBundle->OnUserSwitch(1); + + EXPECT_EQ(dhNotificationCloneBundle->dhCloneBundleQueue_, nullptr); +} \ No newline at end of file diff --git a/services/ans/test/unittest/clone_test/mock/mock_notification_clone_util.h b/services/ans/test/unittest/clone_test/mock/mock_notification_clone_util.h new file mode 100644 index 000000000..bdfb2af79 --- /dev/null +++ b/services/ans/test/unittest/clone_test/mock/mock_notification_clone_util.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023-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. + */ +#ifndef BASE_NOTIFICATION_MOCK_NOTIFICATION_CLONE_UTIL_H +#define BASE_NOTIFICATION_MOCK_NOTIFICATION_CLONE_UTIL_H + +#include +#include +#include "notification_clone_util.h" + +namespace OHOS { +namespace Notification { +class MockNotificationCloneUtil : public NotificationCloneUtil { +public: + MOCK_METHOD(int32_t, GetActiveUserId, (), ()); + MOCK_METHOD(int32_t, GetBundleUid, (const std::string bundleName, int32_t userId, int32_t appIndex), ()); +}; +} +} +#endif \ No newline at end of file diff --git a/services/ans/test/unittest/clone_test/notification_clone_bundle_service_test.cpp b/services/ans/test/unittest/clone_test/notification_clone_bundle_service_test.cpp new file mode 100644 index 000000000..828f72499 --- /dev/null +++ b/services/ans/test/unittest/clone_test/notification_clone_bundle_service_test.cpp @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2023 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 +#include "gmock/gmock.h" +#define private public +#define protected public +#include "notification_clone_bundle_service.h" +#include "notification_do_not_disturb_profile.h" +#include "ans_inner_errors.h" +#include "notification_clone_util.h" +#include "advanced_notification_service.h" +#include "mock/mock_notification_clone_util.h" +#undef private +#undef protected + +using namespace testing::ext; +using namespace testing; +using ::testing::_; +using ::testing::SetArgPointee; +using ::testing::Return; +using ::testing::DoAll; +using namespace OHOS; +using namespace Notification; + +// Test suite class +class NotificationCloneBundleTest : public ::testing::Test { +protected: + void SetUp() override + { + // Initialize objects and dependencies + notificationCloneBundle = new NotificationCloneBundle(); + } + + void TearDown() override + { + // Clean up resources + delete notificationCloneBundle; + notificationCloneBundle = nullptr; + } + + NotificationCloneBundle* notificationCloneBundle; +}; + +/** + * @tc.name: OnBackUp_00001 + * @tc.desc: Test clone OnBackUp. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneBundleTest, OnBackUp_00001, Function | SmallTest | Level1) +{ + nlohmann::json jsonObject; + int32_t userId = 300; + auto advancedNotificationService_ = new (std::nothrow) AdvancedNotificationService(); + + MockNotificationCloneUtil* mockCloneUtil = new MockNotificationCloneUtil(); + EXPECT_CALL(*mockCloneUtil, GetActiveUserId()).WillOnce(Return(userId)); + + sptr date = nullptr; + std::vector> profiles = { date }; + auto ret = advancedNotificationService_->AddDoNotDisturbProfiles(profiles); + + ErrCode result = notificationCloneBundle->OnBackup(jsonObject); + notificationCloneBundle->OnRestore(jsonObject); + EXPECT_EQ(result, ERR_OK); +} + +/** + * @tc.name: OnRestoreStart_Test_001 + * @tc.desc: Test that OnRestoreStart does nothing when bundlesInfo_ is empty. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneBundleTest, OnRestoreStart_Test_001, Function | SmallTest | Level1) +{ + // Arrange + std::string bundleName = "testBundle"; + int32_t appIndex = 1; + int32_t userId = 100; + int32_t uid = 200; + + // Ensure bundlesInfo_ is empty + notificationCloneBundle->bundlesInfo_.clear(); + + // Act + notificationCloneBundle->OnRestoreStart(bundleName, appIndex, userId, uid); + + // Assert + EXPECT_TRUE(notificationCloneBundle->bundlesInfo_.empty()); +} + +/** + * @tc.name: OnRestoreStart_Test_002 + * @tc.desc: Test that OnRestoreStart updates and removes the bundle when a match is found. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneBundleTest, OnRestoreStart_Test_002, Function | SmallTest | Level1) +{ + // Arrange + std::string bundleName = "testBundle"; + int32_t appIndex = 1; + int32_t userId = 100; + int32_t uid = 200; + + // Create a matching bundle and add it to bundlesInfo_ + NotificationCloneBundleInfo bundleInfo; + bundleInfo.SetBundleName(bundleName); + bundleInfo.SetAppIndex(appIndex); + bundleInfo.SetUid(150); // Original uid + notificationCloneBundle->bundlesInfo_.push_back(bundleInfo); + + // Act + notificationCloneBundle->OnRestoreStart(bundleName, appIndex, userId, uid); + + // Assert + EXPECT_EQ(notificationCloneBundle->bundlesInfo_.size(), 0); +} + +/** + * @tc.name: OnRestoreStart_Test_003 + * @tc.desc: Test that OnRestoreStart updates and removes the bundle when a match is found. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneBundleTest, OnRestoreStart_Test_003, Function | SmallTest | Level1) +{ + // Arrange + std::string bundleName = "testBundle"; + int32_t appIndex = 1; + int32_t userId = 100; + int32_t uid = 200; + + // Create a non-matching bundle and add it to bundlesInfo_ + NotificationCloneBundleInfo bundleInfo; + bundleInfo.SetBundleName("otherBundle"); + bundleInfo.SetAppIndex(2); + bundleInfo.SetUid(150); + notificationCloneBundle->bundlesInfo_.push_back(bundleInfo); + + // Act + notificationCloneBundle->OnRestoreStart(bundleName, appIndex, userId, uid); + + // Assert + EXPECT_EQ(notificationCloneBundle->bundlesInfo_.size(), 1); +} + +/** + * @tc.name: OnUserSwitch_Test_001 + * @tc.desc: Test OnUserSwitch function when cloneBundleQueue_ is empty + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneBundleTest, OnUserSwitch_Test_001, Function | SmallTest | Level1) +{ + // Ensure cloneDisturbQueue_ is not null + notificationCloneBundle->cloneBundleQueue_ = nullptr; + + // Call the function + notificationCloneBundle->OnUserSwitch(100); + + // Verify that the profile is deleted + EXPECT_EQ(notificationCloneBundle->cloneBundleQueue_, nullptr); +} \ No newline at end of file diff --git a/services/ans/test/unittest/clone_test/notification_clone_disturb_service.cpp b/services/ans/test/unittest/clone_test/notification_clone_disturb_service.cpp new file mode 100644 index 000000000..28acb59e4 --- /dev/null +++ b/services/ans/test/unittest/clone_test/notification_clone_disturb_service.cpp @@ -0,0 +1,345 @@ +/* + * Copyright (c) 2023 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 +#include "gmock/gmock.h" +#define private public +#define protected public +#include "notification_clone_disturb_service.h" +#include "notification_preferences_info.h" +#include "notification_do_not_disturb_profile.h" +#include "ans_inner_errors.h" +#include "notification_preferences.h" +#include "notification_clone_util.h" +#include "mock/mock_notification_clone_util.h" +#undef private +#undef protected + +using namespace testing::ext; +using namespace testing; +using ::testing::_; +using ::testing::SetArgPointee; +using ::testing::Return; +using ::testing::DoAll; +using namespace OHOS; +using namespace Notification; + +// Test suite class +class NotificationCloneDisturbTest : public ::testing::Test { +protected: + void SetUp() override + { + // Initialize objects and dependencies + notificationCloneDisturb = new NotificationCloneDisturb(); + } + + void TearDown() override + { + // Clean up resources + delete notificationCloneDisturb; + } + + NotificationCloneDisturb* notificationCloneDisturb; +}; + +/** + * @tc.name: OnBackUp_00001 + * @tc.desc: Test clone OnBackUp. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneDisturbTest, OnBackUp_00001, Function | SmallTest | Level1) +{ + nlohmann::json jsonObject; + int32_t userId = 100; + auto advancedNotificationService_ = new (std::nothrow) AdvancedNotificationService(); + + MockNotificationCloneUtil* mockCloneUtil = new MockNotificationCloneUtil(); + EXPECT_CALL(*mockCloneUtil, GetActiveUserId()).WillOnce(Return(userId)); + + sptr date = nullptr; + std::vector> profiles = { date }; + auto ret = advancedNotificationService_->AddDoNotDisturbProfiles(profiles); + + ErrCode result = notificationCloneDisturb->OnBackup(jsonObject); + notificationCloneDisturb->OnRestore(jsonObject); + EXPECT_EQ(result, ERR_OK); +} + + +/** + * @tc.name: OnRestore_00001 + * @tc.desc: Test clone OnRestore jsonObject is null. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneDisturbTest, OnRestore_00001, Function | SmallTest | Level1) +{ + nlohmann::json jsonObject = nullptr; + notificationCloneDisturb->OnRestore(jsonObject); + EXPECT_EQ(jsonObject, nullptr); +} + +/** + * @tc.name: GetProfileUid_Test_001 + * @tc.desc: Test that the function sets the UID from uidMap when the key exists in uidMap. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneDisturbTest, GetProfileUid_Test_001, Function | SmallTest | Level1) +{ + int32_t userId = 1; + std::map uidMap; + std::vector trustList; + std::vector exitBunldleList; + std::vector notExitBunldleList; + + // Create a bundle with a known key + NotificationBundleOption bundle; + bundle.SetBundleName("com.example.app"); + bundle.SetAppIndex(1); + std::string key = "com.example.app1"; + uidMap[key] = 12345; + + trustList.push_back(bundle); + + notificationCloneDisturb->GetProfileUid(userId, uidMap, trustList, exitBunldleList, notExitBunldleList); + + EXPECT_EQ(exitBunldleList.size(), 1); + EXPECT_EQ(notExitBunldleList.size(), 0); +} + +/** + * @tc.name: GetProfileUid_Test_002 + * @tc.desc: Test that the function sets the UID from uidMap when the key exists in uidMap. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneDisturbTest, GetProfileUid_Test_002, Function | SmallTest | Level1) +{ + int32_t userId = 1; + std::map uidMap; + std::vector trustList; + std::vector exitBunldleList; + std::vector notExitBunldleList; + + // Create a bundle with an unknown key + NotificationBundleOption bundle; + bundle.SetBundleName("com.example.app"); + bundle.SetAppIndex(1); + + trustList.push_back(bundle); + + MockNotificationCloneUtil* mockCloneUtil = new MockNotificationCloneUtil(); + EXPECT_CALL(*mockCloneUtil, GetBundleUid(_, _, _)).WillOnce(Return(54321)); + + notificationCloneDisturb->GetProfileUid(userId, uidMap, trustList, exitBunldleList, notExitBunldleList); + + EXPECT_EQ(exitBunldleList.size(), 0); + EXPECT_EQ(notExitBunldleList.size(), 1); +} + +/** + * @tc.name: GetProfileUid_Test_003 + * @tc.desc: Test that the function sets the UID from uidMap when the key exists in uidMap. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneDisturbTest, GetProfileUid_Test_003, Function | SmallTest | Level1) +{ + int32_t userId = 1; + std::map uidMap; + std::vector trustList; + std::vector exitBunldleList; + std::vector notExitBunldleList; + + // Create a bundle + NotificationBundleOption bundle; + bundle.SetBundleName("com.example.app"); + bundle.SetAppIndex(1); + + trustList.push_back(bundle); + + MockNotificationCloneUtil* mockCloneUtil = new MockNotificationCloneUtil(); + EXPECT_CALL(*mockCloneUtil, GetBundleUid(_, _, _)).WillOnce(Return(-1)); + + notificationCloneDisturb->GetProfileUid(userId, uidMap, trustList, exitBunldleList, notExitBunldleList); + + EXPECT_EQ(exitBunldleList.size(), 0); + EXPECT_EQ(notExitBunldleList.size(), 1); +} + +/** + * @tc.name: OnRestoreStart_Test_001 + * @tc.desc: Test OnRestoreStart function when profiles_ is empty + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneDisturbTest, OnRestoreStart_Test_001, Function | SmallTest | Level1) +{ + std::string bundleName = "com.example.app"; + int32_t appIndex = 1; + int32_t userId = 100; + int32_t uid = 12345; + + // Ensure profiles_ is empty + notificationCloneDisturb->profiles_.clear(); + + // Call the function + notificationCloneDisturb->OnRestoreStart(bundleName, appIndex, userId, uid); + + // Verify that the function returned without any action + EXPECT_TRUE(notificationCloneDisturb->profiles_.empty()); +} + +/** + * @tc.name: OnRestoreStart_Test_002 + * @tc.desc: Test OnRestoreStart function when cloneDisturbQueue_ is null + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneDisturbTest, OnRestoreStart_Test_002, Function | SmallTest | Level1) +{ + std::string bundleName = "com.example.app"; + int32_t appIndex = 1; + int32_t userId = 100; + int32_t uid = 12345; + + // Ensure cloneDisturbQueue_ is null + notificationCloneDisturb->cloneDisturbQueue_ = nullptr; + + // Call the function + notificationCloneDisturb->OnRestoreStart(bundleName, appIndex, userId, uid); + + // Verify that the function returned without any action + EXPECT_EQ(notificationCloneDisturb->cloneDisturbQueue_, nullptr); +} + +/** + * @tc.name: OnRestoreStart_Test_004 + * @tc.desc: Test OnRestoreStart function when trustList is empty + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneDisturbTest, OnRestoreStart_Test_004, Function | SmallTest | Level1) +{ + std::string bundleName = "com.example.app"; + int32_t appIndex = 1; + int32_t userId = 100; + int32_t uid = 12345; + + // Add a profile to profiles_ with an empty trust list + auto profile = NotificationDoNotDisturbProfile(1, "name", {}); + + // Ensure cloneDisturbQueue_ is not null + notificationCloneDisturb->cloneDisturbQueue_ = std::make_shared("NotificationCloneDisturbQueue"); + + // Call the function + notificationCloneDisturb->OnRestoreStart(bundleName, appIndex, userId, uid); + + // Verify that the profile is deleted + EXPECT_TRUE(notificationCloneDisturb->profiles_.empty()); +} + +/** + * @tc.name: OnUserSwitch_Test_001 + * @tc.desc: Test OnUserSwitch function when cloneDisturbQueue_ is empty + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneDisturbTest, OnUserSwitch_Test_001, Function | SmallTest | Level1) +{ + // Ensure cloneDisturbQueue_ is not null + notificationCloneDisturb->cloneDisturbQueue_ = nullptr; + + // Call the function + notificationCloneDisturb->OnUserSwitch(100); + + // Verify that the profile is deleted + EXPECT_EQ(notificationCloneDisturb->cloneDisturbQueue_, nullptr); +} + +/** + * @tc.name: CheckBundleInfo_Test_001 + * @tc.desc: Test that when a matching bundle is found in trustList, it is added to bundleList and + * removed from trustList. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneDisturbTest, CheckBundleInfo_Test_001, Function | SmallTest | Level1) +{ + std::vector trustList; + std::vector bundleList; + NotificationBundleOption bundle; + bundle.SetBundleName("com.example.app"); + bundle.SetAppIndex(1); + + NotificationBundleOption matchingBundle; + matchingBundle.SetBundleName("com.example.app"); + matchingBundle.SetAppIndex(1); + trustList.push_back(matchingBundle); + + notificationCloneDisturb->CheckBundleInfo(trustList, bundleList, bundle); + + EXPECT_EQ(bundleList.size(), 1); + EXPECT_EQ(trustList.size(), 0); + EXPECT_EQ(bundleList[0].GetBundleName(), "com.example.app"); + EXPECT_EQ(bundleList[0].GetAppIndex(), 1); +} + +/** + * @tc.name: CheckBundleInfo_Test_002 + * @tc.desc: Test that when no matching bundle is found in trustList, the bundle is not added to bundleList. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneDisturbTest, CheckBundleInfo_Test_002, Function | SmallTest | Level1) +{ + std::vector trustList; + std::vector bundleList; + NotificationBundleOption bundle; + bundle.SetBundleName("com.example.app"); + bundle.SetAppIndex(1); + + NotificationBundleOption nonMatchingBundle; + nonMatchingBundle.SetBundleName("com.example.other"); + nonMatchingBundle.SetAppIndex(2); + trustList.push_back(nonMatchingBundle); + + notificationCloneDisturb->CheckBundleInfo(trustList, bundleList, bundle); + + EXPECT_EQ(bundleList.size(), 0); + EXPECT_EQ(trustList.size(), 1); +} + +/** + * @tc.name: CheckBundleInfo_Test_003 + * @tc.desc: Test that when trustList is empty, the bundle is not added to bundleList. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneDisturbTest, CheckBundleInfo_Test_003, Function | SmallTest | Level1) +{ + std::vector trustList; + std::vector bundleList; + NotificationBundleOption bundle; + bundle.SetBundleName("com.example.app"); + bundle.SetAppIndex(1); + + notificationCloneDisturb->CheckBundleInfo(trustList, bundleList, bundle); + + EXPECT_EQ(bundleList.size(), 0); + EXPECT_EQ(trustList.size(), 0); +} \ No newline at end of file diff --git a/services/ans/test/unittest/clone_test/notification_clone_manager_test.cpp b/services/ans/test/unittest/clone_test/notification_clone_manager_test.cpp new file mode 100644 index 000000000..46c6539e3 --- /dev/null +++ b/services/ans/test/unittest/clone_test/notification_clone_manager_test.cpp @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2023 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 +#include "gmock/gmock.h" +#define private public +#define protected public +#include "notification_clone_manager.h" +#include "ans_inner_errors.h" +#undef private +#undef protected + +using namespace testing::ext; +using namespace testing; +using namespace OHOS; +using namespace Notification; + +// Test suite class +class AncoRestoreStartEventSubscriberTest : public ::testing::Test { +protected: + void SetUp() override + { + // Initialize objects and dependencies + ancoRestoreStartEventSubscriber = AncoRestoreStartEventSubscriber::create(); + notificationCloneManager = &NotificationCloneManager::GetInstance(); + } + + void TearDown() override + {} + + std::shared_ptr ancoRestoreStartEventSubscriber = nullptr; + NotificationCloneManager* notificationCloneManager; +}; + +/** + * @tc.name: OnReceiveEvent_Test_001 + * @tc.desc: Test that error is reported when uid is 0 + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AncoRestoreStartEventSubscriberTest, OnReceiveEvent_Test_001, Function | SmallTest | Level1) +{ + EventFwk::Want want; + want.SetParam("bundleName", std::string("testBundle")); + want.SetParam("uid", 0); + EventFwk::CommonEventData data{want}; + + // Act + ancoRestoreStartEventSubscriber->OnReceiveEvent(data); + EXPECT_EQ(data.GetWant().GetIntParam("uid", 0), 0); +} + +/** + * @tc.name: OnReceiveEvent_Test_002 + * @tc.desc: Test that error is reported when uid is negative + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AncoRestoreStartEventSubscriberTest, OnReceiveEvent_Test_002, Function | SmallTest | Level1) +{ + // Arrange + EventFwk::Want want; + want.SetParam("bundleName", std::string("testBundle")); + want.SetParam("uid", -1); + EventFwk::CommonEventData data{want}; + + // Act + ancoRestoreStartEventSubscriber->OnReceiveEvent(data); + EXPECT_EQ(data.GetWant().GetIntParam("uid", 0), -1); +} + +/** + * @tc.name: OnReceiveEvent_Test_003 + * @tc.desc: Test that error is reported when uid is negative + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AncoRestoreStartEventSubscriberTest, OnReceiveEvent_Test_003, Function | SmallTest | Level1) +{ + // Arrange + EventFwk::Want want; + want.SetParam("bundleName", std::string("testBundle")); + want.SetParam("uid", 1); + EventFwk::CommonEventData data{want}; + + // Act + ancoRestoreStartEventSubscriber->OnReceiveEvent(data); + EXPECT_EQ(data.GetWant().GetIntParam("uid", 0), 1); +} + +/** + * @tc.name: OnRestoreStart_Test_001 + * @tc.desc: Test that error is reported when uid is negative + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AncoRestoreStartEventSubscriberTest, OnRestoreStart_Test_001, Function | SmallTest | Level1) +{ + // Arrange + EventFwk::Want want; + want.SetParam("bundleName", std::string("testBundle")); + want.SetParam("index", -1); + + // Act + notificationCloneManager->OnRestoreStart(want); + EXPECT_EQ(want.GetIntParam("index", 0), -1); +} + +/** + * @tc.name: OnRestoreStart_Test_002 + * @tc.desc: Test that error is reported when uid is negative + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AncoRestoreStartEventSubscriberTest, OnRestoreStart_Test_002, Function | SmallTest | Level1) +{ + // Arrange + EventFwk::Want want; + want.SetParam("bundleName", std::string("")); + want.SetParam("index", 1); + + // Act + notificationCloneManager->OnRestoreStart(want); + EXPECT_EQ(want.GetIntParam("index", 0), 1); +} + +/** + * @tc.name: OnDhRestoreStart_Test_001 + * @tc.desc: Test that error is reported when uid is negative + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AncoRestoreStartEventSubscriberTest, OnDhRestoreStart_Test_001, Function | SmallTest | Level1) +{ + std::string bundleName = "testBundle"; + int32_t uid = 12345; + + // Ensure cloneTemplates is empty + notificationCloneManager->cloneTemplates.clear(); + + // Call the function + notificationCloneManager->OnDhRestoreStart(bundleName, uid); + + EXPECT_TRUE(notificationCloneManager->cloneTemplates.empty()); +} \ No newline at end of file diff --git a/services/ans/test/unittest/clone_test/notification_clone_util_test.cpp b/services/ans/test/unittest/clone_test/notification_clone_util_test.cpp new file mode 100644 index 000000000..58c4155e3 --- /dev/null +++ b/services/ans/test/unittest/clone_test/notification_clone_util_test.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2023 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 +#include "gmock/gmock.h" +#define private public +#define protected public +#include "ans_inner_errors.h" +#include "notification_clone_util.h" +#undef private +#undef protected + +using namespace testing::ext; +using namespace testing; +using namespace OHOS; +using namespace Notification; + +// Test suite class +class NotificationCloneUtilTest : public ::testing::Test { +protected: + void SetUp() override + { + // Initialize objects and dependencies + notificationCloneUtil = new NotificationCloneUtil(); + } + + void TearDown() override + { + delete notificationCloneUtil; + notificationCloneUtil = nullptr; + } + + NotificationCloneUtil* notificationCloneUtil; +}; + +/** + * @tc.name: GetBundleUid_Test_001 + * @tc.desc: Test that error is reported when appIndex is -1 + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneUtilTest, GetBundleUid_Test_001, Function | SmallTest | Level1) +{ + // Arrange + std::string bundleName = "com.example.app"; + int32_t userId = 100; + int32_t appIndex = -1; + + int32_t actualUid = notificationCloneUtil->GetBundleUid(bundleName, userId, appIndex); + + EXPECT_EQ(actualUid, 1000); +} + +/** + * @tc.name: GetBundleUid_Test_002 + * @tc.desc: Test that error is reported when appIndex is 0 + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationCloneUtilTest, GetBundleUid_Test_002, Function | SmallTest | Level1) +{ + // Arrange + std::string bundleName = "com.example.app"; + int32_t userId = 100; + int32_t appIndex = 0; + + int32_t actualUid = notificationCloneUtil->GetBundleUid(bundleName, userId, appIndex); + + EXPECT_EQ(actualUid, -1); +} \ No newline at end of file -- Gitee From 1555845a2d41c3fd6e509b3a02757dd8b42ec263 Mon Sep 17 00:00:00 2001 From: z30053788 Date: Sat, 14 Jun 2025 14:23:13 +0800 Subject: [PATCH 2/2] update Signed-off-by: z30053788 Change-Id: If9cb92e6c976fb360c0ca43152e712bd59a3af35 --- .../unittest/clone_test/notification_clone_disturb_service.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/services/ans/test/unittest/clone_test/notification_clone_disturb_service.cpp b/services/ans/test/unittest/clone_test/notification_clone_disturb_service.cpp index 28acb59e4..58cef3cd6 100644 --- a/services/ans/test/unittest/clone_test/notification_clone_disturb_service.cpp +++ b/services/ans/test/unittest/clone_test/notification_clone_disturb_service.cpp @@ -49,6 +49,7 @@ protected: { // Clean up resources delete notificationCloneDisturb; + notificationCloneDisturb = nullptr; } NotificationCloneDisturb* notificationCloneDisturb; -- Gitee