Add unitest for data control 05/243105/9
authorSukHyung, Kang <shine.kang@samsung.com>
Thu, 3 Sep 2020 06:13:47 +0000 (15:13 +0900)
committerSukHyung, Kang <shine.kang@samsung.com>
Wed, 9 Sep 2020 08:47:46 +0000 (17:47 +0900)
Change-Id: I0e7774a01ca0f0afc2502d4a6e697aa7a48ed4e7
Signed-off-by: SukHyung, Kang <shine.kang@samsung.com>
CMakeLists.txt
packaging/data-control.spec
tests/CMakeLists.txt [new file with mode: 0644]
tests/mock/mock_hook.h [new file with mode: 0644]
tests/mock/module_mock.h [new file with mode: 0644]
tests/mock/test_fixture.cc [new file with mode: 0644]
tests/mock/test_fixture.h [new file with mode: 0644]
tests/unit_tests/CMakeLists.txt [new file with mode: 0644]
tests/unit_tests/src/test_data_control_bulk.cc [new file with mode: 0644]
tests/unit_tests/src/test_main.cc [new file with mode: 0644]

index 8a89789..a83649a 100644 (file)
@@ -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)
index e2a8f46..5b556c3 100644 (file)
@@ -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 (file)
index 0000000..c90fac8
--- /dev/null
@@ -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 (file)
index 0000000..79831d7
--- /dev/null
@@ -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<MOCK_CLASS>().f()
+#define MOCK_HOOK_P1(MOCK_CLASS, f, p1)                                        \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1)
+#define MOCK_HOOK_P2(MOCK_CLASS, f, p1, p2)                                    \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2)
+#define MOCK_HOOK_P3(MOCK_CLASS, f, p1, p2, p3)                                \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3)
+#define MOCK_HOOK_P4(MOCK_CLASS, f, p1, p2, p3, p4)                            \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4)
+#define MOCK_HOOK_P5(MOCK_CLASS, f, p1, p2, p3, p4, p5)                        \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4, p5)
+#define MOCK_HOOK_P6(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6)                    \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4, p5, p6)
+#define MOCK_HOOK_P7(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7)                \
+    TestFixture::GetMock<MOCK_CLASS>().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<MOCK_CLASS>().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<MOCK_CLASS>().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 (file)
index 0000000..0934014
--- /dev/null
@@ -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 (file)
index 0000000..27f5666
--- /dev/null
@@ -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 <memory>
+
+std::unique_ptr<ModuleMock> TestFixture::mock_;
diff --git a/tests/mock/test_fixture.h b/tests/mock/test_fixture.h
new file mode 100644 (file)
index 0000000..db223f1
--- /dev/null
@@ -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 <gtest/gtest.h>
+
+#include <memory>
+#include <stdexcept>
+#include <string>
+#include <utility>
+
+#include "module_mock.h"
+
+class TestFixture : public ::testing::Test {
+ public:
+  explicit TestFixture(std::unique_ptr<ModuleMock>&& mock) {
+    mock_ = std::move(mock);
+  }
+  virtual ~TestFixture() {
+    mock_.reset();
+  }
+
+  virtual void SetUp() {}
+  virtual void TearDown() {}
+
+  template <typename T>
+  static T& GetMock() {
+    auto ptr = dynamic_cast<T*>(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<ModuleMock> mock_;
+};
+
+#endif  // MOCK_TEST_FIXTURE_H_
diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..5a3e660
--- /dev/null
@@ -0,0 +1,44 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.8)\r
+PROJECT(data_control_unittests C CXX)\r
+\r
+INCLUDE(FindPkgConfig)\r
+PKG_CHECK_MODULES(data_control_unittests REQUIRED\r
+    gmock\r
+    dlog\r
+    bundle\r
+    glib-2.0\r
+    gio-2.0\r
+    sqlite3\r
+    aul\r
+)\r
+\r
+FOREACH(flag ${data_control_unittests_CFLAGS})\r
+    SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")\r
+ENDFOREACH(flag)\r
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -Wall -Werror")\r
+\r
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -std=c++14")\r
+SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")\r
+SET(CMAKE_CXX_FLAGS_RELEASE "-O2")\r
+\r
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../include)\r
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../src)\r
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../mock)\r
+\r
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src TEST_SOURCES)\r
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../mock MOCK_SOURCES)\r
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../src LIB_SOURCES)\r
+\r
+ADD_EXECUTABLE(${PROJECT_NAME}\r
+    ${TEST_SOURCES}\r
+    ${LIB_SOURCES}\r
+    ${MOCK_SOURCES}\r
+)\r
+\r
+SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS}")\r
+TARGET_LINK_LIBRARIES(${PROJECT_NAME}\r
+    ${data_control_unittests_LDFLAGS}\r
+    data-control\r
+)\r
+\r
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION /usr/bin/)\r
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 (file)
index 0000000..5727f5d
--- /dev/null
@@ -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 <stdlib.h>
+#include <gtest/gtest.h>
+#include <stdio.h>
+#include <bundle.h>
+#include <bundle_cpp.h>
+#include <memory>
+
+#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<Mocks>()) {}
+  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 (file)
index 0000000..c2eb730
--- /dev/null
@@ -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 <gtest/gtest.h>
+#include <gmock/gmock.h>
+
+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;
+}