From 6629df7c2a1b4fa4b1178416785fd540ed645191 Mon Sep 17 00:00:00 2001 From: xinxin13 Date: Wed, 28 Dec 2022 11:24:51 +0800 Subject: [PATCH 1/5] add tdd code Signed-off-by: xinxin13 --- test/unittest/BUILD.gn | 1 + test/unittest/runtime_test/BUILD.gn | 40 +++ .../runtime_test/hdc_register_test.cpp | 99 +++++++ .../static_subscriber_extension_test/BUILD.gn | 78 +++++ .../static_subscriber_extension_test.cpp | 271 ++++++++++++++++++ 5 files changed, 489 insertions(+) create mode 100644 test/unittest/runtime_test/hdc_register_test.cpp create mode 100644 test/unittest/static_subscriber_extension_test/BUILD.gn create mode 100644 test/unittest/static_subscriber_extension_test/static_subscriber_extension_test.cpp diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index ed1cbd770de..918f29387d3 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -384,6 +384,7 @@ group("unittest") { "sender_info_test:unittest", "service_extension_context_test:unittest", "start_options_test:unittest", + "static_subscriber_extension_test:unittest", "stop_user_callback_proxy_test:unittest", "stop_user_callback_stub_test:unittest", "sys_mgr_client_test:unittest", diff --git a/test/unittest/runtime_test/BUILD.gn b/test/unittest/runtime_test/BUILD.gn index 1b463af0f77..6eef004b2a2 100644 --- a/test/unittest/runtime_test/BUILD.gn +++ b/test/unittest/runtime_test/BUILD.gn @@ -16,6 +16,11 @@ import("//foundation/ability/ability_runtime/ability_runtime.gni") module_output_path = "ability_runtime/abilitymgr" +config("coverage_flags") { + cflags = [ "--coverage" ] + ldflags = [ "--coverage" ] +} + ohos_unittest("runtime_test") { module_out_path = module_output_path @@ -72,10 +77,45 @@ ohos_unittest("js_runtime_test") { ] } +ohos_unittest("hdc_register_test") { + module_out_path = module_output_path + + include_dirs = [ + "${ability_runtime_native_path}/runtime", + "${ability_runtime_native_path}/runtime/utils/include", + "//third_party/zlib/contrib/minizip", + "//third_party/zlib", + ] + + sources = [ + # add mock file + "//foundation/ability/ability_runtime/frameworks/native/runtime/hdc_register.cpp", + "hdc_register_test.cpp", + ] + + configs = [ + "${ability_runtime_services_path}/abilitymgr:abilityms_config", + ":coverage_flags", + ] + deps = [ + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_runtime:runtime", + "c_utils:utils", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "napi:ace_napi", + ] +} + group("unittest") { testonly = true deps = [ + ":hdc_register_test", ":js_runtime_test", ":runtime_test", ] diff --git a/test/unittest/runtime_test/hdc_register_test.cpp b/test/unittest/runtime_test/hdc_register_test.cpp new file mode 100644 index 00000000000..60a521b052f --- /dev/null +++ b/test/unittest/runtime_test/hdc_register_test.cpp @@ -0,0 +1,99 @@ +/* + * 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 +#define protected public +#include "js_runtime.h" +#include "js_runtime_utils.h" +#include "js_worker.h" +#include "hdc_register.h" +#undef private +#undef protected + +#include "event_runner.h" +#include "mock_js_runtime.h" +#include "hilog_wrapper.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace AbilityRuntime { +class HdcRegisterTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; +}; + +void HdcRegisterTest::SetUpTestCase() +{} + +void HdcRegisterTest::TearDownTestCase() +{} + +void HdcRegisterTest::SetUp() +{} + +void HdcRegisterTest::TearDown() +{} + +/** + * @tc.name: HdcRegisterTest_0100 + * @tc.desc: HdcRegisterTest Test + * @tc.type: FUNC + */ +HWTEST_F(HdcRegisterTest, HdcRegisterTest_0100, TestSize.Level0) +{ + const std::string bundleName = ""; + auto &pHdcRegister = AbilityRuntime::HdcRegister::Get(); + pHdcRegister.StartHdcRegister(bundleName); + + EXPECT_TRUE(true); +} + +/** + * @tc.name: HdcRegisterTest_0200 + * @tc.desc: HdcRegisterTest Test + * @tc.type: FUNC + */ +HWTEST_F(HdcRegisterTest, HdcRegisterTest_0200, TestSize.Level0) +{ + auto &pHdcRegister = AbilityRuntime::HdcRegister::Get(); + pHdcRegister.registerHandler_ = nullptr; + pHdcRegister.StopHdcRegister(); + + EXPECT_TRUE(true); +} + +/** + * @tc.name: HdcRegisterTest_0300 + * @tc.desc: HdcRegisterTest Test + * @tc.type: FUNC + */ +HWTEST_F(HdcRegisterTest, HdcRegisterTest_0300, TestSize.Level0) +{ + const std::string bundleName = "123"; + auto &pHdcRegister = AbilityRuntime::HdcRegister::Get(); + pHdcRegister.StartHdcRegister(bundleName); + pHdcRegister.StopHdcRegister(); + + EXPECT_TRUE(true); +} +} // namespace AbilityRuntime +} // namespace OHOS diff --git a/test/unittest/static_subscriber_extension_test/BUILD.gn b/test/unittest/static_subscriber_extension_test/BUILD.gn new file mode 100644 index 00000000000..f5d1bd88282 --- /dev/null +++ b/test/unittest/static_subscriber_extension_test/BUILD.gn @@ -0,0 +1,78 @@ +# 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") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +module_output_path = "ability_runtime/abilitymgr" + +#subscriber_extension_path = "${ability_runtime_path}/frameworks/native/ability/native" + +config("coverage_flags") { + if (ability_runtime_feature_coverage) { + cflags = [ "--coverage" ] + ldflags = [ "--coverage" ] + } +} + +ohos_unittest("static_subscriber_extension_test") { + module_out_path = module_output_path + + include_dirs = [ + "${ability_runtime_native_path}/appkit/ability_runtime", + "${ability_runtime_path}/interfaces/kits/native/ability/native", + "${ability_runtime_services_path}/common/include", + ] + + sources = [ + "${ability_runtime_native_path}/ability/native/js_static_subscriber_extension.cpp", + "${ability_runtime_native_path}/ability/native/js_static_subscriber_extension_context.cpp", + "${ability_runtime_native_path}/ability/native/static_subscriber_extension.cpp", + "${ability_runtime_native_path}/ability/native/static_subscriber_stub_imp.cpp", + "${ability_runtime_native_path}/appkit/ability_runtime/static_subscriber_extension_context.cpp", + + #"${subscriber_extension_path}/static_subscriber_extension.cpp", + #"remote_register_service_proxy_test.cpp", + "static_subscriber_extension_test.cpp", + ] + + configs = [ ":coverage_flags" ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + deps = [ + "${ability_runtime_napi_path}/inner/napi_common:napi_common", + "${ability_runtime_native_path}/appkit:app_context", + "${ability_runtime_path}/frameworks/native/ability/native:abilitykit_native", + "${ability_runtime_path}/frameworks/native/ability/native:static_subscriber_ipc", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:ability_context_native", + "ability_runtime:runtime", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] +} + +group("unittest") { + testonly = true + deps = [ ":static_subscriber_extension_test" ] +} diff --git a/test/unittest/static_subscriber_extension_test/static_subscriber_extension_test.cpp b/test/unittest/static_subscriber_extension_test/static_subscriber_extension_test.cpp new file mode 100644 index 00000000000..949d634a31b --- /dev/null +++ b/test/unittest/static_subscriber_extension_test/static_subscriber_extension_test.cpp @@ -0,0 +1,271 @@ +/* + * 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 +#include + +#define private public +#define protected public +#include "runtime.h" +#include "static_subscriber_extension.h" +#include "static_subscriber_extension_context.h" +#undef private +#undef protected + +using namespace testing::ext; +using namespace testing; +using namespace std::chrono; + +namespace OHOS { +namespace AbilityRuntime { +using namespace OHOS::AppExecFwk; +class StaticSubscriberExtensionTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void StaticSubscriberExtensionTest::SetUpTestCase(void) +{} + +void StaticSubscriberExtensionTest::TearDownTestCase(void) +{} + +void StaticSubscriberExtensionTest::SetUp(void) +{} + +void StaticSubscriberExtensionTest::TearDown(void) +{} + +class MockRuntime : public Runtime { +public: + MockRuntime() {}; + virtual ~MockRuntime() {}; + + Language GetLanguage() const + { + return language; + }; + + void StartDebugMode(bool needBreakPoint) override + {}; + + bool BuildJsStackInfoList(uint32_t tid, std::vector& jsFrames) override + { + return true; + }; + + void DumpHeapSnapshot(bool isPrivate) override + {}; + + void NotifyApplicationState(bool isBackground) override + {}; + + void PreloadSystemModule(const std::string& moduleName) override + {}; + + void FinishPreload() override + {}; + + bool LoadRepairPatch(const std::string& patchFile, const std::string& baseFile) override + { + return true; + }; + + bool NotifyHotReloadPage() override + { + return true; + }; + + bool UnLoadRepairPatch(const std::string& patchFile) override + { + return true; + }; + + Language language; +}; + +class MockStaticSubscriberExtension : public StaticSubscriberExtension +{ +public: + MockStaticSubscriberExtension() {}; + virtual ~MockStaticSubscriberExtension() {}; + + void OnReceiveEvent(std::shared_ptr data) override + { + flag = true; + } + + bool flag = false; +}; + +/* +* @tc.number: AbilityRuntime_StaticSubscriberExtension_Create_001 +* @tc.name: Create +* @tc.desc: Verify function Create normal branch, Create object pointer is not nullptr +*/ +HWTEST_F( + StaticSubscriberExtensionTest, AbilityRuntime_StaticSubscriberExtension_Create_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityRuntime_StaticSubscriberExtension_Create_001 start."; + std::unique_ptr runtime = nullptr; + auto resulft = StaticSubscriberExtension::Create(runtime); + EXPECT_TRUE(resulft != nullptr); + delete resulft; + resulft = nullptr; + GTEST_LOG_(INFO) << "AbilityRuntime_StaticSubscriberExtension_Create_001 end."; +} + +/* +* @tc.number: AbilityRuntime_StaticSubscriberExtension_Create_002 +* @tc.name: Create +* @tc.desc: Verify function Create normal branch, Create object pointer is not nullptr +*/ +HWTEST_F( + StaticSubscriberExtensionTest, AbilityRuntime_StaticSubscriberExtension_Create_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityRuntime_StaticSubscriberExtension_Create_002 start."; + std::unique_ptr mockRuntime = std::make_unique(); + mockRuntime->language = static_cast(-1); + std::unique_ptr runtime = std::move(mockRuntime); + auto resulft = StaticSubscriberExtension::Create(runtime); + EXPECT_TRUE(resulft != nullptr); + delete resulft; + resulft = nullptr; + GTEST_LOG_(INFO) << "AbilityRuntime_StaticSubscriberExtension_Create_002 end."; +} + +/* +* @tc.number: AbilityRuntime_StaticSubscriberExtension_Create_003 +* @tc.name: Create +* @tc.desc: Verify function Create normal branch, Create object pointer is not nullptr +*/ +HWTEST_F( + StaticSubscriberExtensionTest, AbilityRuntime_StaticSubscriberExtension_Create_003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityRuntime_StaticSubscriberExtension_Create_003 start."; + std::unique_ptr mockRuntime = std::make_unique(); + mockRuntime->language = MockRuntime::Language::JS; + std::unique_ptr runtime = std::move(mockRuntime); + auto resulft = StaticSubscriberExtension::Create(runtime); + EXPECT_TRUE(resulft != nullptr); + delete resulft; + resulft = nullptr; + GTEST_LOG_(INFO) << "AbilityRuntime_StaticSubscriberExtension_Create_003 end."; +} + +/* +* @tc.number: AbilityRuntime_StaticSubscriberExtension_CreateAndInitContext_001 +* @tc.name: CreateAndInitContext +* @tc.desc: Verify function CreateAndInitContext normal branch, CreateAndInitContext return pointer is not nullptr +*/ +HWTEST_F( + StaticSubscriberExtensionTest, AbilityRuntime_StaticSubscriberExtension_CreateAndInitContext_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityRuntime_StaticSubscriberExtension_CreateAndInitContext_001 start."; + std::unique_ptr runtime = nullptr; + auto testObject = StaticSubscriberExtension::Create(runtime); + EXPECT_TRUE(testObject != nullptr); + + const std::shared_ptr record = nullptr; + const std::shared_ptr application = nullptr; + std::shared_ptr handler = nullptr; + const sptr token = nullptr; + auto resulft = testObject->CreateAndInitContext(record, application, handler, token); + + EXPECT_TRUE(resulft != nullptr); + delete testObject; + testObject = nullptr; + GTEST_LOG_(INFO) << "AbilityRuntime_StaticSubscriberExtension_CreateAndInitContext_001 end."; +} + +/* +* @tc.number: AbilityRuntime_StaticSubscriberExtension_CreateAndInitContext_002 +* @tc.name: CreateAndInitContext +* @tc.desc: Verify function CreateAndInitContext normal branch, CreateAndInitContext return pointer is not nullptr +*/ +HWTEST_F( + StaticSubscriberExtensionTest, AbilityRuntime_StaticSubscriberExtension_CreateAndInitContext_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityRuntime_StaticSubscriberExtension_CreateAndInitContext_002 start."; + std::unique_ptr runtime = nullptr; + auto testObject = StaticSubscriberExtension::Create(runtime); + EXPECT_TRUE(testObject != nullptr); + + const std::shared_ptr info = nullptr; + const std::shared_ptr application = nullptr; + std::shared_ptr handler = nullptr; + const sptr token = nullptr; + const std::shared_ptr record = std::make_shared(info, token); + auto resulft = testObject->CreateAndInitContext(record, application, handler, token); + + EXPECT_TRUE(resulft != nullptr); + delete testObject; + testObject = nullptr; + GTEST_LOG_(INFO) << "AbilityRuntime_StaticSubscriberExtension_CreateAndInitContext_002 end."; +} + +/* +* @tc.number: AbilityRuntime_StaticSubscriberExtension_Init_001 +* @tc.name: Init +* @tc.desc: Verify function Init normal branch, GetContext function return pointer is not nullptr +*/ +HWTEST_F( + StaticSubscriberExtensionTest, AbilityRuntime_StaticSubscriberExtension_Init_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityRuntime_StaticSubscriberExtension_Init_001 start."; + std::unique_ptr runtime = nullptr; + auto testObject = StaticSubscriberExtension::Create(runtime); + EXPECT_TRUE(testObject != nullptr); + EXPECT_TRUE(testObject->GetContext() == nullptr); + + const std::shared_ptr info = nullptr; + const std::shared_ptr application = nullptr; + std::shared_ptr handler = nullptr; + const sptr token = nullptr; + const std::shared_ptr record = std::make_shared(info, token); + testObject->Init(record, application, handler, token); + + EXPECT_TRUE(testObject->GetContext() != nullptr); + delete testObject; + testObject = nullptr; + GTEST_LOG_(INFO) << "AbilityRuntime_StaticSubscriberExtension_Init_001 end."; +} + +/* +* @tc.number: AbilityRuntime_StaticSubscriberExtension_OnReceiveEvent_001 +* @tc.name: OnReceiveEvent +* @tc.desc: Verify function OnReceiveEvent normal branch, OnReceiveEvent called +*/ +HWTEST_F( + StaticSubscriberExtensionTest, AbilityRuntime_StaticSubscriberExtension_OnReceiveEvent_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbilityRuntime_StaticSubscriberExtension_OnReceiveEvent_001 start."; + auto testObject = std::make_shared(); + EXPECT_TRUE(testObject != nullptr); + EXPECT_FALSE(testObject->flag); + std::shared_ptr testExtension = testObject; + + std::shared_ptr data = nullptr; + testObject->OnReceiveEvent(data); + + EXPECT_TRUE(testObject->flag); + GTEST_LOG_(INFO) << "AbilityRuntime_StaticSubscriberExtension_OnReceiveEvent_001 end."; +} +} // namespace AbilityRuntime +} // namespace OHOS \ No newline at end of file -- Gitee From f55d75c6d290cd805c8de58fd8eb31cddadae9dc Mon Sep 17 00:00:00 2001 From: xinxin13 Date: Wed, 28 Dec 2022 14:44:18 +0800 Subject: [PATCH 2/5] add context deal tdd test code Signed-off-by: xinxin13 --- .../BUILD.gn | 6 + .../context_deal_test.cpp | 754 +++++++++++++++++- 2 files changed, 759 insertions(+), 1 deletion(-) diff --git a/test/unittest/frameworks_kits_appkit_native_test/BUILD.gn b/test/unittest/frameworks_kits_appkit_native_test/BUILD.gn index 785b5533e31..0f67ecd9e5d 100644 --- a/test/unittest/frameworks_kits_appkit_native_test/BUILD.gn +++ b/test/unittest/frameworks_kits_appkit_native_test/BUILD.gn @@ -173,6 +173,11 @@ ohos_unittest("context_deal_test") { "context_deal_test.cpp", ] + if (ability_runtime_feature_coverage) { + cflags = [ "--coverage" ] + ldflags = [ "--coverage" ] + } + configs = [ ":module_context_config" ] deps = [ @@ -188,6 +193,7 @@ ohos_unittest("context_deal_test") { external_deps = [ "ability_base:configuration", "ability_base:want", + "ability_base:zuri", "ability_runtime:runtime", "bundle_framework:appexecfwk_base", "c_utils:utils", diff --git a/test/unittest/frameworks_kits_appkit_native_test/context_deal_test.cpp b/test/unittest/frameworks_kits_appkit_native_test/context_deal_test.cpp index d6eed10673b..756616eef1d 100644 --- a/test/unittest/frameworks_kits_appkit_native_test/context_deal_test.cpp +++ b/test/unittest/frameworks_kits_appkit_native_test/context_deal_test.cpp @@ -15,6 +15,8 @@ #include #include +#define private public +#define protected public #include "ohos_application.h" #include "ability.h" #include "context_deal.h" @@ -23,6 +25,8 @@ #include "sys_mgr_client.h" #include "mock_bundle_manager.h" #include "mock_ability_manager_client.h" +#undef private +#undef protected namespace OHOS { namespace AppExecFwk { @@ -91,6 +95,19 @@ HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetBundleName_0100, Function | EXPECT_STREQ(context_->GetBundleName().c_str(), bundleName.c_str()); } +/** + * @tc.number: AppExecFwk_ContextDeal_GetBundleName_0200 + * @tc.name: GetBundleName + * @tc.desc: Verify that the GetBundleName return value is empty. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetBundleName_0200, Function | MediumTest | Level1) +{ + std::shared_ptr info = nullptr; + context_->SetApplicationInfo(info); + + EXPECT_TRUE(context_->GetBundleName().empty()); +} + /** * @tc.number: AppExecFwk_ContextDeal_GetBundleManager_0100 * @tc.name: GetBundleManager @@ -117,6 +134,22 @@ HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetBundleCodePath_0100, Functio EXPECT_STREQ(context_->GetBundleCodePath().c_str(), "/data/storage/el1/bundle"); } +/** + * @tc.number: AppExecFwk_ContextDeal_GetBundleCodePath_0200 + * @tc.name: GetBundleCodePath + * @tc.desc: Verify that the GetBundleCodePath return value is correct. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetBundleCodePath_0200, Function | MediumTest | Level1) +{ + std::shared_ptr info = std::make_shared(); + std::string codePath = "/data/app/el1/bundle/public"; + info->codePath = codePath; + context_->isCreateBySystemApp_ = true; + context_->SetApplicationInfo(info); + + EXPECT_STREQ(context_->GetBundleCodePath().c_str(), Constants::LOCAL_BUNDLES); +} + /** * @tc.number: AppExecFwk_ContextDeal_GetApplicationInfo_0100 * @tc.name: GetApplicationInfo @@ -147,6 +180,22 @@ HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetBundleResourcePath_0100, Fun EXPECT_STREQ(context_->GetBundleResourcePath().c_str(), resourcePath.c_str()); } +/** + * @tc.number: AppExecFwk_ContextDeal_GetBundleResourcePath_0200 + * @tc.name: GetBundleResourcePath + * @tc.desc: Verify that the GetBundleResourcePath return value is correct. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetBundleResourcePath_0200, Function | MediumTest | Level1) +{ + std::shared_ptr info = std::make_shared(); + std::string resourcePath = "/data/app/el1/bundle/public"; + info->resourcePath = resourcePath; + context_->isCreateBySystemApp_ = true; + context_->SetAbilityInfo(info); + + EXPECT_STREQ(context_->GetBundleResourcePath().c_str(), Constants::LOCAL_BUNDLES); +} + /** * @tc.number: AppExecFwk_ContextDeal_GetAbilityManager_0100 * @tc.name: GetAbilityManager @@ -204,6 +253,22 @@ HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetDatabaseDir_0100, Function | EXPECT_STREQ(context_->GetDatabaseDir().c_str(), "/data/storage/el2/database"); } +/** + * @tc.number: AppExecFwk_ContextDeal_GetDatabaseDir_0200 + * @tc.name: GetDatabaseDir + * @tc.desc: Verify that the GetDatabaseDir return value is correct. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetDatabaseDir_0200, Function | MediumTest | Level1) +{ + std::shared_ptr info = std::make_shared(); + std::string dir = "dataBaseDir"; + info->dataBaseDir = dir; + context_->flags_ = ContextDeal::CONTEXT_CREATE_BY_SYSTEM_APP; + context_->SetApplicationInfo(info); + + EXPECT_STREQ(context_->GetDatabaseDir().c_str(), "/data/app/el2/0/database/"); +} + /** * @tc.number: AppExecFwk_ContextDeal_GetFilesDir_0100 * @tc.name: GetFilesDir @@ -322,5 +387,692 @@ HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetProcessName_0100, Function | context_->SetProcessInfo(processinfo); EXPECT_STREQ(name.c_str(), context_->GetProcessName().c_str()); } -} // namespace AppExecFwk + +/** + * @tc.number: AppExecFwk_ContextDeal_SetProcessInfo_0100 + * @tc.name: SetProcessInfo + * @tc.desc: Verify that the SetProcessInfo input parameter is nullptr. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_SetProcessInfo_0100, Function | MediumTest | Level1) +{ + std::shared_ptr processinfo = nullptr; + context_->SetProcessInfo(processinfo); + std::string name = ""; + EXPECT_STREQ(name.c_str(), context_->GetProcessName().c_str()); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_SetApplicationInfo_0100 + * @tc.name: SetApplicationInfo + * @tc.desc: Verify that the SetApplicationInfo input parameter is nullptr. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_SetApplicationInfo_0100, Function | MediumTest | Level1) +{ + std::shared_ptr info = nullptr; + context_->SetApplicationInfo(info); + EXPECT_TRUE(context_->GetApplicationInfo() == nullptr); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_SetApplicationContext_0100 + * @tc.name: SetApplicationContext + * @tc.desc: Verify that the SetApplicationContext input parameter is nullptr. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_SetApplicationContext_0100, Function | MediumTest | Level1) +{ + std::shared_ptr info = nullptr; + context_->SetApplicationContext(info); + EXPECT_TRUE(context_->GetApplicationContext() == nullptr); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_SetAbilityInfo_0100 + * @tc.name: SetAbilityInfo + * @tc.desc: Verify that the SetAbilityInfo input parameter is nullptr. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_SetAbilityInfo_0100, Function | MediumTest | Level1) +{ + std::shared_ptr info = nullptr; + context_->SetAbilityInfo(info); + EXPECT_TRUE(context_->GetAbilityInfo() == nullptr); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_SetAbilityInfo_0200 + * @tc.name: SetAbilityInfo + * @tc.desc: Verify that the SetAbilityInfo input parameter is nullptr. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_SetAbilityInfo_0200, Function | MediumTest | Level1) +{ + std::shared_ptr info = nullptr; + context_->SetAbilityInfo(info); + EXPECT_TRUE(context_->GetAbilityInfo() == nullptr); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_SetContext_0100 + * @tc.name: SetContext + * @tc.desc: Verify that the SetContext input parameter is nullptr. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_SetContext_0100, Function | MediumTest | Level1) +{ + std::shared_ptr info = nullptr; + context_->SetContext(info); + EXPECT_TRUE(context_->GetContext() == nullptr); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_SetProfile_0100 + * @tc.name: SetProfile + * @tc.desc: Verify that the SetProfile input parameter is nullptr. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_SetProfile_0100, Function | MediumTest | Level1) +{ + std::shared_ptr info = nullptr; + context_->SetProfile(info); + EXPECT_TRUE(context_->GetProfile() == nullptr); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetDir_0100 + * @tc.name: GetDir + * @tc.desc: Verify that the GetDir return value is not empty. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetDir_0100, Function | MediumTest | Level1) +{ + std::shared_ptr info = std::make_shared(); + info->dataDir = "OHOS"; + const std::string name = "ohos"; + constexpr int32_t mode = 0; + context_->SetApplicationInfo(info); + auto resulft = context_->GetDir(name, mode); + EXPECT_FALSE(resulft.empty()); + resulft = context_->GetDir(name, mode); + EXPECT_FALSE(resulft.empty()); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetDir_0200 + * @tc.name: GetDir + * @tc.desc: Verify that the GetDir return value is empty. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetDir_0200, Function | MediumTest | Level1) +{ + std::shared_ptr info = nullptr; + const std::string name = "ohos"; + constexpr int32_t mode = 0; + context_->SetApplicationInfo(info); + auto resulft = context_->GetDir(name, mode); + EXPECT_TRUE(resulft.empty()); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetNoBackupFilesDir_0100 + * @tc.name: GetNoBackupFilesDir + * @tc.desc: Verify that the GetNoBackupFilesDir return value is not empty. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetNoBackupFilesDir_0100, Function | MediumTest | Level1) +{ + auto resulft = context_->GetNoBackupFilesDir(); + EXPECT_FALSE(resulft.empty()); + resulft = context_->GetNoBackupFilesDir(); + EXPECT_FALSE(resulft.empty()); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetDistributedDir_0100 + * @tc.name: GetDistributedDir + * @tc.desc: Verify that the GetDistributedDir return value is not empty. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetDistributedDir_0100, Function | MediumTest | Level1) +{ + context_->flags_ = ContextDeal::CONTEXT_CREATE_BY_SYSTEM_APP; + auto resulft = context_->GetDistributedDir(); + EXPECT_FALSE(resulft.empty()); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetDistributedDir_0200 + * @tc.name: GetDistributedDir + * @tc.desc: Verify that the GetDistributedDir return value is not empty. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetDistributedDir_0200, Function | MediumTest | Level1) +{ + auto resulft = context_->GetDistributedDir(); + EXPECT_FALSE(resulft.empty()); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetHapModuleInfo_0100 + * @tc.name: GetHapModuleInfo + * @tc.desc: Verify that the GetHapModuleInfo return value is not nullptr. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetHapModuleInfo_0100, Function | MediumTest | Level1) +{ + std::shared_ptr info = std::make_shared(); + context_->SetAbilityInfo(info); + + auto resulft = context_->GetHapModuleInfo(); + EXPECT_TRUE(resulft != nullptr); + resulft = context_->GetHapModuleInfo(); + EXPECT_TRUE(resulft != nullptr); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetHapModuleInfo_0200 + * @tc.name: GetHapModuleInfo + * @tc.desc: Verify that the GetHapModuleInfo return value is nullptr. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetHapModuleInfo_0200, Function | MediumTest | Level1) +{ + auto resulft = context_->GetHapModuleInfo(); + EXPECT_TRUE(resulft == nullptr); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_IsCreateBySystemApp_0100 + * @tc.name: IsCreateBySystemApp + * @tc.desc: Verify that the IsCreateBySystemApp return value is false. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_IsCreateBySystemApp_0100, Function | MediumTest | Level1) +{ + context_->flags_ = 0; + EXPECT_FALSE(context_->IsCreateBySystemApp()); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_IsCreateBySystemApp_0200 + * @tc.name: IsCreateBySystemApp + * @tc.desc: Verify that the IsCreateBySystemApp return value is true. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_IsCreateBySystemApp_0200, Function | MediumTest | Level1) +{ + context_->flags_ = ContextDeal::CONTEXT_CREATE_BY_SYSTEM_APP; + EXPECT_TRUE(context_->IsCreateBySystemApp()); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetBaseDir_0100 + * @tc.name: GetBaseDir + * @tc.desc: Verify that the GetBaseDir return value is not empty. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetBaseDir_0100, Function | MediumTest | Level1) +{ + EXPECT_FALSE(context_->GetBaseDir().empty()); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetBaseDir_0200 + * @tc.name: GetBaseDir + * @tc.desc: Verify that the GetBaseDir return value is not empty. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetBaseDir_0200, Function | MediumTest | Level1) +{ + context_->flags_ = ContextDeal::CONTEXT_CREATE_BY_SYSTEM_APP; + EXPECT_FALSE(context_->GetBaseDir().empty()); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetColorMode_0100 + * @tc.name: GetColorMode + * @tc.desc: Verify that the GetColorMode return value is AUTO. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetColorMode_0100, Function | MediumTest | Level1) +{ + EXPECT_EQ(context_->GetColorMode(), static_cast(AppExecFwk::ModuleColorMode::AUTO)); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetColorMode_0200 + * @tc.name: GetColorMode + * @tc.desc: Verify that the GetColorMode return value is not DARK. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetColorMode_0200, Function | MediumTest | Level1) +{ + context_->hapModuleInfoLocal_ = std::make_shared(); + context_->hapModuleInfoLocal_->colorMode = AppExecFwk::ModuleColorMode::DARK; + EXPECT_EQ(context_->GetColorMode(), static_cast(AppExecFwk::ModuleColorMode::DARK)); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_SetColorMode_0100 + * @tc.name: SetColorMode + * @tc.desc: Verify that the SetColorMode return value is not DARK. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_SetColorMode_0100, Function | MediumTest | Level1) +{ + context_->SetColorMode(static_cast(AppExecFwk::ModuleColorMode::DARK)); + EXPECT_NE(context_->GetColorMode(), static_cast(AppExecFwk::ModuleColorMode::DARK)); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_SetColorMode_0200 + * @tc.name: SetColorMode + * @tc.desc: Verify that the SetColorMode return value is correct. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_SetColorMode_0200, Function | MediumTest | Level1) +{ + context_->hapModuleInfoLocal_ = std::make_shared(); + context_->SetColorMode(static_cast(AppExecFwk::ModuleColorMode::DARK)); + EXPECT_EQ(context_->GetColorMode(), static_cast(AppExecFwk::ModuleColorMode::DARK)); + context_->SetColorMode(static_cast(AppExecFwk::ModuleColorMode::LIGHT)); + EXPECT_EQ(context_->GetColorMode(), static_cast(AppExecFwk::ModuleColorMode::LIGHT)); + context_->SetColorMode(static_cast(AppExecFwk::ModuleColorMode::AUTO)); + EXPECT_EQ(context_->GetColorMode(), static_cast(AppExecFwk::ModuleColorMode::AUTO)); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetDisplayOrientation_0100 + * @tc.name: GetDisplayOrientation + * @tc.desc: Verify that the GetDisplayOrientation return value is correct. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetDisplayOrientation_0100, Function | MediumTest | Level1) +{ + std::shared_ptr info = std::make_shared(); + info->orientation = DisplayOrientation::LANDSCAPE; + context_->SetAbilityInfo(info); + EXPECT_EQ(context_->GetDisplayOrientation(), static_cast(DisplayOrientation::LANDSCAPE)); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetDisplayOrientation_0200 + * @tc.name: GetDisplayOrientation + * @tc.desc: Verify that the GetDisplayOrientation return value is -1. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetDisplayOrientation_0200, Function | MediumTest | Level1) +{ + context_->hapModuleInfoLocal_ = std::make_shared(); + EXPECT_EQ(context_->GetThemeId(), -1); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetThemeId_0200 + * @tc.name: GetThemeId + * @tc.desc: Verify that the GetThemeId return value is -1. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetThemeId_0200, Function | MediumTest | Level1) +{ + EXPECT_EQ(context_->GetThemeId(), -1); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetTheme_0100 + * @tc.name: GetTheme + * @tc.desc: Verify that the GetTheme return value is empty. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetTheme_0100, Function | MediumTest | Level1) +{ + auto resulft = context_->GetTheme(); + EXPECT_TRUE(resulft.empty()); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetTheme_0200 + * @tc.name: GetTheme + * @tc.desc: Verify that the GetTheme return value is not empty. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetTheme_0200, Function | MediumTest | Level1) +{ + context_->theme_.emplace("TestKey", "TestValue"); + auto resulft = context_->GetTheme(); + EXPECT_FALSE(resulft.empty()); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetCurrentAccountId_0100 + * @tc.name: GetCurrentAccountId + * @tc.desc: Verify that the GetCurrentAccountId return value is not empty. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetCurrentAccountId_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + auto resulft = context_->GetCurrentAccountId(); + EXPECT_EQ(resulft, 0); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_CreateDirIfNotExist_0100 + * @tc.name: CreateDirIfNotExist + * @tc.desc: Verify that the CreateDirIfNotExist execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_CreateDirIfNotExist_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + const std::string dir = "./"; + context_->CreateDirIfNotExist(dir); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_initResourceManager_0100 + * @tc.name: initResourceManager + * @tc.desc: Verify that the initResourceManager execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_initResourceManager_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + const std::shared_ptr resMgr(Global::Resource::CreateResourceManager()); + context_->initResourceManager(resMgr); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_SetPattern_0100 + * @tc.name: SetPattern + * @tc.desc: Verify that the SetPattern execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_SetPattern_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + constexpr int32_t patternId = 0; + context_->SetPattern(patternId); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_SetPattern_0200 + * @tc.name: SetPattern + * @tc.desc: Verify that the SetPattern execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_SetPattern_0200, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + const std::shared_ptr resMgr(Global::Resource::CreateResourceManager()); + context_->initResourceManager(resMgr); + constexpr int32_t patternId = 0; + context_->SetPattern(patternId); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetAbilityPackageContext_0100 + * @tc.name: GetAbilityPackageContext + * @tc.desc: Verify that the GetAbilityPackageContext execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetAbilityPackageContext_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + EXPECT_TRUE(context_->GetAbilityPackageContext() == nullptr); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetCallingBundle_0100 + * @tc.name: GetCallingBundle + * @tc.desc: Verify that the GetCallingBundle execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetCallingBundle_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + EXPECT_TRUE(context_->GetCallingBundle() == std::string("")); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_RequestPermissionsFromUser_0100 + * @tc.name: RequestPermissionsFromUser + * @tc.desc: Verify that the RequestPermissionsFromUser execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_RequestPermissionsFromUser_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + std::vector permissions; + std::vector permissionsState; + auto task = [] (const std::vector &listStr, const std::vector &listInt) {}; + context_->RequestPermissionsFromUser(permissions, permissionsState, task); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_TerminateAbility_0100 + * @tc.name: TerminateAbility + * @tc.desc: Verify that the TerminateAbility execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_TerminateAbility_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + EXPECT_EQ(context_->TerminateAbility(), ERR_INVALID_VALUE); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_ConnectAbility_0100 + * @tc.name: ConnectAbility + * @tc.desc: Verify that the ConnectAbility execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_ConnectAbility_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + const sptr conn; + Want want; + EXPECT_FALSE(context_->ConnectAbility(want, conn)); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_DisconnectAbility_0100 + * @tc.name: DisconnectAbility + * @tc.desc: Verify that the DisconnectAbility execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_DisconnectAbility_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + const sptr conn; + EXPECT_EQ(context_->DisconnectAbility(conn), ERR_INVALID_VALUE); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetToken_0100 + * @tc.name: GetToken + * @tc.desc: Verify that the GetToken execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetToken_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + EXPECT_TRUE(context_->GetToken() == nullptr); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetCaller_0100 + * @tc.name: GetCaller + * @tc.desc: Verify that the GetCaller execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetCaller_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + const Uri uri(context_->uriString_); + EXPECT_TRUE(context_->GetCaller() == uri); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_SerUriString_0100 + * @tc.name: SerUriString + * @tc.desc: Verify that the SerUriString execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_SerUriString_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + const std::string uriStr = "testUri"; + context_->SerUriString(uriStr); + EXPECT_TRUE(uriStr == context_->uriString_); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetString_0100 + * @tc.name: GetString + * @tc.desc: Verify that the GetString execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetString_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + constexpr int32_t resId = 0; + + EXPECT_TRUE(context_->GetString(resId) == std::string("")); + + const std::shared_ptr resMgr(Global::Resource::CreateResourceManager()); + context_->initResourceManager(resMgr); + EXPECT_TRUE(context_->GetString(resId) == std::string("")); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetStringArray_0100 + * @tc.name: GetStringArray + * @tc.desc: Verify that the GetStringArray execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetStringArray_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + constexpr int32_t resId = 0; + + auto resArray1 = context_->GetStringArray(resId); + EXPECT_TRUE(resArray1.empty()); + + const std::shared_ptr resMgr(Global::Resource::CreateResourceManager()); + context_->initResourceManager(resMgr); + auto resArray2 = context_->GetStringArray(resId); + EXPECT_TRUE(resArray2.empty()); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetIntArray_0100 + * @tc.name: GetIntArray + * @tc.desc: Verify that the GetIntArray execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetIntArray_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + constexpr int32_t resId = 0; + + auto resArray1 = context_->GetIntArray(resId); + EXPECT_TRUE(resArray1.empty()); + + const std::shared_ptr resMgr(Global::Resource::CreateResourceManager()); + context_->initResourceManager(resMgr); + auto resArray2 = context_->GetIntArray(resId); + EXPECT_TRUE(resArray2.empty()); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_SetTheme_0100 + * @tc.name: SetTheme + * @tc.desc: Verify that the SetTheme execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_SetTheme_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + constexpr int32_t themeId = 0; + + context_->SetTheme(themeId); + + const std::shared_ptr resMgr(Global::Resource::CreateResourceManager()); + context_->initResourceManager(resMgr); + context_->SetTheme(themeId); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetPattern_0100 + * @tc.name: GetPattern + * @tc.desc: Verify that the GetPattern execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetPattern_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + + auto resMap1 = context_->GetPattern(); + EXPECT_TRUE(resMap1.empty()); + + context_->pattern_.emplace("abc", "ABC"); + auto resMap2 = context_->GetPattern(); + EXPECT_FALSE(resMap2.empty()); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_TerminateAbilityResult_0100 + * @tc.name: TerminateAbilityResult + * @tc.desc: Verify that the TerminateAbilityResult execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_TerminateAbilityResult_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + constexpr int32_t startId = 0; + EXPECT_FALSE(context_->TerminateAbilityResult(startId)); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetPreferencesDir_0100 + * @tc.name: GetPreferencesDir + * @tc.desc: Verify that the GetPreferencesDir execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetPreferencesDir_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + const std::string expectationDir = "/data/storage/el2/base/preferences"; + const std::string resDir = context_->GetPreferencesDir(); + EXPECT_TRUE(expectationDir == resDir); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_SetLifeCycleStateInfo_0100 + * @tc.name: SetLifeCycleStateInfo + * @tc.desc: Verify that the SetLifeCycleStateInfo execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_SetLifeCycleStateInfo_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + const AAFwk::LifeCycleStateInfo info = {}; + context_->SetLifeCycleStateInfo(info); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetMissionId_0100 + * @tc.name: GetMissionId + * @tc.desc: Verify that the GetMissionId execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetMissionId_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + EXPECT_EQ(context_->GetMissionId(), context_->lifeCycleStateInfo_.missionId); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_GetLifeCycleStateInfo_0100 + * @tc.name: GetLifeCycleStateInfo + * @tc.desc: Verify that the GetLifeCycleStateInfo execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_GetLifeCycleStateInfo_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + context_->GetLifeCycleStateInfo(); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_StartAbilities_0100 + * @tc.name: StartAbilities + * @tc.desc: Verify that the StartAbilities execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_StartAbilities_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + const std::vector wants; + context_->StartAbilities(wants); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_SetRunner_0100 + * @tc.name: SetRunner + * @tc.desc: Verify that the SetRunner execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_SetRunner_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + const std::shared_ptr runner; + context_->SetRunner(runner); +} + +/** + * @tc.number: AppExecFwk_ContextDeal_HapModuleInfoRequestInit_0100 + * @tc.name: HapModuleInfoRequestInit + * @tc.desc: Verify that the HapModuleInfoRequestInit execute normally. + */ +HWTEST_F(ContextDealTest, AppExecFwk_ContextDeal_HapModuleInfoRequestInit_0100, Function | MediumTest | Level1) +{ + EXPECT_TRUE(context_ != nullptr); + const std::shared_ptr runner; + EXPECT_FALSE(context_->HapModuleInfoRequestInit()); + + context_->abilityInfo_ = std::make_shared(); + EXPECT_TRUE(context_->HapModuleInfoRequestInit()); } +} // namespace AppExecFwk +} // OHOS \ No newline at end of file -- Gitee From 1a2102f348bc4b346bbef7002a27cf70559c2204 Mon Sep 17 00:00:00 2001 From: xinxin13 Date: Wed, 28 Dec 2022 14:57:38 +0800 Subject: [PATCH 3/5] add task handler client test tdd code Signed-off-by: xinxin13 --- .../task_handler_client_test.cpp | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test/unittest/frameworks_kits_ability_native_test/task_handler_client_test.cpp b/test/unittest/frameworks_kits_ability_native_test/task_handler_client_test.cpp index e971d18d466..50cefa9c903 100644 --- a/test/unittest/frameworks_kits_ability_native_test/task_handler_client_test.cpp +++ b/test/unittest/frameworks_kits_ability_native_test/task_handler_client_test.cpp @@ -64,20 +64,6 @@ HWTEST_F(TaskHandlerClientTest, AaFwk_TaskHandlerClient_GetInstance_0100, Functi GTEST_LOG_(INFO) << "AaFwk_TaskHandlerClient_GetInstance_0100 end"; } -/** - * @tc.number: AaFwk_TaskHandlerClient_CreateRunner_0100 - * @tc.name: CreateRunner - * @tc.desc: Test whether TaskHandlerClient::CreateRunner is true. - */ -HWTEST_F(TaskHandlerClientTest, AaFwk_TaskHandlerClient_CreateRunner_0100, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_TaskHandlerClient_CreateRunner_0100 start"; - - EXPECT_EQ(TaskHandlerClient::GetInstance()->CreateRunner(), true); - - GTEST_LOG_(INFO) << "AaFwk_TaskHandlerClient_CreateRunner_0100 end"; -} - /** * @tc.number: AaFwk_TaskHandlerClient_PostTask_0100 * @tc.name: PostTask @@ -93,5 +79,19 @@ HWTEST_F(TaskHandlerClientTest, AaFwk_TaskHandlerClient_PostTask_0100, Function GTEST_LOG_(INFO) << "AaFwk_TaskHandlerClient_PostTask_0100 end"; } + +/** + * @tc.number: AaFwk_TaskHandlerClient_CreateRunner_0100 + * @tc.name: CreateRunner + * @tc.desc: Test whether TaskHandlerClient::CreateRunner is true. + */ +HWTEST_F(TaskHandlerClientTest, AaFwk_TaskHandlerClient_CreateRunner_0100, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) << "AaFwk_TaskHandlerClient_CreateRunner_0100 start"; + + EXPECT_EQ(TaskHandlerClient::GetInstance()->CreateRunner(), true); + + GTEST_LOG_(INFO) << "AaFwk_TaskHandlerClient_CreateRunner_0100 end"; +} } // namespace AppExecFwk } // namespace OHOS -- Gitee From 00ccfa796854a20595333151d6376c752848c414 Mon Sep 17 00:00:00 2001 From: xinxin13 Date: Wed, 28 Dec 2022 15:30:47 +0800 Subject: [PATCH 4/5] add tdd code Signed-off-by: xinxin13 --- test/unittest/BUILD.gn | 3 + .../static_subscriber_proxy_test/BUILD.gn | 64 +++++++++ .../static_subscriber_proxy_test.cpp | 127 +++++++++++++++++ .../static_subscriber_stub_imp_test/BUILD.gn | 69 +++++++++ .../static_subscriber_stub_imp_test.cpp | 87 ++++++++++++ .../static_subscriber_stub_test/BUILD.gn | 64 +++++++++ .../static_subscriber_stub_test.cpp | 133 ++++++++++++++++++ 7 files changed, 547 insertions(+) create mode 100755 test/unittest/static_subscriber_proxy_test/BUILD.gn create mode 100644 test/unittest/static_subscriber_proxy_test/static_subscriber_proxy_test.cpp create mode 100755 test/unittest/static_subscriber_stub_imp_test/BUILD.gn create mode 100644 test/unittest/static_subscriber_stub_imp_test/static_subscriber_stub_imp_test.cpp create mode 100755 test/unittest/static_subscriber_stub_test/BUILD.gn create mode 100644 test/unittest/static_subscriber_stub_test/static_subscriber_stub_test.cpp diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 918f29387d3..b2ec7979ff0 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -385,6 +385,9 @@ group("unittest") { "service_extension_context_test:unittest", "start_options_test:unittest", "static_subscriber_extension_test:unittest", + "static_subscriber_proxy_test:unittest", + "static_subscriber_stub_imp_test:unittest", + "static_subscriber_stub_test:unittest", "stop_user_callback_proxy_test:unittest", "stop_user_callback_stub_test:unittest", "sys_mgr_client_test:unittest", diff --git a/test/unittest/static_subscriber_proxy_test/BUILD.gn b/test/unittest/static_subscriber_proxy_test/BUILD.gn new file mode 100755 index 00000000000..6bfe286323e --- /dev/null +++ b/test/unittest/static_subscriber_proxy_test/BUILD.gn @@ -0,0 +1,64 @@ +# 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") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +module_output_path = "ability_runtime/abilitymgr" + +config("coverage_flags") { + if (ability_runtime_feature_coverage) { + cflags = [ "--coverage" ] + ldflags = [ "--coverage" ] + } +} + +ohos_unittest("static_subscriber_proxy_test") { + module_out_path = module_output_path + + cflags_cc = [] + include_dirs = [] + + sources = [ "static_subscriber_proxy_test.cpp" ] + + configs = [ + "${ability_runtime_services_path}/abilitymgr:abilityms_config", + ":coverage_flags", + ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ + "${ability_runtime_native_path}/ability/native:static_subscriber_ipc", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:want", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] + + if (background_task_mgr_continuous_task_enable) { + external_deps += [ "background_task_mgr:bgtaskmgr_innerkits" ] + } +} + +group("unittest") { + testonly = true + + deps = [ ":static_subscriber_proxy_test" ] +} diff --git a/test/unittest/static_subscriber_proxy_test/static_subscriber_proxy_test.cpp b/test/unittest/static_subscriber_proxy_test/static_subscriber_proxy_test.cpp new file mode 100644 index 00000000000..595997191ad --- /dev/null +++ b/test/unittest/static_subscriber_proxy_test/static_subscriber_proxy_test.cpp @@ -0,0 +1,127 @@ +/* + * 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 +#include + +#define private public +#define protected public +#include "message_parcel.h" +#include "static_subscriber_proxy.h" +#include "static_subscriber_stub.h" +#undef private +#undef protected + +using namespace testing::ext; +using namespace testing; +using namespace std::chrono; + +namespace OHOS { +namespace AppExecFwk { +class StaticSubscriberProxyTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void StaticSubscriberProxyTest::SetUpTestCase(void) +{} + +void StaticSubscriberProxyTest::TearDownTestCase(void) +{} + +void StaticSubscriberProxyTest::SetUp(void) +{} + +void StaticSubscriberProxyTest::TearDown(void) +{} + +class MockStaticSubscriber : public StaticSubscriberStub { +public: + MockStaticSubscriber() {}; + virtual ~MockStaticSubscriber() {}; + + int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override + { + GTEST_LOG_(INFO) << "MockRegisterService::SendRequest called. return value " << returnCode_; + reply.WriteInt32(ERR_NONE); + return returnCode_; + } + + ErrCode OnReceiveEvent(CommonEventData* inData) override + { + return 0; + } + + int32_t returnCode_ = ERR_NONE; +}; + +class MockStaticSubscriberStub : public StaticSubscriberStub { +public: + MockStaticSubscriberStub() {}; + virtual ~MockStaticSubscriberStub() {}; + + int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override + { + GTEST_LOG_(INFO) << "MockRegisterService::SendRequest called. return value " << returnCode_; + return returnCode_; + } + + ErrCode OnReceiveEvent(CommonEventData* inData) override + { + return 0; + } + + int32_t returnCode_ = 1; +}; + +/** + * @tc.number: StaticSubscriberProxy_OnReceiveEvent_001 + * @tc.name: OnReceiveEvent + * @tc.desc: Pass in normal parameters, and the test program executes correctly without abnormal exit + */ +HWTEST_F(StaticSubscriberProxyTest, StaticSubscriberProxy_OnReceiveEvent_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StaticSubscriberProxy_OnReceiveEvent_001 start."; + sptr object = new (std::nothrow) MockStaticSubscriber(); + sptr testProxy = new (std::nothrow) StaticSubscriberProxy(object); + EXPECT_TRUE(testProxy != nullptr); + EXPECT_TRUE(testProxy->remoteObject_ != nullptr); + CommonEventData* inData = nullptr; + EXPECT_EQ(testProxy->OnReceiveEvent(inData), ERR_OK); + GTEST_LOG_(INFO) << "StaticSubscriberProxy_OnReceiveEvent_001 end."; +} + +/** + * @tc.number: StaticSubscriberProxy_OnReceiveEvent_002 + * @tc.name: OnReceiveEvent + * @tc.desc: Pass in normal parameters, and the test program executes correctly without abnormal exit + */ +HWTEST_F(StaticSubscriberProxyTest, StaticSubscriberProxy_OnReceiveEvent_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StaticSubscriberProxy_OnReceiveEvent_002 start."; + sptr object = new (std::nothrow) MockStaticSubscriberStub(); + sptr testProxy = new (std::nothrow) StaticSubscriberProxy(object); + EXPECT_TRUE(testProxy != nullptr); + EXPECT_TRUE(testProxy->remoteObject_ != nullptr); + CommonEventData* inData = nullptr; + EXPECT_EQ(testProxy->OnReceiveEvent(inData), 1); + GTEST_LOG_(INFO) << "StaticSubscriberProxy_OnReceiveEvent_002 end."; +} +} // namespace AppExecFwk +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/static_subscriber_stub_imp_test/BUILD.gn b/test/unittest/static_subscriber_stub_imp_test/BUILD.gn new file mode 100755 index 00000000000..30f8e271aec --- /dev/null +++ b/test/unittest/static_subscriber_stub_imp_test/BUILD.gn @@ -0,0 +1,69 @@ +# 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") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +module_output_path = "ability_runtime/abilitymgr" + +config("coverage_flags") { + if (ability_runtime_feature_coverage) { + cflags = [ "--coverage" ] + ldflags = [ "--coverage" ] + } +} + +ohos_unittest("static_subscriber_stub_imp_test") { + module_out_path = module_output_path + + cflags_cc = [] + include_dirs = + [ "${ability_runtime_path}/test/mock/frameworks_kits_runtime_test" ] + + sources = [ "static_subscriber_stub_imp_test.cpp" ] + + configs = [ + "${ability_runtime_services_path}/abilitymgr:abilityms_config", + ":coverage_flags", + ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ + "${ability_runtime_napi_path}/inner/napi_common:napi_common", + "${ability_runtime_native_path}/ability/native:abilitykit_native", + "${ability_runtime_native_path}/ability/native:static_subscriber_extension", + "${ability_runtime_native_path}/ability/native:static_subscriber_ipc", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:runtime", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] + + if (background_task_mgr_continuous_task_enable) { + external_deps += [ "background_task_mgr:bgtaskmgr_innerkits" ] + } +} + +group("unittest") { + testonly = true + + deps = [ ":static_subscriber_stub_imp_test" ] +} diff --git a/test/unittest/static_subscriber_stub_imp_test/static_subscriber_stub_imp_test.cpp b/test/unittest/static_subscriber_stub_imp_test/static_subscriber_stub_imp_test.cpp new file mode 100644 index 00000000000..8d7f5b97fda --- /dev/null +++ b/test/unittest/static_subscriber_stub_imp_test/static_subscriber_stub_imp_test.cpp @@ -0,0 +1,87 @@ +/* + * 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 +#include + +#define private public +#define protected public +#include "static_subscriber_stub_imp.h" +#include "js_runtime.h" +#include "mock_runtime.h" +#undef private +#undef protected + +using namespace testing::ext; +using namespace testing; +using namespace std::chrono; + +namespace OHOS { +namespace AppExecFwk { +class StaticSubscriberStubImpTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void StaticSubscriberStubImpTest::SetUpTestCase(void) +{} + +void StaticSubscriberStubImpTest::TearDownTestCase(void) +{} + +void StaticSubscriberStubImpTest::SetUp(void) +{} + +void StaticSubscriberStubImpTest::TearDown(void) +{} + +/** + * @tc.number: StaticSubscriberStubImp_OnReceiveEvent_001 + * @tc.name: OnReceiveEvent + * @tc.desc: The extension is not a null ptr value. The test program executes as expected and does not exit abnormally + */ +HWTEST_F(StaticSubscriberStubImpTest, StaticSubscriberStubImp_OnReceiveEvent_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StaticSubscriberStubImp_OnReceiveEvent_001 start."; + AbilityRuntime::MockRuntime jsRuntime; + std::shared_ptr extension = std::make_shared(jsRuntime); + sptr object = new (std::nothrow) StaticSubscriberStubImp(extension); + EXPECT_TRUE(object != nullptr); + CommonEventData* data = nullptr; + EXPECT_EQ(object->OnReceiveEvent(data), 0); + GTEST_LOG_(INFO) << "StaticSubscriberStubImp_OnReceiveEvent_001 end."; +} + +/** + * @tc.number: StaticSubscriberStubImp_OnReceiveEvent_002 + * @tc.name: OnReceiveEvent + * @tc.desc: The extension is a null ptr value. The test program executes as expected and does not exit abnormally + */ +HWTEST_F(StaticSubscriberStubImpTest, StaticSubscriberStubImp_OnReceiveEvent_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StaticSubscriberStubImp_OnReceiveEvent_002 start."; + const std::shared_ptr extension; + sptr object = new (std::nothrow) StaticSubscriberStubImp(extension); + EXPECT_TRUE(object != nullptr); + CommonEventData* data = nullptr; + EXPECT_EQ(object->OnReceiveEvent(data), -1); + GTEST_LOG_(INFO) << "StaticSubscriberStubImp_OnReceiveEvent_002 end."; +} +} // namespace AppExecFwk +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/static_subscriber_stub_test/BUILD.gn b/test/unittest/static_subscriber_stub_test/BUILD.gn new file mode 100755 index 00000000000..396f99a712a --- /dev/null +++ b/test/unittest/static_subscriber_stub_test/BUILD.gn @@ -0,0 +1,64 @@ +# 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") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +module_output_path = "ability_runtime/abilitymgr" + +config("coverage_flags") { + if (ability_runtime_feature_coverage) { + cflags = [ "--coverage" ] + ldflags = [ "--coverage" ] + } +} + +ohos_unittest("static_subscriber_stub_test") { + module_out_path = module_output_path + + cflags_cc = [] + include_dirs = [] + + sources = [ "static_subscriber_stub_test.cpp" ] + + configs = [ + "${ability_runtime_services_path}/abilitymgr:abilityms_config", + ":coverage_flags", + ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ + "${ability_runtime_native_path}/ability/native:static_subscriber_ipc", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:want", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] + + if (background_task_mgr_continuous_task_enable) { + external_deps += [ "background_task_mgr:bgtaskmgr_innerkits" ] + } +} + +group("unittest") { + testonly = true + + deps = [ ":static_subscriber_stub_test" ] +} diff --git a/test/unittest/static_subscriber_stub_test/static_subscriber_stub_test.cpp b/test/unittest/static_subscriber_stub_test/static_subscriber_stub_test.cpp new file mode 100644 index 00000000000..f3ce2b7aac9 --- /dev/null +++ b/test/unittest/static_subscriber_stub_test/static_subscriber_stub_test.cpp @@ -0,0 +1,133 @@ +/* + * 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 +#include + +#define private public +#define protected public +#include "message_parcel.h" +#include "static_subscriber_proxy.h" +#include "static_subscriber_stub.h" +#undef private +#undef protected + +using namespace testing::ext; +using namespace testing; +using namespace std::chrono; + +namespace OHOS { +namespace AppExecFwk { +class StaticSubscriberStubTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void StaticSubscriberStubTest::SetUpTestCase(void) +{} + +void StaticSubscriberStubTest::TearDownTestCase(void) +{} + +void StaticSubscriberStubTest::SetUp(void) +{} + +void StaticSubscriberStubTest::TearDown(void) +{} + +class MockStaticSubscriber : public StaticSubscriberStub { +public: + MockStaticSubscriber() {}; + virtual ~MockStaticSubscriber() {}; + + int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override + { + GTEST_LOG_(INFO) << "MockRegisterService::SendRequest called. return value " << returnCode_; + reply.WriteInt32(ERR_NONE); + return returnCode_; + } + + ErrCode OnReceiveEvent(CommonEventData* inData) override + { + GTEST_LOG_(INFO) << "MockRegisterService::OnReceiveEvent called."; + return 0; + } + + int32_t returnCode_ = ERR_NONE; +}; + +/** + * @tc.number: StaticSubscriberStub_OnRemoteRequest_001 + * @tc.name: OnRemoteRequest + * @tc.desc: Parameter code is default value, execute the corresponding + * branch and return the corresponding return value + */ +HWTEST_F(StaticSubscriberStubTest, StaticSubscriberStub_OnRemoteRequest_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StaticSubscriberStub_OnRemoteRequest_001 start."; + sptr object = new (std::nothrow) MockStaticSubscriber(); + EXPECT_TRUE(object != nullptr); + uint32_t code = 0; + MessageParcel data; + MessageParcel reply; + MessageOption option; + EXPECT_TRUE(data.WriteInterfaceToken(u"OHOS.AppExecFwk.IStaticSubscriber")); + EXPECT_EQ(object->OnRemoteRequest(code, data, reply, option), IPC_STUB_UNKNOW_TRANS_ERR); + GTEST_LOG_(INFO) << "StaticSubscriberStub_OnRemoteRequest_001 end."; +} + +/** + * @tc.number: StaticSubscriberStub_OnRemoteRequest_002 + * @tc.name: OnRemoteRequest + * @tc.desc: The parameter data is different from the GetDescriptor field. Execute the corresponding + * branch and return the corresponding return value + */ +HWTEST_F(StaticSubscriberStubTest, StaticSubscriberStub_OnRemoteRequest_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StaticSubscriberStub_OnRemoteRequest_002 start."; + sptr object = new (std::nothrow) MockStaticSubscriber(); + EXPECT_TRUE(object != nullptr); + uint32_t code = 0; + MessageParcel data; + MessageParcel reply; + MessageOption option; + EXPECT_EQ(object->OnRemoteRequest(code, data, reply, option), ERR_TRANSACTION_FAILED); + GTEST_LOG_(INFO) << "StaticSubscriberStub_OnRemoteRequest_002 end."; +} + +/** + * @tc.number: StaticSubscriberStub_OnRemoteRequest_003 + * @tc.name: OnRemoteRequest + * @tc.desc: Parameter code is COMMAND_ON_RECEIVE_EVENT value, execute the corresponding + * branch and return the corresponding return value + */ +HWTEST_F(StaticSubscriberStubTest, StaticSubscriberStub_OnRemoteRequest_003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StaticSubscriberStub_OnRemoteRequest_003 start."; + sptr object = new (std::nothrow) MockStaticSubscriber(); + EXPECT_TRUE(object != nullptr); + MessageParcel data; + MessageParcel reply; + MessageOption option; + EXPECT_TRUE(data.WriteInterfaceToken(u"OHOS.AppExecFwk.IStaticSubscriber")); + EXPECT_EQ(object->OnRemoteRequest(MockStaticSubscriber::COMMAND_ON_RECEIVE_EVENT, data, reply, option), ERR_NONE); + GTEST_LOG_(INFO) << "StaticSubscriberStub_OnRemoteRequest_003 end."; +} +} // namespace AppExecFwk +} // namespace OHOS \ No newline at end of file -- Gitee From 44bc85cd4a871af05ab58d1cfc8d8abc189bd2ad Mon Sep 17 00:00:00 2001 From: xinxin13 Date: Wed, 28 Dec 2022 17:29:07 +0800 Subject: [PATCH 5/5] add tdd file Signed-off-by: xinxin13 --- interfaces/kits/native/ability/native/extension_base.inl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/interfaces/kits/native/ability/native/extension_base.inl b/interfaces/kits/native/ability/native/extension_base.inl index 8ed6dc58a9f..b66968063f3 100644 --- a/interfaces/kits/native/ability/native/extension_base.inl +++ b/interfaces/kits/native/ability/native/extension_base.inl @@ -40,6 +40,10 @@ std::shared_ptr ExtensionBase::CreateAndInitContext(const std::shared_ptr< std::shared_ptr context = std::make_shared(); context->SetToken(token); auto appContext = Context::GetApplicationContext(); + if (appContext == nullptr) { + HILOG_ERROR("ServiceExtension::CreateAndInitContext appContext is nullptr"); + return context; + } context->SetApplicationInfo(appContext->GetApplicationInfo()); context->SetResourceManager(appContext->GetResourceManager()); context->SetParentContext(appContext); -- Gitee