From 54adf156c820ab936d0ec32c36ee157f40fd8afd Mon Sep 17 00:00:00 2001 From: fangJinliang1 Date: Tue, 14 Jun 2022 19:15:31 +0800 Subject: [PATCH] add removalWantAgent ut Signed-off-by: fangJinliang1 Change-Id: I7b9795452e05d2c4239af276fbcabfdadce8a088 --- frameworks/test/moduletest/BUILD.gn | 1 + .../ans_innerkits_module_publish_test.cpp | 66 +++++++++++++++++++ .../advanced_notification_service_test.cpp | 2 + .../mock/mock_access_token_helper.cpp | 7 +- 4 files changed, 75 insertions(+), 1 deletion(-) diff --git a/frameworks/test/moduletest/BUILD.gn b/frameworks/test/moduletest/BUILD.gn index ea024698b..5f73269a6 100644 --- a/frameworks/test/moduletest/BUILD.gn +++ b/frameworks/test/moduletest/BUILD.gn @@ -139,6 +139,7 @@ ohos_moduletest("ans_innerkits_module_publish_test") { configs = [ "//utils/native/base:utils_config" ] deps = [ + "${aafwk_kits_path}/appkit:app_context", "${core_path}:ans_core", "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", diff --git a/frameworks/test/moduletest/ans_innerkits_module_publish_test.cpp b/frameworks/test/moduletest/ans_innerkits_module_publish_test.cpp index cf3785364..594f9295b 100644 --- a/frameworks/test/moduletest/ans_innerkits_module_publish_test.cpp +++ b/frameworks/test/moduletest/ans_innerkits_module_publish_test.cpp @@ -56,6 +56,7 @@ const int32_t CASE_THIRTEEN = 13; const int32_t CASE_FOURTEEN = 14; const int32_t CASE_FIFTEEN = 15; const int32_t CASE_SIXTEEN = 16; +const int32_t CASE_SEVENTEEN = 17; const int32_t PIXEL_MAP_TEST_WIDTH = 32; const int32_t PIXEL_MAP_TEST_HEIGHT = 32; @@ -145,6 +146,8 @@ public: CheckCaseFifteenResult(notificationRequest); } else if (CASE_SIXTEEN == notificationRequest.GetNotificationId()) { CheckCaseSixteenResult(notificationRequest); + } else if (CASE_SEVENTEEN == notificationRequest.GetNotificationId()) { + CheckCaseSeventeenResult(notificationRequest); } else { GTEST_LOG_(INFO) << "ANS_Interface_MT_Publish::OnConsumed do nothing!!!!!"; } @@ -395,6 +398,13 @@ private: EXPECT_EQ(NotificationConstant::FlagStatus::NONE, notiFlags->IsVibrationEnabled()); } } + + void CheckCaseSeventeenResult(NotificationRequest notificationRequest) + { + std::shared_ptr removalWantAgent = + notificationRequest.GetRemovalWantAgent(); + EXPECT_NE(removalWantAgent, nullptr); + } }; class CompletedCallbackTest : public AbilityRuntime::WantAgent::CompletedCallback { @@ -1519,5 +1529,61 @@ HWTEST_F(AnsInnerKitsModulePublishTest, ANS_Interface_MT_Slot_Enalbe_00200, Func EXPECT_EQ(enable, true); EXPECT_EQ(0, NotificationHelper::PublishNotification(req)); } + +/** + * @tc.number : ANS_Interface_MT_Publish_09000 + * @tc.name : Publish_09000 + * @tc.desc : Add removalWantAgent, make a subscriber and publish a removalWantAgent notification. + * @tc.expected : Add removalWantAgent success, make a subscriber and publish removalWantAgent notification success. + */ +HWTEST_F(AnsInnerKitsModulePublishTest, ANS_Interface_MT_Publish_09000, Function | MediumTest | Level1) +{ + NotificationSlot slot(NotificationConstant::OTHER); + EXPECT_EQ(0, NotificationHelper::AddNotificationSlot(slot)); + auto subscriber = TestAnsSubscriber(); + NotificationSubscribeInfo info = NotificationSubscribeInfo(); + info.AddAppName("bundleName"); + info.AddAppUserId(SUBSCRIBE_USER_ALL); + g_subscribe_mtx.lock(); + EXPECT_EQ(0, NotificationHelper::SubscribeNotification(subscriber, info)); + WaitOnSubscribeResult(); + + std::shared_ptr normalContent = std::make_shared(); + EXPECT_NE(normalContent, nullptr); + std::shared_ptr content = std::make_shared(normalContent); + EXPECT_NE(content, nullptr); + + auto want = std::make_shared(); + EXPECT_NE(want, nullptr); + want->SetAction("usual.event.REMOVAL_WANTAGENT"); + AppExecFwk::ElementName element("device", "bundleName", "abilityName"); + want->SetElement(element); + std::vector> wants { want }; + std::vector flags { + AbilityRuntime::WantAgent::WantAgentConstant::Flags::CONSTANT_FLAG }; + AbilityRuntime::WantAgent::WantAgentInfo paramsInfo( + 10, + AbilityRuntime::WantAgent::WantAgentConstant::OperationType::SEND_COMMON_EVENT, + flags, wants, nullptr + ); + + std::shared_ptr context = + std::make_shared(); + std::shared_ptr wantAgent = + AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(context, paramsInfo); + EXPECT_NE(wantAgent, nullptr); + + NotificationRequest req; + req.SetContent(content); + req.SetSlotType(NotificationConstant::OTHER); + req.SetNotificationId(CASE_SEVENTEEN); + req.SetRemovalWantAgent(wantAgent); + g_consumed_mtx.lock(); + EXPECT_EQ(0, NotificationHelper::PublishNotification(req)); + WaitOnConsumed(); + g_unsubscribe_mtx.lock(); + EXPECT_EQ(0, NotificationHelper::UnSubscribeNotification(subscriber, info)); + WaitOnUnsubscribeResult(); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 26342a3f1..26c4c0c17 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -179,6 +179,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00200, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00300, Function | SmallTest | Level1) { IPCSkeleton::SetCallingUid(NON_SYSTEM_APP_UID); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::CUSTOM); @@ -1587,6 +1588,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10900, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_11000, Function | SmallTest | Level1) { IPCSkeleton::SetCallingUid(SYSTEM_SERVICE_UID); + IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::OTHER); diff --git a/services/ans/test/unittest/mock/mock_access_token_helper.cpp b/services/ans/test/unittest/mock/mock_access_token_helper.cpp index ead552ce0..903342e7a 100644 --- a/services/ans/test/unittest/mock/mock_access_token_helper.cpp +++ b/services/ans/test/unittest/mock/mock_access_token_helper.cpp @@ -15,6 +15,7 @@ #include "access_token_helper.h" #include "ans_ut_constant.h" +#include "ipc_skeleton.h" namespace OHOS { namespace Notification { @@ -31,7 +32,11 @@ bool AccessTokenHelper::VerifyNativeToken(const Security::AccessToken::AccessTok bool AccessTokenHelper::IsSystemHap() { - return true; + Security::AccessToken::AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); + if (tokenId == NATIVE_TOKEN) { + return true; + } + return false; } } // namespace Notification } // namespace OHOS \ No newline at end of file -- Gitee