From e1e6234991c142085b458335200a69f3bc1e6586 Mon Sep 17 00:00:00 2001 From: chenyuyan Date: Thu, 27 Oct 2022 18:40:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9Eans=5Fcallback=E2=80=94?= =?UTF-8?q?=E2=80=94stubTDD=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyuyan Change-Id: Idcd9c42cb88e9abfdd6772a63a8f056a23280387 --- frameworks/core/src/ans_callback_stub.cpp | 4 +- frameworks/core/test/unittest/BUILD.gn | 7 +- .../unittest/ans_callback_stub_test/BUILD.gn | 50 ++++++++ .../ans_callback_stub_test.cpp | 114 ++++++++++++++++++ 4 files changed, 171 insertions(+), 4 deletions(-) create mode 100644 frameworks/core/test/unittest/ans_callback_stub_test/BUILD.gn create mode 100644 frameworks/core/test/unittest/ans_callback_stub_test/ans_callback_stub_test.cpp diff --git a/frameworks/core/src/ans_callback_stub.cpp b/frameworks/core/src/ans_callback_stub.cpp index 4d01d6131..ecf76a676 100644 --- a/frameworks/core/src/ans_callback_stub.cpp +++ b/frameworks/core/src/ans_callback_stub.cpp @@ -37,8 +37,8 @@ int32_t AnsCallbackStub::OnRemoteRequest( if (InterfaceCode::ON_ENABLE_NOTIFICATION_CALLBACK == code) { bool result = false; if (!data.ReadBool(result)) { - ANS_LOGE("Notification not allowed by user."); - return ERR_ANS_PERMISSION_DENIED; + ANS_LOGE("read notification enabled result failed."); + return ERR_ANS_PARCELABLE_FAILED; } ANS_LOGD("result = %{public}d", result); OnEnableNotification(result); diff --git a/frameworks/core/test/unittest/BUILD.gn b/frameworks/core/test/unittest/BUILD.gn index 03136185d..81153e024 100644 --- a/frameworks/core/test/unittest/BUILD.gn +++ b/frameworks/core/test/unittest/BUILD.gn @@ -9,11 +9,14 @@ # 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. +# limitations under the License. group("unittest") { testonly = true deps = [] - deps += [ "ans_manager_death_recipient_test:unittest" ] + deps += [ + "ans_manager_death_recipient_test:unittest", + "ans_callback_stub_test:unittest" + ] } diff --git a/frameworks/core/test/unittest/ans_callback_stub_test/BUILD.gn b/frameworks/core/test/unittest/ans_callback_stub_test/BUILD.gn new file mode 100644 index 000000000..83bb9456a --- /dev/null +++ b/frameworks/core/test/unittest/ans_callback_stub_test/BUILD.gn @@ -0,0 +1,50 @@ +# 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/ohos.gni") +import("//build/test.gni") + +module_output_path = "${component_name}/unittest" + +ohos_unittest("ans_callback_stub_test") { + module_out_path = module_output_path + include_dirs = [ + "${core_path}/include", + "//commonlibrary/c_utils/base/include", + ] + + sources = [ "ans_callback_stub_test.cpp" ] + + deps = [ "${core_path}:ans_core" ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:wantagent_innerkits", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "multimedia_image_framework:image_native", + "relational_store:native_rdb", + ] + + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + +group("unittest") { + testonly = true + deps = [] + + deps += [ ":ans_callback_stub_test" ] +} \ No newline at end of file diff --git a/frameworks/core/test/unittest/ans_callback_stub_test/ans_callback_stub_test.cpp b/frameworks/core/test/unittest/ans_callback_stub_test/ans_callback_stub_test.cpp new file mode 100644 index 000000000..27fa55c32 --- /dev/null +++ b/frameworks/core/test/unittest/ans_callback_stub_test/ans_callback_stub_test.cpp @@ -0,0 +1,114 @@ +/* + * 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 + +#include "ans_callback_stub.h" +#include "ans_inner_errors.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Notification; + +class AnsCallbackStubImpl : public AnsCallbackStub { + bool OnEnableNotification(bool isAllow) override { + return false; + } +}; + +class AnsCallbackStubTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + + void SetUp() { + ansCallbackStub_ = new AnsCallbackStubImpl(); + } + void TearDown() {} + sptr ansCallbackStub_; +}; + +/** + * @tc.name: OnRemoteRequest01 + * @tc.desc: Test if the descriptor is wrong. + * @tc.type: FUNC + * @tc.require: issueI5XO2O + */ +HWTEST_F(AnsCallbackStubTest, OnRemoteRequest01, Function | SmallTest | Level1) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + + data.WriteInterfaceToken(u"error.GetDescriptor"); + + int32_t ret = ansCallbackStub_->OnRemoteRequest(0, data, reply, option); + EXPECT_EQ(ret, (int)OBJECT_NULL); +} + +/** + * @tc.name: OnRemoteRequest02 + * @tc.desc: Test if can not read a bool from data failed. + * @tc.type: FUNC + * @tc.require: issueI5XO2O + */ +HWTEST_F(AnsCallbackStubTest, OnRemoteRequest02, Function | SmallTest | Level1) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + + data.WriteInterfaceToken(AnsCallbackStub::GetDescriptor()); + + int32_t ret = ansCallbackStub_->OnRemoteRequest(0, data, reply, option); + EXPECT_EQ(ret, (int)ERR_ANS_PARCELABLE_FAILED); +} + +/** + * @tc.name: OnRemoteRequest03 + * @tc.desc: Test unknow code failed. + * @tc.type: FUNC + * @tc.require: issueI5XO2O + */ +HWTEST_F(AnsCallbackStubTest, OnRemoteRequest03, Function | SmallTest | Level1) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + + data.WriteInterfaceToken(AnsCallbackStub::GetDescriptor()); + + int32_t ret = ansCallbackStub_->OnRemoteRequest(1, data, reply, option); + EXPECT_EQ(ret, (int)IPC_STUB_UNKNOW_TRANS_ERR); +} + +/** + * @tc.name: OnRemoteRequest04 + * @tc.desc: Test OnRemoteRequest success. + * @tc.type: FUNC + * @tc.require: issueI5XO2O + */ +HWTEST_F(AnsCallbackStubTest, OnRemoteRequest04, Function | SmallTest | Level1) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + + data.WriteInterfaceToken(AnsCallbackStub::GetDescriptor()); + data.WriteBool(true); + + int32_t ret = ansCallbackStub_->OnRemoteRequest(0, data, reply, option); + EXPECT_EQ(ret, (int)NO_ERROR); +} -- Gitee From 0759b11bda845d9682a89e69e83e11f42c088616 Mon Sep 17 00:00:00 2001 From: chenyuyan Date: Thu, 27 Oct 2022 10:55:49 +0000 Subject: [PATCH 2/2] update frameworks/core/test/unittest/BUILD.gn. Signed-off-by: chenyuyan --- frameworks/core/test/unittest/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/core/test/unittest/BUILD.gn b/frameworks/core/test/unittest/BUILD.gn index 81153e024..6a047e709 100644 --- a/frameworks/core/test/unittest/BUILD.gn +++ b/frameworks/core/test/unittest/BUILD.gn @@ -16,7 +16,7 @@ group("unittest") { deps = [] deps += [ + "ans_callback_stub_test:unittest", "ans_manager_death_recipient_test:unittest", - "ans_callback_stub_test:unittest" ] } -- Gitee