diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index 23ed4aee26159a1f1ed477e3e6aef84f9e51d22d..17381a62eea6c037a6106a56b46a99f183fd8758 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -210,6 +210,7 @@ ohos_unittest("notification_subscriber_manager_test") { "include", "/${services_path}/ans/include", "${services_path}/ans/test/unittest/mock/include", + "${services_path}/../frameworks/core/test/unittest/mock", "${services_path}/../framworks/core/include", ] @@ -243,6 +244,10 @@ ohos_unittest("notification_subscriber_manager_test") { include_dirs += [ "${services_path}/distributed/include" ] } + if (notification_smart_reminder_supported) { + defines += [ "NOTIFICATION_SMART_REMINDER_SUPPORTED" ] + } + external_deps = [ "ability_base:want", "ability_base:zuri", diff --git a/services/ans/test/unittest/notification_subscriber_manager_test.cpp b/services/ans/test/unittest/notification_subscriber_manager_test.cpp index d8b077435ee2fdf6e9c03f30c410fe950a558cca..f39d7016d152c19e152d5e5a6b00e92292a5e8b8 100644 --- a/services/ans/test/unittest/notification_subscriber_manager_test.cpp +++ b/services/ans/test/unittest/notification_subscriber_manager_test.cpp @@ -26,6 +26,7 @@ #include "ans_inner_errors.h" #include "ans_subscriber_listener.h" +#include "mock_i_remote_object.h" extern void MockGetOsAccountLocalIdFromUid(bool mockRet, uint8_t mockCase = 0); @@ -34,6 +35,49 @@ using namespace testing; namespace OHOS { namespace Notification { +class MockAnsSubscriberTest : public MockAnsSubscriber { +public: + sptr AsObject() override + { + return new MockIRemoteObject(); + } + + ErrCode OnConsumed(const sptr ¬ification, + const sptr ¬ificationMap) override + { + isCalled_ = true; + return ERR_OK; + } + + ErrCode OnConsumed(const sptr ¬ification) override + { + isCalled_ = true; + return ERR_OK; + }; + + ErrCode OnConsumedWithMaxCapacity( + const sptr ¬ification, + const sptr ¬ificationMap) override + { + isCalled_ = true; + return ERR_OK; + }; + + ErrCode OnConsumedWithMaxCapacity(const sptr ¬ification) override + { + isCalled_ = true; + return ERR_OK; + }; + + bool IsCalled() + { + return isCalled_; + } + +private: + bool isCalled_{false}; +}; + class NotificationSubscriberManagerTest : public testing::Test { public: static void SetUpTestCase(); @@ -999,5 +1043,327 @@ HWTEST_F(NotificationSubscriberManagerTest, OnRemoteDied_001, Function | SmallTe notificationSubscriberManager_->OnRemoteDied(obj); EXPECT_NE(notificationSubscriberManager_, nullptr); } +/** + * @tc.name: NotifyConsumedInner_001 + * @tc.desc: Test NotifyConsumedInner when notification is nullptr + * @tc.type: FUNC + */ +HWTEST_F(NotificationSubscriberManagerTest, NotifyConsumedInner_001, Function | SmallTest | Level1) +{ + sptr notification = nullptr; + sptr notificationMap = nullptr; + + NotificationSubscriberManager notificationSubscriberManager; + notificationSubscriberManager.NotifyConsumedInner(notification, notificationMap); + + ASSERT_EQ(notification, nullptr); +} + +/** + * @tc.name: NotifyConsumedInner_002 + * @tc.desc: Test NotifyConsumedInner when notificationMap is not nullptr and notification type is not liveview + * @tc.type: FUNC + */ +HWTEST_F(NotificationSubscriberManagerTest, NotifyConsumedInner_002, Function | SmallTest | Level1) +{ + NotificationSubscriberManager notificationSubscriberManager; + sptr subscriber(new (std::nothrow) MockAnsSubscriberTest()); + const sptr subscribeInfo = new NotificationSubscribeInfo(); + subscribeInfo->AddAppUserId(SUBSCRIBE_USER_ALL); + notificationSubscriberManager.AddSubscriberInner(subscriber, subscribeInfo); + sptr request = new NotificationRequest(); + request->SetCreatorUid(DEFAULT_UID); + request->SetOwnerBundleName("test1"); + sptr notification = new Notification(request); + sptr notificationMap = new (std::nothrow) NotificationSortingMap(); + + notificationSubscriberManager.NotifyConsumedInner(notification, notificationMap); + + auto isCall = subscriber->IsCalled(); + ASSERT_TRUE(isCall); +} + +/** + * @tc.name: NotifyConsumedInner_003 + * @tc.desc: Test NotifyConsumedInner when notificationMap is not nullptr and notification type is liveview + * @tc.type: FUNC + */ +HWTEST_F(NotificationSubscriberManagerTest, NotifyConsumedInner_003, Function | SmallTest | Level1) +{ + NotificationSubscriberManager notificationSubscriberManager; + sptr subscriber(new (std::nothrow) MockAnsSubscriberTest()); + const sptr subscribeInfo = new NotificationSubscribeInfo(); + subscribeInfo->AddAppUserId(SUBSCRIBE_USER_ALL); + notificationSubscriberManager.AddSubscriberInner(subscriber, subscribeInfo); + sptr request = new NotificationRequest(); + request->SetCreatorUid(DEFAULT_UID); + request->SetOwnerBundleName("test1"); + request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + auto liveViewContent = std::make_shared(); + auto content = std::make_shared(liveViewContent); + request->SetContent(content); + sptr notification = new Notification(request); + sptr notificationMap = new (std::nothrow) NotificationSortingMap(); + + notificationSubscriberManager.NotifyConsumedInner(notification, notificationMap); + + auto isCall = subscriber->IsCalled(); + ASSERT_TRUE(isCall); +} + +/** + * @tc.name: NotifyConsumedInner_004 + * @tc.desc: Test NotifyConsumedInner when notificationMap is nullptr and notification type is not liveview + * @tc.type: FUNC + */ +HWTEST_F(NotificationSubscriberManagerTest, NotifyConsumedInner_004, Function | SmallTest | Level1) +{ + NotificationSubscriberManager notificationSubscriberManager; + sptr subscriber(new (std::nothrow) MockAnsSubscriberTest()); + const sptr subscribeInfo = new NotificationSubscribeInfo(); + subscribeInfo->AddAppUserId(SUBSCRIBE_USER_ALL); + notificationSubscriberManager.AddSubscriberInner(subscriber, subscribeInfo); + sptr request = new NotificationRequest(); + request->SetCreatorUid(DEFAULT_UID); + request->SetOwnerBundleName("test1"); + sptr notification = new Notification(request); + sptr notificationMap = nullptr; + + notificationSubscriberManager.NotifyConsumedInner(notification, notificationMap); + + auto isCall = subscriber->IsCalled(); + ASSERT_TRUE(isCall); +} + +/** + * @tc.name: NotifyConsumedInner_005 + * @tc.desc: Test NotifyConsumedInner when notificationMap is nullptr and notification type is liveview + * @tc.type: FUNC + */ +HWTEST_F(NotificationSubscriberManagerTest, NotifyConsumedInner_005, Function | SmallTest | Level1) +{ + NotificationSubscriberManager notificationSubscriberManager; + sptr subscriber(new (std::nothrow) MockAnsSubscriberTest()); + const sptr subscribeInfo = new NotificationSubscribeInfo(); + subscribeInfo->AddAppUserId(SUBSCRIBE_USER_ALL); + notificationSubscriberManager.AddSubscriberInner(subscriber, subscribeInfo); + sptr request = new NotificationRequest(); + request->SetCreatorUid(DEFAULT_UID); + request->SetOwnerBundleName("test1"); + request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + auto liveViewContent = std::make_shared(); + auto content = std::make_shared(liveViewContent); + request->SetContent(content); + sptr notification = new Notification(request); + sptr notificationMap = nullptr; + + notificationSubscriberManager.NotifyConsumedInner(notification, notificationMap); + + auto isCall = subscriber->IsCalled(); + ASSERT_TRUE(isCall); +} + +#ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED +/** + * @tc.name: GetIsEnableEffectedRemind_001 + * @tc.desc: Test GetIsEnableEffectedRemind when subscriberRecordList_ is empty + * @tc.type: FUNC + */ +HWTEST_F(NotificationSubscriberManagerTest, GetIsEnableEffectedRemind_001, Function | SmallTest | Level1) +{ + NotificationSubscriberManager notificationSubscriberManager; + + auto ret = notificationSubscriberManager.GetIsEnableEffectedRemind(); + + ASSERT_FALSE(ret); +} + +/** + * @tc.name: GetIsEnableEffectedRemind_002 + * @tc.desc: Test GetIsEnableEffectedRemind when subscriberRecordList_ is not empty + * @tc.type: FUNC + */ +HWTEST_F(NotificationSubscriberManagerTest, GetIsEnableEffectedRemind_002, Function | SmallTest | Level1) +{ + NotificationSubscriberManager notificationSubscriberManager; + sptr subscriber(new (std::nothrow) MockAnsSubscriberTest()); + const sptr subscribeInfo = new NotificationSubscribeInfo(); + subscribeInfo->AddAppUserId(SUBSCRIBE_USER_ALL); + subscribeInfo->AddDeviceType(NotificationConstant::PC_DEVICE_TYPE); + notificationSubscriberManager.AddSubscriberInner(subscriber, subscribeInfo); + + auto ret = notificationSubscriberManager.GetIsEnableEffectedRemind(); + + ASSERT_TRUE(ret); +} +/** + * @tc.name: IsDeviceTypeSubscriberd_001 + * @tc.desc: Test IsDeviceTypeSubscriberd when subscriberRecordList_ is empty + * @tc.type: FUNC + */ +HWTEST_F(NotificationSubscriberManagerTest, IsDeviceTypeSubscriberd_001, Function | SmallTest | Level1) +{ + NotificationSubscriberManager notificationSubscriberManager; + std::string deviceType = NotificationConstant::PC_DEVICE_TYPE; + + auto ret = notificationSubscriberManager.IsDeviceTypeSubscriberd(deviceType); + + ASSERT_FALSE(ret); +} + +/** + * @tc.name: IsDeviceTypeSubscriberd_002 + * @tc.desc: Test IsDeviceTypeSubscriberd when subscriberRecordList_ is not empty + * @tc.type: FUNC + */ +HWTEST_F(NotificationSubscriberManagerTest, IsDeviceTypeSubscriberd_002, Function | SmallTest | Level1) +{ + NotificationSubscriberManager notificationSubscriberManager; + std::string deviceType = NotificationConstant::PC_DEVICE_TYPE; + sptr subscriber(new (std::nothrow) MockAnsSubscriberTest()); + const sptr subscribeInfo = new NotificationSubscribeInfo(); + subscribeInfo->AddAppUserId(SUBSCRIBE_USER_ALL); + subscribeInfo->AddDeviceType(deviceType); + notificationSubscriberManager.AddSubscriberInner(subscriber, subscribeInfo); + + auto ret = notificationSubscriberManager.IsDeviceTypeSubscriberd(deviceType); + + ASSERT_TRUE(ret); +} + +/** + * @tc.name: IsDeviceTypeAffordConsume_001 + * @tc.desc: Test IsDeviceTypeAffordConsume + * @tc.type: FUNC + */ +HWTEST_F(NotificationSubscriberManagerTest, IsDeviceTypeAffordConsume_001, Function | SmallTest | Level1) +{ + NotificationSubscriberManager notificationSubscriberManager; + std::string deviceType = NotificationConstant::PC_DEVICE_TYPE; + sptr subscriber(new (std::nothrow) MockAnsSubscriberTest()); + const sptr subscribeInfo = new NotificationSubscribeInfo(); + subscribeInfo->AddAppUserId(SUBSCRIBE_USER_ALL); + subscribeInfo->AddDeviceType(deviceType); + notificationSubscriberManager.AddSubscriberInner(subscriber, subscribeInfo); + sptr request = new NotificationRequest(); + bool result = false; + + notificationSubscriberManager.IsDeviceTypeAffordConsume(deviceType, request, result); + + ASSERT_TRUE(result); +} +#endif + +/** + * @tc.name: TrackCodeLog_002 + * @tc.desc: Test TrackCodeLog + * @tc.type: FUNC + */ +HWTEST_F(NotificationSubscriberManagerTest, TrackCodeLog_002, Function | SmallTest | Level1) +{ + NotificationSubscriberManager notificationSubscriberManager; + sptr notification = nullptr; + bool wearableFlag = false; + bool headsetFlag = false; + bool keyNodeFlag = false; + + notificationSubscriberManager.TrackCodeLog(notification, wearableFlag, headsetFlag, keyNodeFlag); + + ASSERT_EQ(notification, nullptr); +} + +/** + * @tc.name: TrackCodeLog_003 + * @tc.desc: Test TrackCodeLog + * @tc.type: FUNC + */ +HWTEST_F(NotificationSubscriberManagerTest, TrackCodeLog_003, Function | SmallTest | Level1) +{ + NotificationSubscriberManager notificationSubscriberManager; + sptr request = nullptr; + sptr notification = new Notification(request); + bool wearableFlag = false; + bool headsetFlag = false; + bool keyNodeFlag = false; + + notificationSubscriberManager.TrackCodeLog(notification, wearableFlag, headsetFlag, keyNodeFlag); + + ASSERT_EQ(notification->GetNotificationRequestPoint(), nullptr); +} + +/** + * @tc.name: TrackCodeLog_004 + * @tc.desc: Test TrackCodeLog + * @tc.type: FUNC + */ +HWTEST_F(NotificationSubscriberManagerTest, TrackCodeLog_004, Function | SmallTest | Level1) +{ + NotificationSubscriberManager notificationSubscriberManager; + sptr request = new NotificationRequest(); + request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + auto liveViewContent = std::make_shared(); + auto content = std::make_shared(liveViewContent); + request->SetContent(content); + sptr notification = new Notification(request); + bool wearableFlag = true; + bool headsetFlag = true; + bool keyNodeFlag = false; + + notificationSubscriberManager.TrackCodeLog(notification, wearableFlag, headsetFlag, keyNodeFlag); + + ASSERT_NE(notification->GetNotificationRequestPoint(), nullptr); + ASSERT_TRUE(wearableFlag); + ASSERT_TRUE(headsetFlag); +} + +/** + * @tc.name: TrackCodeLog_005 + * @tc.desc: Test TrackCodeLog + * @tc.type: FUNC + */ +HWTEST_F(NotificationSubscriberManagerTest, TrackCodeLog_005, Function | SmallTest | Level1) +{ + NotificationSubscriberManager notificationSubscriberManager; + sptr request = new NotificationRequest(); + request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + auto liveViewContent = std::make_shared(); + auto content = std::make_shared(liveViewContent); + request->SetContent(content); + sptr notification = new Notification(request); + bool wearableFlag = true; + bool headsetFlag = false; + bool keyNodeFlag = false; + + notificationSubscriberManager.TrackCodeLog(notification, wearableFlag, headsetFlag, keyNodeFlag); + + ASSERT_NE(notification->GetNotificationRequestPoint(), nullptr); + ASSERT_TRUE(wearableFlag); + ASSERT_FALSE(headsetFlag); +} + +/** + * @tc.name: TrackCodeLog_006 + * @tc.desc: Test TrackCodeLog + * @tc.type: FUNC + */ +HWTEST_F(NotificationSubscriberManagerTest, TrackCodeLog_006, Function | SmallTest | Level1) +{ + NotificationSubscriberManager notificationSubscriberManager; + sptr request = new NotificationRequest(); + request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + auto liveViewContent = std::make_shared(); + auto content = std::make_shared(liveViewContent); + request->SetContent(content); + sptr notification = new Notification(request); + bool wearableFlag = false; + bool headsetFlag = true; + bool keyNodeFlag = false; + + notificationSubscriberManager.TrackCodeLog(notification, wearableFlag, headsetFlag, keyNodeFlag); + + ASSERT_NE(notification->GetNotificationRequestPoint(), nullptr); + ASSERT_TRUE(headsetFlag); + ASSERT_FALSE(wearableFlag); +} } // namespace Notification } // namespace OHOS