From d04d30ff611ccdb186c53d57183e03b7e225635a Mon Sep 17 00:00:00 2001 From: zhang_hao_zheng Date: Sat, 12 Jul 2025 15:48:28 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85tdd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhang_hao_zheng Change-Id: I7af6481125681a79940e920cd024211e2994955d --- test/unittest/native/BUILD.gn | 22 +++ .../native/ability_context_test/BUILD.gn | 41 ++++++ .../ability_context_test.cpp | 133 +++++++++++++++++ .../native/extension_base_test/BUILD.gn | 41 ++++++ .../extension_base_test.cpp | 139 ++++++++++++++++++ 5 files changed, 376 insertions(+) create mode 100644 test/unittest/native/BUILD.gn create mode 100644 test/unittest/native/ability_context_test/BUILD.gn create mode 100644 test/unittest/native/ability_context_test/ability_context_test.cpp create mode 100644 test/unittest/native/extension_base_test/BUILD.gn create mode 100644 test/unittest/native/extension_base_test/extension_base_test.cpp diff --git a/test/unittest/native/BUILD.gn b/test/unittest/native/BUILD.gn new file mode 100644 index 00000000000..2b4d4127db5 --- /dev/null +++ b/test/unittest/native/BUILD.gn @@ -0,0 +1,22 @@ +# Copyright (c) 2021-2025 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/ohos.gni") + +group("native_unittests") { + testonly = true + deps = [ + "extension_base_test:extension_base_test", + "ability_context_test:ability_context_test", + ] +} \ No newline at end of file diff --git a/test/unittest/native/ability_context_test/BUILD.gn b/test/unittest/native/ability_context_test/BUILD.gn new file mode 100644 index 00000000000..7e837b76b12 --- /dev/null +++ b/test/unittest/native/ability_context_test/BUILD.gn @@ -0,0 +1,41 @@ +# Copyright (c) 2021-2025 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/ohos.gni") + +ohos_unittest("ability_context_test") { + module_out_path = module_output_path + sources = [ + "ability_context_test.cpp", + ] + + include_dirs = [ + "//foundation/ability/ability_runtime/interfaces/kits/native/ability/ability_runtime", + "//foundation/ability/ability_runtime/interfaces/innerkits/include", + "//base/ability/ability_runtime/interfaces/innerkits/include", + ] + + deps = [ + "//foundation/ability/ability_runtime/interfaces/kits/native/ability/ability_runtime:ability_context_impl", + "//third_party/googletest:gtest_main", + ] + + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + ] + + install_enable = true + part_name = "ability_runtime" +} \ No newline at end of file diff --git a/test/unittest/native/ability_context_test/ability_context_test.cpp b/test/unittest/native/ability_context_test/ability_context_test.cpp new file mode 100644 index 00000000000..9a0b4b1ea15 --- /dev/null +++ b/test/unittest/native/ability_context_test/ability_context_test.cpp @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2021-2025 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 "gtest/gtest.h" +#include "ability_context_impl.h" +#include "ability_connect_callback.h" +#include "want.h" + +namespace OHOS { +namespace AbilityRuntime { +namespace { + const std::string TEST_BUNDLE_NAME = "com.example.test"; + const std::string TEST_ABILITY_NAME = "TestAbility"; +} + +class MockAbilityConnectCallback : public AbilityConnectCallback { +public: + void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr &remoteObject, + int resultCode) override {} + void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override {} +}; + +class AbilityContextTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void AbilityContextTest::SetUpTestCase() +{} + +void AbilityContextTest::TearDownTestCase() +{} + +void AbilityContextTest::SetUp() +{} + +void AbilityContextTest::TearDown() +{} + +/** + * @tc.name: ConnectAppServiceExtensionAbility_0100 + * @tc.desc: Test ConnectAppServiceExtensionAbility with empty want + * @tc.type: FUNC + * @tc.require: issueI6K4G7 + */ +TEST_F(AbilityContextTest, ConnectAppServiceExtensionAbility_0100) +{ + AAFwk::Want want; + sptr callback = new MockAbilityConnectCallback(); + AbilityContextImpl context; + ErrCode result = context.ConnectAppServiceExtensionAbility(want, callback); + EXPECT_EQ(result, ERR_OK); +} + +/** + * @tc.name: ConnectAppServiceExtensionAbility_0200 + * @tc.desc: Test ConnectAppServiceExtensionAbility with normal want + * @tc.type: FUNC + * @tc.require: issueI6K4G7 + */ +TEST_F(AbilityContextTest, ConnectAppServiceExtensionAbility_0200) +{ + AAFwk::Want want; + want.SetElementName(TEST_BUNDLE_NAME, TEST_ABILITY_NAME); + sptr callback = new MockAbilityConnectCallback(); + AbilityContextImpl context; + ErrCode result = context.ConnectAppServiceExtensionAbility(want, callback); + EXPECT_EQ(result, ERR_OK); +} + +/** + * @tc.name: ConnectAppServiceExtensionAbility_0300 + * @tc.desc: Test ConnectAppServiceExtensionAbility with null callback + * @tc.type: FUNC + * @tc.require: issueI6K4G7 + */ +TEST_F(AbilityContextTest, ConnectAppServiceExtensionAbility_0300) +{ + AAFwk::Want want; + want.SetElementName(TEST_BUNDLE_NAME, TEST_ABILITY_NAME); + AbilityContextImpl context; + ErrCode result = context.ConnectAppServiceExtensionAbility(want, nullptr); + EXPECT_NE(result, ERR_OK); +} + +/** + * @tc.name: ConnectAppServiceExtensionAbility_0400 + * @tc.desc: Test ConnectAppServiceExtensionAbility with invalid bundle name + * @tc.type: FUNC + * @tc.require: issueI6K4G7 + */ +TEST_F(AbilityContextTest, ConnectAppServiceExtensionAbility_0400) +{ + AAFwk::Want want; + want.SetElementName("", TEST_ABILITY_NAME); + sptr callback = new MockAbilityConnectCallback(); + AbilityContextImpl context; + ErrCode result = context.ConnectAppServiceExtensionAbility(want, callback); + EXPECT_NE(result, ERR_OK); +} + +/** + * @tc.name: ConnectAppServiceExtensionAbility_0500 + * @tc.desc: Test ConnectAppServiceExtensionAbility with invalid ability name + * @tc.type: FUNC + * @tc.require: issueI6K4G7 + */ +TEST_F(AbilityContextTest, ConnectAppServiceExtensionAbility_0500) +{ + AAFwk::Want want; + want.SetElementName(TEST_BUNDLE_NAME, ""); + sptr callback = new MockAbilityConnectCallback(); + AbilityContextImpl context; + ErrCode result = context.ConnectAppServiceExtensionAbility(want, callback); + EXPECT_NE(result, ERR_OK); +} +} // namespace AbilityRuntime +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/native/extension_base_test/BUILD.gn b/test/unittest/native/extension_base_test/BUILD.gn new file mode 100644 index 00000000000..c4cd262a506 --- /dev/null +++ b/test/unittest/native/extension_base_test/BUILD.gn @@ -0,0 +1,41 @@ +# Copyright (c) 2021-2025 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/ohos.gni") + +ohos_unittest("extension_base_test") { + module_out_path = module_output_path + sources = [ + "extension_base_test.cpp", + ] + + include_dirs = [ + "//foundation/ability/ability_runtime/interfaces/kits/native/ability/native", + "//foundation/ability/ability_runtime/interfaces/innerkits/include", + "//base/ability/ability_runtime/interfaces/innerkits/include", + ] + + deps = [ + "//foundation/ability/ability_runtime/interfaces/kits/native/ability/native:extension_base", + "//third_party/googletest:gtest_main", + ] + + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + ] + + install_enable = true + part_name = "ability_runtime" +} \ No newline at end of file diff --git a/test/unittest/native/extension_base_test/extension_base_test.cpp b/test/unittest/native/extension_base_test/extension_base_test.cpp new file mode 100644 index 00000000000..aca749c0868 --- /dev/null +++ b/test/unittest/native/extension_base_test/extension_base_test.cpp @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2021-2025 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 "gtest/gtest.h" +#include "extension_base.h" +#include "element_name.h" +#include "extension_context.h" + +namespace OHOS { +namespace AbilityRuntime { +namespace { + const std::string TEST_REQUEST_ID = "test_request_id"; + const std::string TEST_MESSAGE = "test_message"; + const std::string TEST_DEVICE_ID = "test_device_id"; + const std::string TEST_BUNDLE_NAME = "test_bundle_name"; + const std::string TEST_ABILITY_NAME = "test_ability_name"; +} + +class MockExtensionContext : public ExtensionContext { +public: + MockExtensionContext() = default; + virtual ~MockExtensionContext() = default; +}; + +class ExtensionBaseTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void ExtensionBaseTest::SetUpTestCase() +{} + +void ExtensionBaseTest::TearDownTestCase() +{} + +void ExtensionBaseTest::SetUp() +{} + +void ExtensionBaseTest::TearDown() +{} + +/** + * @tc.name: OnExtensionAbilityRequestFailure_0100 + * @tc.desc: Test OnExtensionAbilityRequestFailure with empty parameters + * @tc.type: FUNC + * @tc.require: issueI6K4G7 + */ +TEST_F(ExtensionBaseTest, OnExtensionAbilityRequestFailure_0100) +{ + AppExecFwk::ElementName element; + ExtensionBase extension; + extension.OnExtensionAbilityRequestFailure("", element, ""); +} + +/** + * @tc.name: OnExtensionAbilityRequestFailure_0200 + * @tc.desc: Test OnExtensionAbilityRequestFailure with normal parameters + * @tc.type: FUNC + * @tc.require: issueI6K4G7 + */ +TEST_F(ExtensionBaseTest, OnExtensionAbilityRequestFailure_0200) +{ + AppExecFwk::ElementName element(TEST_DEVICE_ID, TEST_BUNDLE_NAME, TEST_ABILITY_NAME); + ExtensionBase extension; + extension.OnExtensionAbilityRequestFailure(TEST_REQUEST_ID, element, TEST_MESSAGE); +} + +/** + * @tc.name: OnExtensionAbilityRequestFailure_0300 + * @tc.desc: Test OnExtensionAbilityRequestFailure with long parameters + * @tc.type: FUNC + * @tc.require: issueI6K4G7 + */ +TEST_F(ExtensionBaseTest, OnExtensionAbilityRequestFailure_0300) +{ + std::string longRequestId(1024, 'a'); + std::string longMessage(1024, 'b'); + AppExecFwk::ElementName element(TEST_DEVICE_ID, TEST_BUNDLE_NAME, TEST_ABILITY_NAME); + ExtensionBase extension; + extension.OnExtensionAbilityRequestFailure(longRequestId, element, longMessage); +} + +/** + * @tc.name: OnExtensionAbilityRequestSuccess_0100 + * @tc.desc: Test OnExtensionAbilityRequestSuccess with empty parameters + * @tc.type: FUNC + * @tc.require: issueI6K4G7 + */ +TEST_F(ExtensionBaseTest, OnExtensionAbilityRequestSuccess_0100) +{ + AppExecFwk::ElementName element; + ExtensionBase extension; + extension.OnExtensionAbilityRequestSuccess("", element, ""); +} + +/** + * @tc.name: OnExtensionAbilityRequestSuccess_0200 + * @tc.desc: Test OnExtensionAbilityRequestSuccess with normal parameters + * @tc.type: FUNC + * @tc.require: issueI6K4G7 + */ +TEST_F(ExtensionBaseTest, OnExtensionAbilityRequestSuccess_0200) +{ + AppExecFwk::ElementName element(TEST_DEVICE_ID, TEST_BUNDLE_NAME, TEST_ABILITY_NAME); + ExtensionBase extension; + extension.OnExtensionAbilityRequestSuccess(TEST_REQUEST_ID, element, TEST_MESSAGE); +} + +/** + * @tc.name: OnExtensionAbilityRequestSuccess_0300 + * @tc.desc: Test OnExtensionAbilityRequestSuccess with long parameters + * @tc.type: FUNC + * @tc.require: issueI6K4G7 + */ +TEST_F(ExtensionBaseTest, OnExtensionAbilityRequestSuccess_0300) +{ + std::string longRequestId(1024, 'a'); + std::string longMessage(1024, 'b'); + AppExecFwk::ElementName element(TEST_DEVICE_ID, TEST_BUNDLE_NAME, TEST_ABILITY_NAME); + ExtensionBase extension; + extension.OnExtensionAbilityRequestSuccess(longRequestId, element, longMessage); +} +} // namespace AbilityRuntime +} // namespace OHOS \ No newline at end of file -- Gitee