!7258 新增 AutoFillExtension相关 UT用例

Merge pull request !7258 from xinking129/master
This commit is contained in:
openharmony_ci
2023-12-23 08:55:48 +00:00
committed by Gitee
6 changed files with 636 additions and 0 deletions
+1
View File
@@ -429,6 +429,7 @@ group("unittest") {
"free_install_manager_test:unittest",
"image_info_test:unittest",
"implicit_start_processor_test:unittest",
"js_auto_fill_extension_test:unittest",
"js_service_extension_test:unittest",
"lifecycle_deal_test:unittest",
"lifecycle_test:unittest",
@@ -2418,6 +2418,68 @@ ohos_unittest("share_extension_test") {
]
}
ohos_unittest("auto_fill_extension_module_loader_test") {
module_out_path = module_output_path
include_dirs = [ "${ability_runtime_path}/interfaces/kits/native/ability/native/auto_fill_extension_ability" ]
sources = [
"${ability_runtime_native_path}/ability/native/auto_fill_extension_ability/auto_fill_extension_module_loader.cpp",
"auto_fill_extension_module_loader_test.cpp",
]
configs = [ ":module_private_config" ]
deps = [
"${ability_runtime_innerkits_path}/runtime:runtime",
"${ability_runtime_native_path}/ability/native:abilitykit_native",
"${ability_runtime_native_path}/ability/native:auto_fill_extension",
"${ability_runtime_native_path}/appkit:app_context",
"${ability_runtime_native_path}/appkit:appkit_native",
"//third_party/googletest:gtest_main",
]
external_deps = [
"bundle_framework:appexecfwk_base",
"c_utils:utils",
"hilog:libhilog",
"ipc:ipc_core",
"napi:ace_napi",
]
}
ohos_unittest("auto_fill_extension_test") {
module_out_path = module_output_path
include_dirs = [
"${ability_runtime_test_path}/mock/frameworks_kits_ability_native_test/include",
"${ability_runtime_path}/interfaces/kits/native/ability/native/auto_fill_extension_ability",
]
sources = [ "auto_fill_extension_test.cpp" ]
configs = [ ":module_private_config" ]
deps = [
"${ability_runtime_innerkits_path}/ability_manager:ability_manager",
"${ability_runtime_innerkits_path}/runtime:runtime",
"${ability_runtime_native_path}/ability/native:abilitykit_native",
"${ability_runtime_native_path}/ability/native:auto_fill_extension",
"${ability_runtime_native_path}/appkit:app_context",
"${ability_runtime_native_path}/appkit:appkit_native",
"//third_party/googletest:gtest_main",
]
external_deps = [
"ability_base:base",
"ability_base:want",
"bundle_framework:appexecfwk_base",
"c_utils:utils",
"eventhandler:libeventhandler",
"hilog:libhilog",
"ipc:ipc_core",
"napi:ace_napi",
]
}
###############################################################################
group("unittest") {
@@ -2445,6 +2507,8 @@ group("unittest") {
":abilityruntime_test",
":action_extension_module_loader_test",
":action_extension_test",
":auto_fill_extension_module_loader_test",
":auto_fill_extension_test",
":continuation_test",
":data_ability_helper_impl_test",
":data_ability_helper_test",
@@ -0,0 +1,100 @@
/*
* Copyright (c) 2023 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 <dlfcn.h>
#include <gtest/gtest.h>
#include "auto_fill_extension_module_loader.h"
namespace OHOS {
namespace AbilityRuntime {
using namespace testing::ext;
class AutoFillExtensionModuleLoaderTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void AutoFillExtensionModuleLoaderTest::SetUpTestCase(void)
{}
void AutoFillExtensionModuleLoaderTest::TearDownTestCase(void)
{}
void AutoFillExtensionModuleLoaderTest::SetUp(void)
{}
void AutoFillExtensionModuleLoaderTest::TearDown(void)
{}
/**
* @tc.name: GetExtensionModule_0100
* @tc.desc: Verify can load AutoFillExtensionModule successfully.
* @tc.type: FUNC.
*/
HWTEST_F(AutoFillExtensionModuleLoaderTest, GetExtensionModule_0100, Function | MediumTest | Level1)
{
GTEST_LOG_(INFO) << "GetExtensionModule_0100 start";
void* handle = dlopen("/system/lib/extensionability/libauto_fill_extension_module.z.so", RTLD_LAZY);
if (handle != nullptr) {
auto obj = reinterpret_cast<AutoFillExtensionModuleLoader*>(
dlsym(handle, "OHOS_EXTENSION_GetExtensionModule"));
EXPECT_TRUE(obj != nullptr);
}
dlclose(handle);
GTEST_LOG_(INFO) << "GetExtensionModule_0100 end";
}
/**
* @tc.name: Create_0100
* @tc.desc: Verify can create AutoFillExtensionModule successfully.
* @tc.type: FUNC
*/
HWTEST_F(AutoFillExtensionModuleLoaderTest, Create_0100, Function | MediumTest | Level1)
{
GTEST_LOG_(INFO) << "Create_0100 start";
std::unique_ptr<Runtime> runtime;
auto extension = AutoFillExtensionModuleLoader::GetInstance().Create(runtime);
EXPECT_TRUE(extension != nullptr);
GTEST_LOG_(INFO) << "Create_0100 end";
}
/**
* @tc.name: GetParams_0100
* @tc.desc: Verify can get params successfully.
* @tc.type: FUNC
*/
HWTEST_F(AutoFillExtensionModuleLoaderTest, GetParams_0100, Function | MediumTest | Level1)
{
GTEST_LOG_(INFO) << "GetParams_0100 start";
auto params = AutoFillExtensionModuleLoader::GetInstance().GetParams();
std::string key = "type";
auto finder = params.find(key);
if (finder != params.end()) {
EXPECT_STREQ(finder->second.c_str(), "501");
}
key = "name";
auto iter = params.find(key);
if (iter != params.end()) {
EXPECT_STREQ(iter->second.c_str(), "AutoFillExtensionAbility");
}
GTEST_LOG_(INFO) << "GetParams_0100 end";
}
} // namespace AbilityRuntime
} // namespace OHOS
@@ -0,0 +1,215 @@
/*
* Copyright (c) 2023 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_handler.h"
#define private public
#include "context_impl.h"
#include "auto_fill_extension.h"
#include "auto_fill_extension_context.h"
#undef private
#include "mock_ability_token.h"
#include "ohos_application.h"
#include "runtime.h"
namespace OHOS {
namespace AbilityRuntime {
using namespace testing::ext;
namespace {
const std::string BUNDLE_NAME = "com.ohos.settingsdata";
const std::string MODULE_NAME = "entry";
}
class AutoFillExtensionTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void AutoFillExtensionTest::SetUpTestCase(void)
{}
void AutoFillExtensionTest::TearDownTestCase(void)
{}
void AutoFillExtensionTest::SetUp(void)
{}
void AutoFillExtensionTest::TearDown(void)
{}
/**
* @tc.name: Create_0100
* @tc.desc: The runtime is nullptr, and the verification of create is successful.
* @tc.type: FUNC
*/
HWTEST_F(AutoFillExtensionTest, Create_0100, Function | MediumTest | Level1)
{
GTEST_LOG_(INFO) << "Create_0100 start";
std::unique_ptr<Runtime> runtime;
auto autoFillExtension = AutoFillExtension::Create(runtime);
EXPECT_TRUE(autoFillExtension != nullptr);
GTEST_LOG_(INFO) << "Create_0100 end";
}
/**
* @tc.name: Create_0200
* @tc.desc: The language is js, and the verification of create is successful.
* @tc.type: FUNC
*/
HWTEST_F(AutoFillExtensionTest, Create_0200, Function | MediumTest | Level1)
{
GTEST_LOG_(INFO) << "Create_0200 start";
Runtime::Options options;
auto runtime = Runtime::Create(options);
auto autoFillExtension = AutoFillExtension::Create(runtime);
EXPECT_TRUE(autoFillExtension != nullptr);
GTEST_LOG_(INFO) << "Create_0200 end";
}
/**
* @tc.name: Init_0100
* @tc.desc: Verify the initialization is successful.
* @tc.type: FUNC
*/
HWTEST_F(AutoFillExtensionTest, Init_0100, Function | MediumTest | Level1)
{
GTEST_LOG_(INFO) << "Init_0100 start";
Runtime::Options options;
std::unique_ptr<Runtime> runtime = Runtime::Create(options);
auto autoFillExtension = AutoFillExtension::Create(runtime);
EXPECT_NE(autoFillExtension, nullptr);
auto abilityInfo = std::make_shared<AppExecFwk::AbilityInfo>();
sptr<IRemoteObject> token = new AppExecFwk::MockAbilityToken();
EXPECT_NE(token, nullptr);
auto record = std::make_shared<AppExecFwk::AbilityLocalRecord>(abilityInfo, token);
EXPECT_NE(record, nullptr);
auto application = std::make_shared<AppExecFwk::OHOSApplication>();
EXPECT_NE(application, nullptr);
auto contextImpl = std::make_shared<ContextImpl>();
auto applicationContext = ApplicationContext::GetInstance();
applicationContext->AttachContextImpl(contextImpl);
application->SetApplicationContext(applicationContext);
auto handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
EXPECT_NE(handler, nullptr);
autoFillExtension->Init(record, application, handler, token);
EXPECT_TRUE(autoFillExtension->context_ != nullptr);
GTEST_LOG_(INFO) << "Init_0100 end";
}
/**
* @tc.name: CreateAndInitContext_0100
* @tc.desc: Verify that CreateAndInitContext function calls normally.
* @tc.type: FUNC
*/
HWTEST_F(AutoFillExtensionTest, CreateAndInitContext_0100, Function | MediumTest | Level1)
{
GTEST_LOG_(INFO) << "CreateAndInitContext_0100 start";
Runtime::Options options;
std::unique_ptr<Runtime> runtime = Runtime::Create(options);
auto autoFillExtension = AutoFillExtension::Create(runtime);
EXPECT_NE(autoFillExtension, nullptr);
auto abilityInfo = std::make_shared<AppExecFwk::AbilityInfo>();
EXPECT_NE(abilityInfo, nullptr);
abilityInfo->applicationInfo.multiProjects = true;
abilityInfo->moduleName = MODULE_NAME;
abilityInfo->bundleName = BUNDLE_NAME;
sptr<IRemoteObject> token = new AppExecFwk::MockAbilityToken();
EXPECT_NE(token, nullptr);
auto record = std::make_shared<AppExecFwk::AbilityLocalRecord>(abilityInfo, token);
EXPECT_NE(record, nullptr);
auto application = std::make_shared<AppExecFwk::OHOSApplication>();
EXPECT_NE(application, nullptr);
auto contextImpl = std::make_shared<ContextImpl>();
auto applicationContext = ApplicationContext::GetInstance();
applicationContext->AttachContextImpl(contextImpl);
application->SetApplicationContext(applicationContext);
auto handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
EXPECT_NE(handler, nullptr);
auto context = autoFillExtension->CreateAndInitContext(record, application, handler, token);
EXPECT_NE(context, nullptr);
EXPECT_EQ(context->GetToken(), token);
auto appContext = Context::GetApplicationContext();
auto appInfo = appContext->GetApplicationInfo();
EXPECT_EQ(context->GetApplicationInfo(), appInfo);
auto resourceManager = appContext->GetResourceManager();
EXPECT_EQ(context->GetResourceManager(), resourceManager);
EXPECT_EQ(context->parentContext_, appContext);
EXPECT_EQ(context->GetAbilityInfo(), abilityInfo);
EXPECT_EQ(context->GetConfiguration(), appContext->GetConfiguration());
GTEST_LOG_(INFO) << "CreateAndInitContext_0100 end";
}
/**
* @tc.name: CreateAndInitContext_0200
* @tc.desc: Check nullptr AbilityLocalRecord.
* @tc.type: FUNC
*/
HWTEST_F(AutoFillExtensionTest, CreateAndInitContext_0200, Function | MediumTest | Level1)
{
GTEST_LOG_(INFO) << "CreateAndInitContext_0200 start";
Runtime::Options options;
std::unique_ptr<Runtime> runtime = Runtime::Create(options);
auto autoFillExtension = AutoFillExtension::Create(runtime);
EXPECT_NE(autoFillExtension, nullptr);
auto abilityInfo = std::make_shared<AppExecFwk::AbilityInfo>();
EXPECT_NE(abilityInfo, nullptr);
abilityInfo->moduleName = MODULE_NAME;
abilityInfo->bundleName = BUNDLE_NAME;
sptr<IRemoteObject> token = new AppExecFwk::MockAbilityToken();
EXPECT_NE(token, nullptr);
auto application = std::make_shared<AppExecFwk::OHOSApplication>();
EXPECT_NE(application, nullptr);
auto contextImpl = std::make_shared<ContextImpl>();
auto applicationContext = ApplicationContext::GetInstance();
applicationContext->AttachContextImpl(contextImpl);
application->SetApplicationContext(applicationContext);
auto handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
EXPECT_NE(handler, nullptr);
std::shared_ptr<AppExecFwk::AbilityLocalRecord> record = nullptr;
auto context = autoFillExtension->CreateAndInitContext(record, application, handler, token);
EXPECT_NE(context, nullptr);
EXPECT_EQ(context->GetToken(), token);
auto appContext = Context::GetApplicationContext();
auto appInfo = appContext->GetApplicationInfo();
EXPECT_EQ(context->GetApplicationInfo(), appInfo);
auto resourceManager = appContext->GetResourceManager();
EXPECT_EQ(context->GetResourceManager(), resourceManager);
EXPECT_EQ(context->parentContext_, appContext);
EXPECT_EQ(context->GetAbilityInfo(), nullptr);
EXPECT_EQ(context->GetConfiguration(), nullptr);
GTEST_LOG_(INFO) << "CreateAndInitContext_0200 end";
}
} // namespace AbilityRuntime
} // namespace OHOS
@@ -0,0 +1,79 @@
# Copyright (c) 2023 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/auto_fill_extension"
ohos_unittest("js_auto_fill_extension_test") {
module_out_path = module_output_path
include_dirs = []
include_dirs = [
"${ability_runtime_innerkits_path}/app_manager/include/appmgr",
"${ability_runtime_native_path}/runtime",
"${ability_runtime_native_path}/runtime/utils/include",
"${ability_runtime_path}/interfaces/kits/native/ability/ability_runtime",
"${ability_runtime_path}/interfaces/kits/native/ability/native/auto_fill_extension_ability",
"${ability_runtime_path}/interfaces/kits/native/appkit/app",
"${ability_runtime_test_path}/mock/frameworks_kits_ability_native_test/include",
"//third_party/jsoncpp/include",
"//third_party/json/include",
]
sources = [ "js_auto_fill_extension_test.cpp" ]
configs = [ "${ability_runtime_services_path}/abilitymgr:abilityms_config" ]
deps = [
"${ability_runtime_native_path}/ability/native:abilitykit_native",
"${ability_runtime_native_path}/ability/native:auto_fill_extension",
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
"//third_party/icu/icu4c:shared_icuuc",
]
external_deps = [
"ability_base:base",
"ability_base:configuration",
"ability_base:view_data",
"ability_base:want",
"ability_runtime:ability_manager",
"ability_runtime:app_context",
"ability_runtime:appkit_native",
"ability_runtime:extensionkit_native",
"ability_runtime:runtime",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"c_utils:utils",
"ets_runtime:libark_jsruntime",
"eventhandler:libeventhandler",
"hilog:libhilog",
"ipc:ipc_core",
"napi:ace_napi",
"resource_management:global_resmgr",
]
if (ability_runtime_graphics) {
external_deps += [
"image_framework:image",
"window_manager:libdm",
"window_manager:libwm",
]
}
}
group("unittest") {
testonly = true
deps = [ ":js_auto_fill_extension_test" ]
}
@@ -0,0 +1,177 @@
/*
* Copyright (c) 2023 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 <gtest/hwext/gtest-multithread.h>
#include "ability_handler.h"
#include "ability_local_record.h"
#include "auto_fill_extension_context.h"
#include "configuration_convertor.h"
#define private public
#define protected public
#include "configuration_utils.h"
#undef private
#undef protected
#include "hilog_wrapper.h"
#include "iremote_object.h"
#define private public
#define protected public
#include "js_auto_fill_extension.h"
#include "js_auto_fill_extension_context.h"
#undef private
#undef protected
#include "js_runtime.h"
#include "mock_ability_token.h"
#include "ohos_application.h"
#include "runtime.h"
#include "string_wrapper.h"
#include "want.h"
#ifdef SUPPORT_GRAPHICS
#include "locale_config.h"
#include "window_scene.h"
#endif
using namespace testing;
using namespace testing::ext;
using namespace testing::mt;
namespace OHOS {
namespace AbilityRuntime {
namespace {
constexpr char JS_AUTO_FILL_EXTENSION_TASK_RUNNER[] = "JsAutoFillExtension";
constexpr char DEFAULT_LANGUAGE[] = "zh_CN";
} // namespace
class JsAutoFillExtensionTest : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp() override;
void TearDown() override;
std::shared_ptr<JsAutoFillExtension> jsAutoFillExtension_ = nullptr;
std::shared_ptr<ApplicationContext> applicationContext_ = nullptr;
std::unique_ptr<Runtime> jsRuntime_ = nullptr;
private:
void CreateJsAutoFillExtension();
};
void JsAutoFillExtensionTest::SetUpTestCase()
{}
void JsAutoFillExtensionTest::TearDownTestCase()
{}
void JsAutoFillExtensionTest::SetUp()
{
CreateJsAutoFillExtension();
}
void JsAutoFillExtensionTest::TearDown()
{}
void JsAutoFillExtensionTest::CreateJsAutoFillExtension()
{
std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
sptr<IRemoteObject> token(new (std::nothrow) MockAbilityToken());
std::shared_ptr<AbilityLocalRecord> record = std::make_shared<AbilityLocalRecord>(abilityInfo, token);
std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
Configuration config;
config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, DEFAULT_LANGUAGE);
config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, ConfigurationInner::COLOR_MODE_LIGHT);
contextImpl->SetConfiguration(std::make_shared<Configuration>(config));
std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager());
std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
#ifdef SUPPORT_GRAPHICS
UErrorCode status = U_ZERO_ERROR;
icu::Locale locale = icu::Locale::forLanguageTag("zh", status);
HILOG_INFO("language: %{public}s, script: %{public}s, region: %{public}s", locale.getLanguage(),
locale.getScript(), locale.getCountry());
resConfig->SetLocaleInfo(locale);
#endif
Global::Resource::RState updateRet = resourceManager->UpdateResConfig(*resConfig);
if (updateRet != Global::Resource::RState::SUCCESS) {
HILOG_ERROR("Init locale failed.");
}
contextImpl->SetResourceManager(resourceManager);
std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
AbilityRuntime::ApplicationContext::GetInstance();
applicationContext->AttachContextImpl(contextImpl);
application->SetApplicationContext(applicationContext);
applicationContext_ = applicationContext;
std::shared_ptr<AbilityHandler> handler = std::make_shared<AbilityHandler>(nullptr);
auto eventRunner = AppExecFwk::EventRunner::Create(JS_AUTO_FILL_EXTENSION_TASK_RUNNER);
Runtime::Options options;
options.preload = true;
options.eventRunner = eventRunner;
jsRuntime_ = JsRuntime::Create(options);
ASSERT_NE(jsRuntime_, nullptr);
JsAutoFillExtension *extension = JsAutoFillExtension::Create(jsRuntime_);
ASSERT_NE(extension, nullptr);
jsAutoFillExtension_.reset(extension);
jsAutoFillExtension_->Init(record, application, handler, token);
}
/**
* @tc.name: Configuration_0100
* @tc.desc: Js auto fill extension init.
* @tc.type: FUNC
*/
HWTEST_F(JsAutoFillExtensionTest, Init_0100, TestSize.Level1)
{
ASSERT_NE(jsAutoFillExtension_, nullptr);
ASSERT_NE(applicationContext_, nullptr);
auto context = jsAutoFillExtension_->GetContext();
ASSERT_NE(context, nullptr);
auto configuration = context->GetConfiguration();
EXPECT_NE(configuration, nullptr);
auto appConfig = applicationContext_->GetConfiguration();
EXPECT_NE(appConfig, nullptr);
}
/**
* @tc.name: OnStart_0100
* @tc.desc: Js auto fill extension OnStart.
* @tc.type: FUNC
*/
HWTEST_F(JsAutoFillExtensionTest, OnStart_0100, TestSize.Level1)
{
Want want;
ASSERT_NE(jsAutoFillExtension_, nullptr);
jsAutoFillExtension_->OnStart(want);
auto context = jsAutoFillExtension_->GetContext();
ASSERT_NE(context, nullptr);
auto configuration = context->GetConfiguration();
EXPECT_NE(configuration, nullptr);
auto resourceManager = context->GetResourceManager();
EXPECT_NE(resourceManager, nullptr);
auto appConfig = applicationContext_->GetConfiguration();
EXPECT_NE(appConfig, nullptr);
}
} // namespace AbilityRuntime
} // namespace OHOS