Add unittest 87/238387/4
authormk5004.lee <mk5004.lee@samsung.com>
Mon, 13 Jul 2020 08:07:41 +0000 (17:07 +0900)
committermk5004.lee <mk5004.lee@samsung.com>
Wed, 22 Jul 2020 08:30:20 +0000 (17:30 +0900)
Change-Id: I555fe2cb3f4b6f1fcdcdad68be0d77121ea42ce3
Signed-off-by: mk5004.lee <mk5004.lee@samsung.com>
17 files changed:
CMakeLists.txt
packaging/libshortcut.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/system_info_mock.cc [new file with mode: 0644]
tests/mock/system_info_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/test/Makefile [moved from test/Makefile with 100% similarity]
tests/test/application.c [moved from test/application.c with 100% similarity]
tests/test/homescreen.c [moved from test/homescreen.c with 100% similarity]
tests/test/icon.c [moved from test/icon.c with 100% similarity]
tests/test/shortcut.c [moved from test/shortcut.c with 100% similarity]
tests/unit_tests/CMakeLists.txt [new file with mode: 0644]
tests/unit_tests/src/test_main.cc [new file with mode: 0644]
tests/unit_tests/src/test_shortcut_manager.cc [new file with mode: 0644]

index ea60a32..f8dd624 100644 (file)
@@ -2,5 +2,6 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
 
 set(CMAKE_SKIP_BUILD_RPATH true)
 
-ADD_SUBDIRECTORY("pkgmgr_shortcut")
-ADD_SUBDIRECTORY("lib")
+ADD_SUBDIRECTORY(pkgmgr_shortcut)
+ADD_SUBDIRECTORY(lib)
+ADD_SUBDIRECTORY(tests)
index 297bb19..69320b6 100644 (file)
@@ -21,10 +21,38 @@ BuildRequires: pkgconfig(capi-base-common)
 BuildRequires: pkgconfig(aul)
 BuildRequires: pkgconfig(libtzplatform-config)
 BuildRequires: pkgconfig(capi-system-info)
+BuildRequires: pkgconfig(gmock)
+
+%if 0%{?gcov:1}
+BuildRequires:  lcov
+BuildRequires:  zip
+%endif
 
 %description
 [Shortcut] AddToHome feature supporting library for menu/home screen developers.
 
+%package devel
+Summary:    AddToHome feature supporting library development files
+Group:      Development/Libraries
+Requires:   %{name} = %{version}-%{release}
+
+%description devel
+[Shortcut] AddToHome feature supporting library for menu/home screen developers(dev).
+
+#################################################
+# capi-appfw-app-common-unittests
+#################################################
+%package -n shortcut_unittests
+Summary:    GTest for shortcut API
+Group:      Development/Libraries
+Requires:   %{name}
+
+%description -n shortcut_unittests
+GTest for shortcut API
+
+#################################################
+# gcov
+#################################################
 %if 0%{?gcov:1}
 %package gcov
 Summary:  Shortcut API(gcov)
@@ -34,14 +62,6 @@ Group:    System/API
 gcov objects of an Shortcut library
 %endif
 
-%package devel
-Summary:    AddToHome feature supporting library development files
-Group:      Development/Libraries
-Requires:   %{name} = %{version}-%{release}
-
-%description devel
-[Shortcut] AddToHome feature supporting library for menu/home screen developers(dev).
-
 %prep
 %setup -q
 cp %{SOURCE1001} .
@@ -67,6 +87,15 @@ mkdir -p gcov-obj
 find . -name '*.gcno' -exec cp '{}' gcov-obj ';'
 %endif
 
+%check
+ctest --output-on-failure %{?_smp_mflags}
+%if 0%{?gcov:1}
+lcov -c --ignore-errors graph --no-external -q -d . -o shortcut.info
+genhtml shortcut.info -o shortcut.out
+zip -r shortcut.zip shortcut.out
+install -m 0644 shortcut.zip %{buildroot}%{_datadir}/gcov/badge.zip
+%endif
+
 %install
 rm -rf %{buildroot}
 
@@ -107,9 +136,16 @@ fi
 %{_libdir}/pkgconfig/shortcut.pc
 %{_libdir}/libshortcut.so
 
+#################################################
+# shortcut_unittests
+#################################################
+%files -n shortcut_unittests
+%{_bindir}/shortcut_unittests
 
+#################################################
+# gcov
+#################################################
 %if 0%{?gcov:1}
 %files gcov
-%{_datadir}/gcov/obj/*
+%{_datadir}/gcov/*
 %endif
-# End of a file
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..449cb2a
--- /dev/null
@@ -0,0 +1,8 @@
+IF(NOT DEFINED MINIMUM_BUILD)
+ENABLE_TESTING()
+SET(SHORTCUT_UNIT_TESTS shortcut_unittests)
+ADD_TEST(NAME ${SHORTCUT_UNIT_TESTS} COMMAND ${SHORTCUT_UNIT_TESTS})
+
+ADD_SUBDIRECTORY(unit_tests)
+ADD_DEPENDENCIES(${SHORTCUT_UNIT_TESTS} shortcut)
+ENDIF(NOT DEFINED MINIMUM_BUILD)
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/system_info_mock.cc b/tests/mock/system_info_mock.cc
new file mode 100644 (file)
index 0000000..cd6917e
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * 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 "system_info_mock.h"
+#include "mock_hook.h"
+#include "test_fixture.h"
+
+extern "C" int system_info_get_platform_bool(const char* arg0, bool* arg1) {
+  return MOCK_HOOK_P2(SystemInfoMock, system_info_get_platform_bool, arg0, arg1);
+}
diff --git a/tests/mock/system_info_mock.h b/tests/mock/system_info_mock.h
new file mode 100644 (file)
index 0000000..eecbc52
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * 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_SYSTEM_INFO_MOCK_H_
+#define MOCK_SYSTEM_INFO_MOCK_H_
+
+#include <tizen.h>
+#include <gmock/gmock.h>
+
+#include "module_mock.h"
+
+class SystemInfoMock : public virtual ModuleMock {
+ public:
+  virtual ~SystemInfoMock() {}
+
+  MOCK_METHOD2(system_info_get_platform_bool, int(const char*, bool*));
+};
+
+#endif  // MOCK_SYSTEM_INFO_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_
similarity index 100%
rename from test/Makefile
rename to tests/test/Makefile
similarity index 100%
rename from test/application.c
rename to tests/test/application.c
similarity index 100%
rename from test/homescreen.c
rename to tests/test/homescreen.c
similarity index 100%
rename from test/icon.c
rename to tests/test/icon.c
similarity index 100%
rename from test/shortcut.c
rename to tests/test/shortcut.c
diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..325a58d
--- /dev/null
@@ -0,0 +1,46 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.8)\r
+PROJECT(shortcut_unittests C CXX)\r
+\r
+INCLUDE(FindPkgConfig)\r
+PKG_CHECK_MODULES(shortcut_unittests REQUIRED\r
+       gmock\r
+       dlog\r
+       sqlite3\r
+       libxml-2.0\r
+       glib-2.0\r
+       db-util\r
+       vconf\r
+       capi-base-common\r
+       aul\r
+       capi-system-info\r
+       libtzplatform-config\r
+)\r
+\r
+FOREACH(flag ${shortcut_unittests_CFLAGS})\r
+    SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")\r
+ENDFOREACH(flag)\r
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -Wall -Werror -Winline")\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}/../../lib/include)\r
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../lib/src)\r
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../mock)\r
+\r
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src SOURCES)\r
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../lib/src LIB_SOURCES)\r
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../mock MOCK_SOURCES)\r
+\r
+ADD_EXECUTABLE(${PROJECT_NAME}\r
+       ${SOURCES}\r
+       ${MOCK_SOURCES}\r
+       #${LIB_SOURCES}\r
+)\r
+\r
+TARGET_LINK_LIBRARIES(${PROJECT_NAME}\r
+    ${shortcut_unittests_LDFLAGS}\r
+)\r
+\r
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION /usr/bin/)\r
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;
+}
diff --git a/tests/unit_tests/src/test_shortcut_manager.cc b/tests/unit_tests/src/test_shortcut_manager.cc
new file mode 100644 (file)
index 0000000..8b3e1f6
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * 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 <shortcut_manager.h>
+
+#include <memory>
+
+#include "system_info_mock.h"
+#include "test_fixture.h"
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::Return;
+using ::testing::SetArgPointee;
+using ::testing::Invoke;
+
+class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
+
+class ShortcutTest : public TestFixture {
+ public:
+  ShortcutTest() : TestFixture(std::make_unique<Mocks>()) {}
+  virtual ~ShortcutTest() {}
+
+  virtual void SetUp() {
+  }
+
+  virtual void TearDown() {
+  }
+};