From 068e0973e8a2b6e359cbfbd85ae79cf6622ae348 Mon Sep 17 00:00:00 2001 From: cheerful_ricky Date: Mon, 9 Jun 2025 20:03:07 +0800 Subject: [PATCH] add anm setting command UT Signed-off-by: cheerful_ricky --- tools/dump/src/notification_shell_command.cpp | 24 +- tools/test/mock/mock_ans_notification.cpp | 48 ++ .../notification_shell_command_dump_test.cpp | 456 +++++++++++++++++- 3 files changed, 509 insertions(+), 19 deletions(-) diff --git a/tools/dump/src/notification_shell_command.cpp b/tools/dump/src/notification_shell_command.cpp index 3b8881245..840beb0ef 100644 --- a/tools/dump/src/notification_shell_command.cpp +++ b/tools/dump/src/notification_shell_command.cpp @@ -367,9 +367,7 @@ ErrCode NotificationShellCommand::RunSetEnableCmd() } else { resultReceiver_.append("set notification enabled failed\n"); std::string message = GetAnsErrMessage(ErrorToExternal(ret)); - if (message != "") { - resultReceiver_.append("failed reason is " + message + "\n"); - } + resultReceiver_.append("failed reason is " + message + "\n"); } return ret; } @@ -396,9 +394,7 @@ ErrCode NotificationShellCommand::RunGetDeviceStatusCmd() } else { resultReceiver_.append("Get device status failed\n"); std::string message = GetAnsErrMessage(ErrorToExternal(ret)); - if (message != "") { - resultReceiver_.append("failed reason is " + message + "\n"); - } + resultReceiver_.append("failed reason is " + message + "\n"); } return ret; } @@ -429,9 +425,7 @@ ErrCode NotificationShellCommand::RunSetDeviceStatusCmd() } else { resultReceiver_.append("set device status failed\n"); std::string message = GetAnsErrMessage(ErrorToExternal(ret)); - if (message != "") { - resultReceiver_.append("failed reason is " + message + "\n"); - } + resultReceiver_.append("failed reason is " + message + "\n"); } return ret; } @@ -461,9 +455,7 @@ ErrCode NotificationShellCommand::RunSetSmartReminderEnabledCmd() } else { resultReceiver_.append("set collaboration switch failed\n"); std::string message = GetAnsErrMessage(ErrorToExternal(ret)); - if (message != "") { - resultReceiver_.append("failed reason is " + message + "\n"); - } + resultReceiver_.append("failed reason is " + message + "\n"); } return ret; } @@ -503,9 +495,7 @@ ErrCode NotificationShellCommand::RunSetDistributedEnabledByBundleCmd() } else { resultReceiver_.append("set bundle collaboration switch failed\n"); std::string message = GetAnsErrMessage(ErrorToExternal(ret)); - if (message != "") { - resultReceiver_.append("failed reason is " + message + "\n"); - } + resultReceiver_.append("failed reason is " + message + "\n"); } return ret; } @@ -546,9 +536,7 @@ ErrCode NotificationShellCommand::RunSetDistributedEnabledBySlotCmd() } else { resultReceiver_.append("set slot collaboration switch failed\n"); std::string message = GetAnsErrMessage(ErrorToExternal(ret)); - if (message != "") { - resultReceiver_.append("failed reason is " + message + "\n"); - } + resultReceiver_.append("failed reason is " + message + "\n"); } return ret; } diff --git a/tools/test/mock/mock_ans_notification.cpp b/tools/test/mock/mock_ans_notification.cpp index 071d92d7e..21da3b101 100644 --- a/tools/test/mock/mock_ans_notification.cpp +++ b/tools/test/mock/mock_ans_notification.cpp @@ -28,5 +28,53 @@ ErrCode AnsNotification::SetNotificationsEnabledForSpecifiedBundle( return ERR_ANS_SERVICE_NOT_CONNECTED; } } + +ErrCode AnsNotification::GetTargetDeviceStatus(const std::string &deviceType, int32_t &status) +{ + if (deviceType == "phone") { + return ERR_OK; + } else { + return ERR_ANS_SERVICE_NOT_CONNECTED; + } +} + +ErrCode AnsNotification::SetTargetDeviceStatus(const std::string &deviceType, const uint32_t status, + const std::string deveiceId) +{ + if (deviceType == "phone") { + return ERR_OK; + } else { + return ERR_ANS_SERVICE_NOT_CONNECTED; + } +} + +ErrCode AnsNotification::SetSmartReminderEnabled(const std::string &deviceType, const bool enabled) +{ + if (deviceType == "phone") { + return ERR_OK; + } else { + return ERR_ANS_SERVICE_NOT_CONNECTED; + } +} + +ErrCode AnsNotification::SetDistributedEnabledByBundle(const NotificationBundleOption &bundleOption, + const std::string &deviceType, const bool enabled) +{ + if (deviceType == "phone") { + return ERR_OK; + } else { + return ERR_ANS_SERVICE_NOT_CONNECTED; + } +} + +ErrCode AnsNotification::SetDistributedEnabledBySlot( + const NotificationConstant::SlotType &slotType, const std::string &deviceType, const bool enabled) +{ + if (deviceType == "phone") { + 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/notification_shell_command_dump_test.cpp b/tools/test/unittest/dump/notification_shell_command_dump_test.cpp index 91ffb4aaa..6098ed635 100644 --- a/tools/test/unittest/dump/notification_shell_command_dump_test.cpp +++ b/tools/test/unittest/dump/notification_shell_command_dump_test.cpp @@ -60,6 +60,21 @@ static char g_dumpActiveUser[] = " --user-id, -u dump the info filter by the specified userId\n" " --receiver, -r dump the info filter by the specified receiver userId\n"; +constexpr char SETTING_HELP_MSG[] = + "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" + " --set-device-status -d set device status, eg: -d device:1\n" + " --collaboration-switch -k set collaboration status, eg: -k wearable:1\n" + " --collaboration-switch-bundle -b set bundle collaboration switch status\n" + " eg: -b wearable:example:10100:1\n" + " --collaboration-switch-slot -o set slot collaboration switch status\n" + " --get-device-status -o set device status\n" + " eg: -o wearable:0:1\n"; + static char g_enableErrorInformation[] = "error: option 'e' requires a value.\n" "usage: anm setting []\n" @@ -566,7 +581,8 @@ HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_1700, Function | Medium NotificationShellCommand cmd(argc, argv); - EXPECT_EQ(cmd.ExecCommand(), "set notification enabled failed\n"); + EXPECT_EQ(cmd.ExecCommand(), + "set notification enabled failed\nfailed reason is Failed to connect to the service\n"); } /** @@ -654,6 +670,444 @@ HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_2100, Function | Medium EXPECT_EQ(cmd.ExecCommand(), "set notification enabled success\n"); } +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -g phone" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2100, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-g", + (char *)"phone", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + EXPECT_EQ(cmd.ExecCommand(), "Get device status success: 0\n"); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -g 2in1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2101, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-g", + (char *)"2in1", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + EXPECT_EQ(cmd.ExecCommand(), + "Get device status failed\nfailed reason is Failed to connect to the service\n"); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -g phone" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2102, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-g", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + std::string result = "error: option 'g' requires a value.\n"; + result.append(SETTING_HELP_MSG); + EXPECT_EQ(cmd.ExecCommand(), result); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -g phone" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2103, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-g", + (char *)"", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + EXPECT_EQ(cmd.ExecCommand(), "error: getting information error\n"); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -d phone:1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2200, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-d", + (char *)"phone:1", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + EXPECT_EQ(cmd.ExecCommand(), "set device status success\n"); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -d 2in1:1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2201, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-d", + (char *)"2in1:1", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + EXPECT_EQ(cmd.ExecCommand(), "set device status failed\nfailed reason is Failed to connect to the service\n"); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -d phone:1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2202, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-d", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + std::string result = "error: option 'd' requires a value.\n"; + result.append(SETTING_HELP_MSG); + EXPECT_EQ(cmd.ExecCommand(), result); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -d 2in1:1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2203, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-d", + (char *)"2in1", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + std::string result = "error: setting information error\n"; + result.append(SETTING_HELP_MSG); + EXPECT_EQ(cmd.ExecCommand(), result); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -k phone:1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2300, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-k", + (char *)"phone:1", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + EXPECT_EQ(cmd.ExecCommand(), "set collaboration switch success\n"); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -k phone:1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2301, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-k", + (char *)"2in1:1", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + EXPECT_EQ(cmd.ExecCommand(), + "set collaboration switch failed\nfailed reason is Failed to connect to the service\n"); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -k phone:1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2302, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-k", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + std::string result = "error: option 'k' requires a value.\n"; + result.append(SETTING_HELP_MSG); + EXPECT_EQ(cmd.ExecCommand(), result); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -k phone:1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2303, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-k", + (char *)"phone:1:", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + std::string result = "error: setting information error\n"; + result.append(SETTING_HELP_MSG); + EXPECT_EQ(cmd.ExecCommand(), result); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -b phone:1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2400, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-b", + (char *)"phone:bundleName:10001:1", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + EXPECT_EQ(cmd.ExecCommand(), "set bundle collaboration switch success\n"); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -b phone:1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2401, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-b", + (char *)"2in1:bundleName:10001:1", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + EXPECT_EQ(cmd.ExecCommand(), + "set bundle collaboration switch failed\nfailed reason is Failed to connect to the service\n"); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -b phone:1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2402, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-b", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + std::string result = "error: option 'b' requires a value.\n"; + result.append(SETTING_HELP_MSG); + EXPECT_EQ(cmd.ExecCommand(), result); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -b phone:1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2403, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-b", + (char *)"2in1:bundleName:10001:1:", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + std::string result = "error: setting information error\n"; + result.append(SETTING_HELP_MSG); + EXPECT_EQ(cmd.ExecCommand(), result); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -b phone:1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2500, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-o", + (char *)"phone:2:1", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + EXPECT_EQ(cmd.ExecCommand(), "set slot collaboration switch success\n"); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -b phone:1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2501, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-o", + (char *)"2in1:2:1", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + EXPECT_EQ(cmd.ExecCommand(), + "set slot collaboration switch failed\nfailed reason is Failed to connect to the service\n"); +} + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -b phone:1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2502, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-o", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + std::string result = "error: option 'o' requires a value.\n"; + result.append(SETTING_HELP_MSG); + EXPECT_EQ(cmd.ExecCommand(), result); +} + + +/** + * @tc.number: Anm_Command_Dump_2100 + * @tc.name: RunAsSettingCommand + * @tc.desc: Verify the "anm setting -b phone:1" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Setting_2503, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)enable_.c_str(), + (char *)"-o", + (char *)"2in1:2:1:", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + std::string result = "error: setting information error\n"; + result.append(SETTING_HELP_MSG); + EXPECT_EQ(cmd.ExecCommand(), result); +} + /** * @tc.number: Anm_Command_Dump_2200 * @tc.name: ExecCommand -- Gitee