From 0379bc5fdceb97574a1c344657ddc192524dad3a Mon Sep 17 00:00:00 2001 From: tantingting Date: Sat, 20 Jul 2024 14:56:33 +0800 Subject: [PATCH] add tdd Signed-off-by: tantingting --- bundle.json | 1 + cj_environment/test/unittest/BUILD.gn | 21 ++ .../unittest/cj_environment_test/BUILD.gn | 39 ++ .../cj_environment_test.cpp | 349 ++++++++++++++++++ 4 files changed, 410 insertions(+) create mode 100644 cj_environment/test/unittest/BUILD.gn create mode 100644 cj_environment/test/unittest/cj_environment_test/BUILD.gn create mode 100644 cj_environment/test/unittest/cj_environment_test/cj_environment_test.cpp diff --git a/bundle.json b/bundle.json index 2a5ef4c438..684d70d648 100644 --- a/bundle.json +++ b/bundle.json @@ -471,6 +471,7 @@ "//foundation/ability/ability_runtime/tools/test:moduletest", "//foundation/ability/ability_runtime/tools/test:systemtest", "//foundation/ability/ability_runtime/tools/test:unittest", + "//foundation/ability/ability_runtime/cj_environment/test/unittest:unittest", "//foundation/ability/ability_runtime/js_environment/test/unittest:unittest", "//foundation/ability/ability_runtime/service_router_framework:test_target" ] diff --git a/cj_environment/test/unittest/BUILD.gn b/cj_environment/test/unittest/BUILD.gn new file mode 100644 index 0000000000..0cd400b514 --- /dev/null +++ b/cj_environment/test/unittest/BUILD.gn @@ -0,0 +1,21 @@ +# 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/test.gni") +import("../../cj_environment.gni") + +group("unittest") { + testonly = true + + deps = [ "cj_environment_test:unittest" ] +} diff --git a/cj_environment/test/unittest/cj_environment_test/BUILD.gn b/cj_environment/test/unittest/cj_environment_test/BUILD.gn new file mode 100644 index 0000000000..f4b4aac4a7 --- /dev/null +++ b/cj_environment/test/unittest/cj_environment_test/BUILD.gn @@ -0,0 +1,39 @@ +# 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/test.gni") + +module_output_path = "ability_runtime/cj_environment" + +ohos_unittest("cj_environment_test") { + module_out_path = module_output_path + sources = [ "cj_environment_test.cpp" ] + sources += [ "cj_invoker.h" ] + + deps = [ + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_runtime:cj_environment", + "hilog:libhilog", + ] +} + +group("unittest") { + testonly = true + deps = [] + + deps += [ ":cj_environment_test" ] +} diff --git a/cj_environment/test/unittest/cj_environment_test/cj_environment_test.cpp b/cj_environment/test/unittest/cj_environment_test/cj_environment_test.cpp new file mode 100644 index 0000000000..3256a2e5c7 --- /dev/null +++ b/cj_environment/test/unittest/cj_environment_test/cj_environment_test.cpp @@ -0,0 +1,349 @@ +/* + * 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 +#include +#define private public +#define protected public +#include "cj_environment.h" +#include "cj_invoker.h" +#undef private +#undef protected + +using namespace testing; +using namespace testing::ext; +using namespace testing::mt; + +namespace OHOS { + +class CjEnvironmentTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; +}; + +void CjEnvironmentTest::SetUpTestCase() {} + +void CjEnvironmentTest::TearDownTestCase() {} + +void CjEnvironmentTest::SetUp() {} + +void CjEnvironmentTest::TearDown() {} + +void RegisterCJUncaughtExceptionHandlerTest(const CJUncaughtExceptionInfo &handle) {} + +/** + * @tc.name: CJEnvironment_GetInstance_0001 + * @tc.desc: JsRuntime test for UpdatePkgContextInfoJson. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, CJEnvironment_GetInstance_0100, TestSize.Level1) +{ + auto cJEnvironment = std::make_shared(); + CJEnvironment *ret = nullptr; + ret = cJEnvironment->GetInstance(); + EXPECT_NE(ret, nullptr); +} + +/** + * @tc.name: CJEnvironment_IsRuntimeStarted_0001 + * @tc.desc: JsRuntime test for IsRuntimeStarted. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, CJEnvironment_IsRuntimeStarted_0100, TestSize.Level1) +{ + auto cJEnvironment = std::make_shared(); + bool ret = cJEnvironment->IsRuntimeStarted(); + EXPECT_EQ(ret, false); +} + +/** + * @tc.name: CJEnvironment_SetSanitizerKindRuntimeVersion_0001 + * @tc.desc: JsRuntime test for SetSanitizerKindRuntimeVersion. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, CJEnvironment_SetSanitizerKindRuntimeVersion_0100, TestSize.Level1) +{ + auto cJEnvironment = std::make_shared(); + SanitizerKind kind = SanitizerKind::ASAN; + + cJEnvironment->SetSanitizerKindRuntimeVersion(kind); + EXPECT_NE(cJEnvironment->sanitizerKind_, SanitizerKind::NONE); +} + +/** + * @tc.name: CJEnvironment_InitCJAppNS_0001 + * @tc.desc: JsRuntime test for InitCJAppNS. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, CJEnvironment_InitCJAppNS_0100, TestSize.Level1) +{ + auto cJEnvironment = std::make_shared(); + std::string path = "ability_runtime/CjEnvironmentTest"; + + cJEnvironment->InitCJAppNS(path); + EXPECT_NE(cJEnvironment->cjAppNSName, nullptr); +} + +/** + * @tc.name: CJEnvironment_InitCJSDKNS_0001 + * @tc.desc: JsRuntime test for InitCJSDKNS. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, CJEnvironment_InitCJSDKNS_0100, TestSize.Level1) +{ + auto cJEnvironment = std::make_shared(); + std::string path = "ability_runtime/CjEnvironmentTest"; + cJEnvironment->InitCJSDKNS(path); + EXPECT_NE(cJEnvironment->cjAppNSName, nullptr); +} + +/** + * @tc.name: CJEnvironment_InitCJSysNS_0001 + * @tc.desc: JsRuntime test for InitCJSysNS. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, CJEnvironment_InitCJSysNS_0100, TestSize.Level1) +{ + auto cJEnvironment = std::make_shared(); + std::string path = "ability_runtime/CjEnvironmentTest"; + cJEnvironment->InitCJSysNS(path); + std::string getTempCjAppNSName = cJEnvironment->cjAppNSName; + EXPECT_NE(cJEnvironment->cjAppNSName, nullptr); +} + +/** + * @tc.name: CJEnvironment_InitCJChipSDKNS_0001 + * @tc.desc: JsRuntime test for InitCJChipSDKNS. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, CJEnvironment_InitCJChipSDKNS_0100, TestSize.Level1) +{ + auto cJEnvironment = std::make_shared(); + std::string path = "ability_runtime/CjEnvironmentTest"; + cJEnvironment->InitCJChipSDKNS(path); + EXPECT_NE(cJEnvironment->cjAppNSName, nullptr); +} + +/** + * @tc.name: CJEnvironment_StartRuntime_0001 + * @tc.desc: JsRuntime test for StartRuntime. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, CJEnvironment_StartRuntime_0100, TestSize.Level1) +{ + auto cJEnvironment = std::make_shared(); + + bool ret = cJEnvironment->StartRuntime(); + EXPECT_EQ(ret, false); + + cJEnvironment->isRuntimeStarted_ = true; + ret = cJEnvironment->StartRuntime(); + EXPECT_EQ(ret, true); +} + +/** + * @tc.name: CJEnvironment_StopRuntime_0001 + * @tc.desc: JsRuntime test for StopRuntime. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, CJEnvironment_StopRuntime_0100, TestSize.Level1) +{ + auto cJEnvironment = std::make_shared(); + cJEnvironment->StopRuntime(); + EXPECT_EQ(cJEnvironment->isRuntimeStarted_, false); + + cJEnvironment->isUISchedulerStarted_ = true; + EXPECT_EQ(cJEnvironment->isRuntimeStarted_, false); +} + +/** + * @tc.name: CJEnvironment_RegisterCJUncaughtExceptionHandler_0001 + * @tc.desc: JsRuntime test for RegisterCJUncaughtExceptionHandler. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, CJEnvironment_RegisterCJUncaughtExceptionHandler_0100, TestSize.Level1) +{ + // using RegisterUncaughtExceptionType = void (*)(const CJUncaughtExceptionInfo& handle); + CJEnvironment cJEnvironment; + CJUncaughtExceptionInfo handle; + handle.hapPath = "/test1/"; + handle.uncaughtTask = [](const char* summary, const CJErrorObject errorObj) {}; + + CJRuntimeAPI api { + .InitCJRuntime = nullptr, + .InitUIScheduler = nullptr, + .RunUIScheduler = nullptr, + .FiniCJRuntime = nullptr, + .InitCJLibrary = nullptr, + .RegisterEventHandlerCallbacks = nullptr, + .RegisterCJUncaughtExceptionHandler = RegisterCJUncaughtExceptionHandlerTest, + }; + + CJEnvironment::lazyApis_ = api; + + cJEnvironment.RegisterCJUncaughtExceptionHandler(handle); + + EXPECT_NE(cJEnvironment.lazyApis_.RegisterCJUncaughtExceptionHandler, nullptr); +} + +/** + * @tc.name: CJEnvironment_IsUISchedulerStarted_0001 + * @tc.desc: JsRuntime test for IsUISchedulerStarted. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, CJEnvironment_IsUISchedulerStarted_0100, TestSize.Level1) +{ + auto cJEnvironment = std::make_shared(); + bool ret = cJEnvironment->IsUISchedulerStarted(); + EXPECT_EQ(ret, false); +} + +/** + * @tc.name: StartUIScheduler_0100 + * @tc.desc: Test when isUISchedulerStarted_ is true. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, StartUIScheduler_0100, TestSize.Level1) +{ + auto cjEnv = std::make_shared(); + cjEnv->isUISchedulerStarted_ = true; + auto res = cjEnv->StartUIScheduler(); + EXPECT_EQ(res, true); +} + +/** + * @tc.name: StopUIScheduler_0100 + * @tc.desc: Test StopUIScheduler. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, StopUIScheduler_0100, TestSize.Level1) +{ + auto cjEnv = std::make_shared(); + EXPECT_NE(cjEnv, nullptr); + cjEnv->StopUIScheduler(); +} + +/** + * @tc.name: LoadCJLibrary_0100 + * @tc.desc: Test LoadCJLibrary. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, LoadCJLibrary_0100, TestSize.Level1) +{ + auto cjEnv = std::make_shared(); + char dlNames[5] = "Name"; + char* dlName = dlNames; + auto res = cjEnv->LoadCJLibrary(dlName); + EXPECT_EQ(res, nullptr); +} + +/** + * @tc.name: LoadCJLibrary_0200 + * @tc.desc: Test LoadCJLibrary. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, LoadCJLibrary_0200, TestSize.Level1) +{ + auto cjEnv = std::make_shared(); + CJEnvironment::LibraryKind kind = CJEnvironment::SYSTEM; + char dlNames[] = "Name"; + char* dlName = dlNames; + auto res = cjEnv->LoadCJLibrary(kind, dlName); + EXPECT_EQ(res, nullptr); +} + +/** + * @tc.name: UnLoadCJLibrary_0100 + * @tc.desc: Test LoadCJLibrary. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, UnLoadCJLibrary_0100, TestSize.Level1) +{ + auto cjEnv = std::make_shared(); + EXPECT_NE(cjEnv, nullptr); + cjEnv->UnLoadCJLibrary(nullptr); +} + +/** + * @tc.name: GetUIScheduler_0100 + * @tc.desc: Test GetUIScheduler. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, GetUIScheduler_0100, TestSize.Level1) +{ + auto cjEnv = std::make_shared(); + cjEnv->isUISchedulerStarted_ = true; + auto res = cjEnv->GetUIScheduler(); + EXPECT_EQ(res, nullptr); +} + +/** + * @tc.name: GetSymbol_0100 + * @tc.desc: Test GetSymbol. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, GetSymbol_0100, TestSize.Level1) +{ + auto cjEnv = std::make_shared(); + EXPECT_NE(cjEnv, nullptr); + void* dso = nullptr; + char symbols[] = "symbol"; + char* symbol = symbols; + auto res = cjEnv->GetSymbol(dso, symbol); + EXPECT_EQ(res, nullptr); +} + +/** + * @tc.name: StartDebugger_0100 + * @tc.desc: Test StartDebugger. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, StartDebugger_0100, TestSize.Level1) +{ + auto cjEnv = std::make_shared(); + EXPECT_NE(cjEnv, nullptr); + auto res = cjEnv->StartDebugger(); + EXPECT_EQ(res, false); +} + +/** + * @tc.name: PostTask_0100 + * @tc.desc: Test PostTask. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, PostTask_0100, TestSize.Level1) +{ + auto cjEnv = std::make_shared(); + TaskFuncType task = nullptr; + auto res = cjEnv->PostTask(task); + EXPECT_EQ(res, false); +} + +/** + * @tc.name: HasHigherPriorityTask_0100 + * @tc.desc: Test HasHigherPriorityTask. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, HasHigherPriorityTask_0100, TestSize.Level1) +{ + auto cjEnv = std::make_shared(); + EXPECT_NE(cjEnv, nullptr); + auto res = cjEnv->HasHigherPriorityTask(); + EXPECT_EQ(res, false); +} +} // namespace OHOS \ No newline at end of file