diff --git a/frameworks/core/src/ans_callback_stub.cpp b/frameworks/core/src/ans_callback_stub.cpp index 4d01d61317675c357695ec939dc515080c301f3d..ecf76a676387dffa10374969e913fb2ba91858fc 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 03136185da5b14ed8e73a376be4c9bf75c142309..6a047e70908010796852710bf692316544602ae1 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_callback_stub_test:unittest", + "ans_manager_death_recipient_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 0000000000000000000000000000000000000000..83bb9456a010bb2a31f01850537c5afdcd45681a --- /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 0000000000000000000000000000000000000000..27fa55c329652caeb763a3e0a04705533b484924 --- /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); +}