From 1338e47f0f5fdd34e9113d868249d7c227913785 Mon Sep 17 00:00:00 2001 From: liuyanzhi Date: Wed, 29 Jun 2022 16:33:08 +0800 Subject: [PATCH] add anm test Signed-off-by: liuyanzhi Change-Id: I52d4d2b8e900424e350aab8ac125c192c1bb8aad --- tools/BUILD.gn | 11 +- tools/dump/BUILD.gn | 6 +- tools/test/BUILD.gn | 20 ++ tools/test/mock/mock_ans_manager_stub.cpp | 43 +++ tools/test/mock/mock_ans_manager_stub.h | 52 ++++ tools/test/unittest/dump/BUILD.gn | 76 ++++++ .../notification_shell_command_dump_test.cpp | 253 ++++++++++++++++++ 7 files changed, 459 insertions(+), 2 deletions(-) create mode 100644 tools/test/BUILD.gn create mode 100644 tools/test/mock/mock_ans_manager_stub.cpp create mode 100644 tools/test/mock/mock_ans_manager_stub.h create mode 100644 tools/test/unittest/dump/BUILD.gn create mode 100644 tools/test/unittest/dump/notification_shell_command_dump_test.cpp diff --git a/tools/BUILD.gn b/tools/BUILD.gn index 9f5755073..9b582c4d1 100644 --- a/tools/BUILD.gn +++ b/tools/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# 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 @@ -11,6 +11,15 @@ # See the License for the specific language governing permissions and # limitations under the License. +import("//base/notification/distributed_notification_service/notification.gni") +import("//build/ohos.gni") + group("tools_shell") { deps = [ "dump:anm_target" ] } + +group("unittest") { + testonly = true + + deps = [ "test:unittest" ] +} diff --git a/tools/dump/BUILD.gn b/tools/dump/BUILD.gn index 5bf0206b7..99e4e4418 100644 --- a/tools/dump/BUILD.gn +++ b/tools/dump/BUILD.gn @@ -18,8 +18,12 @@ group("anm_target") { deps = [ ":anm" ] } -ohos_executable("anm") { +config("tools_dump_config") { include_dirs = [ "${tools_path}/dump/include" ] +} + +ohos_executable("anm") { + public_configs = [ ":tools_dump_config" ] sources = [ "src/main.cpp", diff --git a/tools/test/BUILD.gn b/tools/test/BUILD.gn new file mode 100644 index 000000000..fd4548013 --- /dev/null +++ b/tools/test/BUILD.gn @@ -0,0 +1,20 @@ +# Copyright (c) 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. + +import("//build/test.gni") + +group("unittest") { + testonly = true + + deps = [ "unittest/dump:unittest" ] +} diff --git a/tools/test/mock/mock_ans_manager_stub.cpp b/tools/test/mock/mock_ans_manager_stub.cpp new file mode 100644 index 000000000..5f66ac3be --- /dev/null +++ b/tools/test/mock/mock_ans_manager_stub.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 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 "mock_ans_manager_stub.h" + +#include "ans_inner_errors.h" + +namespace OHOS { +namespace Notification { +std::string MockAnsManagerStub::GetCmd() +{ + return cmd_; +} + +std::string MockAnsManagerStub::GetBundle() +{ + return bundle_; +} + +ErrCode MockAnsManagerStub::ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId, + std::vector &dumpInfo) +{ + ANS_LOGE("enter"); + ErrCode result = ERR_ANS_NOT_ALLOWED; + cmd_ = cmd; + bundle_ = bundle; + userId_ = userId; + return result; +} +} // namespace Notification +} // namespace OHOSc diff --git a/tools/test/mock/mock_ans_manager_stub.h b/tools/test/mock/mock_ans_manager_stub.h new file mode 100644 index 000000000..1cb8eb328 --- /dev/null +++ b/tools/test/mock/mock_ans_manager_stub.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 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. + */ + +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_TOOLS_MOCK_ANS_MANAGER_STUB_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_TOOLS_MOCK_ANS_MANAGER_STUB_H + +#include "gmock/gmock.h" + +#include "ans_log_wrapper.h" +#include "ans_manager_stub.h" + +namespace OHOS { +namespace Notification { +class MockAnsManagerStub : public AnsManagerStub { +public: + + std::string GetCmd(); + std::string GetBundle(); + + /** + * @brief Obtains specific datas via specified dump option. + * + * @param cmd Indicates the specified dump command. + * @param bundle Indicates the specified bundle name. + * @param userId Indicates the specified userId. + * @param dumpInfo Indicates the container containing datas. + * @return Returns check result. + */ + virtual ErrCode ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId, + std::vector &dumpInfo) override; + +private: + std::string cmd_; + std::string bundle_; + int32_t userId_; +}; +} // namespace EventFwk +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_TOOLS_MOCK_ANS_MANAGER_STUB_H diff --git a/tools/test/unittest/dump/BUILD.gn b/tools/test/unittest/dump/BUILD.gn new file mode 100644 index 000000000..9a77709ff --- /dev/null +++ b/tools/test/unittest/dump/BUILD.gn @@ -0,0 +1,76 @@ +# Copyright (c) 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. + +import("//base/notification/distributed_notification_service/notification.gni") +import("//build/test.gni") + +module_output_path = "distributed_notification_service/tools" + +config("tools_dump_config_mock") { + include_dirs = [ + "//third_party/googletest/googlemock/include", + "${tools_path}/test/mock", + "${component_path}/services/ans/include", + ] +} + +tools_dump_mock_sources = + [ "${tools_path}/test/mock/mock_ans_manager_stub.cpp" ] + +ohos_unittest("notification_shell_command_dump_test") { + module_out_path = module_output_path + + include_dirs = [] + + sources = [ + "${tools_path}/dump/src/notification_shell_command.cpp", + "${tools_path}/dump/src/shell_command.cpp", + "notification_shell_command_dump_test.cpp", + ] + sources += tools_dump_mock_sources + + configs = [ + "${tools_path}/dump:tools_dump_config", + ":tools_dump_config_mock", + ] + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + deps = [ + "${core_path}:ans_core", + "${frameworks_module_ans_path}:ans_innerkits", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "common_event_service:cesfwk_innerkits", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "relational_store:native_rdb", + ] +} + +group("unittest") { + testonly = true + + deps = [ ":notification_shell_command_dump_test" ] +} diff --git a/tools/test/unittest/dump/notification_shell_command_dump_test.cpp b/tools/test/unittest/dump/notification_shell_command_dump_test.cpp new file mode 100644 index 000000000..21a1cf43a --- /dev/null +++ b/tools/test/unittest/dump/notification_shell_command_dump_test.cpp @@ -0,0 +1,253 @@ +/* + * Copyright (c) 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 + +#define private public +#include "notification_shell_command.h" +#undef private +#include "ans_manager_interface.h" +#include "mock_ans_manager_stub.h" +#include "singleton.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Notification; + +namespace { + static char DUMP_HELP_MSG[] = + "request a option 'A' or 'R' or 'D'\n" + "usage: anm dump []\n" + "options list:\n" + " --help, -h help menu\n" + " --active, -A list all active notifications\n" + " --recent, -R list recent notifications\n" + " --bundle, -b specified a bundle filter\n"; + + static char DUMP_ACTIVE_BOUND[] = + "error: option 'b' requires a value.\n" + "usage: anm dump []\n" + "options list:\n" + " --help, -h help menu\n" + " --active, -A list all active notifications\n" + " --recent, -R list recent notifications\n" + " --bundle, -b specified a bundle filter\n"; + + static char BUNDLE_NAME[] = "example"; + static char COMMAND_ACTIVE[] = "active"; + static char COMMAND_RECENT[] = "recent"; +} + +class AnmManagerDumpTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; + + void MakeMockObjects(); + + std::string cmd_ = "dump"; + std::string toolName_ = "anm"; + sptr proxyPtr_; + sptr stubPtr_; +}; + +void AnmManagerDumpTest::SetUpTestCase() +{} + +void AnmManagerDumpTest::TearDownTestCase() +{} + +void AnmManagerDumpTest::SetUp() +{ + // reset optind to 0 + optind = 0; + + // make mock objects + MakeMockObjects(); +} + +void AnmManagerDumpTest::TearDown() +{} + +void AnmManagerDumpTest::MakeMockObjects() +{ + // mock a stub + stubPtr_ = new (std::nothrow) MockAnsManagerStub(); + + // mock a proxy + proxyPtr_ = iface_cast(stubPtr_); + + // set the mock proxy + auto ansNotificationPtr = DelayedSingleton::GetInstance(); + ansNotificationPtr->ansManagerProxy_ = proxyPtr_; +} + +/** + * @tc.number: Anm_Command_Dump_0100 + * @tc.name: ExecCommand + * @tc.desc: Verify the "anm dump -h" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_0100, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)cmd_.c_str(), + (char *)"-h", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + EXPECT_EQ(cmd.ExecCommand(), DUMP_HELP_MSG); +} + +/** + * @tc.number: Anm_Command_Dump_0200 + * @tc.name: ExecCommand + * @tc.desc: Verify the "anm dump -A" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_0200, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)cmd_.c_str(), + (char *)"-A", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + cmd.ExecCommand(); + + EXPECT_EQ(stubPtr_->GetCmd(), COMMAND_ACTIVE); +} + +/** + * @tc.number: Anm_Command_Dump_0300 + * @tc.name: ExecCommand + * @tc.desc: Verify the "anm dump -R" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_0300, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)cmd_.c_str(), + (char *)"-R", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + cmd.ExecCommand(); + + EXPECT_EQ(stubPtr_->GetCmd(), COMMAND_RECENT); +} + +/** + * @tc.number: Anm_Command_Dump_0400 + * @tc.name: ExecCommand + * @tc.desc: Verify the "anm dump -R -b" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_0400, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)cmd_.c_str(), + (char *)"-R", + (char *)"-b", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + EXPECT_EQ(cmd.ExecCommand(), DUMP_ACTIVE_BOUND); +} + +/** + * @tc.number: Anm_Command_Dump_0500 + * @tc.name: ExecCommand + * @tc.desc: Verify the "anm dump -A -b example" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_0500, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)cmd_.c_str(), + (char *)"-A", + (char *)"-b", + (char *)"example", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + cmd.ExecCommand(); + + EXPECT_EQ(stubPtr_->GetCmd(), COMMAND_ACTIVE); + EXPECT_EQ(stubPtr_->GetBundle(), BUNDLE_NAME); +} + +/** + * @tc.number: Anm_Command_Dump_0600 + * @tc.name: ExecCommand + * @tc.desc: Verify the "anm dump -A -b" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_0600, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)cmd_.c_str(), + (char *)"-A", + (char *)"-b", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + EXPECT_EQ(cmd.ExecCommand(), DUMP_ACTIVE_BOUND); +} + +/** + * @tc.number: Anm_Command_Dump_0700 + * @tc.name: ExecCommand + * @tc.desc: Verify the "anm dump -R -b example" command. + */ +HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_0700, Function | MediumTest | Level1) +{ + char *argv[] = { + (char *)toolName_.c_str(), + (char *)cmd_.c_str(), + (char *)"-R", + (char *)"-b", + (char *)"example", + (char *)"", + }; + int argc = sizeof(argv) / sizeof(argv[0]) - 1; + + NotificationShellCommand cmd(argc, argv); + + cmd.ExecCommand(); + + EXPECT_EQ(stubPtr_->GetCmd(), COMMAND_RECENT); + EXPECT_EQ(stubPtr_->GetBundle(), BUNDLE_NAME); +} -- Gitee