Description:Add tdd to js env init.

Sig:SIG_ApplicationFramework
Feature or BugFix: Feature
Binary Source: No

Signed-off-by: zhangyafei.echo <zhangyafei12@huawei.com>
Change-Id: I39875b27062cae6652585acd3bde107484b5ce20
This commit is contained in:
zhangyafei.echo 2023-04-04 17:28:11 +08:00
parent bfb52ca3cf
commit 2b4c04efd4
2 changed files with 80 additions and 0 deletions

View File

@ -16,15 +16,18 @@
#include "js_environment.h"
#include <gtest/gtest.h>
#include <gtest/hwext/gtest-multithread.h>
#include <cstdarg>
#include <string>
#include "ecmascript/napi/include/jsnapi.h"
#include "js_env_logger.h"
#include "ohos_js_env_logger.h"
#include "ohos_js_environment_impl.h"
using namespace testing;
using namespace testing::ext;
using namespace testing::mt;
namespace OHOS {
namespace JsEnv {
@ -60,6 +63,8 @@ HWTEST_F(JsEnvironmentTest, JsEnvInitialize_0100, TestSize.Level0)
{
auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
ASSERT_NE(jsEnv, nullptr);
ASSERT_EQ(jsEnv->GetVM(), nullptr);
ASSERT_EQ(jsEnv->GetNativeEngine(), nullptr);
panda::RuntimeOption pandaOption;
auto ret = jsEnv->Initialize(pandaOption, static_cast<void*>(this));
@ -72,6 +77,30 @@ HWTEST_F(JsEnvironmentTest, JsEnvInitialize_0100, TestSize.Level0)
EXPECT_NE(nativeEngine, nullptr);
}
/**
* @tc.name: JsEnvInitialize_0200
* @tc.desc: Initialize js environment in multi thread.
* @tc.type: FUNC
* @tc.require: issueI6KODF
*/
HWTEST_F(JsEnvironmentTest, JsEnvInitialize_0200, TestSize.Level0)
{
JSENV_LOG_I("Running in multi-thread, using default thread number.");
auto task = []() {
JSENV_LOG_I("Running in thread %{public}" PRIu64 "", gettid());
auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
ASSERT_NE(jsEnv, nullptr);
panda::RuntimeOption pandaOption;
ASSERT_EQ(jsEnv->Initialize(pandaOption, nullptr), true);
EXPECT_NE(jsEnv->GetVM(), nullptr);
EXPECT_NE(jsEnv->GetNativeEngine(), nullptr);
};
GTEST_RUN_TASK(task);
}
/**
* @tc.name: LoadScript_0100
* @tc.desc: load script with invalid engine.

View File

@ -14,6 +14,7 @@
*/
#include <gtest/gtest.h>
#include <gtest/hwext/gtest-multithread.h>
#define private public
#define protected public
@ -29,6 +30,7 @@
using namespace testing;
using namespace testing::ext;
using namespace testing::mt;
namespace OHOS {
namespace AbilityRuntime {
@ -580,5 +582,54 @@ HWTEST_F(JsRuntimeTest, JsRuntimeUpdateModuleNameAndAssetPathTest_0100, TestSize
HILOG_INFO("UpdateModuleNameAndAssetPath end");
}
/**
* @tc.name: JsRuntimeInitialize_0100
* @tc.desc: Initialize js runtime in multi thread.
* @tc.type: FUNC
* @tc.require: issueI6KODF
*/
HWTEST_F(JsRuntimeTest, JsRuntimeInitialize_0100, TestSize.Level0)
{
HILOG_INFO("Running in multi-thread, using default thread number.");
auto task = []() {
HILOG_INFO("Running in thread %{public}d", gettid());
AbilityRuntime::Runtime::Options options;
options.loadAce = false;
options.preload = false;
options.isStageModel = false;
auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
ASSERT_NE(jsRuntime, nullptr);
EXPECT_NE(jsRuntime->GetEcmaVm(), nullptr);
EXPECT_NE(jsRuntime->GetNativeEnginePointer(), nullptr);
};
GTEST_RUN_TASK(task);
}
/**
* @tc.name: JsRuntimeInitialize_0200
* @tc.desc: preload js runtime.
* @tc.type: FUNC
* @tc.require: issueI6KODF
*/
HWTEST_F(JsRuntimeTest, JsRuntimeInitialize_0200, TestSize.Level0)
{
AbilityRuntime::Runtime::Options options;
options.preload = true;
auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
ASSERT_NE(jsRuntime, nullptr);
EXPECT_NE(jsRuntime->GetEcmaVm(), nullptr);
EXPECT_NE(jsRuntime->GetNativeEnginePointer(), nullptr);
options.preload = false;
jsRuntime = AbilityRuntime::JsRuntime::Create(options);
ASSERT_NE(jsRuntime, nullptr);
EXPECT_NE(jsRuntime->GetEcmaVm(), nullptr);
EXPECT_NE(jsRuntime->GetNativeEnginePointer(), nullptr);
}
} // namespace AbilityRuntime
} // namespace OHOS