From 6ea7d12648e6247b7c6df2a4d0d53320491a14e0 Mon Sep 17 00:00:00 2001 From: "zhangyafei.echo" Date: Thu, 8 Aug 2024 22:35:40 +0800 Subject: [PATCH] Description:add tdd to ability stage context. Sig:SIG_ApplicationFramework Feature or BugFix: Feature Binary Source: No Signed-off-by: zhangyafei.echo Change-Id: I958b44bc8e1c547ee80d7a7ce348f174265f7e9e --- test/unittest/BUILD.gn | 3 +- .../ability_stage_context_test/BUILD.gn | 80 ++++++++ .../ability_stage_context_test.cpp | 187 ++++++++++++++++++ 3 files changed, 269 insertions(+), 1 deletion(-) create mode 100644 test/unittest/ability_stage_context_test/BUILD.gn create mode 100644 test/unittest/ability_stage_context_test/ability_stage_context_test.cpp diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 56c25e5523..61c3af2ea7 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -358,7 +358,8 @@ group("unittest") { "ability_service_extension_test:unittest", "ability_service_log_test:unittest", "ability_service_start_test:unittest", - "ability_timeout_test:unittest", + "ability_stage_context_test:unittest", + "ability_timeout_test", "ability_token_proxy_test:unittest", "ability_token_stub_test:unittest", "advanced_security_mode_manager_test:unittest", diff --git a/test/unittest/ability_stage_context_test/BUILD.gn b/test/unittest/ability_stage_context_test/BUILD.gn new file mode 100644 index 0000000000..9153b47dc2 --- /dev/null +++ b/test/unittest/ability_stage_context_test/BUILD.gn @@ -0,0 +1,80 @@ +# Copyright (c) 2024 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") +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +ohos_unittest("ability_stage_context_test") { + module_out_path = "ability_runtime/context" + + cflags_cc = [] + + include_dirs = [ + "${ability_runtime_innerkits_path}/app_manager/include/appmgr", + "${ability_runtime_test_path}/mock/frameworks_kits_ability_native_test/include", + "${ability_runtime_test_path}/mock/common/include", + ] + + sources = [ "ability_stage_context_test.cpp" ] + + configs = [ "${ability_runtime_services_path}/abilitymgr:abilityms_config" ] + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + deps = [ + "${ability_runtime_innerkits_path}/ability_manager:ability_manager", + "${ability_runtime_innerkits_path}/ability_manager:ability_start_options", + "${ability_runtime_innerkits_path}/app_manager:app_manager", + "${ability_runtime_innerkits_path}/deps_wrapper:ability_deps_wrapper", + "${ability_runtime_innerkits_path}/runtime:runtime", + "${ability_runtime_native_path}/appkit:app_context", + "${ability_runtime_native_path}/appkit:appkit_manager_helper", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:configuration", + "ability_base:extractortool", + "ability_base:session_info", + "ability_base:want", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "hilog:libhilog", + "hitrace:hitrace_meter", + "init:libbegetutil", + "ipc:ipc_core", + "json:nlohmann_json_static", + "napi:ace_napi", + "resource_management:global_resmgr", + "samgr:samgr_proxy", + ] + + if (ability_runtime_graphics) { + external_deps += [ + "i18n:intl_util", + "icu:shared_icuuc", + ] + } +} + +group("unittest") { + testonly = true + deps = [ ":ability_stage_context_test" ] +} diff --git a/test/unittest/ability_stage_context_test/ability_stage_context_test.cpp b/test/unittest/ability_stage_context_test/ability_stage_context_test.cpp new file mode 100644 index 0000000000..e8581ccded --- /dev/null +++ b/test/unittest/ability_stage_context_test/ability_stage_context_test.cpp @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2024 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 "hilog_tag_wrapper.h" +#include "ability_stage_context.h" +#include "application_context.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace AbilityRuntime { +class AbilityStageContextTest : public testing::Test { +public: + + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; +}; + +void AbilityStageContextTest::SetUpTestCase(void) +{} + +void AbilityStageContextTest::TearDownTestCase(void) +{} + +void AbilityStageContextTest::SetUp() +{} + +void AbilityStageContextTest::TearDown() +{} + +/** + * @tc.name: AbilityStageContextTest_0100 + * @tc.desc: Ability stage basic func test. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AbilityStageContextTest, AbilityStageContextTest_0100, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "begin."); + auto context = AbilityRuntime::ApplicationContext::GetInstance(); + auto abilityStageContext = std::make_shared(); + abilityStageContext->SetParentContext(context); + + auto abilityInfo = std::make_shared(); + abilityStageContext->InitHapModuleInfo(abilityInfo); + + AppExecFwk::HapModuleInfo hapModuleInfo; + abilityStageContext->InitHapModuleInfo(hapModuleInfo); + + auto gotHapModuleInfo = abilityStageContext->GetHapModuleInfo(); + EXPECT_NE(gotHapModuleInfo, nullptr); + + auto config = std::make_shared(); + abilityStageContext->SetConfiguration(config); + + auto gotConfig = abilityStageContext->GetConfiguration(); + EXPECT_NE(gotConfig, nullptr); + + std::shared_ptr resMgr(Global::Resource::CreateResourceManager()); + abilityStageContext->SetResourceManager(resMgr); + auto gotResMgr = abilityStageContext->GetResourceManager(); + EXPECT_NE(gotResMgr, nullptr); + + auto gotBundleName = abilityStageContext->GetBundleName(); + EXPECT_EQ(gotBundleName, ""); + + auto gotAppInfo = abilityStageContext->GetApplicationInfo(); + EXPECT_EQ(gotAppInfo, nullptr); + TAG_LOGI(AAFwkTag::TEST, "end."); +} + +/** + * @tc.name: AbilityStageContextTest_0200 + * @tc.desc: Ability stage basic func test. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AbilityStageContextTest, AbilityStageContextTest_0200, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "begin."); + auto context = AbilityRuntime::ApplicationContext::GetInstance(); + auto abilityStageContext = std::make_shared(); + abilityStageContext->SetParentContext(context); + + auto bundleContext = abilityStageContext->CreateBundleContext("com.test.bundleName"); + EXPECT_EQ(bundleContext, nullptr); + + auto moduleContext1 = abilityStageContext->CreateModuleContext("moudleName"); + EXPECT_EQ(moduleContext1, nullptr); + + auto moduleContext2 = abilityStageContext->CreateModuleContext("com.test.bundleName", "moudleName"); + EXPECT_EQ(moduleContext2, nullptr); + + auto moduleResMgr = abilityStageContext->CreateModuleResourceManager("com.test.bundleName", "moudleName"); + EXPECT_EQ(moduleResMgr, nullptr); + + std::shared_ptr hspResMgr(Global::Resource::CreateResourceManager()); + auto ret = abilityStageContext->CreateSystemHspModuleResourceManager("com.test.bundleName", "moudleName", + hspResMgr); + EXPECT_NE(ret, ERR_OK); + EXPECT_NE(hspResMgr, nullptr); + TAG_LOGI(AAFwkTag::TEST, "end."); +} + +/** + * @tc.name: AbilityStageContextTest_0300 + * @tc.desc: Ability stage basic func test. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AbilityStageContextTest, AbilityStageContextTest_0300, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "begin."); + auto context = AbilityRuntime::ApplicationContext::GetInstance(); + auto abilityStageContext = std::make_shared(); + abilityStageContext->SetParentContext(context); + + EXPECT_EQ(abilityStageContext->GetBundleCodePath(), ""); + EXPECT_EQ(abilityStageContext->GetBundleCodeDir(), ""); + EXPECT_EQ(abilityStageContext->GetCacheDir(), "/data/storage/el2/base/haps//cache"); + EXPECT_EQ(abilityStageContext->GetTempDir(), "/data/storage/el2/base/haps//temp"); + EXPECT_EQ(abilityStageContext->GetResourceDir(), ""); + EXPECT_EQ(abilityStageContext->GetFilesDir(), "/data/storage/el2/base/haps//files"); + EXPECT_EQ(abilityStageContext->GetDatabaseDir(), "/data/storage/el2/database/"); + EXPECT_EQ(abilityStageContext->GetPreferencesDir(), "/data/storage/el2/base/haps//preferences"); + EXPECT_EQ(abilityStageContext->GetGroupDir("1"), ""); + + std::string sysDatabaseDir; + auto ret = abilityStageContext->GetSystemDatabaseDir("1", false, sysDatabaseDir); + EXPECT_NE(ret, ERR_OK); + EXPECT_EQ(sysDatabaseDir, ""); + + std::string sysPreferencesDir; + ret = abilityStageContext->GetSystemPreferencesDir("1", false, sysPreferencesDir); + EXPECT_EQ(sysDatabaseDir, ""); + + EXPECT_EQ(abilityStageContext->GetDistributedFilesDir(), "/data/storage/el2/distributedfiles"); + EXPECT_EQ(abilityStageContext->GetCloudFileDir(), "/data/storage/el2/cloud"); + EXPECT_EQ(abilityStageContext->GetBaseDir(), "/data/storage/el2/base/haps/"); + TAG_LOGI(AAFwkTag::TEST, "end."); +} + + +/** + * @tc.name: AbilityStageContextTest_0400 + * @tc.desc: Ability stage basic func test. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AbilityStageContextTest, AbilityStageContextTest_0400, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "begin."); + auto context = AbilityRuntime::ApplicationContext::GetInstance(); + auto abilityStageContext = std::make_shared(); + abilityStageContext->SetParentContext(context); + + EXPECT_EQ(abilityStageContext->IsUpdatingConfigurations(), false); + EXPECT_EQ(abilityStageContext->PrintDrawnCompleted(), false); + + auto token = abilityStageContext->GetToken(); + abilityStageContext->SetToken(token); + + abilityStageContext->SwitchArea(1); + EXPECT_EQ(abilityStageContext->GetArea(), 1); + + EXPECT_EQ(abilityStageContext->GetDeviceType(), Global::Resource::DeviceType::DEVICE_PHONE); + TAG_LOGI(AAFwkTag::TEST, "end."); +} +} // namespace AbilityRuntime +} // namespace OHOS