Add unittests 02/244302/3
authorInkyun Kil <inkyun.kil@samsung.com>
Thu, 17 Sep 2020 05:35:33 +0000 (14:35 +0900)
committerInkyun Kil <inkyun.kil@samsung.com>
Fri, 18 Sep 2020 07:39:51 +0000 (16:39 +0900)
Change-Id: Ief777950601eabd39772242efeb225217d9d4ebf
Signed-off-by: Inkyun Kil <inkyun.kil@samsung.com>
12 files changed:
CMakeLists.txt
include/minicontrol.h
packaging/minicontrol.spec
unittests/CMakeLists.txt [new file with mode: 0644]
unittests/main.cc [new file with mode: 0644]
unittests/minicontrol_test.cc [new file with mode: 0644]
unittests/mock/elementary_mock.cc [new file with mode: 0644]
unittests/mock/elementary_mock.h [new file with mode: 0644]
unittests/mock/mock_hook.h [new file with mode: 0644]
unittests/mock/module_mock.h [new file with mode: 0644]
unittests/mock/test_fixture.cc [new file with mode: 0644]
unittests/mock/test_fixture.h [new file with mode: 0644]

index 5c8afbdb3a4a48bbdcd15bee3b032d03735f8ee6..3e3f528e3a8bd255b85cefd69accd716d9e1969c 100755 (executable)
@@ -1,5 +1,5 @@
 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
-PROJECT(minicontrol C)
+PROJECT(minicontrol C CXX)
 
 SET(PREFIX ${CMAKE_INSTALL_PREFIX})
 SET(EXEC_PREFIX "\${prefix}")
@@ -70,3 +70,4 @@ FOREACH(hfile ${INSTALL_HEADERS})
        INSTALL(FILES ${CMAKE_SOURCE_DIR}/include/${hfile} DESTINATION include/${PROJECT_NAME})
 ENDFOREACH(hfile)
 
+ADD_SUBDIRECTORY(unittests)
\ No newline at end of file
index e38488ae155f39d8e1c27f732d81f7aabfa6523a..e8ec42f9a78819dfa51b50531bd7c7380bf6a717 100644 (file)
@@ -19,9 +19,9 @@
 
 #include <minicontrol-internal.h>
 #include <minicontrol-provider.h>
-#include <minicontrol-provider_internal.h>
+#include <minicontrol-provider-internal.h>
 #include <minicontrol-viewer.h>
-#include <minicontrol-viewer_internal.h>
+#include <minicontrol-viewer-internal.h>
 
 
 #endif /* _MINICONTROL_H_DEF_ */
index 0e5a6752be0d22eadafc9725b64bd2149007549f..858fbd0af85204a284062152a37755cccb2f74d5 100644 (file)
@@ -13,9 +13,16 @@ BuildRequires: pkgconfig(ecore-evas)
 BuildRequires: pkgconfig(dlog)
 BuildRequires: pkgconfig(bundle)
 BuildRequires: pkgconfig(capi-system-info)
+BuildRequires: pkgconfig(gmock)
 BuildRequires: cmake
 Requires(post): /sbin/ldconfig
 requires(postun): /sbin/ldconfig
+
+%if 0%{?gcov:1}
+BuildRequires: lcov
+BuildRequires: zip
+%endif
+
 %description
 Minicontrol is a small control panel on notification panel or lock screen. This package supports to develop minicontrols.
 
@@ -71,6 +78,16 @@ mkdir -p %{buildroot}%{_datadir}/gcov/obj
 install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj
 %endif
 
+%check
+cd unittests
+LD_LIBRARY_PATH=../ ctest -V
+%if 0%{?gcov:1}
+lcov -c --ignore-errors graph --no-external -q -d . -o minicontrol.info
+genhtml minicontrol.info -o minicontrol.out
+zip -r minicontrol.zip minicontrol.out
+install -m 0644 minicontrol.zip %{buildroot}%{_datadir}/gcov/
+%endif
+
 %post
 /sbin/ldconfig
 
@@ -96,5 +113,5 @@ install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj
 
 %if 0%{?gcov:1}
 %files gcov
-%{_datadir}/gcov/obj/*
+%{_datadir}/gcov/*
 %endif
diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..63d2984
--- /dev/null
@@ -0,0 +1,20 @@
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} TEST_SRCS)
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/mock MOCK_SRCS)
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../include/)
+
+ENABLE_TESTING()
+
+SET(TARGET_TEST "minicontrol-unittest")
+ADD_EXECUTABLE(${TARGET_TEST} ${TEST_SRCS} ${MOCK_SRCS})
+ADD_TEST(${TARGET_TEST} ${TARGET_TEST})
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(test REQUIRED dlog glib-2.0 elementary)
+FOREACH(flag ${test_CFLAGS})
+    SET(EXTRA_CXXFLAGS_test "${EXTRA_CXXFLAGS_test} ${flag}")
+ENDFOREACH(flag)
+
+SET(${EXTRA_CXXFLAGS_test} "${EXTRA_CXXFLAGS_test} --std=c++14")
+SET_TARGET_PROPERTIES(${TARGET_TEST} PROPERTIES COMPILE_FLAGS ${EXTRA_CXXFLAGS_test})
+
+TARGET_LINK_LIBRARIES(${TARGET_TEST} gmock minicontrol-viewer)
\ No newline at end of file
diff --git a/unittests/main.cc b/unittests/main.cc
new file mode 100644 (file)
index 0000000..beffc8a
--- /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;
+}
\ No newline at end of file
diff --git a/unittests/minicontrol_test.cc b/unittests/minicontrol_test.cc
new file mode 100644 (file)
index 0000000..7931fac
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * 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 <gmock/gmock.h>
+
+#include <minicontrol.h>
+
+#include "mock/elementary_mock.h"
+#include "mock/test_fixture.h"
+
+class Mocks : public ::testing::NiceMock<ElementaryMock> {};
+
+class MinicontrolTest : public TestFixture {
+ public:
+  MinicontrolTest() : TestFixture(std::make_unique<Mocks>()) {}
+  virtual ~MinicontrolTest() {}
+
+  virtual void SetUp() {}
+  virtual void TearDown() {}
+};
+
+TEST_F(MinicontrolTest, minicontrol_viewer_add_n) {
+  Evas_Object* plug = minicontrol_viewer_add(nullptr, "minicontrol_name");
+  EXPECT_EQ(plug, nullptr);
+}
\ No newline at end of file
diff --git a/unittests/mock/elementary_mock.cc b/unittests/mock/elementary_mock.cc
new file mode 100644 (file)
index 0000000..d581c70
--- /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.
+ */
+
+#include "elementary_mock.h"
+
+#include "mock_hook.h"
+#include "test_fixture.h"
+
+
+extern "C" Evas_Object* elm_plug_add(Evas_Object* arg0) {
+  MOCK_HOOK_P1(ElementaryMock, elm_plug_add, arg0);
+}
diff --git a/unittests/mock/elementary_mock.h b/unittests/mock/elementary_mock.h
new file mode 100644 (file)
index 0000000..948dc4b
--- /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_ELEMENTARY_MOCK_H_
+#define MOCK_ELEMENTARY_MOCK_H_
+#include <gmock/gmock.h>
+
+#include <Elementary.h>
+
+#include "module_mock.h"
+
+class ElementaryMock : public virtual ModuleMock {
+ public:
+  virtual ~ElementaryMock() {}
+
+  MOCK_METHOD1(elm_plug_add, Evas_Object*(Evas_Object*));
+};
+
+#endif  // MOCK_ELEMENTARY_MOCK_H_
\ No newline at end of file
diff --git a/unittests/mock/mock_hook.h b/unittests/mock/mock_hook.h
new file mode 100644 (file)
index 0000000..6d81cbc
--- /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_
\ No newline at end of file
diff --git a/unittests/mock/module_mock.h b/unittests/mock/module_mock.h
new file mode 100644 (file)
index 0000000..d9dd808
--- /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_
\ No newline at end of file
diff --git a/unittests/mock/test_fixture.cc b/unittests/mock/test_fixture.cc
new file mode 100644 (file)
index 0000000..85fe344
--- /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 <memory>
+
+#include "test_fixture.h"
+
+std::unique_ptr<ModuleMock> TestFixture::mock_;
\ No newline at end of file
diff --git a/unittests/mock/test_fixture.h b/unittests/mock/test_fixture.h
new file mode 100644 (file)
index 0000000..8db6123
--- /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_
\ No newline at end of file