diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index 19aad802afe6e395c6d164b5b74e5584d14bf45f..5bc67461c8c686f7a0cd588e8f1bb6f9073ad8af 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -47,6 +47,8 @@ ohos_unittest("ans_unit_test") { "notification_local_live_view_subscriber_manager_test.cpp", "notification_slot_filter_test.cpp", "permission_filter_test.cpp", + "reminder_affected_test.cpp", + "smart_reminder_center_test.cpp", ] deps = [ diff --git a/services/ans/test/unittest/reminder_affected_test.cpp b/services/ans/test/unittest/reminder_affected_test.cpp index 9bf78470322c934bdf7d12979a0f0bf8992eeca6..8b3c67afbbb4bae0d89fd476d3ae4765be44a854 100644 --- a/services/ans/test/unittest/reminder_affected_test.cpp +++ b/services/ans/test/unittest/reminder_affected_test.cpp @@ -13,9 +13,13 @@ * limitations under the License. */ +#ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED +#include "gtest/gtest.h" + #define private public #define protected public #include "reminder_affected.h" +#include "string_utils.h" #undef private #undef protected @@ -29,5 +33,117 @@ public: void SetUp() {}; void TearDown() {}; }; + +/** + * @tc.name: Test FromJson + * @tc.desc: Test FromJson + * @tc.type: FUNC + */ +HWTEST_F(ReminderAffectedTest, FromJson_00001, Function | SmallTest | Level1) +{ + ReminderAffected reminderAffected; + nlohmann::json jsonObject = nlohmann::json{ + "test", "test" + }; + auto res = reminderAffected.FromJson(jsonObject); + ASSERT_FALSE(res); +} + +/** + * @tc.name: Test ValidAndGetAffectedBy + * @tc.desc: Test ValidAndGetAffectedBy + * @tc.type: FUNC + */ +HWTEST_F(ReminderAffectedTest, ValidAndGetAffectedBy_00001, Function | SmallTest | Level1) +{ + ReminderAffected reminderAffected; + nlohmann::json jsonObject = nlohmann::json{ + ReminderAffected::AFFECTED_BY, "test" + }; + + std::vector> affectedBy; + auto res = reminderAffected.ValidAndGetAffectedBy(jsonObject, affectedBy); + ASSERT_FALSE(res); +} + +/** + * @tc.name: Test ValidAndGetAffectedBy + * @tc.desc: Test ValidAndGetAffectedBy + * @tc.type: FUNC + */ +HWTEST_F(ReminderAffectedTest, ValidAndGetAffectedBy_00002, Function | SmallTest | Level1) +{ + ReminderAffected reminderAffected; + nlohmann::json jsonObject = nlohmann::json{ + ReminderAffected::AFFECTED_BY, {{"test", "test"}, {ReminderAffected::DEVICE_TYPE, ""}} + }; + std::vector> affectedBy; + auto res = reminderAffected.ValidAndGetAffectedBy(jsonObject, affectedBy); + ASSERT_FALSE(res); + + jsonObject = nlohmann::json{ + ReminderAffected::AFFECTED_BY, {} + }; + res = reminderAffected.ValidAndGetAffectedBy(jsonObject, affectedBy); + ASSERT_FALSE(res); +} + +/** + * @tc.name: Test ValidStatus + * @tc.desc: Test ValidStatus + * @tc.type: FUNC + */ +HWTEST_F(ReminderAffectedTest, ValidStatus_00001, Function | SmallTest | Level1) +{ + ReminderAffected reminderAffected; + nlohmann::json jsonObject = nlohmann::json{ + {ReminderAffected::DEVICE_TYPE, "test"}, {ReminderAffected::STATUS, 1} + }; + + std::string status = "test1"; + auto res = reminderAffected.ValidStatus(jsonObject, status); + ASSERT_FALSE(res); + ASSERT_EQ(status, "test1"); + + jsonObject = nlohmann::json{ + {ReminderAffected::DEVICE_TYPE, "test"}, {ReminderAffected::STATUS, ""} + }; + res = reminderAffected.ValidStatus(jsonObject, status); + ASSERT_TRUE(res); + ASSERT_EQ(status, "test1"); + + jsonObject = nlohmann::json{ + {ReminderAffected::DEVICE_TYPE, "test"}, {ReminderAffected::STATUS, "123"} + }; + res = reminderAffected.ValidStatus(jsonObject, status); + ASSERT_FALSE(res); + ASSERT_EQ(status, "test1"); + + jsonObject = nlohmann::json{ + {ReminderAffected::DEVICE_TYPE, "test"}, {ReminderAffected::STATUS, "xxx1"} + }; + res = reminderAffected.ValidStatus(jsonObject, status); + ASSERT_TRUE(res); + ASSERT_EQ(status, "xxx1"); +} + +/** + * @tc.name: Test StringUtils + * @tc.desc: Test StringUtils + * @tc.type: FUNC + */ +HWTEST_F(ReminderAffectedTest, StringUtils_00001, Function | SmallTest | Level1) +{ + const std::string str = "test, test"; + const std::string splitFlag = ","; + std::vector res; + + StringUtils::Split("", splitFlag, res); + ASSERT_EQ(res.size(), 0); + + StringUtils::Split(str, splitFlag, res); + ASSERT_EQ(res.size(), 2); +} } // namespace Notification } // namespace OHOS +#endif \ No newline at end of file diff --git a/services/ans/test/unittest/smart_reminder_center_test.cpp b/services/ans/test/unittest/smart_reminder_center_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8de0acbc7e19cab33d1b73cb97541a54199f7485 --- /dev/null +++ b/services/ans/test/unittest/smart_reminder_center_test.cpp @@ -0,0 +1,207 @@ +/* + * 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. + */ + +#ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED +#include "gtest/gtest.h" +#define private public +#define protected public +#include "notification_preferences.h" +#include "smart_reminder_center.h" +#include "ans_inner_errors.h" +#undef private +#undef protected + +using namespace testing::ext; +namespace OHOS { +namespace Notification { + +class SmartReminderCenterTest : public testing::Test { +public: + SmartReminderCenterTest() + {} + ~SmartReminderCenterTest() + {} + static void SetUpTestCas(void) {}; + static void TearDownTestCase(void) {}; + void SetUp(); + void TearDown() {}; +public: + std::shared_ptr smartReminderCenter_; +}; + +void SmartReminderCenterTest::SetUp(void) +{ + smartReminderCenter_ = DelayedSingleton::GetInstance(); +} + +/** + * @tc.name: Test IsNeedSynergy + * @tc.desc: Test IsNeedSynergy + * @tc.type: FUNC + */ +HWTEST_F(SmartReminderCenterTest, IsNeedSynergy_00001, Function | SmallTest | Level1) +{ + NotificationConstant::SlotType slotType = NotificationConstant::SlotType::SOCIAL_COMMUNICATION; + string deviceType = "test"; + string ownerBundleName = "testName"; + int32_t ownerUid = 100; + + auto res = smartReminderCenter_->IsNeedSynergy(slotType, deviceType, ownerBundleName, ownerUid); + ASSERT_FALSE(res); + + auto err = NotificationPreferences::GetInstance()->SetSmartReminderEnabled(deviceType, true); + ASSERT_EQ(err, ERR_OK); + res = smartReminderCenter_->IsNeedSynergy(slotType, deviceType, ownerBundleName, ownerUid); + ASSERT_FALSE(res); + + err = NotificationPreferences::GetInstance()->SetSmartReminderEnabled(deviceType, true); + ASSERT_EQ(err, ERR_OK); + res = smartReminderCenter_->IsNeedSynergy(slotType, deviceType, ownerBundleName, ownerUid); + ASSERT_FALSE(res); + + sptr bundleOption(new NotificationBundleOption(ownerBundleName, ownerUid)); + err = NotificationPreferences::GetInstance()->SetDistributedEnabledByBundle(bundleOption, deviceType, true); + ASSERT_EQ(err, ERR_OK); + res = smartReminderCenter_->IsNeedSynergy(slotType, deviceType, ownerBundleName, ownerUid); + ASSERT_TRUE(res); +} + +/** + * @tc.name: Test HandleAffectedReminder + * @tc.desc: Test HandleAffectedReminder + * @tc.type: FUNC + */ +HWTEST_F(SmartReminderCenterTest, HandleAffectedReminder_00001, Function | SmallTest | Level1) +{ + string deviceType = "test"; + shared_ptr reminderAffected = make_shared(); + std::vector> affectedBy; + auto affectedByOne = std::make_pair("test", "0000"); + affectedBy.push_back(affectedByOne); + reminderAffected->affectedBy_ = affectedBy; + reminderAffected->reminderFlags_ = make_shared(); + + set validDevices; + validDevices.insert("test"); + + shared_ptr>> notificationFlagsOfDevices = + make_shared>>(); + + auto res = smartReminderCenter_->HandleAffectedReminder( + deviceType, reminderAffected, validDevices, notificationFlagsOfDevices); + ASSERT_TRUE(res); + + auto affectedByTwo = std::make_pair("test111", "1111"); + affectedBy.push_back(affectedByTwo); + reminderAffected->affectedBy_ = affectedBy; + res = smartReminderCenter_->HandleAffectedReminder( + deviceType, reminderAffected, validDevices, notificationFlagsOfDevices); + ASSERT_FALSE(res); +} + +/** + * @tc.name: Test IsCollaborationAllowed + * @tc.desc: Test IsCollaborationAllowed + * @tc.type: FUNC + */ +HWTEST_F(SmartReminderCenterTest, IsCollaborationAllowed_00001, Function | SmallTest | Level1) +{ + sptr request(new NotificationRequest(1)); + auto res = smartReminderCenter_->IsCollaborationAllowed(request); + ASSERT_TRUE(res); + + request->SetIsSystemApp(true); + request->SetNotDistributed(true); + res = smartReminderCenter_->IsCollaborationAllowed(request); + ASSERT_FALSE(res); + + request->SetNotDistributed(false); + request->SetForceDistributed(true); + res = smartReminderCenter_->IsCollaborationAllowed(request); + ASSERT_TRUE(res); + + request->SetForceDistributed(false); + res = smartReminderCenter_->IsCollaborationAllowed(request); + ASSERT_TRUE(res); +} + +/** + * @tc.name: Test ReminderDecisionProcess + * @tc.desc: Test ReminderDecisionProcess + * @tc.type: FUNC + */ +HWTEST_F(SmartReminderCenterTest, ReminderDecisionProcess_00001, Function | SmallTest | Level1) +{ + sptr request(new NotificationRequest(1)); + request->SetIsSystemApp(true); + request->SetNotDistributed(true); + auto deviceFlags = request->GetDeviceFlags(); + ASSERT_EQ(deviceFlags, nullptr); + + smartReminderCenter_->ReminderDecisionProcess(request); + deviceFlags = request->GetDeviceFlags(); + ASSERT_NE(deviceFlags, nullptr); +} + +/** + * @tc.name: Test ReminderDecisionProcess + * @tc.desc: Test ReminderDecisionProcess + * @tc.type: FUNC + */ +HWTEST_F(SmartReminderCenterTest, InitValidDevices_00001, Function | SmallTest | Level1) +{ + // need subscriber + sptr request(new NotificationRequest(1)); + request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + + set validDevices; + NotificationPreferences::GetInstance()->SetDistributedEnabledBySlot( + request->GetSlotType(), "headset", true); + smartReminderCenter_->InitValidDevices(validDevices, request); + ASSERT_EQ(request->GetNotificationControlFlags(), 0); +} + +/** + * @tc.name: Test ReminderDecisionProcess + * @tc.desc: Test ReminderDecisionProcess + * @tc.type: FUNC + */ +HWTEST_F(SmartReminderCenterTest, InitValidDevices_00002, Function | SmallTest | Level1) +{ + // need subscriber + std::string ownerBundleName = "test"; + int32_t ownerUid = 100; + sptr request(new NotificationRequest(1)); + request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); + request->SetOwnerBundleName(ownerBundleName); + request->SetOwnerUid(ownerUid); + + std::string deviceType = "headset"; + auto res = NotificationPreferences::GetInstance()->SetSmartReminderEnabled(deviceType, true); + ASSERT_EQ(res, 0); + + sptr bundleOption( + new (std::nothrow) NotificationBundleOption(ownerBundleName, ownerUid)); + res = NotificationPreferences::GetInstance()->SetDistributedEnabledByBundle( + bundleOption, deviceType, true); + ASSERT_EQ(res, 0); + + set validDevices; + smartReminderCenter_->InitValidDevices(validDevices, request); + ASSERT_EQ(request->GetNotificationControlFlags(), 0); +} +} //namespace Notification +} //namespace OHOS +#endif \ No newline at end of file