From 53eb17c306002f66a4fec991e9a4b8e061108f2f Mon Sep 17 00:00:00 2001 From: zhangzezhong Date: Wed, 11 Jun 2025 20:09:32 +0800 Subject: [PATCH] add JsStartupConfig unittest cases for ability_runtime Signed-off-by: zhangzezhong --- test/unittest/BUILD.gn | 1 + test/unittest/app_start/BUILD.gn | 18 + .../app_start/js_startup_config_test/BUILD.gn | 86 ++++ .../js_startup_config_test.cpp | 469 ++++++++++++++++++ .../app_start/mock/mock_js_runtime.cpp | 98 ++++ test/unittest/app_start/mock/mock_my_flag.cpp | 26 + test/unittest/app_start/mock/mock_my_flag.h | 31 ++ 7 files changed, 729 insertions(+) create mode 100644 test/unittest/app_start/BUILD.gn create mode 100644 test/unittest/app_start/js_startup_config_test/BUILD.gn create mode 100644 test/unittest/app_start/js_startup_config_test/js_startup_config_test.cpp create mode 100644 test/unittest/app_start/mock/mock_js_runtime.cpp create mode 100644 test/unittest/app_start/mock/mock_my_flag.cpp create mode 100644 test/unittest/app_start/mock/mock_my_flag.h diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 66264662c4d..06920ebec76 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -474,6 +474,7 @@ group("unittest") { "window_options_utils_test:unittest", "window_visibility_changed_listener_test:unittest", "../new_test/unit_test:unittest", + "app_start:unittest", ] if (ability_runtime_auto_fill) { diff --git a/test/unittest/app_start/BUILD.gn b/test/unittest/app_start/BUILD.gn new file mode 100644 index 00000000000..c2d2ca49a12 --- /dev/null +++ b/test/unittest/app_start/BUILD.gn @@ -0,0 +1,18 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +group("unittest") { + testonly = true + + deps = [ "js_startup_config_test:unittest" ] +} diff --git a/test/unittest/app_start/js_startup_config_test/BUILD.gn b/test/unittest/app_start/js_startup_config_test/BUILD.gn new file mode 100644 index 00000000000..abdaff26714 --- /dev/null +++ b/test/unittest/app_start/js_startup_config_test/BUILD.gn @@ -0,0 +1,86 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +ohos_unittest("js_startup_config_test") { + module_out_path = "ability_runtime/ability_runtime/app_start" + + cflags_cc = [] + + include_dirs = [ + "${ability_runtime_path}/interfaces/kits/native/appkit/app_startup", + "${ability_runtime_path}/test/unittest/app_start/mock", + "${ability_runtime_path}/frameworks/native/runtime", + ] + + sources = [ + "${ability_runtime_path}/test/unittest/app_start/mock/mock_js_runtime.cpp", + "${ability_runtime_path}/test/unittest/app_start/mock/mock_my_flag.cpp", + "js_startup_config_test.cpp", + ] + + configs = [ "${ability_runtime_services_path}/abilitymgr:abilityms_config" ] + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + deps = [ + "${ability_runtime_abilitymgr_path}/:abilityms", + "${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_napi_path}/inner/napi_common:napi_common", + "${ability_runtime_native_path}/ability/native:ability_thread", + "${ability_runtime_native_path}/ability/native:abilitykit_native", + "${ability_runtime_native_path}/ability/native:configuration_helper", + "${ability_runtime_native_path}/ability/native:extensionkit_native", + "${ability_runtime_native_path}/ability/native:uiabilitykit_native", + "${ability_runtime_native_path}/appkit:app_context", + "${ability_runtime_native_path}/appkit:app_context_utils", + "${ability_runtime_native_path}/appkit:appkit_manager_helper", + "${ability_runtime_native_path}/appkit:appkit_native", + "${ability_runtime_path}/js_environment/frameworks/js_environment:js_environment", + "${ability_runtime_path}/utils/global/freeze:freeze_util", + "${ability_runtime_services_path}/common:app_util", + "${ability_runtime_services_path}/common:event_report", + ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_deps_wrapper", + "ability_runtime:ability_manager", + "ability_runtime:js_environment", + "bundle_framework:libappexecfwk_common", + "c_utils:utils", + "ffrt:libffrt", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "napi:ace_napi", + ] +} + +group("unittest") { + testonly = true + deps = [ ":js_startup_config_test" ] +} diff --git a/test/unittest/app_start/js_startup_config_test/js_startup_config_test.cpp b/test/unittest/app_start/js_startup_config_test/js_startup_config_test.cpp new file mode 100644 index 00000000000..10624bdf10b --- /dev/null +++ b/test/unittest/app_start/js_startup_config_test/js_startup_config_test.cpp @@ -0,0 +1,469 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#define private public +#define protected public +#include "js_startup_config.h" +#include "js_environment.h" +#include "startup_task_result.h" +#undef private +#undef protected +#include "want_params.h" +#include "string_wrapper.h" +#include "napi_common_execute_result.h" +#include "napi_common_util.h" +#include "napi_common_want.h" +#include "native_reference.h" +#include "js_runtime.h" +#include "mock_my_flag.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace AbilityRuntime { +napi_value NapiTestFunc(napi_env env, napi_callback_info info) +{ + return nullptr; +} +namespace { +constexpr int32_t ERR_OK = 0; +constexpr int32_t DEFAULT_AWAIT_TIMEOUT_MS = 10000; +const std::string DEFAULT_CUSTOMIZATION = "default"; +const std::string INTERNAL_ERROR = "internal error."; +const std::string RESULT_MESSAGE = "result message."; +const int32_t TEST_VALUE = 123; +} +class JsStartupConfigTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; +}; + +static std::shared_ptr jsRuntime_ = nullptr; +class NativeReferenceMock : public NativeReference { +public: + static std::string propertyName_; + NativeReferenceMock() = default; + virtual ~NativeReferenceMock() = default; + MOCK_METHOD0(Ref, uint32_t()); + MOCK_METHOD0(Unref, uint32_t()); + MOCK_METHOD0(Get, napi_value()); + MOCK_METHOD0(GetData, void*()); + virtual operator napi_value() override + { + return reinterpret_cast(this); + } + MOCK_METHOD0(SetDeleteSelf, void()); + MOCK_METHOD0(GetRefCount, uint32_t()); + MOCK_METHOD0(GetFinalRun, bool()); + napi_value GetNapiValue() override + { + if (MyFlag::isGetNapiValueNullptr_) { + return nullptr; + } + MyFlag::isGetNapiEnvNullptr_ = false; + napi_env env = jsRuntime_->GetNapiEnv(); + napi_value object = AppExecFwk::CreateJSObject(env); + + napi_value value; + napi_create_int32(env, TEST_VALUE, &value); + napi_set_named_property(env, object, propertyName_.c_str(), value); + return object; + return nullptr; + } +}; +std::string NativeReferenceMock::propertyName_ = ""; + +void JsStartupConfigTest::SetUpTestCase(void) +{ + jsRuntime_ = std::make_shared(); +} + +void JsStartupConfigTest::TearDownTestCase(void) +{} + +void JsStartupConfigTest::SetUp() +{} + +void JsStartupConfigTest::TearDown() +{} + +/* +* Feature: JsInsightIntentEntry +* Function: Init +* SubFunction: NA +*/ +HWTEST_F(JsStartupConfigTest, JsInsightIntentEntryInit_001, TestSize.Level1) +{ + auto jsStartupConfig = std::make_shared(nullptr); + MyFlag::isGetNapiValueNullptr_ = true; + auto want = std::make_shared(); + std::unique_ptr ptr = nullptr; + auto res = jsStartupConfig->Init(ptr, want); + EXPECT_EQ(res, ERR_STARTUP_INTERNAL_ERROR); +} + +/* +* Feature: JsInsightIntentEntry +* Function: Init +* SubFunction: NA +*/ +HWTEST_F(JsStartupConfigTest, JsInsightIntentEntryInit_002, TestSize.Level1) +{ + auto jsRuntime = std::make_shared(); + MyFlag::isGetNapiValueNullptr_ = true; + auto jsStartupConfig = std::make_shared(nullptr); + auto want = std::make_shared(); + std::unique_ptr ptr = std::make_unique(); + auto res = jsStartupConfig->Init(ptr, want); + EXPECT_EQ(res, ERR_STARTUP_INTERNAL_ERROR); +} + +/* +* Feature: JsInsightIntentEntry +* Function: Init +* SubFunction: NA +*/ +HWTEST_F(JsStartupConfigTest, JsInsightIntentEntryInit_003, TestSize.Level1) +{ + MyFlag::isGetNapiEnvNullptr_ = false; + MyFlag::isGetNapiValueNullptr_ = false; + panda::RuntimeOption pandaOption; + jsRuntime_->jsEnv_->Initialize(pandaOption, static_cast(this)); + auto jsStartupConfig = std::make_shared(jsRuntime_->GetNapiEnv()); + auto want = std::make_shared(); + std::unique_ptr ptr = std::make_unique(); + NativeReferenceMock::propertyName_ = "onConfig"; + auto res = jsStartupConfig->Init(ptr, want); + EXPECT_EQ(res, ERR_STARTUP_INTERNAL_ERROR); +} + +/* +* Feature: JsInsightIntentEntry +* Function: Init +* SubFunction: NA +*/ +HWTEST_F(JsStartupConfigTest, JsInsightIntentEntryInit_004, TestSize.Level1) +{ + auto jsRuntime = std::make_shared(); + MyFlag::isGetNapiValueNullptr_ = true; + MyFlag::isGetNapiEnvNullptr_ = false; + panda::RuntimeOption pandaOption; + jsRuntime = std::make_shared(); + jsRuntime->jsEnv_->Initialize(pandaOption, static_cast(this)); + auto jsStartupConfig = std::make_shared(jsRuntime->GetNapiEnv()); + napi_value object = AppExecFwk::CreateJSObject(jsRuntime->GetNapiEnv()); + auto res = jsStartupConfig->Init(object); + EXPECT_EQ(res, ERR_OK); +} + +/* +* Feature: JsInsightIntentEntry +* Function: InitAwaitTimeout +* SubFunction: NA +*/ +HWTEST_F(JsStartupConfigTest, JsInsightIntentEntryInitAwaitTimeout_001, TestSize.Level1) +{ + auto jsRuntime = std::make_shared(); + MyFlag::isGetNapiEnvNullptr_ = false; + panda::RuntimeOption pandaOption; + jsRuntime = std::make_shared(); + jsRuntime->jsEnv_->Initialize(pandaOption, static_cast(this)); + + auto jsStartupConfig = std::make_shared(nullptr); + jsStartupConfig->awaitTimeoutMs_ = DEFAULT_AWAIT_TIMEOUT_MS - 1; + napi_value object = AppExecFwk::CreateJSObject(jsRuntime->GetNapiEnv()); + jsStartupConfig->InitAwaitTimeout(jsRuntime->GetNapiEnv(), object); + EXPECT_EQ(jsStartupConfig->awaitTimeoutMs_, DEFAULT_AWAIT_TIMEOUT_MS - 1); +} + +/* +* Feature: JsInsightIntentEntry +* Function: InitAwaitTimeout +* SubFunction: NA +*/ +HWTEST_F(JsStartupConfigTest, JsInsightIntentEntryInitAwaitTimeout_002, TestSize.Level1) +{ + auto jsRuntime = std::make_shared(); + MyFlag::isGetNapiEnvNullptr_ = false; + panda::RuntimeOption pandaOption; + jsRuntime = std::make_shared(); + jsRuntime->jsEnv_->Initialize(pandaOption, static_cast(this)); + + auto jsStartupConfig = std::make_shared(nullptr); + jsStartupConfig->awaitTimeoutMs_ = 0; + napi_value object = AppExecFwk::CreateJSObject(jsRuntime->GetNapiEnv()); + napi_value value; + napi_create_int32(jsRuntime->GetNapiEnv(), -1, &value); + napi_set_named_property(jsRuntime->GetNapiEnv(), object, "timeoutMs", value); + + jsStartupConfig->InitAwaitTimeout(jsRuntime->GetNapiEnv(), object); + EXPECT_EQ(jsStartupConfig->awaitTimeoutMs_, DEFAULT_AWAIT_TIMEOUT_MS); +} + +/* +* Feature: JsInsightIntentEntry +* Function: InitListener +* SubFunction: NA +*/ +HWTEST_F(JsStartupConfigTest, JsInsightIntentEntryInitListener_001, TestSize.Level1) +{ + auto jsRuntime = std::make_shared(); + MyFlag::isGetNapiEnvNullptr_ = false; + panda::RuntimeOption pandaOption; + jsRuntime = std::make_shared(); + jsRuntime->jsEnv_->Initialize(pandaOption, static_cast(this)); + + auto jsStartupConfig = std::make_shared(nullptr); + jsStartupConfig->listener_ = nullptr; + + jsStartupConfig->InitListener(jsRuntime->GetNapiEnv(), nullptr); + EXPECT_EQ(jsStartupConfig->listener_, nullptr); +} + +/* +* Feature: JsInsightIntentEntry +* Function: InitListener +* SubFunction: NA +*/ +HWTEST_F(JsStartupConfigTest, JsInsightIntentEntryInitListener_002, TestSize.Level1) +{ + auto jsRuntime = std::make_shared(); + MyFlag::isGetNapiEnvNullptr_ = false; + panda::RuntimeOption pandaOption; + jsRuntime = std::make_shared(); + jsRuntime->jsEnv_->Initialize(pandaOption, static_cast(this)); + + auto jsStartupConfig = std::make_shared(nullptr); + jsStartupConfig->listener_ = nullptr; + napi_value object = AppExecFwk::CreateJSObject(jsRuntime->GetNapiEnv()); + napi_value value; + napi_create_int32(jsRuntime->GetNapiEnv(), 0, &value); + napi_set_named_property(jsRuntime->GetNapiEnv(), object, "startupListener", value); + + jsStartupConfig->InitListener(jsRuntime->GetNapiEnv(), object); + EXPECT_EQ(jsStartupConfig->listener_, nullptr); +} + +/* +* Feature: JsInsightIntentEntry +* Function: InitListener +* SubFunction: NA +*/ +HWTEST_F(JsStartupConfigTest, JsInsightIntentEntryInitListener_003, TestSize.Level1) +{ + auto jsRuntime = std::make_shared(); + MyFlag::isGetNapiEnvNullptr_ = false; + panda::RuntimeOption pandaOption; + jsRuntime = std::make_shared(); + jsRuntime->jsEnv_->Initialize(pandaOption, static_cast(this)); + + auto jsStartupConfig = std::make_shared(nullptr); + jsStartupConfig->listener_ = nullptr; + napi_value object = AppExecFwk::CreateJSObject(jsRuntime->GetNapiEnv()); + napi_value listener = AppExecFwk::CreateJSObject(jsRuntime->GetNapiEnv()); + napi_value value; + napi_create_int32(jsRuntime->GetNapiEnv(), 0, &value); + napi_set_named_property(jsRuntime->GetNapiEnv(), listener, "onCompleted", value); + napi_set_named_property(jsRuntime->GetNapiEnv(), object, "startupListener", listener); + + jsStartupConfig->InitListener(jsRuntime->GetNapiEnv(), object); + EXPECT_NE(jsStartupConfig->listener_, nullptr); +} + +/* +* Feature: JsInsightIntentEntry +* Function: InitCustomization +* SubFunction: NA +*/ +HWTEST_F(JsStartupConfigTest, JsInsightIntentEntryInitCustomization_001, TestSize.Level1) +{ + auto jsRuntime = std::make_shared(); + MyFlag::isGetNapiEnvNullptr_ = false; + panda::RuntimeOption pandaOption; + jsRuntime = std::make_shared(); + jsRuntime->jsEnv_->Initialize(pandaOption, static_cast(this)); + + auto jsStartupConfig = std::make_shared(nullptr); + jsStartupConfig->customization_ = DEFAULT_CUSTOMIZATION; + + jsStartupConfig->InitCustomization(jsRuntime->GetNapiEnv(), nullptr, nullptr); + EXPECT_EQ(jsStartupConfig->customization_, DEFAULT_CUSTOMIZATION); +} + +/* +* Feature: JsInsightIntentEntry +* Function: InitCustomization +* SubFunction: NA +*/ +HWTEST_F(JsStartupConfigTest, JsInsightIntentEntryInitCustomization_002, TestSize.Level1) +{ + auto jsRuntime = std::make_shared(); + MyFlag::isGetNapiEnvNullptr_ = false; + panda::RuntimeOption pandaOption; + jsRuntime = std::make_shared(); + jsRuntime->jsEnv_->Initialize(pandaOption, static_cast(this)); + + auto jsStartupConfig = std::make_shared(nullptr); + jsStartupConfig->customization_ = DEFAULT_CUSTOMIZATION; + napi_value configEntry = AppExecFwk::CreateJSObject(jsRuntime->GetNapiEnv()); + auto want = std::make_shared(); + + jsStartupConfig->InitCustomization(jsRuntime->GetNapiEnv(), configEntry, want); + EXPECT_EQ(jsStartupConfig->customization_, DEFAULT_CUSTOMIZATION); +} + +/* +* Feature: JsInsightIntentEntry +* Function: InitCustomization +* SubFunction: NA +*/ +HWTEST_F(JsStartupConfigTest, JsInsightIntentEntryInitCustomization_003, TestSize.Level1) +{ + auto jsRuntime = std::make_shared(); + MyFlag::isGetNapiEnvNullptr_ = false; + panda::RuntimeOption pandaOption; + jsRuntime = std::make_shared(); + jsRuntime->jsEnv_->Initialize(pandaOption, static_cast(this)); + + auto jsStartupConfig = std::make_shared(nullptr); + jsStartupConfig->customization_ = DEFAULT_CUSTOMIZATION; + + napi_value configEntry = AppExecFwk::CreateJSObject(jsRuntime->GetNapiEnv()); + napi_value value; + napi_create_int32(jsRuntime->GetNapiEnv(), 0, &value); + napi_set_named_property(jsRuntime->GetNapiEnv(), configEntry, "onRequestCustomMatchRule", value); + + auto want = std::make_shared(); + + jsStartupConfig->InitCustomization(jsRuntime->GetNapiEnv(), configEntry, want); + EXPECT_EQ(jsStartupConfig->customization_, DEFAULT_CUSTOMIZATION); +} + +/* +* Feature: JsInsightIntentEntry +* Function: InitCustomization +* SubFunction: NA +*/ +HWTEST_F(JsStartupConfigTest, JsInsightIntentEntryInitCustomization_004, TestSize.Level1) +{ + auto jsRuntime = std::make_shared(); + MyFlag::isGetNapiEnvNullptr_ = false; + panda::RuntimeOption pandaOption; + jsRuntime = std::make_shared(); + jsRuntime->jsEnv_->Initialize(pandaOption, static_cast(this)); + + auto jsStartupConfig = std::make_shared(nullptr); + jsStartupConfig->customization_ = DEFAULT_CUSTOMIZATION; + + napi_value configEntry = AppExecFwk::CreateJSObject(jsRuntime->GetNapiEnv()); + napi_value fn; + napi_create_function(jsRuntime->GetNapiEnv(), "test", 0, NapiTestFunc, NULL, &fn); + napi_set_named_property(jsRuntime->GetNapiEnv(), configEntry, "onRequestCustomMatchRule", fn); + auto want = std::make_shared(); + + jsStartupConfig->InitCustomization(jsRuntime->GetNapiEnv(), configEntry, want); + EXPECT_NE(jsStartupConfig->customization_, DEFAULT_CUSTOMIZATION); +} + +/* +* Feature: JsInsightIntentEntry +* Function: BuildResult +* SubFunction: NA +*/ +HWTEST_F(JsStartupConfigTest, JsInsightIntentEntryBuildResult_001, TestSize.Level1) +{ + auto jsRuntime = std::make_shared(); + MyFlag::isGetNapiEnvNullptr_ = false; + panda::RuntimeOption pandaOption; + jsRuntime = std::make_shared(); + jsRuntime->jsEnv_->Initialize(pandaOption, static_cast(this)); + + auto jsStartupConfig = std::make_shared(nullptr); + auto res = jsStartupConfig->BuildResult(jsRuntime->GetNapiEnv(), nullptr); + + napi_value messageProp; + napi_get_named_property(jsRuntime->GetNapiEnv(), res, "message", &messageProp); + char errorMessage[1024]; + memset_s(errorMessage, sizeof(errorMessage), 0, sizeof(errorMessage)); + size_t copied; + auto status = napi_get_value_string_utf8(jsRuntime->GetNapiEnv(), messageProp, errorMessage, + sizeof(errorMessage), &copied); + EXPECT_EQ(status, napi_ok); + std::string errStr(errorMessage); + EXPECT_EQ(copied, INTERNAL_ERROR.size()); + EXPECT_EQ(errStr, INTERNAL_ERROR); +} + +/* +* Feature: JsInsightIntentEntry +* Function: BuildResult +* SubFunction: NA +*/ +HWTEST_F(JsStartupConfigTest, JsInsightIntentEntryBuildResult_002, TestSize.Level1) +{ + auto jsRuntime = std::make_shared(); + MyFlag::isGetNapiEnvNullptr_ = false; + panda::RuntimeOption pandaOption; + jsRuntime = std::make_shared(); + jsRuntime->jsEnv_->Initialize(pandaOption, static_cast(this)); + + auto jsStartupConfig = std::make_shared(nullptr); + auto startupTaskResult = std::make_shared(); + startupTaskResult->resultCode_ = -1; + startupTaskResult->resultMessage_ = RESULT_MESSAGE; + auto res = jsStartupConfig->BuildResult(jsRuntime->GetNapiEnv(), startupTaskResult); + + napi_value messageProp; + napi_get_named_property(jsRuntime->GetNapiEnv(), res, "message", &messageProp); + char errorMessage[1024]; + memset_s(errorMessage, sizeof(errorMessage), 0, sizeof(errorMessage)); + size_t copied; + auto status = napi_get_value_string_utf8(jsRuntime->GetNapiEnv(), messageProp, errorMessage, + sizeof(errorMessage), &copied); + EXPECT_EQ(status, napi_ok); + std::string errStr(errorMessage); + EXPECT_EQ(copied, RESULT_MESSAGE.size()); + EXPECT_EQ(errStr, RESULT_MESSAGE); +} + +/* +* Feature: JsInsightIntentEntry +* Function: BuildResult +* SubFunction: NA +*/ +HWTEST_F(JsStartupConfigTest, JsInsightIntentEntryBuildResult_003, TestSize.Level1) +{ + auto jsRuntime = std::make_shared(); + MyFlag::isGetNapiEnvNullptr_ = false; + panda::RuntimeOption pandaOption; + jsRuntime = std::make_shared(); + jsRuntime->jsEnv_->Initialize(pandaOption, static_cast(this)); + + auto jsStartupConfig = std::make_shared(nullptr); + auto startupTaskResult = std::make_shared(); + startupTaskResult->resultCode_ = ERR_OK; + auto res = jsStartupConfig->BuildResult(jsRuntime->GetNapiEnv(), startupTaskResult); + napi_value messageProp; + EXPECT_NE(napi_get_named_property(jsRuntime->GetNapiEnv(), res, "message", &messageProp), napi_ok); +} +} // namespace AbilityRuntime +} // namespace OHOS diff --git a/test/unittest/app_start/mock/mock_js_runtime.cpp b/test/unittest/app_start/mock/mock_js_runtime.cpp new file mode 100644 index 00000000000..48c2e85bffd --- /dev/null +++ b/test/unittest/app_start/mock/mock_js_runtime.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mock_my_flag.h" +#include "js_runtime.h" +#include +#include "native_reference.h" +#include "ohos_js_environment_impl.h" +#include "js_environment.h" + +namespace OHOS { +namespace AbilityRuntime { +class NativeReferenceMock : public NativeReference { +public: + NativeReferenceMock() = default; + virtual ~NativeReferenceMock() = default; + MOCK_METHOD0(Ref, uint32_t()); + MOCK_METHOD0(Unref, uint32_t()); + MOCK_METHOD0(Get, napi_value()); + MOCK_METHOD0(GetData, void*()); + virtual operator napi_value() override + { + return reinterpret_cast(this); + } + MOCK_METHOD0(SetDeleteSelf, void()); + MOCK_METHOD0(GetRefCount, uint32_t()); + MOCK_METHOD0(GetFinalRun, bool()); + napi_value GetNapiValue() override + { + return nullptr; + } +}; + +napi_value VirtualFunc(napi_env env, napi_callback_info info) +{ + return nullptr; +} + +JsRuntime::JsRuntime() +{ + jsEnv_ = std::make_shared(std::make_unique()); +} + +JsRuntime::~JsRuntime() +{} + +std::unique_ptr JsRuntime::LoadSystemModuleByEngine( + napi_env env, const std::string& moduleName, const napi_value* argv, size_t argc) +{ + if (MyFlag::isLoadSystemModuleByEngine_) { + return std::make_unique(); + } + return nullptr; +} + +std::unique_ptr JsRuntime::LoadSystemModule( + const std::string& moduleName, const napi_value* argv, size_t argc) +{ + return nullptr; +} + +napi_value JsRuntime::GetExportObjectFromOhmUrl(const std::string &srcEntrance, const std::string &key) +{ + if (MyFlag::isGetExportObjectFromOhmUrlNullptr_) { + return nullptr; + } + napi_value fn; + napi_create_function(GetNapiEnv(), NULL, 0, VirtualFunc, NULL, &fn); + return fn; +} + +bool JsRuntime::ExecuteSecureWithOhmUrl(const std::string &moduleName, const std::string &hapPath, + const std::string &srcEntrance) +{ + return MyFlag::isExecuteSecureWithOhmUrl_; +} + +napi_env JsRuntime::GetNapiEnv() const +{ + if (MyFlag::isGetNapiEnvNullptr_) { + return nullptr; + } + return reinterpret_cast(jsEnv_->GetNativeEngine()); +} +} // namespace AbilityRuntime +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/app_start/mock/mock_my_flag.cpp b/test/unittest/app_start/mock/mock_my_flag.cpp new file mode 100644 index 00000000000..d03b28a833d --- /dev/null +++ b/test/unittest/app_start/mock/mock_my_flag.cpp @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mock_my_flag.h" + +namespace OHOS { +namespace AbilityRuntime { +bool MyFlag::isExecuteSecureWithOhmUrl_ = false; +bool MyFlag::isGetNapiEnvNullptr_ = false; +bool MyFlag::isLoadSystemModuleByEngine_ = false; +bool MyFlag::isGetExportObjectFromOhmUrlNullptr_ = false; +bool MyFlag::isGetNapiValueNullptr_ = false; +} // namespace AbilityRuntime +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/app_start/mock/mock_my_flag.h b/test/unittest/app_start/mock/mock_my_flag.h new file mode 100644 index 00000000000..b9291de64c3 --- /dev/null +++ b/test/unittest/app_start/mock/mock_my_flag.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MOCK_MY_FLAG_H +#define MOCK_MY_FLAG_H + +namespace OHOS { +namespace AbilityRuntime { +class MyFlag { +public: + static bool isExecuteSecureWithOhmUrl_; + static bool isGetNapiEnvNullptr_; + static bool isLoadSystemModuleByEngine_; + static bool isGetExportObjectFromOhmUrlNullptr_; + static bool isGetNapiValueNullptr_; +}; +} // namespace AbilityRuntime +} // namespace OHOS +#endif // MOCK_MY_FLAG_H \ No newline at end of file -- Gitee