diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 60b4a295f89f22f88bb5f1cbdee752f039acf396..5c973860715ebba9b0e6da6eadc3763c7c495a38 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -1996,6 +1996,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15200, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15300, Function | SmallTest | Level1) { + IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); std::string cmd = "CMD"; std::string bundle = "Bundle"; int32_t userId = 4; @@ -2015,5 +2016,79 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15400, std::vector args; EXPECT_EQ(advancedNotificationService_->Dump(fd, args), (int)ERR_OK); } + +/** + * @tc.number : AdvancedNotificationServiceTest_15500 + * @tc.name : OnReceiveEvent_0100 + * @tc.desc : Test OnReceiveEvent function userid req = new (std::nothrow) NotificationRequest(1); + EXPECT_NE(req, nullptr); + req->SetSlotType(NotificationConstant::SlotType::OTHER); + req->SetLabel("req's label"); + std::string label = "publish's label"; + std::shared_ptr normalContent = std::make_shared(); + EXPECT_NE(normalContent, nullptr); + normalContent->SetText("normalContent's text"); + normalContent->SetTitle("normalContent's title"); + std::shared_ptr content = std::make_shared(normalContent); + EXPECT_NE(content, nullptr); + req->SetContent(content); + req->SetCreatorUserId(DEFAULT_USER_ID); + EXPECT_EQ(advancedNotificationService_->Publish(label, req), ERR_OK); + SleepForFC(); + + EventFwk::Want want; + EventFwk::CommonEventData data; + data.SetWant(want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED)); + data.SetCode(50); + advancedNotificationService_->systemEventObserver_->OnReceiveEvent(data); + + std::stringstream key; + key << "_" << req->GetCreatorUserId() << "_" << req->GetCreatorUid() << "_" + << req->GetLabel() << "_" << req->GetNotificationId(); + + EXPECT_EQ(advancedNotificationService_->IsNotificationExists(key.str()), true); +} + +/** + * @tc.number : AdvancedNotificationServiceTest_15600 + * @tc.name : OnReceiveEvent_0200 + * @tc.desc : Test OnReceiveEvent function when userid>DEFAULT_USER_ID + * @tc.require : I5TIQR + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15600, Function | SmallTest | Level1) +{ + sptr req = new (std::nothrow) NotificationRequest(1); + EXPECT_NE(req, nullptr); + req->SetSlotType(NotificationConstant::SlotType::OTHER); + req->SetLabel("req's label"); + std::string label = "publish's label"; + std::shared_ptr normalContent = std::make_shared(); + EXPECT_NE(normalContent, nullptr); + normalContent->SetText("normalContent's text"); + normalContent->SetTitle("normalContent's title"); + std::shared_ptr content = std::make_shared(normalContent); + EXPECT_NE(content, nullptr); + req->SetContent(content); + req->SetCreatorUserId(DEFAULT_USER_ID); + EXPECT_EQ(advancedNotificationService_->Publish(label, req), ERR_OK); + SleepForFC(); + + EventFwk::Want want; + EventFwk::CommonEventData data; + data.SetWant(want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED)); + data.SetCode(200); + advancedNotificationService_->systemEventObserver_->OnReceiveEvent(data); + + std::stringstream key; + key << "_" << req->GetCreatorUserId() << "_" << req->GetCreatorUid() << "_" + << req->GetLabel() << "_" << req->GetNotificationId(); + + EXPECT_EQ(advancedNotificationService_->IsNotificationExists(key.str()), true); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/tools/test/mock/mock_ans_notification.cpp b/tools/test/mock/mock_ans_notification.cpp new file mode 100644 index 0000000000000000000000000000000000000000..071d92d7e22b125f0f571f96facaf1878c945dd5 --- /dev/null +++ b/tools/test/mock/mock_ans_notification.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021-2022 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 "ans_notification.h" +#include "ans_inner_errors.h" + +namespace OHOS { +namespace Notification { + +ErrCode AnsNotification::SetNotificationsEnabledForSpecifiedBundle( + const NotificationBundleOption &bundleOption, const std::string &deviceId, bool enabled) +{ + if (bundleOption.GetBundleName() == "gg") { + return ERR_OK; + } else { + return ERR_ANS_SERVICE_NOT_CONNECTED; + } +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/tools/test/unittest/dump/BUILD.gn b/tools/test/unittest/dump/BUILD.gn index 0891923de76a237ac63313166c87e705d14aa5dd..dab5f124614f9ca4cd7d4a574dd371b1ad2e73f3 100644 --- a/tools/test/unittest/dump/BUILD.gn +++ b/tools/test/unittest/dump/BUILD.gn @@ -35,6 +35,7 @@ ohos_unittest("notification_shell_command_dump_test") { sources = [ "${tools_path}/dump/src/notification_shell_command.cpp", "${tools_path}/dump/src/shell_command.cpp", + "${tools_path}/test/mock/mock_ans_notification.cpp", "notification_shell_command_dump_test.cpp", ] sources += tools_dump_mock_sources diff --git a/tools/test/unittest/dump/notification_shell_command_dump_test.cpp b/tools/test/unittest/dump/notification_shell_command_dump_test.cpp index 73a22b3b58f2affb9beac69e05e66af6111eec86..e7db2ca6b4abb336820f3be7c48720740d4fe531 100644 --- a/tools/test/unittest/dump/notification_shell_command_dump_test.cpp +++ b/tools/test/unittest/dump/notification_shell_command_dump_test.cpp @@ -70,6 +70,14 @@ static char g_enableBundleNameNull[] = " --recent-count -c set the max count of recent notifications keeping in memory\n" " --enable-notification -e set notification enabled for the bundle, eg: -e com.example:10100:1\n"; +static char g_enableObjectNull[] = +"error: object is null\n" +"error: object is null\n" +"usage: anm setting []\n" +"options list:\n --help, -h help menu\n" +" --recent-count -c set the max count of recent notifications keeping in memory\n" +" --enable-notification -e set notification enabled for the bundle, eg: -e com.example:10100:1\n"; + static char g_bundleName[] = "example"; static char g_commandActive[] = "active"; static char g_commandRecent[] = "recent"; @@ -534,4 +542,48 @@ HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_1900, Function | Medium EXPECT_EQ(cmd.ExecCommand(), g_enableBundleNameNull); } + +/** + * @tc.number: Anm_Command_Dump_2000 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -e bundleName:uid:enable" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_2000, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-e", + (char *)"22", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + EXPECT_EQ(cmd.RunSetEnableCmd(), cmd.RunAsSettingCommand()); + EXPECT_EQ(cmd.ExecCommand(), g_enableObjectNull); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -e bundleName:uid:enable" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_2100, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-e", + (char *)"gg:ss:aa", + (char *)"22", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + EXPECT_EQ(cmd.ExecCommand(), "set notification enabled success\n"); +} } // namespace \ No newline at end of file