From d6fe4144ec5c5204330ef86cccc59aa84299f8d8 Mon Sep 17 00:00:00 2001 From: wuhy_irobot2013 Date: Tue, 17 Aug 2021 15:12:29 +0800 Subject: [PATCH] add async/require/stack tdd test Signed-off-by: wuhy_irobot2013 Change-Id: I1503fb8ff4267697318731b452efc071600802d2 --- .../module_manager/test/unittest/BUILD.gn | 27 ++ .../common/require_module_tdd_test.cpp | 288 ++++++++++++++ .../unittest/common/require_module_tdd_test.h | 57 +++ .../async/test/unittest/BUILD.gn | 27 ++ .../common/js_async_work_tdd_test.cpp | 207 ++++++++++ .../unittest/common/js_async_work_tdd_test.h | 51 +++ .../core/components/test/unittest/BUILD.gn | 1 + .../components/test/unittest/common/BUILD.gn | 11 + .../test/unittest/common/stack_tdd_test.cpp | 369 ++++++++++++++++++ .../test/unittest/common/stack_tdd_test.h | 63 +++ test/BUILD.gn | 4 + test/moduletest/common/BUILD.gn | 3 + 12 files changed, 1108 insertions(+) create mode 100644 frameworks/module_manager/test/unittest/BUILD.gn create mode 100644 frameworks/module_manager/test/unittest/common/require_module_tdd_test.cpp create mode 100644 frameworks/module_manager/test/unittest/common/require_module_tdd_test.h create mode 100644 frameworks/native_engine/async/test/unittest/BUILD.gn create mode 100644 frameworks/native_engine/async/test/unittest/common/js_async_work_tdd_test.cpp create mode 100644 frameworks/native_engine/async/test/unittest/common/js_async_work_tdd_test.h create mode 100644 frameworks/src/core/components/test/unittest/common/stack_tdd_test.cpp create mode 100644 frameworks/src/core/components/test/unittest/common/stack_tdd_test.h diff --git a/frameworks/module_manager/test/unittest/BUILD.gn b/frameworks/module_manager/test/unittest/BUILD.gn new file mode 100644 index 0000000..4c1d348 --- /dev/null +++ b/frameworks/module_manager/test/unittest/BUILD.gn @@ -0,0 +1,27 @@ +#Copyright (c) 2021 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/lite/config/test.gni") +import("//foundation/ace/ace_engine_lite/test/ace_test_config.gni") + +unittest("js_frameworks_test_require_module") { + output_extension = "bin" + output_dir = test_output_root + configs = [ "$ace_lite_root/test:test_common_config" ] + sources = [ "common/require_module_tdd_test.cpp" ] + deps = ace_test_deps +} + +group("module_manager_unittest") { + deps = [ ":js_frameworks_test_require_module" ] +} diff --git a/frameworks/module_manager/test/unittest/common/require_module_tdd_test.cpp b/frameworks/module_manager/test/unittest/common/require_module_tdd_test.cpp new file mode 100644 index 0000000..4826aa5 --- /dev/null +++ b/frameworks/module_manager/test/unittest/common/require_module_tdd_test.cpp @@ -0,0 +1,288 @@ +/* + * Copyright (c) 2021 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 "require_module_tdd_test.h" +#include "internal/jsi_internal.h" +#include "jerryscript.h" +#include "jsi.h" +#include "module_manager.h" + +namespace OHOS { +namespace ACELite { +RequireModuleTddTest::RequireModuleTddTest() : moduleManager_(nullptr) {} + +void RequireModuleTddTest::SetUp() +{ + jerry_init(JERRY_INIT_EMPTY); + moduleManager_ = ModuleManager::GetInstance(); +} + +void RequireModuleTddTest::TearDown() +{ + moduleManager_->CleanUpModule(); + moduleManager_->OnTerminate(); + jerry_cleanup(); +} + +void RequireModuleTddTest::RequireModuleTest001() +{ + TDD_CASE_BEGIN(); + /** + * @tc.steps: step1. require a module with module name registered + */ + constexpr char *moduleName = "system.app"; + JSIValue moduleRequired = moduleManager_->RequireModule(moduleName); + /** + * @tc.expected: step1. moduleRequired is not undefined + */ + EXPECT_FALSE(JSI::ValueIsUndefined(moduleRequired)); + JSI::ReleaseValue(moduleRequired); + TDD_CASE_END(); +} + +void RequireModuleTddTest::RequireModuleTest002() +{ + TDD_CASE_BEGIN(); + /** + * @tc.steps: step1. require a module with module name unregistered + */ + constexpr char *moduleName = "system.unregistered"; + JSIValue moduleRequired = moduleManager_->RequireModule(moduleName); + /** + * @tc.expected: step1. moduleRequired is undefined + */ + EXPECT_TRUE(JSI::ValueIsUndefined(moduleRequired)); + JSI::ReleaseValue(moduleRequired); + TDD_CASE_END(); +} + +void RequireModuleTddTest::RequireModuleTest003() +{ + TDD_CASE_BEGIN(); + /** + * @tc.steps: step1. require a module with incorrect category + */ + constexpr char *moduleName = "service.app"; + JSIValue moduleRequired = moduleManager_->RequireModule(moduleName); + /** + * @tc.expected: step1. moduleRequired is undefined + */ + EXPECT_TRUE(JSI::ValueIsUndefined(moduleRequired)); + JSI::ReleaseValue(moduleRequired); + TDD_CASE_END(); +} + +void RequireModuleTddTest::RequireModuleTest004() +{ + TDD_CASE_BEGIN(); + /** + * @tc.steps: step1. require a module with illegal module name + */ + constexpr char *moduleName = "illegal"; + JSIValue moduleRequired = moduleManager_->RequireModule(moduleName); + /** + * @tc.expected: step1. moduleRequired is undefined + */ + EXPECT_TRUE(JSI::ValueIsUndefined(moduleRequired)); + JSI::ReleaseValue(moduleRequired); + TDD_CASE_END(); +} + +void RequireModuleTddTest::RequireModuleTest005() +{ + TDD_CASE_BEGIN(); + /** + * @tc.steps: step1. require app module + */ + constexpr char *moduleName = "system.app"; + JSIValue moduleRequired = moduleManager_->RequireModule(moduleName); + /** + * @tc.expected: step1. moduleRequired is not undefined + */ + EXPECT_FALSE(JSI::ValueIsUndefined(moduleRequired)); + /** + * @tc.steps: step2. get "getInfo" property of moduleRequired + */ + constexpr char *funcName = "getInfo"; + JSIValue jFunc = JSI::GetNamedProperty(moduleRequired, funcName); + /** + * @tc.expected: step2. jFunc is not undefined + */ + EXPECT_FALSE(JSI::ValueIsUndefined(jFunc)); + /** + * @tc.steps: step3. call jFunc and the value returned is undefined + */ + jerry_value_t jResult = jerry_call_function(AS_JERRY_VALUE(jFunc), AS_JERRY_VALUE(moduleRequired), nullptr, 0); + JSIValue result = AS_JSI_VALUE(jResult); + /** + * @tc.expected: step3. AppModule native function will be called an result is undefined + */ + EXPECT_TRUE(JSI::ValueIsUndefined(result)); + JSI::ReleaseValueList(moduleRequired, jFunc, result); + TDD_CASE_END(); +} + +void RequireModuleTddTest::RequireModuleTest006() +{ + TDD_CASE_BEGIN(); + /** + * @tc.steps: step1. require app module + */ + constexpr char *moduleName = "system.app"; + JSIValue moduleRequired = moduleManager_->RequireModule(moduleName); + /** + * @tc.expected: step1. moduleRequired is not undefined + */ + EXPECT_FALSE(JSI::ValueIsUndefined(moduleRequired)); + /** + * @tc.steps: step2. get "terminate" property of moduleRequired + */ + constexpr char *funcName = "terminate"; + JSIValue jFunc = JSI::GetNamedProperty(moduleRequired, funcName); + /** + * @tc.expected: step2. jFunc is not undefined + */ + EXPECT_FALSE(JSI::ValueIsUndefined(jFunc)); + /** + * @tc.steps: step3. call jFunc + */ + jerry_value_t jResult = jerry_call_function(AS_JERRY_VALUE(jFunc), AS_JERRY_VALUE(moduleRequired), nullptr, 0); + JSIValue result = AS_JSI_VALUE(jResult); + /** + * @tc.expected: step3. AppModule native function will be called an result is undefined + */ + EXPECT_TRUE(JSI::ValueIsUndefined(result)); + JSI::ReleaseValueList(moduleRequired, jFunc, result); + TDD_CASE_END(); +} + +void RequireModuleTddTest::RequireModuleTest007() +{ + TDD_CASE_BEGIN(); + /** + * @tc.steps: step1. require router module + */ + constexpr char *moduleName = "system.router"; + JSIValue moduleRequired = moduleManager_->RequireModule(moduleName); + /** + * @tc.expected: step1. moduleRequired is not undefined + */ + EXPECT_FALSE(JSI::ValueIsUndefined(moduleRequired)); + /** + * @tc.steps: step2. get "replace" property of moduleRequired + */ + constexpr char *funcName = "replace"; + JSIValue jFunc = JSI::GetNamedProperty(moduleRequired, funcName); + /** + * @tc.expected: step2. jFunc is not undefined + */ + EXPECT_FALSE(JSI::ValueIsUndefined(jFunc)); + /** + * @tc.steps: step3. call jFunc + */ + jerry_value_t jResult = jerry_call_function(AS_JERRY_VALUE(jFunc), AS_JERRY_VALUE(moduleRequired), nullptr, 0); + JSIValue result = AS_JSI_VALUE(jResult); + /** + * @tc.expected: step3. RouterModule native function will be called an result is undefined + */ + EXPECT_TRUE(JSI::ValueIsError(result)); + JSI::ReleaseValueList(moduleRequired, jFunc, result); + TDD_CASE_END(); +} + +void RequireModuleTddTest::RunTests() +{ + RequireModuleTest001(); + RequireModuleTest002(); + RequireModuleTest003(); + RequireModuleTest004(); + RequireModuleTest005(); + RequireModuleTest006(); + RequireModuleTest007(); +} + +#ifdef TDD_ASSERTIONS +/** + * @tc.name: RequireModuleTest001 + * @tc.desc: Verify require a registered module. + * @tc.require: AR000DSEHC + */ +HWTEST_F(RequireModuleTddTest, test001, TestSize.Level1) +{ + RequireModuleTddTest::RequireModuleTest001(); +} + +/** + * @tc.name: RequireModuleTest002 + * @tc.desc: Verify require an unregistered module. + * @tc.require: AR000DSEHC + */ +HWTEST_F(RequireModuleTddTest, test002, TestSize.Level1) +{ + RequireModuleTddTest::RequireModuleTest002(); +} + +/** + * @tc.name: RequireModuleTest003 + * @tc.desc: Verify require a module with incorrect category. + * @tc.require: AR000DSEHC + */ +HWTEST_F(RequireModuleTddTest, test003, TestSize.Level1) +{ + RequireModuleTddTest::RequireModuleTest003(); +} + +/** + * @tc.name: RequireModuleTest004 + * @tc.desc: Verify require a module with illegal module name. + * @tc.require: AR000DSEHC + */ +HWTEST_F(RequireModuleTddTest, test004, TestSize.Level1) +{ + RequireModuleTddTest::RequireModuleTest004(); +} + +/** + * @tc.name: RequireModuleTest005 + * @tc.desc: Verify require app module, and call it's getInfo(). + * @tc.require: AR000DSEHC + */ +HWTEST_F(RequireModuleTddTest, test005, TestSize.Level1) +{ + RequireModuleTddTest::RequireModuleTest005(); +} + +/** + * @tc.name: RequireModuleTest006 + * @tc.desc: Verify require app module, and call it's terminate(). + * @tc.require: AR000DSEHC + */ +HWTEST_F(RequireModuleTddTest, test006, TestSize.Level1) +{ + RequireModuleTddTest::RequireModuleTest006(); +} + +/** + * @tc.name: RequireModuleTest007 + * @tc.desc: Verify require router module, and call it's replace(). + * @tc.require: AR000DSEHC + */ +HWTEST_F(RequireModuleTddTest, test007, TestSize.Level1) +{ + RequireModuleTddTest::RequireModuleTest007(); +} +#endif +} // namespace ACELite +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/module_manager/test/unittest/common/require_module_tdd_test.h b/frameworks/module_manager/test/unittest/common/require_module_tdd_test.h new file mode 100644 index 0000000..cd18918 --- /dev/null +++ b/frameworks/module_manager/test/unittest/common/require_module_tdd_test.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2021 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 OHOS_ACELITE_REQUIRE_MODULE_TDD_TEST_H +#define OHOS_ACELITE_REQUIRE_MODULE_TDD_TEST_H + +#ifdef TDD_ASSERTIONS +#include +#include +#endif + +#include "acelite_config.h" +#include "module_manager.h" +#include "test_common.h" + +namespace OHOS { +namespace ACELite { +#ifdef TDD_ASSERTIONS +using namespace std; +using namespace testing::ext; +class RequireModuleTddTest : public testing::Test { +#else +class RequireModuleTddTest { +#endif +public: + RequireModuleTddTest(); + ~RequireModuleTddTest(){}; + void SetUp(); + void TearDown(); + void RequireModuleTest001(); + void RequireModuleTest002(); + void RequireModuleTest003(); + void RequireModuleTest004(); + void RequireModuleTest005(); + void RequireModuleTest006(); + void RequireModuleTest007(); + void RunTests(); + +private: + ModuleManager *moduleManager_; +}; +} // namespace ACELite +} // namespace OHOS + +#endif // OHOS_ACELITE_REQUIRE_MODULE_TDD_TEST_H diff --git a/frameworks/native_engine/async/test/unittest/BUILD.gn b/frameworks/native_engine/async/test/unittest/BUILD.gn new file mode 100644 index 0000000..e0195fa --- /dev/null +++ b/frameworks/native_engine/async/test/unittest/BUILD.gn @@ -0,0 +1,27 @@ +#Copyright (c) 2021 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/lite/config/test.gni") +import("//foundation/ace/ace_engine_lite/test/ace_test_config.gni") + +unittest("js_frameworks_test_js_async_work") { + output_extension = "bin" + output_dir = test_output_root + configs = [ "$ace_lite_root/test:test_common_config" ] + sources = [ "common/js_async_work_tdd_test.cpp" ] + deps = ace_test_deps +} + +group("async_unittest") { + deps = [ ":js_frameworks_test_js_async_work" ] +} diff --git a/frameworks/native_engine/async/test/unittest/common/js_async_work_tdd_test.cpp b/frameworks/native_engine/async/test/unittest/common/js_async_work_tdd_test.cpp new file mode 100644 index 0000000..361ead8 --- /dev/null +++ b/frameworks/native_engine/async/test/unittest/common/js_async_work_tdd_test.cpp @@ -0,0 +1,207 @@ +/* + * Copyright (c) 2021 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 "js_async_work_tdd_test.h" +#include "js_async_work.h" + +namespace OHOS { +namespace ACELite { +static int8_t g_statusCode = 0; + +void JsAsyncWorkTddTest::SetUp() +{ + g_statusCode = -1; +} + +static void Execute(void *data) +{ + (void)data; +} + +static void ExecuteWithCode(void *data, int8_t statusCode) +{ + (void)data; + g_statusCode = statusCode; +} + +void JsAsyncWorkTddTest::JsAsyncWorkTest001() +{ + TDD_CASE_BEGIN(); + /** + * @tc.steps: step1. call DispatchAsyncWork with valid parameters + */ + bool res = JsAsyncWork::DispatchAsyncWork(Execute, nullptr); + /** + * @tc.expected: step1. res is false due to null AceAbility instance + */ + EXPECT_FALSE(res); + /** + * @tc.steps: step2. call overloaded DispatchAsyncWork with valid parameters + */ + res = JsAsyncWork::DispatchAsyncWork(ExecuteWithCode, nullptr); + /** + * @tc.expected: step2. res is false due to null AceAbility instance + */ + EXPECT_FALSE(res); + TDD_CASE_END(); +} + +void JsAsyncWorkTddTest::JsAsyncWorkTest002() +{ + TDD_CASE_BEGIN(); + /** + * @tc.steps: step1. call DispatchAsyncWork with invalid parameters + */ + AsyncWorkHandler handler1 = nullptr; + bool res = JsAsyncWork::DispatchAsyncWork(handler1, nullptr); + /** + * @tc.expected: step1. res is false due to invalid parameters, and error log can be checked + */ + EXPECT_FALSE(res); + /** + * @tc.steps: step2. call overloaded DispatchAsyncWork with invalid parameters + */ + AsyncHandler handler2 = nullptr; + res = JsAsyncWork::DispatchAsyncWork(handler2, nullptr); + /** + * @tc.expected: step2. res is false due to invalid parameters, and error log can be checked + */ + EXPECT_FALSE(res); + TDD_CASE_END(); +} + +void JsAsyncWorkTddTest::JsAsyncWorkTest003() +{ + TDD_CASE_BEGIN(); + /** + * @tc.steps: step1. call ExecuteAsyncWork with valid parameters + */ + AsyncWork *asyncWork = new AsyncWork(); + EXPECT_TRUE(asyncWork != nullptr); + + asyncWork->workHandler = Execute; + asyncWork->handler = ExecuteWithCode; + JsAsyncWork::ExecuteAsyncWork(asyncWork); + JsAsyncWork::ExecuteAsyncWork(asyncWork, JsAsyncWork::ERR_OK); + /** + * @tc.expected: step1. log can be checked and g_statusCode = JsAsyncWork::ERR_OK + */ + EXPECT_TRUE(g_statusCode == JsAsyncWork::ERR_OK); + if (asyncWork != nullptr) { + delete asyncWork; + asyncWork = nullptr; + } + TDD_CASE_END(); +} + +void JsAsyncWorkTddTest::JsAsyncWorkTest004() +{ + TDD_CASE_BEGIN(); + /** + * @tc.steps: step1. call ExecuteAsyncWork with null asyncWork + */ + AsyncWork *asyncWork = nullptr; + JsAsyncWork::ExecuteAsyncWork(asyncWork); + JsAsyncWork::ExecuteAsyncWork(asyncWork, JsAsyncWork::ERR_OK); + /** + * @tc.expected: step1. error log can be checked and g_statusCode != JsAsyncWork::ERR_OK + */ + EXPECT_TRUE(g_statusCode != JsAsyncWork::ERR_OK); + TDD_CASE_END(); +} + +void JsAsyncWorkTddTest::JsAsyncWorkTest005() +{ + TDD_CASE_BEGIN(); + /** + * @tc.steps: step1. call ExecuteAsyncWork with null workHandler + */ + AsyncWork *asyncWork = new AsyncWork(); + EXPECT_TRUE(asyncWork != nullptr); + JsAsyncWork::ExecuteAsyncWork(asyncWork); + JsAsyncWork::ExecuteAsyncWork(asyncWork, JsAsyncWork::ERR_OK); + /** + * @tc.expected: step1. error log can be checked and g_statusCode != JsAsyncWork::ERR_OK + */ + EXPECT_TRUE(g_statusCode != JsAsyncWork::ERR_OK); + if (asyncWork != nullptr) { + delete asyncWork; + asyncWork = nullptr; + } + TDD_CASE_END(); +} + +void JsAsyncWorkTddTest::RunTests() +{ + JsAsyncWorkTest001(); + JsAsyncWorkTest002(); + JsAsyncWorkTest003(); + JsAsyncWorkTest004(); + JsAsyncWorkTest005(); +} + +#ifdef TDD_ASSERTIONS +/** + * @tc.name: JsAsyncWorkTest001 + * @tc.desc: Verify DispatchAsyncWork with valid parameters. + * @tc.require: AR000DSEHQ + */ +HWTEST_F(JsAsyncWorkTddTest, test001, TestSize.Level1) +{ + JsAsyncWorkTddTest::JsAsyncWorkTest001(); +} + +/** + * @tc.name: JsAsyncWorkTest002 + * @tc.desc: Verify DispatchAsyncWork with invalid parameters. + * @tc.require: AR000DSEHQ + */ +HWTEST_F(JsAsyncWorkTddTest, test002, TestSize.Level1) +{ + JsAsyncWorkTddTest::JsAsyncWorkTest002(); +} + +/** + * @tc.name: JsAsyncWorkTest003 + * @tc.desc: Verify ExcuteAsyncWork with valid parameters. + * @tc.require: AR000DSEHQ + */ +HWTEST_F(JsAsyncWorkTddTest, test003, TestSize.Level1) +{ + JsAsyncWorkTddTest::JsAsyncWorkTest003(); +} + +/** + * @tc.name: JsAsyncWorkTest004 + * @tc.desc: Verify ExcuteAsyncWork with invalid parameters. + * @tc.require: AR000DSEHQ + */ +HWTEST_F(JsAsyncWorkTddTest, test004, TestSize.Level1) +{ + JsAsyncWorkTddTest::JsAsyncWorkTest004(); +} + +/** + * @tc.name: JsAsyncWorkTest005 + * @tc.desc: Verify ExcuteAsyncWork with invalid parameters. + * @tc.require: AR000DSEHQ + */ +HWTEST_F(JsAsyncWorkTddTest, test005, TestSize.Level1) +{ + JsAsyncWorkTddTest::JsAsyncWorkTest005(); +} +#endif +} // namespace ACELite +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native_engine/async/test/unittest/common/js_async_work_tdd_test.h b/frameworks/native_engine/async/test/unittest/common/js_async_work_tdd_test.h new file mode 100644 index 0000000..1822451 --- /dev/null +++ b/frameworks/native_engine/async/test/unittest/common/js_async_work_tdd_test.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021 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 OHOS_ACELITE_JS_ASYNC_WORK_TDD_TEST_H +#define OHOS_ACELITE_JS_ASYNC_WORK_TDD_TEST_H + +#ifdef TDD_ASSERTIONS +#include +#include +#endif + +#include "acelite_config.h" +#include "test_common.h" + +namespace OHOS { +namespace ACELite { +#ifdef TDD_ASSERTIONS +using namespace std; +using namespace testing::ext; +class JsAsyncWorkTddTest : public testing::Test { +#else +class JsAsyncWorkTddTest { +#endif +public: + JsAsyncWorkTddTest(){}; + ~JsAsyncWorkTddTest(){}; + void SetUp(); + void TearDown(){}; + void JsAsyncWorkTest001(); + void JsAsyncWorkTest002(); + void JsAsyncWorkTest003(); + void JsAsyncWorkTest004(); + void JsAsyncWorkTest005(); + void RunTests(); +}; +} // namespace ACELite +} // namespace OHOS + +#endif // OHOS_ACELITE_JS_ASYNC_WORK_TDD_TEST_H diff --git a/frameworks/src/core/components/test/unittest/BUILD.gn b/frameworks/src/core/components/test/unittest/BUILD.gn index f36d963..f8e8c13 100755 --- a/frameworks/src/core/components/test/unittest/BUILD.gn +++ b/frameworks/src/core/components/test/unittest/BUILD.gn @@ -32,6 +32,7 @@ group("components_unittest") { "common:js_frameworks_test_pickerview", "common:js_frameworks_test_radio_switch_width", "common:js_frameworks_test_slider", + "common:js_frameworks_test_stack", "common:js_frameworks_test_swiper", "common:js_frameworks_test_switch", "common:js_frameworks_test_text", diff --git a/frameworks/src/core/components/test/unittest/common/BUILD.gn b/frameworks/src/core/components/test/unittest/common/BUILD.gn index 42d5681..17a1170 100755 --- a/frameworks/src/core/components/test/unittest/common/BUILD.gn +++ b/frameworks/src/core/components/test/unittest/common/BUILD.gn @@ -267,3 +267,14 @@ unittest("js_frameworks_test_input_event") { ] deps = ace_test_deps } + +unittest("js_frameworks_test_stack") { + output_extension = "bin" + output_dir = test_output_root + configs = [ "$ace_lite_root/test:test_common_config" ] + sources = [ + "root_component_mock.cpp", + "stack_tdd_test.cpp", + ] + deps = ace_test_deps +} diff --git a/frameworks/src/core/components/test/unittest/common/stack_tdd_test.cpp b/frameworks/src/core/components/test/unittest/common/stack_tdd_test.cpp new file mode 100644 index 0000000..8b232e2 --- /dev/null +++ b/frameworks/src/core/components/test/unittest/common/stack_tdd_test.cpp @@ -0,0 +1,369 @@ +/* + * Copyright (c) 2021 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 "stack_tdd_test.h" +#include "js_app_context.h" +#include "js_debugger_config.h" +#include "stack_component.h" + +namespace OHOS { +namespace ACELite { +StackTddTest::StackTddTest() : optionsObj_(UNDEFINED), attrsObj_(UNDEFINED), styleObj_(UNDEFINED) +{ + componentNameId_ = KeyParser::ParseKeyId("stack"); +} + +void StackTddTest::SetUp() +{ + Debugger::GetInstance().SetupJSContext(); + jerry_init(JERRY_INIT_EMPTY); + + optionsObj_ = jerry_get_global_object(); + attrsObj_ = jerry_create_object(); + JerrySetNamedProperty(optionsObj_, "attrs", attrsObj_); + + styleObj_ = jerry_create_object(); + JerrySetNamedProperty(optionsObj_, "staticStyle", styleObj_); + rootComponentMock_.PrepareRootContainer(); +} + +void StackTddTest::TearDown() +{ + JsAppContext::GetInstance()->ReleaseStyles(); + ReleaseJerryValue(attrsObj_, styleObj_, optionsObj_, VA_ARG_END_FLAG); + jerry_cleanup(); + Debugger::GetInstance().ReleaseJSContext(); +} + +Component *StackTddTest::GetRenderedComponent(uint16_t componentKeyId, jerry_value_t children) const +{ + Component *component = ComponentFactory::CreateComponent(componentKeyId, optionsObj_, children); + rootComponentMock_.RenderComponent(*component); + return component; +} + +Component *StackTddTest::CreateChildComponent(jerry_value_t childOptions) const +{ + jerry_value_t children = jerry_create_null(); + Component *stack = ComponentFactory::CreateComponent(KeyParser::ParseKeyId("stack"), childOptions, children); + stack->Render(); + jerry_release_value(children); + return stack; +} + +void StackTddTest::ReleaseComponent(Component *&component) const +{ + if (component != nullptr) { + component->Release(); + delete component; + component = nullptr; + } +} + +void StackTddTest::ComponentStackTest001() +{ + TDD_CASE_BEGIN(); + /* * + * @tc.steps: step1. set style + */ + jerry_value_t children = jerry_create_null(); + Component *component = GetRenderedComponent(componentNameId_, children); + EXPECT_TRUE(component != nullptr); + UIViewGroup *stackView = reinterpret_cast(component->GetComponentRootView()); + /* * + * @tc.expected: step1. check childView = nullptr + */ + EXPECT_TRUE((stackView != nullptr) && (stackView->GetChildrenHead() == nullptr)); + ReleaseComponent(component); + jerry_release_value(children); + TDD_CASE_END(); +} + +void StackTddTest::ComponentStackTest002() +{ + TDD_CASE_BEGIN(); + /* * + * @tc.steps: step1. create child component + */ + jerry_value_t childOptions = jerry_create_null(); + Component *child = CreateChildComponent(childOptions); + EXPECT_TRUE(child != nullptr); + /* * + * @tc.steps: step2. set children object + */ + const uint32_t childrenNum = 1; + jerry_value_t children = jerry_create_array(childrenNum); + jerry_release_value(jerry_set_property_by_index(children, 0, child->GetNativeElement())); + /* * + * @tc.steps: step3. create stack component + */ + Component *component = GetRenderedComponent(componentNameId_, children); + EXPECT_TRUE(component != nullptr); + UIViewGroup *stackView = reinterpret_cast(component->GetComponentRootView()); + /* * + * @tc.expected: step3. check stackView != nullptr && childView != nullptr + */ + EXPECT_TRUE((stackView != nullptr) && (stackView->GetChildrenHead() != nullptr)); + ReleaseComponent(child); + ReleaseComponent(component); + ReleaseJerryValue(childOptions, children, VA_ARG_END_FLAG); + TDD_CASE_END(); +} + +void StackTddTest::ComponentStackTest003() +{ + TDD_CASE_BEGIN(); + /* * + * @tc.steps: step1. create two children components + */ + jerry_value_t childOptions = jerry_create_null(); + Component *child1 = CreateChildComponent(childOptions); + EXPECT_TRUE(child1 != nullptr); + Component *child2 = CreateChildComponent(childOptions); + EXPECT_TRUE(child2 != nullptr); + /* * + * @tc.steps: step2. set children object + */ + const uint32_t childrenNum = 2; + jerry_value_t children = jerry_create_array(childrenNum); + jerry_release_value(jerry_set_property_by_index(children, 0, child1->GetNativeElement())); + jerry_release_value(jerry_set_property_by_index(children, 1, child2->GetNativeElement())); + /* * + * @tc.steps: step3. create stack component + */ + Component *component = GetRenderedComponent(componentNameId_, children); + EXPECT_TRUE(component != nullptr); + UIViewGroup *stackView = reinterpret_cast(component->GetComponentRootView()); + EXPECT_TRUE(stackView != nullptr); + /* * + * @tc.steps: step4. get children views + */ + UIView *childView1 = stackView->GetChildrenHead(); + EXPECT_TRUE(childView1 != nullptr); + UIView *childView2 = childView1->GetNextSibling(); + /* * + * @tc.expected: step4. check two children views + */ + EXPECT_TRUE((childView2 != nullptr) && (childView1 != childView2)); + + ReleaseComponent(child1); + ReleaseComponent(child2); + ReleaseComponent(component); + ReleaseJerryValue(childOptions, children, VA_ARG_END_FLAG); + TDD_CASE_END(); +} + +void StackTddTest::ComponentStackTest004() +{ + TDD_CASE_BEGIN(); + /* * + * @tc.steps: step1. create child attributes object + */ + jerry_value_t childAttrsObj = jerry_create_object(); + JerrySetStringProperty(childAttrsObj, "id", "child01"); + jerry_value_t childOptions = jerry_create_object(); + JerrySetNamedProperty(childOptions, "attrs", childAttrsObj); + Component *child = CreateChildComponent(childOptions); + EXPECT_TRUE(child != nullptr); + /* * + * @tc.steps: step2. set child to children object + */ + const uint32_t childrenNum = 1; + jerry_value_t children = jerry_create_array(childrenNum); + jerry_release_value(jerry_set_property_by_index(children, 0, child->GetNativeElement())); + /* * + * @tc.steps: step3. create stack component + */ + Component *component = GetRenderedComponent(componentNameId_, children); + EXPECT_TRUE(component != nullptr); + UIViewGroup *stackView = reinterpret_cast(component->GetComponentRootView()); + EXPECT_TRUE(stackView != nullptr); + /* * + * @tc.steps: step4. get child view + */ + UIView *childView = stackView->GetChildrenHead(); + EXPECT_TRUE(childView != nullptr); + /* * + * @tc.expected: step4. check the child view attribute + */ + EXPECT_STREQ(childView->GetViewId(), "child01"); + + ReleaseComponent(child); + ReleaseComponent(component); + ReleaseJerryValue(childAttrsObj, childOptions, children, VA_ARG_END_FLAG); + TDD_CASE_END(); +} + +void StackTddTest::ComponentStackTest005() +{ + TDD_CASE_BEGIN(); + /* * + * @tc.steps: step1. create child styles object, left = 5, top = 10 + */ + jerry_value_t childStylesObj = jerry_create_object(); + int16_t left = 5; + int16_t top = 10; + JerrySetNumberProperty(childStylesObj, "left", left); + JerrySetNumberProperty(childStylesObj, "top", top); + jerry_value_t childOptions = jerry_create_object(); + JerrySetNamedProperty(childOptions, "staticStyle", childStylesObj); + Component *child = CreateChildComponent(childOptions); + EXPECT_TRUE(child != nullptr); + /* * + * @tc.steps: step2. set child to children object + */ + const uint32_t childrenNum = 1; + jerry_value_t children = jerry_create_array(childrenNum); + jerry_release_value(jerry_set_property_by_index(children, 0, child->GetNativeElement())); + /* * + * @tc.steps: step3. create stack component + */ + Component *component = GetRenderedComponent(componentNameId_, children); + EXPECT_TRUE(component != nullptr); + UIViewGroup *stackView = reinterpret_cast(component->GetComponentRootView()); + EXPECT_TRUE(stackView != nullptr); + /* * + * @tc.steps: step4. get child view + */ + UIView *childView = stackView->GetChildrenHead(); + EXPECT_TRUE(childView != nullptr); + /* * + * @tc.expected: step4. check the child view styles + */ + EXPECT_TRUE((childView->GetX() == left) && (childView->GetY() == top)); + + ReleaseComponent(child); + ReleaseComponent(component); + ReleaseJerryValue(childStylesObj, childOptions, children, VA_ARG_END_FLAG); + TDD_CASE_END(); +} + +void StackTddTest::ComponentStackTest006() +{ + TDD_CASE_BEGIN(); + /* * + * @tc.steps: step1. create child styles object, width = 100, height = 50 + */ + jerry_value_t childStylesObj = jerry_create_object(); + int16_t width = 100; + int16_t height = 50; + JerrySetNumberProperty(childStylesObj, "width", width); + JerrySetNumberProperty(childStylesObj, "height", height); + jerry_value_t childOptions = jerry_create_object(); + JerrySetNamedProperty(childOptions, "staticStyle", childStylesObj); + Component *child = CreateChildComponent(childOptions); + EXPECT_TRUE(child != nullptr); + /* * + * @tc.steps: step2. set child to children object + */ + const uint32_t childrenNum = 1; + jerry_value_t children = jerry_create_array(childrenNum); + jerry_release_value(jerry_set_property_by_index(children, 0, child->GetNativeElement())); + /* * + * @tc.steps: step3. create stack component + */ + Component *component = GetRenderedComponent(componentNameId_, children); + EXPECT_TRUE(component != nullptr); + UIViewGroup *stackView = reinterpret_cast(component->GetComponentRootView()); + EXPECT_TRUE(stackView != nullptr); + /* * + * @tc.steps: step4. get child view + */ + UIView *childView = stackView->GetChildrenHead(); + EXPECT_TRUE(childView != nullptr); + /* * + * @tc.expected: step4. check the child view styles + */ + EXPECT_TRUE((childView->GetWidth() == width) && (childView->GetHeight() == height)); + + ReleaseComponent(child); + ReleaseComponent(component); + ReleaseJerryValue(childStylesObj, childOptions, children, VA_ARG_END_FLAG); + TDD_CASE_END(); +} + +void StackTddTest::RunTests() +{ + ComponentStackTest001(); + ComponentStackTest002(); + ComponentStackTest003(); + ComponentStackTest004(); + ComponentStackTest005(); + ComponentStackTest006(); +} + +#ifdef TDD_ASSERTIONS +/* * + * @tc.name: ComponentStackTest001 + * @tc.desc: Verify no child component set. + * @tc.require: AR000DSEF9 + */ +HWTEST_F(StackTddTest, test001, TestSize.Level1) +{ + StackTddTest::ComponentStackTest001(); +} + +/* * + * @tc.name: ComponentStackTest002 + * @tc.desc: Verify child component can be set. + * @tc.require: AR000DSEF9 + */ +HWTEST_F(StackTddTest, test002, TestSize.Level0) +{ + StackTddTest::ComponentStackTest002(); +} + +/* * + * @tc.name: ComponentStackTest003 + * @tc.desc: Verify multiple children components can be set. + * @tc.require: AR000DSEF9 + */ +HWTEST_F(StackTddTest, test003, TestSize.Level0) +{ + StackTddTest::ComponentStackTest003(); +} + +/* * + * @tc.name: ComponentStackTest004 + * @tc.desc: Verify attribute of child component can be set correctly. + * @tc.require: AR000DSEF9 + */ +HWTEST_F(StackTddTest, test004, TestSize.Level1) +{ + StackTddTest::ComponentStackTest004(); +} + +/* * + * @tc.name: ComponentStackTest005 + * @tc.desc: Verify left and top styles of child component can be set correctly. + * @tc.require: AR000DSEF9 + */ +HWTEST_F(StackTddTest, test005, TestSize.Level1) +{ + StackTddTest::ComponentStackTest005(); +} + +/* * + * @tc.name: ComponentStackTest006 + * @tc.desc: Verify width and height styles of child component can be set correctly. + * @tc.require: AR000DSEF9 + */ +HWTEST_F(StackTddTest, test006, TestSize.Level1) +{ + StackTddTest::ComponentStackTest006(); +} +#endif +} // namespace ACELite +} // namespace OHOS diff --git a/frameworks/src/core/components/test/unittest/common/stack_tdd_test.h b/frameworks/src/core/components/test/unittest/common/stack_tdd_test.h new file mode 100644 index 0000000..f18e60d --- /dev/null +++ b/frameworks/src/core/components/test/unittest/common/stack_tdd_test.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2021 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 OHOS_ACELITE_STACK_TDD_TEST_H +#define OHOS_ACELITE_STACK_TDD_TEST_H + +#ifdef TDD_ASSERTIONS +#include +#include +#endif + +#include "component_factory.h" +#include "root_component_mock.h" +#include "test_common.h" + +namespace OHOS { +namespace ACELite { +#ifdef TDD_ASSERTIONS +using namespace std; +using namespace testing::ext; +class StackTddTest : public testing::Test { +#else +class StackTddTest { +#endif +public: + StackTddTest(); + ~StackTddTest(){}; + void SetUp(); + void TearDown(); + void ComponentStackTest001(); + void ComponentStackTest002(); + void ComponentStackTest003(); + void ComponentStackTest004(); + void ComponentStackTest005(); + void ComponentStackTest006(); + void RunTests(); + +private: + Component *GetRenderedComponent(uint16_t componentKeyId, jerry_value_t children) const; + Component *CreateChildComponent(jerry_value_t childOptions) const; + void ReleaseComponent(Component *&component) const; + jerry_value_t optionsObj_; + jerry_value_t attrsObj_; + jerry_value_t styleObj_; + RootComponentMock rootComponentMock_; + uint16_t componentNameId_; +}; +} // namespace ACELite +} // namespace OHOS + +#endif // OHOS_ACELITE_STACK_TDD_TEST_H diff --git a/test/BUILD.gn b/test/BUILD.gn index 6def8de..dbed285 100755 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -19,7 +19,9 @@ components_unittest_root = "$ace_lite_root/frameworks/src/core/components/test/unittest" context_unittest_root = "$ace_lite_root/frameworks/src/core/context/test/unittest" +async_unittest_root = "$NATIVE_ENGINE_PATH/async/test/unittest" jsi_unittest_root = "$NATIVE_ENGINE_PATH/jsi/test/unittest" +module_manager_unittest_root = "$MODULE_MANAGER_PATH/test/unittest" modules_unittest_root = "$ace_lite_root/frameworks/src/core/modules/test/unittest" stylemgr_unittest_root = @@ -43,10 +45,12 @@ config("test_whole_archive_config") { group("unittest") { deps = [ "$ace_lite_root/test/moduletest/common:door_unittest", + "$async_unittest_root:async_unittest", "$cache_manager_unittest_root:cache_manager_unittest", "$components_unittest_root:components_unittest", "$context_unittest_root:js_frameworks_unittest", "$jsi_unittest_root:jsi_unittest", + "$module_manager_unittest_root:module_manager_unittest", "$modules_unittest_root:modules_unittest", "$router_unittest_root:router_module_unittest", "$stylemgr_unittest_root:stylemgr_unittest", diff --git a/test/moduletest/common/BUILD.gn b/test/moduletest/common/BUILD.gn index 56fdba2..2df8f9c 100644 --- a/test/moduletest/common/BUILD.gn +++ b/test/moduletest/common/BUILD.gn @@ -25,6 +25,8 @@ unittest("js_frameworks_tdd_door") { sources = all_acelite_source_files sources += [ "$ACE_LITE_COMMON_PATH/memory/cache/test/unittest/common/cache_manager_tdd_test.cpp", + "$MODULE_MANAGER_PATH/test/unittest/common/require_module_tdd_test.cpp", + "$NATIVE_ENGINE_PATH/async/test/unittest/common/js_async_work_tdd_test.cpp", "$NATIVE_ENGINE_PATH/jsi/test/unittest/common/jsi_interface_tdd_test.cpp", "$ace_lite_root/frameworks/src/core/components/test/unittest/common/root_component_mock.cpp", "$ace_lite_root/test/moduletest/common/base_test.cpp", @@ -40,6 +42,7 @@ unittest("js_frameworks_tdd_door") { "$ace_lite_src_core/components/test/unittest/common/percent_tdd_test.cpp", "$ace_lite_src_core/components/test/unittest/common/radio_switch_tdd_test.cpp", "$ace_lite_src_core/components/test/unittest/common/slider_tdd_test.cpp", + "$ace_lite_src_core/components/test/unittest/common/stack_tdd_test.cpp", "$ace_lite_src_core/components/test/unittest/common/switch_tdd_test.cpp", "$ace_lite_src_core/components/test/unittest/common/text_tdd_exception_test.cpp", "$ace_lite_src_core/components/test/unittest/common/text_tdd_test.cpp",