From: SukHyung, Kang Date: Thu, 3 Sep 2020 06:13:47 +0000 (+0900) Subject: Add unitest for data control X-Git-Tag: submit/tizen/20201110.022219~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F05%2F243105%2F9;p=platform%2Fcore%2Fappfw%2Fdata-control.git Add unitest for data control Change-Id: I0e7774a01ca0f0afc2502d4a6e697aa7a48ed4e7 Signed-off-by: SukHyung, Kang --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a89789..a83649a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,3 +47,12 @@ INSTALL(FILES ${CMAKE_SOURCE_DIR}/capi-${PROJECT_NAME}.pc DESTINATION ${LIB_INST INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}) INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/include/ DESTINATION include/appfw FILES_MATCHING PATTERN "*.h") INSTALL(DIRECTORY ./ DESTINATION ${LIB_INSTALL_DIR} FILES_MATCHING PATTERN "*.so*") + +IF(NOT DEFINED MINIMUM_BUILD) +ADD_SUBDIRECTORY(tests) +ENABLE_TESTING() +SET(DATA_CONTROL_UNIT_TESTS data_control_unittests) +ADD_TEST(NAME ${DATA_CONTROL_UNIT_TESTS} COMMAND ${DATA_CONTROL_UNIT_TESTS}) + +ADD_DEPENDENCIES(${DATA_CONTROL_UNIT_TESTS} data-control) +ENDIF(NOT DEFINED MINIMUM_BUILD) diff --git a/packaging/data-control.spec b/packaging/data-control.spec index e2a8f46..5b556c3 100644 --- a/packaging/data-control.spec +++ b/packaging/data-control.spec @@ -14,6 +14,12 @@ BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(capi-base-common) BuildRequires: pkgconfig(cynara-client) BuildRequires: pkgconfig(sqlite3) +BuildRequires: pkgconfig(gmock) + +%if 0%{?gcov:1} +BuildRequires: lcov +BuildRequires: zip +%endif # runtime requires Requires(post): /sbin/ldconfig @@ -33,6 +39,20 @@ Requires: %{name} = %{version}-%{release} %description devel Data Control library (DEV) +################################################# +# data_control_unittests +################################################# +%package -n data_control_unittests +Summary: GTest for Data Control API +Group: Development/Libraries +Requires: %{name} + +%description -n data_control_unittests +GTest for data-control API + +################################################# +# gcov +################################################# %if 0%{?gcov:1} %package gcov Summary: Data Control API(gcov) @@ -64,6 +84,15 @@ mkdir -p gcov-obj find . -name '*.gcno' -exec cp '{}' gcov-obj ';' %endif +%check +ctest -V +%if 0%{?gcov:1} +lcov -c --ignore-errors graph --no-external -q -d . -o data_control.info +genhtml data_control.info -o data_control.out +zip -r data_control.zip data_control.out +install -m 0644 data_control.zip %{buildroot}%{_datadir}/gcov/data_control.zip +%endif + %install rm -rf %{buildroot} @@ -92,7 +121,16 @@ install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj %{_libdir}/pkgconfig/*.pc %{_libdir}/lib%{name}.so +################################################# +# data_control_unittests +################################################# +%files -n data_control_unittests +%{_bindir}/data_control_unittests + +################################################# +# data-control-gcov +################################################# %if 0%{?gcov:1} %files gcov -%{_datadir}/gcov/obj/* +%{_datadir}/gcov/* %endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..c90fac8 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1 @@ +ADD_SUBDIRECTORY(unit_tests) diff --git a/tests/mock/mock_hook.h b/tests/mock/mock_hook.h new file mode 100644 index 0000000..79831d7 --- /dev/null +++ b/tests/mock/mock_hook.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020 Samsung Electronics 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_MOCK_HOOK_H_ +#define MOCK_MOCK_HOOK_H_ + +#define MOCK_HOOK_P0(MOCK_CLASS, f) \ + TestFixture::GetMock().f() +#define MOCK_HOOK_P1(MOCK_CLASS, f, p1) \ + TestFixture::GetMock().f(p1) +#define MOCK_HOOK_P2(MOCK_CLASS, f, p1, p2) \ + TestFixture::GetMock().f(p1, p2) +#define MOCK_HOOK_P3(MOCK_CLASS, f, p1, p2, p3) \ + TestFixture::GetMock().f(p1, p2, p3) +#define MOCK_HOOK_P4(MOCK_CLASS, f, p1, p2, p3, p4) \ + TestFixture::GetMock().f(p1, p2, p3, p4) +#define MOCK_HOOK_P5(MOCK_CLASS, f, p1, p2, p3, p4, p5) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5) +#define MOCK_HOOK_P6(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5, p6) +#define MOCK_HOOK_P7(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5, p6, p7) +#define MOCK_HOOK_P8(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7, p8) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5, p6, p7, p8) +#define MOCK_HOOK_P10(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) \ + TestFixture::GetMock().f( \ + p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) + +#endif // MOCK_MOCK_HOOK_H_ diff --git a/tests/mock/module_mock.h b/tests/mock/module_mock.h new file mode 100644 index 0000000..0934014 --- /dev/null +++ b/tests/mock/module_mock.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2020 Samsung Electronics 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_MODULE_MOCK_H_ +#define MOCK_MODULE_MOCK_H_ + +class ModuleMock { + public: + virtual ~ModuleMock() {} +}; + +#endif // MOCK_MODULE_MOCK_H_ diff --git a/tests/mock/test_fixture.cc b/tests/mock/test_fixture.cc new file mode 100644 index 0000000..27f5666 --- /dev/null +++ b/tests/mock/test_fixture.cc @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2020 Samsung Electronics 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 "test_fixture.h" + +#include + +std::unique_ptr TestFixture::mock_; diff --git a/tests/mock/test_fixture.h b/tests/mock/test_fixture.h new file mode 100644 index 0000000..db223f1 --- /dev/null +++ b/tests/mock/test_fixture.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2020 Samsung Electronics 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_TEST_FIXTURE_H_ +#define MOCK_TEST_FIXTURE_H_ + +#include + +#include +#include +#include +#include + +#include "module_mock.h" + +class TestFixture : public ::testing::Test { + public: + explicit TestFixture(std::unique_ptr&& mock) { + mock_ = std::move(mock); + } + virtual ~TestFixture() { + mock_.reset(); + } + + virtual void SetUp() {} + virtual void TearDown() {} + + template + static T& GetMock() { + auto ptr = dynamic_cast(mock_.get()); + if (!ptr) + throw std::invalid_argument("The test does not provide mock of \"" + + std::string(typeid(T).name()) + "\""); + return *ptr; + } + + static std::unique_ptr mock_; +}; + +#endif // MOCK_TEST_FIXTURE_H_ diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt new file mode 100644 index 0000000..5a3e660 --- /dev/null +++ b/tests/unit_tests/CMakeLists.txt @@ -0,0 +1,44 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.8) +PROJECT(data_control_unittests C CXX) + +INCLUDE(FindPkgConfig) +PKG_CHECK_MODULES(data_control_unittests REQUIRED + gmock + dlog + bundle + glib-2.0 + gio-2.0 + sqlite3 + aul +) + +FOREACH(flag ${data_control_unittests_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -Wall -Werror") + +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -std=c++14") +SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g") +SET(CMAKE_CXX_FLAGS_RELEASE "-O2") + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../include) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../src) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../mock) + +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src TEST_SOURCES) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../mock MOCK_SOURCES) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../src LIB_SOURCES) + +ADD_EXECUTABLE(${PROJECT_NAME} + ${TEST_SOURCES} + ${LIB_SOURCES} + ${MOCK_SOURCES} +) + +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS}") +TARGET_LINK_LIBRARIES(${PROJECT_NAME} + ${data_control_unittests_LDFLAGS} + data-control +) + +INSTALL(TARGETS ${PROJECT_NAME} DESTINATION /usr/bin/) diff --git a/tests/unit_tests/src/test_data_control_bulk.cc b/tests/unit_tests/src/test_data_control_bulk.cc new file mode 100644 index 0000000..5727f5d --- /dev/null +++ b/tests/unit_tests/src/test_data_control_bulk.cc @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2020 Samsung Electronics 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 +#include +#include +#include + +#include "data_control.h" +#include "test_fixture.h" + +using namespace tizen_base; + +class Mocks : public virtual ModuleMock {}; + +class DataControlBulkTest : public TestFixture { + public: + DataControlBulkTest() : TestFixture(std::make_unique()) {} + virtual ~DataControlBulkTest() {} + + virtual void SetUp() { + } + + virtual void TearDown() { + } +}; + +TEST_F(DataControlBulkTest, bulk_data_test) { + int ret; + bundle* ret_b; + Bundle b; + int count; + char* ret_val; + data_control_bulk_data_h bulk_data_h; + + ret = data_control_bulk_data_create(&bulk_data_h); + EXPECT_EQ(ret, DATA_CONTROL_ERROR_NONE); + + b.Add(std::string("testkey"), std::string("testvalue")); + + ret = data_control_bulk_data_add(bulk_data_h, b.GetHandle()); + EXPECT_EQ(ret, DATA_CONTROL_ERROR_NONE); + + ret = data_control_bulk_data_get_count(bulk_data_h, &count); + EXPECT_EQ(ret, DATA_CONTROL_ERROR_NONE); + EXPECT_EQ(count, 1); + + ret = data_control_bulk_data_get_data(bulk_data_h, 0, &ret_b); + EXPECT_EQ(ret, DATA_CONTROL_ERROR_NONE); + + bundle_get_str(ret_b, "testkey", &ret_val); + EXPECT_STREQ(ret_val, "testvalue"); + + ret = data_control_bulk_data_destroy(bulk_data_h); + EXPECT_EQ(ret, DATA_CONTROL_ERROR_NONE); +} + +TEST_F(DataControlBulkTest, buld_result_data_test) { + int ret; + Bundle b; + bundle* ret_b; + int count; + char* ret_val; + int ret_result; + data_control_bulk_result_data_h result_data_h; + + ret = data_control_bulk_result_data_create(&result_data_h); + EXPECT_EQ(ret, DATA_CONTROL_ERROR_NONE); + + b.Add(std::string("testkey"), std::string("testvalue")); + + ret = data_control_bulk_result_data_add(result_data_h, b.GetHandle(), + DATA_CONTROL_ERROR_NONE); + EXPECT_EQ(ret, DATA_CONTROL_ERROR_NONE); + + ret = data_control_bulk_result_data_get_count(result_data_h, &count); + EXPECT_EQ(ret, DATA_CONTROL_ERROR_NONE); + EXPECT_EQ(count, 1); + + ret = data_control_bulk_result_data_get_result_data(result_data_h, 0, &ret_b, &ret_result); + EXPECT_EQ(ret, DATA_CONTROL_ERROR_NONE); + EXPECT_EQ(ret_result, DATA_CONTROL_ERROR_NONE); + + bundle_get_str(ret_b, "testkey", &ret_val); + EXPECT_STREQ(ret_val, "testvalue"); + + ret = data_control_bulk_result_data_destroy(result_data_h); + EXPECT_EQ(ret, DATA_CONTROL_ERROR_NONE); +} diff --git a/tests/unit_tests/src/test_main.cc b/tests/unit_tests/src/test_main.cc new file mode 100644 index 0000000..c2eb730 --- /dev/null +++ b/tests/unit_tests/src/test_main.cc @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2020 Samsung Electronics 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 + +int main(int argc, char** argv) { + int ret = -1; + + try { + testing::InitGoogleTest(&argc, argv); + } catch(...) { + std::cout << "Exception occurred" << std::endl; + } + + try { + ret = RUN_ALL_TESTS(); + } catch (const ::testing::internal::GoogleTestFailureException& e) { + ret = -1; + std::cout << "GoogleTestFailureException was thrown:" << e.what() << std::endl; + } + + return ret; +}