diff --git a/frameworks/core/test/unittest/ans_notification_test/BUILD.gn b/frameworks/core/test/unittest/ans_notification_test/BUILD.gn index a4bd5654f7cfb429eba50cd6226611f160fc173c..9973decea5354cb340e30d3b563d62d9684d3d9b 100644 --- a/frameworks/core/test/unittest/ans_notification_test/BUILD.gn +++ b/frameworks/core/test/unittest/ans_notification_test/BUILD.gn @@ -26,6 +26,7 @@ ohos_unittest("ans_notification_test") { ] sources = [ + "../mock/mock_ans_manager_proxy.cpp", "../mock/mock_message_parcel.cpp", "ans_notification_unit_test.cpp", ] diff --git a/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp b/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp index dfbc7ab6f7c955a03ef290511611dc0823bf762a..17bed8023fca60fc1e7f7078a0cea05e3a4a1aed 100644 --- a/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp @@ -27,6 +27,7 @@ #include "ans_notification.h" #include "ans_subscriber_proxy.h" #include "ans_manager_proxy.h" +#include "notification_subscriber.h" #undef private #undef protected #include "ans_inner_errors.h" @@ -34,15 +35,19 @@ #include "mock_i_remote_object.h" #include "notification.h" #include "singleton.h" -#include "notification_subscriber.h" +#include "mock_ans_manager_proxy.h" +#include "mock_pixel_map.cpp" using namespace testing; using namespace testing::ext; using namespace OHOS; using namespace OHOS::Notification; +extern void MockGetAnsManagerProxy(OHOS::sptr mockRet); +extern void MockPixelMapGetByteCount(int32_t mockRet); extern void MockWriteInterfaceToken(bool mockRet); + namespace OHOS { namespace Notification { class AnsNotificationUnitTest : public testing::Test { @@ -83,6 +88,10 @@ void AnsNotificationUnitTest::TearDown() {} class TestAnsSubscriber : public NotificationSubscriber { public: + TestAnsSubscriber() + {} + virtual ~TestAnsSubscriber() + {} void OnConnected() override {} void OnDisconnected() override @@ -111,6 +120,27 @@ public: {} }; +class TestLocalLiveViewSubscriber : public NotificationLocalLiveViewSubscriber { +public: + void OnConnected() + {} + + void OnDisconnected() + {} + + void OnResponse(int32_t notificationId, sptr buttonOption) + {} + + void OnDied() + {} + +private: + const sptr GetImpl() const + { + return nullptr; + } +}; + /* * @tc.name: GetAnsManagerProxy_0100 * @tc.desc: test GetAnsManagerProxy return false. @@ -128,22 +158,6 @@ HWTEST_F(AnsNotificationUnitTest, GetAnsManagerProxy_0100, Function | MediumTest EXPECT_EQ(res, false); } -/* - * @tc.name: SetNotificationSlotFlagsAsBundle_0100 - * @tc.desc: test GetNotificationSlotFlagsAsBundle. - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationUnitTest, SetNotificationSlotFlagsAsBundle_0100, Function | MediumTest | Level1) -{ - MockWriteInterfaceToken(false); - NotificationBundleOption bundleOptions; - bundleOptions.SetBundleName("bundleName"); - uint64_t num = 1; - ErrCode ret1 = ans_->SetNotificationSlotFlagsAsBundle(bundleOptions, num); - EXPECT_EQ(ret1, ERR_ANS_SERVICE_NOT_CONNECTED); -} - /* * @tc.name: AddSlotByType_0100 * @tc.desc: test AddSlotByType ErrCode ERR_ANS_SERVICE_NOT_CONNECTED. @@ -234,28 +248,6 @@ HWTEST_F(AnsNotificationUnitTest, GetNotificationSlotNumAsBundle_0100, Function EXPECT_EQ(ret1, ERR_ANS_SERVICE_NOT_CONNECTED); } -/* - * @tc.name: GetNotificationSlotNumAsBundle_0200 - * @tc.desc: test GetNotificationSlotNumAsBundle ErrCode ERR_OK. - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationUnitTest, GetNotificationSlotNumAsBundle_0200, Function | MediumTest | Level1) -{ - MockWriteInterfaceToken(false); - sptr iremoteObjects = new (std::nothrow) MockIRemoteObject(); - ASSERT_NE(nullptr, iremoteObjects); - std::shared_ptr proxy = std::make_shared(iremoteObjects); - ASSERT_NE(nullptr, proxy); - ans_->GetAnsManagerProxy(); - NotificationBundleOption bundleOptions; - std::string bundleName = "bundleName"; - bundleOptions.SetBundleName(bundleName); - uint64_t num; - ErrCode ret = ans_->GetNotificationSlotNumAsBundle(bundleOptions, num); - EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); -} - /* * @tc.name: GetNotificationSlotFlagsAsBundle_0100 * @tc.desc: test GetNotificationSlotFlagsAsBundle. @@ -300,54 +292,23 @@ HWTEST_F(AnsNotificationUnitTest, GetNotificationSlotFlagsAsBundle_0200, Functio } /* - * @tc.name: GetNotificationSlotFlagsAsBundle_0300 - * @tc.desc: test GetNotificationSlotFlagsAsBundle errCode ERR_OK. + * @tc.name: SetNotificationSlotFlagsAsBundle_0100 + * @tc.desc: test SetNotificationSlotFlagsAsBundle when proxy is null. * @tc.type: FUNC * @tc.require: #I62SME */ -HWTEST_F(AnsNotificationUnitTest, GetNotificationSlotFlagsAsBundle_0300, Function | MediumTest | Level1) +HWTEST_F(AnsNotificationUnitTest, SetNotificationSlotFlagsAsBundle_0100, Function | MediumTest | Level1) { - MockWriteInterfaceToken(false); - sptr iremoteObjects = new (std::nothrow) MockIRemoteObject(); - ASSERT_NE(nullptr, iremoteObjects); - std::shared_ptr proxy = std::make_shared(iremoteObjects); - ASSERT_NE(nullptr, proxy); - ans_->GetAnsManagerProxy(); NotificationBundleOption bundleOptions; - std::string bundleName = "bundleName"; - bundleOptions.SetBundleName(bundleName); - uint32_t num = 10; - ErrCode ret1 = ans_->GetNotificationSlotFlagsAsBundle(bundleOptions, num); + bundleOptions.SetBundleName("bundleName"); + uint64_t num = 1; + ErrCode ret1 = ans_->SetNotificationSlotFlagsAsBundle(bundleOptions, num); EXPECT_EQ(ret1, ERR_ANS_SERVICE_NOT_CONNECTED); } -/* - * @tc.name: AddNotificationSlots_0100 - * @tc.desc: test AddNotificationSlots exceed MAX_SLOT_NUM. - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationUnitTest, AddNotificationSlots_0100, Function | MediumTest | Level1) -{ - MockWriteInterfaceToken(false); - sptr iremoteObjects = new (std::nothrow) MockIRemoteObject(); - ASSERT_NE(nullptr, iremoteObjects); - std::shared_ptr proxy = std::make_shared(iremoteObjects); - ASSERT_NE(nullptr, proxy); - ans_->GetAnsManagerProxy(); - std::vector slots; - for (int i = 0; i < MAX_SLOT_NUM + 1; i++) { - NotificationConstant::SlotType slotType = NotificationConstant::SlotType::CUSTOM; - sptr notificationSlot = new (std::nothrow) NotificationSlot(slotType); - slots.push_back(*notificationSlot); - } - ErrCode ret = ans_->AddNotificationSlots(slots); - EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); -} - /* * @tc.name: SetNotificationSlotFlagsAsBundle_0200 - * @tc.desc: test GetNotificationSlotFlagsAsBundle. + * @tc.desc: test SetNotificationSlotFlagsAsBundle with invalid bundleOptions. * @tc.type: FUNC * @tc.require: #I62SME */ @@ -366,27 +327,6 @@ HWTEST_F(AnsNotificationUnitTest, SetNotificationSlotFlagsAsBundle_0200, Functio EXPECT_EQ(ret1, ERR_ANS_INVALID_PARAM); } -/* - * @tc.name: SetNotificationSlotFlagsAsBundle_0300 - * @tc.desc: test GetNotificationSlotFlagsAsBundle. - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationUnitTest, SetNotificationSlotFlagsAsBundle_0300, Function | MediumTest | Level1) -{ - MockWriteInterfaceToken(false); - sptr iremoteObjects = new (std::nothrow) MockIRemoteObject(); - ASSERT_NE(nullptr, iremoteObjects); - std::shared_ptr proxy = std::make_shared(iremoteObjects); - ASSERT_NE(nullptr, proxy); - ans_->GetAnsManagerProxy(); - NotificationBundleOption bundleOptions; - bundleOptions.SetBundleName("bundleName"); - uint64_t num = 1; - ErrCode ret1 = ans_->SetNotificationSlotFlagsAsBundle(bundleOptions, num); - EXPECT_EQ(ret1, ERR_ANS_SERVICE_NOT_CONNECTED); -} - /* * @tc.name: CanPopEnableNotificationDialog_0100 * @tc.desc: test CanPopEnableNotificationDialog. @@ -408,6 +348,18 @@ HWTEST_F(AnsNotificationUnitTest, CanPopEnableNotificationDialog_0100, Function EXPECT_EQ(ret1, ERR_ANS_SERVICE_NOT_CONNECTED); } +/* + * @tc.name: RemoveEnableNotificationDialog_0100 + * @tc.desc: test RemoveEnableNotificationDialog when proxy nullptr. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, RemoveEnableNotificationDialog_0100, Function | MediumTest | Level1) +{ + ErrCode ret = ans_->RemoveEnableNotificationDialog(); + EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); +} + /* * @tc.name: RemoveNotifications_0100 * @tc.desc: test RemoveNotifications. @@ -1201,6 +1153,32 @@ HWTEST_F(AnsNotificationUnitTest, SubscribeNotification_0400, Function | MediumT EXPECT_EQ(ret1, ERR_ANS_INVALID_PARAM); } +/* + * @tc.name: SubscribeNotificationSelf_0100 + * @tc.desc: test SubscribeNotificationSelf ErrCode ERR_ANS_SERVICE_NOT_CONNECTED. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, SubscribeNotificationSelf_0100, Function | MediumTest | Level1) +{ + auto subscriber = TestAnsSubscriber(); + ErrCode ret = ans_->SubscribeNotificationSelf(subscriber); + EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); +} + +/* + * @tc.name: SubscribeLocalLiveViewNotification_0100 + * @tc.desc: test SubscribeLocalLiveViewNotification ErrCode ERR_ANS_SERVICE_NOT_CONNECTED. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, SubscribeLocalLiveViewNotification_0100, Function | MediumTest | Level1) +{ + auto subscriber = TestLocalLiveViewSubscriber(); + ErrCode ret = ans_->SubscribeLocalLiveViewNotification(subscriber, false); + EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); +} + /* * @tc.name: GetAllActiveNotifications_0100 * @tc.desc: test GetAllActiveNotifications return false. @@ -1726,6 +1704,33 @@ HWTEST_F(AnsNotificationUnitTest, PublishNotificationForIndirectProxy_0100, Func EXPECT_EQ(res, ERR_ANS_INVALID_PARAM); } +/* + * @tc.name: PublishNotificationForIndirectProxy_0200 + * @tc.desc: test PublishNotificationForIndirectProxy when proxy is nullptr. + * @tc.type: FUNC + */ +HWTEST_F(AnsNotificationUnitTest, PublishNotificationForIndirectProxy_0200, Function | MediumTest | Level1) +{ + NotificationRequest request; + std::shared_ptr normalContent = std::make_shared(); + std::shared_ptr content = std::make_shared(normalContent); + request.SetContent(content); + ErrCode res = ans_->PublishNotificationForIndirectProxy(request); + EXPECT_EQ(res, ERR_ANS_SERVICE_NOT_CONNECTED); +} + +/* + * @tc.name: CancelAsBundle_0100 + * @tc.desc: test CancelAsBundle when proxy is nullptr. + * @tc.type: FUNC + */ +HWTEST_F(AnsNotificationUnitTest, CancelAsBundle_0100, Function | MediumTest | Level1) +{ + NotificationBundleOption ntfBundleOption; + ErrCode res = ans_->CancelAsBundle(ntfBundleOption, 0); + EXPECT_EQ(res, ERR_ANS_SERVICE_NOT_CONNECTED); +} + /* * @tc.name: GetNotificationSettings_0100 * @tc.desc: test GetNotificationSetting. @@ -1751,15 +1756,445 @@ HWTEST_F(AnsNotificationUnitTest, GetNotificationSettings_0100, Function | Mediu */ HWTEST_F(AnsNotificationUnitTest, GetNotificationSettings_0200, Function | MediumTest | Level1) { - MockWriteInterfaceToken(false); - sptr iremoteObjects = new (std::nothrow) MockIRemoteObject(); - ASSERT_NE(nullptr, iremoteObjects); - std::shared_ptr proxy = std::make_shared(iremoteObjects); - ASSERT_NE(nullptr, proxy); - ans_->GetAnsManagerProxy(); + sptr proxy = new (std::nothrow) MockAnsManagerProxy(); + MockGetAnsManagerProxy(proxy); uint32_t slotFlags = 0; ErrCode result = ans_->GetNotificationSettings(slotFlags); - EXPECT_EQ(result, ERR_ANS_SERVICE_NOT_CONNECTED); + EXPECT_EQ(result, ERR_OK); +} + +/* + * @tc.name: AddNotificationSlots_0100 + * @tc.desc: test AddNotificationSlots exceed MAX_SLOT_NUM. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, AddNotificationSlots_0100, Function | MediumTest | Level1) +{ + std::vector slots; + for (int i = 0; i < MAX_SLOT_NUM + 1; i++) { + NotificationConstant::SlotType slotType = NotificationConstant::SlotType::CUSTOM; + sptr notificationSlot = new (std::nothrow) NotificationSlot(slotType); + slots.push_back(*notificationSlot); + } + ErrCode ret = ans_->AddNotificationSlots(slots); + EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM); +} + +/* + * @tc.name: GetNotificationSlotNumAsBundle_0200 + * @tc.desc: test GetNotificationSlotNumAsBundle ErrCode ERR_OK. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, GetNotificationSlotNumAsBundle_0200, Function | MediumTest | Level1) +{ + NotificationBundleOption bundleOptions; + std::string bundleName = "bundleName"; + bundleOptions.SetBundleName(bundleName); + uint64_t num; + ErrCode ret = ans_->GetNotificationSlotNumAsBundle(bundleOptions, num); + EXPECT_EQ(ret, ERR_OK); +} + +/* + * @tc.name: GetNotificationSlotFlagsAsBundle_0300 + * @tc.desc: test GetNotificationSlotFlagsAsBundle errCode ERR_OK. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, GetNotificationSlotFlagsAsBundle_0300, Function | MediumTest | Level1) +{ + NotificationBundleOption bundleOptions; + std::string bundleName = "bundleName"; + bundleOptions.SetBundleName(bundleName); + uint32_t num = 10; + ErrCode ret = ans_->GetNotificationSlotFlagsAsBundle(bundleOptions, num); + EXPECT_EQ(ret, ERR_OK); +} + +/* + * @tc.name: SetNotificationSlotFlagsAsBundle_0300 + * @tc.desc: test GetNotificationSlotFlagsAsBundle. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, SetNotificationSlotFlagsAsBundle_0300, Function | MediumTest | Level1) +{ + NotificationBundleOption bundleOptions; + bundleOptions.SetBundleName("bundleName"); + uint64_t num = 1; + ErrCode ret = ans_->SetNotificationSlotFlagsAsBundle(bundleOptions, num); + EXPECT_EQ(ret, ERR_OK); +} + +/* + * @tc.name: PublishNotification_0200 + * @tc.desc: test PublishNotification ErrCode ERR_ANS_INVALID_PARAM cause IsValidDelayTime fail. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, PublishNotification_0200, Function | MediumTest | Level1) +{ + std::string label = "this is label"; + NotificationRequest request; + std::shared_ptr normalContent = std::make_shared(); + std::shared_ptr content = std::make_shared(normalContent); + request.SetContent(content); + request.SetPublishDelayTime(6); + ErrCode ret = ans_->PublishNotification(label, request, "instanceKey"); + EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM); +} + +/* + * @tc.name: PublishNotification_0300 + * @tc.desc: test PublishNotification ErrCode ERR_ANS_ICON_OVER_SIZE. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, PublishNotification_0300, Function | MediumTest | Level1) +{ + std::string label = "this is label"; + NotificationRequest request; + std::shared_ptr normalContent = std::make_shared(); + std::shared_ptr content = std::make_shared(normalContent); + request.SetContent(content); + MockPixelMapGetByteCount(256 * 1024); + std::shared_ptr pixelMap = std::make_shared(); + request.SetLittleIcon(pixelMap); + ErrCode ret = ans_->PublishNotification(label, request, "instanceKey"); + EXPECT_EQ(ret, ERR_ANS_ICON_OVER_SIZE); +} + +/* + * @tc.name: PublishNotification_0400 + * @tc.desc: test PublishNotification with common liveView. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, PublishNotification_0400, Function | MediumTest | Level1) +{ + std::string label = "this is label"; + NotificationRequest request; + std::shared_ptr liveViewContent = std::make_shared(); + liveViewContent->SetContentType(static_cast(NotificationContent::Type::LIVE_VIEW)); + std::shared_ptr content = std::make_shared(liveViewContent); + request.SetContent(content); + request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + ErrCode ret = ans_->PublishNotification(label, request, "instanceKey"); + EXPECT_EQ(ret, ERR_OK); +} + +/* + * @tc.name: PublishNotificationForIndirectProxy_0300 + * @tc.desc: test PublishNotificationForIndirectProxy ErrCode ERR_ANS_INVALID_PARAM cause CanPublishMediaContent fail. + * @tc.type: FUNC + */ +HWTEST_F(AnsNotificationUnitTest, PublishNotificationForIndirectProxy_0300, Function | MediumTest | Level1) +{ + NotificationRequest request; + std::shared_ptr mediaContent = std::make_shared(); + std::vector actions; + for (int i = 0; i < 3; i++) { + actions.push_back(32); + } + mediaContent->SetShownActions(actions); + mediaContent->SetContentType(static_cast(NotificationContent::Type::MEDIA)); + std::shared_ptr content = std::make_shared(mediaContent); + request.SetContent(content); + ErrCode res = ans_->PublishNotificationForIndirectProxy(request); + EXPECT_EQ(res, ERR_ANS_INVALID_PARAM); +} + +/* + * @tc.name: PublishNotificationForIndirectProxy_0400 + * @tc.desc: test PublishNotificationForIndirectProxy ErrCode ERR_ANS_INVALID_PARAM cause CanPublishLiveViewContent fail + * @tc.type: FUNC + */ +HWTEST_F(AnsNotificationUnitTest, PublishNotificationForIndirectProxy_0400, Function | MediumTest | Level1) +{ + NotificationRequest request; + std::shared_ptr liveContent = std::make_shared(); + liveContent->SetContentType(static_cast(NotificationContent::Type::LIVE_VIEW)); + liveContent->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BUTT); + std::shared_ptr content = std::make_shared(liveContent); + request.SetContent(content); + request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + ErrCode res = ans_->PublishNotificationForIndirectProxy(request); + EXPECT_EQ(res, ERR_ANS_INVALID_PARAM); +} + +/* + * @tc.name: PublishNotificationForIndirectProxy_0500 + * @tc.desc: test PublishNotificationForIndirectProxy ErrCode ERR_ANS_ICON_OVER_SIZE + * @tc.type: FUNC + */ +HWTEST_F(AnsNotificationUnitTest, PublishNotificationForIndirectProxy_0500, Function | MediumTest | Level1) +{ + NotificationRequest request; + std::shared_ptr normalContent = std::make_shared(); + std::shared_ptr content = std::make_shared(normalContent); + request.SetContent(content); + MockPixelMapGetByteCount(256 * 1024); + std::shared_ptr pixelMap = std::make_shared(); + request.SetLittleIcon(pixelMap); + ErrCode res = ans_->PublishNotificationForIndirectProxy(request); + EXPECT_EQ(res, ERR_ANS_ICON_OVER_SIZE); +} + +/* + * @tc.name: PublishNotificationForIndirectProxy_0600 + * @tc.desc: test PublishNotificationForIndirectProxy with common liveView. + * @tc.type: FUNC + */ +HWTEST_F(AnsNotificationUnitTest, PublishNotificationForIndirectProxy_0600, Function | MediumTest | Level1) +{ + NotificationRequest request; + std::shared_ptr liveViewContent = std::make_shared(); + liveViewContent->SetContentType(static_cast(NotificationContent::Type::LIVE_VIEW)); + std::shared_ptr content = std::make_shared(liveViewContent); + request.SetContent(content); + request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + ErrCode res = ans_->PublishNotificationForIndirectProxy(request); + EXPECT_EQ(res, ERR_OK); +} + +/* + * @tc.name: PublishNotificationForIndirectProxy_0700 + * @tc.desc: test PublishNotificationForIndirectProxy normal. + * @tc.type: FUNC + */ +HWTEST_F(AnsNotificationUnitTest, PublishNotificationForIndirectProxy_0700, Function | MediumTest | Level1) +{ + NotificationRequest request; + std::shared_ptr normalContent = std::make_shared(); + std::shared_ptr content = std::make_shared(normalContent); + request.SetContent(content); + ErrCode res = ans_->PublishNotificationForIndirectProxy(request); + EXPECT_EQ(res, ERR_OK); +} + +/* + * @tc.name: PublishNotificationAsBundle_0100 + * @tc.desc: test PublishNotificationAsBundle ErrCode ERR_ANS_ICON_OVER_SIZE + * @tc.type: FUNC + */ +HWTEST_F(AnsNotificationUnitTest, PublishNotificationAsBundle_0100, Function | MediumTest | Level1) +{ + NotificationRequest request; + std::shared_ptr normalContent = std::make_shared(); + std::shared_ptr content = std::make_shared(normalContent); + request.SetContent(content); + MockPixelMapGetByteCount(256 * 1024); + std::shared_ptr pixelMap = std::make_shared(); + request.SetLittleIcon(pixelMap); + std::string representativeBundle = "this is representativeBundle"; + ErrCode res = ans_->PublishNotificationAsBundle(representativeBundle, request); + EXPECT_EQ(res, ERR_ANS_ICON_OVER_SIZE); +} + +/* + * @tc.name: PublishNotificationAsBundle_0200 + * @tc.desc: test PublishNotificationAsBundle with common liveView. + * @tc.type: FUNC + */ +HWTEST_F(AnsNotificationUnitTest, PublishNotificationAsBundle_0200, Function | MediumTest | Level1) +{ + NotificationRequest request; + std::shared_ptr liveViewContent = std::make_shared(); + liveViewContent->SetContentType(static_cast(NotificationContent::Type::LIVE_VIEW)); + std::shared_ptr content = std::make_shared(liveViewContent); + request.SetContent(content); + request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + std::string representativeBundle = "this is representativeBundle"; + ErrCode res = ans_->PublishNotificationAsBundle(representativeBundle, request); + EXPECT_EQ(res, ERR_OK); +} + +/* + * @tc.name: PublishNotificationAsBundle_0300 + * @tc.desc: test PublishNotificationAsBundle normal + * @tc.type: FUNC + */ +HWTEST_F(AnsNotificationUnitTest, PublishNotificationAsBundle_0300, Function | MediumTest | Level1) +{ + NotificationRequest request; + std::shared_ptr normalContent = std::make_shared(); + std::shared_ptr content = std::make_shared(normalContent); + request.SetContent(content); + std::string representativeBundle = "this is representativeBundle"; + ErrCode res = ans_->PublishNotificationAsBundle(representativeBundle, request); + EXPECT_EQ(res, ERR_OK); +} + +/* + * @tc.name: CanPopEnableNotificationDialog_0200 + * @tc.desc: test CanPopEnableNotificationDialog normal + * @tc.type: FUNC + */ +HWTEST_F(AnsNotificationUnitTest, CanPopEnableNotificationDialog_0200, Function | MediumTest | Level1) +{ + sptr client = nullptr; + bool enable = true; + std::string bundleName = ""; + ErrCode ret = ans_->CanPopEnableNotificationDialog(client, enable, bundleName); + EXPECT_EQ(ret, ERR_OK); +} + +/* + * @tc.name: RemoveEnableNotificationDialog_0200 + * @tc.desc: test RemoveEnableNotificationDialog normal. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, RemoveEnableNotificationDialog_0200, Function | MediumTest | Level1) +{ + ErrCode ret = ans_->RemoveEnableNotificationDialog(); + EXPECT_EQ(ret, ERR_OK); +} + +/* + * @tc.name: RequestEnableNotification_0200 + * @tc.desc: test RequestEnableNotification when hostClient nullptr. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, RequestEnableNotification_0200, Function | MediumTest | Level1) +{ + std::string deviceId = "this is deviceId"; + sptr callerToken = nullptr; + sptr client = nullptr; + ErrCode ret = ans_->RequestEnableNotification(deviceId, client, callerToken); + EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM); +} + +/* + * @tc.name: RequestEnableNotification_0300 + * @tc.desc: test RequestEnableNotification normal. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, RequestEnableNotification_0300, Function | MediumTest | Level1) +{ + std::string deviceId = "this is deviceId"; + sptr callerToken = new (std::nothrow) MockIRemoteObject(); + sptr client = nullptr; + AnsDialogHostClient::CreateIfNullptr(client); + client = AnsDialogHostClient::GetInstance(); + ErrCode ret = ans_->RequestEnableNotification(deviceId, client, callerToken); + EXPECT_EQ(ret, ERR_OK); +} + +/* + * @tc.name: RequestEnableNotification_0400 + * @tc.desc: test RequestEnableNotification normal. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, RequestEnableNotification_0400, Function | MediumTest | Level1) +{ + std::string bundleName = "bundleName"; + int32_t uid = 100; + ErrCode ret = ans_->RequestEnableNotification(bundleName, uid); + EXPECT_EQ(ret, ERR_OK); +} + +/* + * @tc.name: GetBundleImportance_0200 + * @tc.desc: test GetBundleImportance ErrCode ERR_ANS_SERVICE_NOT_CONNECTED. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, GetBundleImportance_0200, Function | MediumTest | Level1) +{ + NotificationSlot::NotificationLevel importance = NotificationSlot::NotificationLevel::LEVEL_NONE; + ErrCode ret = ans_->GetBundleImportance(importance); + EXPECT_EQ(ret, ERR_OK); +} + +/* + * @tc.name: SubscribeNotification_0500 + * @tc.desc: test SubscribeNotification when subscriberSptr nullptr. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, SubscribeNotification_0500, Function | MediumTest | Level1) +{ + auto subscriber = TestAnsSubscriber(); + subscriber.impl_ = nullptr; + ErrCode ret = ans_->SubscribeNotification(subscriber); + EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM); +} + +/* + * @tc.name: SubscribeNotificationSelf_0200 + * @tc.desc: test SubscribeNotificationSelf when subscriberSptr nullptr. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, SubscribeNotificationSelf_0200, Function | MediumTest | Level1) +{ + auto subscriber = TestAnsSubscriber(); + subscriber.impl_ = nullptr; + ErrCode ret = ans_->SubscribeNotificationSelf(subscriber); + EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM); +} + +/* + * @tc.name: SubscribeNotificationSelf_0300 + * @tc.desc: test SubscribeNotificationSelf normal. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, SubscribeNotificationSelf_0300, Function | MediumTest | Level1) +{ + auto subscriber = TestAnsSubscriber(); + ErrCode ret = ans_->SubscribeNotificationSelf(subscriber); + EXPECT_EQ(ret, ERR_OK); +} + +/* + * @tc.name: SubscribeLocalLiveViewNotification_0200 + * @tc.desc: test SubscribeLocalLiveViewNotification when subscriberSptr nullptr. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, SubscribeLocalLiveViewNotification_0200, Function | MediumTest | Level1) +{ + auto subscriber = TestLocalLiveViewSubscriber(); + subscriber.impl_ = nullptr; + ErrCode ret = ans_->SubscribeLocalLiveViewNotification(subscriber, false); + EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM); +} + +/* + * @tc.name: SubscribeNotification_0600 + * @tc.desc: test SubscribeNotification when subscriberSptr nullptr. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, SubscribeNotification_0600, Function | MediumTest | Level1) +{ + auto subscriber = TestAnsSubscriber(); + subscriber.impl_ = nullptr; + auto info = NotificationSubscribeInfo(); + ErrCode ret = ans_->SubscribeNotification(subscriber, info); + EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM); +} + +/* + * @tc.name: SubscribeNotification_0700 + * @tc.desc: test SubscribeNotification normal. + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationUnitTest, SubscribeNotification_0700, Function | MediumTest | Level1) +{ + auto subscriber = TestAnsSubscriber(); + auto info = NotificationSubscribeInfo(); + info.AddDeviceType("phone"); + ErrCode ret = ans_->SubscribeNotification(subscriber, info); + EXPECT_EQ(ret, ERR_OK); } } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h b/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..bd331a01b5e43c1ad99970cf133f58aad3adc6ab --- /dev/null +++ b/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h @@ -0,0 +1,192 @@ +/* + * 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. + */ + +#ifndef OHOS_NOTIFICATION_MOCK_ANS_MANAGER_PROXY_H +#define OHOS_NOTIFICATION_MOCK_ANS_MANAGER_PROXY_H + +#include "gmock/gmock.h" + +#include +#include "ians_manager.h" + +namespace OHOS { +namespace Notification { +class MockAnsManagerProxy : public IAnsManager { +public: + MockAnsManagerProxy() = default; + virtual ~MockAnsManagerProxy() {}; + + sptr AsObject() override + { + return nullptr; + } + + ErrCode GetBundleImportance(int32_t& importance) + { + importance = NotificationSlot::LEVEL_HIGH + 1; + return ERR_OK; + } + + MOCK_METHOD2(Publish, ErrCode(const std::string&, const sptr&)); + MOCK_METHOD2(PublishWithMaxCapacity, ErrCode(const std::string&, const sptr&)); + MOCK_METHOD1(PublishNotificationForIndirectProxy, ErrCode(const sptr&)); + MOCK_METHOD1(PublishNotificationForIndirectProxyWithMaxCapacity, ErrCode(const sptr&)); + MOCK_METHOD3(Cancel, ErrCode(int32_t, const std::string&, const std::string&)); + MOCK_METHOD1(CancelAll, ErrCode(const std::string&)); + MOCK_METHOD3(CancelAsBundle, ErrCode(int32_t, const std::string&, int32_t)); + MOCK_METHOD2(CancelAsBundle, ErrCode(const sptr&, int32_t)); + MOCK_METHOD3(CancelAsBundle, ErrCode(const sptr&, int32_t, int32_t)); + MOCK_METHOD1(AddSlotByType, ErrCode(int32_t)); + MOCK_METHOD1(AddSlots, ErrCode(const std::vector>&)); + MOCK_METHOD1(RemoveSlotByType, ErrCode(int32_t)); + MOCK_METHOD0(RemoveAllSlots, ErrCode()); + MOCK_METHOD2(GetSlotByType, ErrCode(int32_t, sptr&)); + MOCK_METHOD1(GetSlots, ErrCode(std::vector>&)); + MOCK_METHOD2(GetSlotNumAsBundle, ErrCode(const sptr&, uint64_t&)); + MOCK_METHOD2(GetActiveNotifications, ErrCode(std::vector>&, const std::string&)); + MOCK_METHOD1(GetActiveNotificationNums, ErrCode(uint64_t&)); + MOCK_METHOD1(GetAllActiveNotifications, ErrCode(std::vector>&)); + MOCK_METHOD2(GetAllNotificationsBySlotType, ErrCode(std::vector>&, int32_t)); + MOCK_METHOD2(GetSpecialActiveNotifications, + ErrCode(const std::vector&, std::vector>&)); + MOCK_METHOD5(GetActiveNotificationByFilter, ErrCode( + const sptr&, + int32_t, + const std::string&, + const std::vector&, + sptr&)); + MOCK_METHOD2(CanPublishAsBundle, ErrCode(const std::string&, bool&)); + MOCK_METHOD2(PublishAsBundle, ErrCode(const sptr&, const std::string&)); + MOCK_METHOD2(PublishAsBundleWithMaxCapacity, ErrCode(const sptr&, const std::string&)); + MOCK_METHOD1(SetNotificationBadgeNum, ErrCode(int32_t)); + MOCK_METHOD1(HasNotificationPolicyAccessPermission, ErrCode(bool&)); + MOCK_METHOD3(TriggerLocalLiveView, + ErrCode(const sptr&, int32_t, const sptr&)); + MOCK_METHOD4(RemoveNotification, ErrCode( + const sptr&, int32_t, const std::string&, int32_t)); + MOCK_METHOD1(RemoveAllNotifications, ErrCode(const sptr&)); + MOCK_METHOD2(RemoveNotifications, ErrCode(const std::vector&, int32_t)); + MOCK_METHOD2(Delete, ErrCode(const std::string&, int32_t)); + MOCK_METHOD1(DeleteByBundle, ErrCode(const sptr&)); + MOCK_METHOD0(DeleteAll, ErrCode()); + MOCK_METHOD2(GetSlotsByBundle, + ErrCode(const sptr&, std::vector>&)); + MOCK_METHOD3(GetSlotByBundle, ErrCode(const sptr&, int32_t, sptr&)); + MOCK_METHOD2(UpdateSlots, + ErrCode(const sptr&, const std::vector>&)); + MOCK_METHOD2(RequestEnableNotification, ErrCode(const std::string&, const sptr&)); + MOCK_METHOD3(RequestEnableNotification, + ErrCode(const std::string&, const sptr&, const sptr&)); + MOCK_METHOD2(RequestEnableNotification, ErrCode(const std::string&, int32_t)); + MOCK_METHOD2(SetNotificationsEnabledForBundle, ErrCode(const std::string&, bool)); + MOCK_METHOD2(SetNotificationsEnabledForAllBundles, ErrCode(const std::string&, bool)); + MOCK_METHOD4(SetNotificationsEnabledForSpecialBundle, + ErrCode(const std::string&, const sptr&, bool, bool)); + MOCK_METHOD2(SetShowBadgeEnabledForBundle, ErrCode(const sptr&, bool)); + MOCK_METHOD2(GetShowBadgeEnabledForBundle, ErrCode(const sptr&, bool&)); + MOCK_METHOD1(GetShowBadgeEnabled, ErrCode(bool&)); + MOCK_METHOD1(Subscribe, ErrCode(const sptr&)); + MOCK_METHOD2(Subscribe, ErrCode(const sptr&, const sptr&)); + MOCK_METHOD1(SubscribeSelf, ErrCode(const sptr&)); + MOCK_METHOD2(SubscribeLocalLiveView, ErrCode(const sptr&, bool)); + MOCK_METHOD3(SubscribeLocalLiveView, + ErrCode(const sptr&, const sptr&, bool)); + MOCK_METHOD1(Unsubscribe, ErrCode(const sptr&)); + MOCK_METHOD2(Unsubscribe, ErrCode(const sptr&, const sptr&)); + MOCK_METHOD1(IsAllowedNotify, ErrCode(bool&)); + MOCK_METHOD1(IsAllowedNotifySelf, ErrCode(bool&)); + MOCK_METHOD3(CanPopEnableNotificationDialog, + ErrCode(const sptr&, bool&, std::string&)); + MOCK_METHOD0(RemoveEnableNotificationDialog, ErrCode()); + MOCK_METHOD2(IsSpecialBundleAllowedNotify, ErrCode(const sptr&, bool&)); + MOCK_METHOD1(SetDoNotDisturbDate, ErrCode(const sptr&)); + MOCK_METHOD1(GetDoNotDisturbDate, ErrCode(sptr&)); + MOCK_METHOD1(AddDoNotDisturbProfiles, ErrCode(const std::vector>&)); + MOCK_METHOD1(RemoveDoNotDisturbProfiles, ErrCode(const std::vector>&)); + MOCK_METHOD1(DoesSupportDoNotDisturbMode, ErrCode(bool&)); + MOCK_METHOD2(IsNeedSilentInDoNotDisturbMode, ErrCode(const std::string&, int32_t)); + MOCK_METHOD2(CancelGroup, ErrCode(const std::string&, const std::string&)); + MOCK_METHOD2(RemoveGroupByBundle, ErrCode(const sptr&, const std::string&)); + MOCK_METHOD1(IsDistributedEnabled, ErrCode(bool&)); + MOCK_METHOD3(SetDistributedEnabledBySlot, ErrCode(int32_t, const std::string&, bool)); + MOCK_METHOD3(IsDistributedEnabledBySlot, ErrCode(int32_t, const std::string&, bool&)); + MOCK_METHOD1(EnableDistributed, ErrCode(bool)); + MOCK_METHOD2(EnableDistributedByBundle, ErrCode(const sptr&, bool)); + MOCK_METHOD1(EnableDistributedSelf, ErrCode(bool)); + MOCK_METHOD2(IsDistributedEnableByBundle, ErrCode(const sptr&, bool&)); + MOCK_METHOD1(GetDeviceRemindType, ErrCode(int32_t&)); + MOCK_METHOD1(PublishContinuousTaskNotification, ErrCode(const sptr&)); + MOCK_METHOD2(CancelContinuousTaskNotification, ErrCode(const std::string&, int32_t)); + MOCK_METHOD2(IsSupportTemplate, ErrCode(const std::string&, bool&)); + MOCK_METHOD2(IsSpecialUserAllowedNotify, ErrCode(int32_t, bool&)); + MOCK_METHOD2(SetNotificationsEnabledByUser, ErrCode(int32_t, bool)); + MOCK_METHOD1(DeleteAllByUser, ErrCode(int32_t)); + MOCK_METHOD2(SetDoNotDisturbDate, ErrCode(int32_t, const sptr&)); + MOCK_METHOD2(GetDoNotDisturbDate, ErrCode(int32_t, sptr&)); + MOCK_METHOD4(SetEnabledForBundleSlot, + ErrCode(const sptr& bundleOption, int32_t, bool, bool)); + MOCK_METHOD3(GetEnabledForBundleSlot, ErrCode(const sptr&, int32_t, bool&)); + MOCK_METHOD2(GetEnabledForBundleSlotSelf, ErrCode(int32_t, bool&)); + MOCK_METHOD5(ShellDump, + ErrCode(const std::string&, const std::string&, int32_t, int32_t, std::vector&)); + MOCK_METHOD2(SetSyncNotificationEnabledWithoutApp, ErrCode(int32_t, bool)); + MOCK_METHOD2(GetSyncNotificationEnabledWithoutApp, ErrCode(int32_t, bool&)); + MOCK_METHOD2(SetBadgeNumber, ErrCode(int32_t, const std::string&)); + MOCK_METHOD2(SetBadgeNumberByBundle, ErrCode(const sptr&, int32_t)); + MOCK_METHOD2(SetBadgeNumberForDhByBundle, ErrCode(const sptr&, int32_t)); + MOCK_METHOD2(GetSlotFlagsAsBundle, ErrCode(const sptr&, uint32_t&)); + MOCK_METHOD2(SetSlotFlagsAsBundle, ErrCode(const sptr&, uint32_t)); + MOCK_METHOD1(GetNotificationSettings, ErrCode(uint32_t&)); + MOCK_METHOD1(GetAllNotificationEnabledBundles, ErrCode(std::vector&)); + MOCK_METHOD1(GetAllLiveViewEnabledBundles, ErrCode(std::vector&)); + MOCK_METHOD2(GetAllDistribuedEnabledBundles, ErrCode(const std::string&, std::vector&)); + MOCK_METHOD2(RegisterPushCallback, ErrCode(const sptr&, const sptr&)); + MOCK_METHOD0(UnregisterPushCallback, ErrCode()); + MOCK_METHOD2(SetAdditionConfig, ErrCode(const std::string&, const std::string&)); + MOCK_METHOD3(SetDistributedEnabledByBundle, + ErrCode(const sptr&, const std::string&, bool)); + MOCK_METHOD2(SetDistributedEnabled, ErrCode(const std::string&, bool)); + MOCK_METHOD2(IsDistributedEnabled, ErrCode(const std::string&, bool&)); + MOCK_METHOD1(GetDistributedAbility, ErrCode(int32_t&)); + MOCK_METHOD4(GetDistributedAuthStatus, ErrCode(const std::string&, const std::string&, int32_t, bool&)); + MOCK_METHOD4(SetDistributedAuthStatus, ErrCode(const std::string&, const std::string&, int32_t, bool)); + MOCK_METHOD2(IsSmartReminderEnabled, ErrCode(const std::string&, bool&)); + MOCK_METHOD2(SetSmartReminderEnabled, ErrCode(const std::string&, bool)); + MOCK_METHOD3(IsDistributedEnabledByBundle, + ErrCode(const sptr&, const std::string&, bool&)); + MOCK_METHOD2(CancelAsBundleWithAgent, ErrCode(const sptr&, int32_t)); + MOCK_METHOD3(SetTargetDeviceStatus, ErrCode(const std::string&, uint32_t, const std::string&)); + MOCK_METHOD5(SetTargetDeviceStatus, ErrCode(const std::string&, uint32_t, uint32_t, const std::string&, int32_t)); + MOCK_METHOD2(GetDoNotDisturbProfile, ErrCode(int64_t, sptr&)); + MOCK_METHOD2(AllowUseReminder, ErrCode(const std::string&, bool&)); + MOCK_METHOD2(UpdateNotificationTimerByUid, ErrCode(int32_t, bool)); + MOCK_METHOD1(DisableNotificationFeature, ErrCode(const sptr&)); + MOCK_METHOD2(GetTargetDeviceStatus, ErrCode(const std::string&, int32_t&)); + MOCK_METHOD2(DistributeOperation, + ErrCode(const sptr&, const sptr&)); + MOCK_METHOD2(ReplyDistributeOperation, ErrCode(const std::string&, int32_t)); + MOCK_METHOD2(GetNotificationRequestByHashCode, ErrCode(const std::string&, sptr&)); + MOCK_METHOD4(SetTargetDeviceBundleList, + ErrCode(const std::string&, const std::string&, int32_t, const std::vector&)); + MOCK_METHOD4(SetTargetDeviceSwitch, ErrCode(const std::string&, const std::string&, bool, bool)); + MOCK_METHOD1(SetHashCodeRule, ErrCode(uint32_t)); + +#ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED + MOCK_METHOD1(RegisterSwingCallback, ErrCode(const sptr&)); +#endif +}; +} // namespace Notification +} // namespace OHOS +#endif // OHOS_NOTIFICATION_MOCK_ANS_MANAGER_PROXY_H \ No newline at end of file