From 7eddff28b394ef107a667740a225d321c506a3fc Mon Sep 17 00:00:00 2001 From: wwx1143030 Date: Mon, 26 Sep 2022 20:52:56 +0800 Subject: [PATCH 1/4] =?UTF-8?q?IssueNo:https://gitee.com/openharmony/notif?= =?UTF-8?q?ication=5Fcommon=5Fevent=5Fservice/issues/I5T8PA=20Description:?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E5=88=86=E6=94=AF=E8=A6=86=E7=9B=96=E7=8E=87?= =?UTF-8?q?=20Sig:SIG=5FApplicationFramework=20Feature=20or=20Bugfix:Bugfi?= =?UTF-8?q?x=20Binary=20Source:=20No?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wwx1143030 Change-Id: I30318f668c9adf287d8ae4d490113bf39285ce58 --- tools/test/mock/mock_ans_notification.cpp | 32 ++++++++++++ tools/test/unittest/dump/BUILD.gn | 1 + .../notification_shell_command_dump_test.cpp | 52 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 tools/test/mock/mock_ans_notification.cpp diff --git a/tools/test/mock/mock_ans_notification.cpp b/tools/test/mock/mock_ans_notification.cpp new file mode 100644 index 000000000..75a8097c1 --- /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 0891923de..27b6717fa 100644 --- a/tools/test/unittest/dump/BUILD.gn +++ b/tools/test/unittest/dump/BUILD.gn @@ -36,6 +36,7 @@ ohos_unittest("notification_shell_command_dump_test") { "${tools_path}/dump/src/notification_shell_command.cpp", "${tools_path}/dump/src/shell_command.cpp", "notification_shell_command_dump_test.cpp", + "${tools_path}/test/mock/mock_ans_notification.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 73a22b3b5..e7db2ca6b 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 -- Gitee From b23ca6b509aaac6fdec91a3b81e77ed50d89e1bb Mon Sep 17 00:00:00 2001 From: wwx1143030 Date: Mon, 26 Sep 2022 21:10:24 +0800 Subject: [PATCH 2/4] =?UTF-8?q?IssueNo:https://gitee.com/openharmony/notif?= =?UTF-8?q?ication=5Fcommon=5Fevent=5Fservice/issues/I5T8PA=20Description:?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E5=88=86=E6=94=AF=E8=A6=86=E7=9B=96=E7=8E=87?= =?UTF-8?q?=20Sig:SIG=5FApplicationFramework=20Feature=20or=20Bugfix:Bugfi?= =?UTF-8?q?x=20Binary=20Source:=20No?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wwx1143030 Change-Id: I300ee5c39899c15c4e12f9ffa0989468906dcb62 --- tools/test/unittest/dump/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/test/unittest/dump/BUILD.gn b/tools/test/unittest/dump/BUILD.gn index 27b6717fa..dab5f1246 100644 --- a/tools/test/unittest/dump/BUILD.gn +++ b/tools/test/unittest/dump/BUILD.gn @@ -35,8 +35,8 @@ ohos_unittest("notification_shell_command_dump_test") { sources = [ "${tools_path}/dump/src/notification_shell_command.cpp", "${tools_path}/dump/src/shell_command.cpp", - "notification_shell_command_dump_test.cpp", "${tools_path}/test/mock/mock_ans_notification.cpp", + "notification_shell_command_dump_test.cpp", ] sources += tools_dump_mock_sources -- Gitee From 708061e17913613caf5f786df41ec9b451a8e7c3 Mon Sep 17 00:00:00 2001 From: wwx1143030 Date: Mon, 26 Sep 2022 21:14:11 +0800 Subject: [PATCH 3/4] =?UTF-8?q?IssueNo:https://gitee.com/openharmony/notif?= =?UTF-8?q?ication=5Fcommon=5Fevent=5Fservice/issues/I5T8PA=20Description:?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E5=88=86=E6=94=AF=E8=A6=86=E7=9B=96=E7=8E=87?= =?UTF-8?q?=20Sig:SIG=5FApplicationFramework=20Feature=20or=20Bugfix:Bugfi?= =?UTF-8?q?x=20Binary=20Source:=20No?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wwx1143030 Change-Id: I3e2b94a7e1bbce7c3f4f452dc498f97b792d425f --- tools/test/mock/mock_ans_notification.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/test/mock/mock_ans_notification.cpp b/tools/test/mock/mock_ans_notification.cpp index 75a8097c1..071d92d7e 100644 --- a/tools/test/mock/mock_ans_notification.cpp +++ b/tools/test/mock/mock_ans_notification.cpp @@ -23,10 +23,10 @@ ErrCode AnsNotification::SetNotificationsEnabledForSpecifiedBundle( const NotificationBundleOption &bundleOption, const std::string &deviceId, bool enabled) { if (bundleOption.GetBundleName() == "gg") { - return ERR_OK; + return ERR_OK; } else { - return ERR_ANS_SERVICE_NOT_CONNECTED; - } + return ERR_ANS_SERVICE_NOT_CONNECTED; + } } } // namespace Notification } // namespace OHOS \ No newline at end of file -- Gitee From a5a87938d7fb6c271eecfb22de4a91d51de2404c Mon Sep 17 00:00:00 2001 From: wwx1143030 Date: Wed, 28 Sep 2022 16:07:07 +0800 Subject: [PATCH 4/4] =?UTF-8?q?IssueNo:https://gitee.com/openharmony/notif?= =?UTF-8?q?ication=5Fcommon=5Fevent=5Fservice/issues/I5TIQP=20Description:?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=8B=E4=BB=B6=E9=80=9A=E7=9F=A5=E5=88=86?= =?UTF-8?q?=E6=94=AFTDDOnReceiveEvent=20Sig:SIG=5FApplicationFramework=20F?= =?UTF-8?q?eature=20or=20Bugfix:Bugfix=20Binary=20Source:=20No?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wwx1143030 Change-Id: I0ac4674716d0d25ee93eda511f56c3bfca1ea71e --- .../advanced_notification_service_test.cpp | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 60b4a295f..5c9738607 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 -- Gitee