summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorInkyun Kil <inkyun.kil@samsung.com>2020-09-17 14:35:33 +0900
committerInkyun Kil <inkyun.kil@samsung.com>2020-09-18 16:39:51 +0900
commita8a5f55faa8880eff38874ca42d8178ba42e9aa7 (patch)
tree5a297380b4d4c6f570cb0c75707d7076690643e6
parentd30ac35e9d358c474f814b786ee6419c986a3d68 (diff)
downloadminicontrol-a8a5f55faa8880eff38874ca42d8178ba42e9aa7.tar.gz
minicontrol-a8a5f55faa8880eff38874ca42d8178ba42e9aa7.tar.bz2
minicontrol-a8a5f55faa8880eff38874ca42d8178ba42e9aa7.zip
Add unittests
Change-Id: Ief777950601eabd39772242efeb225217d9d4ebf Signed-off-by: Inkyun Kil <inkyun.kil@samsung.com>
-rwxr-xr-xCMakeLists.txt3
-rw-r--r--include/minicontrol.h4
-rw-r--r--packaging/minicontrol.spec19
-rw-r--r--unittests/CMakeLists.txt20
-rw-r--r--unittests/main.cc37
-rw-r--r--unittests/minicontrol_test.cc38
-rw-r--r--unittests/mock/elementary_mock.cc25
-rw-r--r--unittests/mock/elementary_mock.h32
-rw-r--r--unittests/mock/mock_hook.h42
-rw-r--r--unittests/mock/module_mock.h25
-rw-r--r--unittests/mock/test_fixture.cc21
-rw-r--r--unittests/mock/test_fixture.h53
12 files changed, 315 insertions, 4 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5c8afbd..3e3f528 100755
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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
diff --git a/include/minicontrol.h b/include/minicontrol.h
index e38488a..e8ec42f 100644
--- a/include/minicontrol.h
+++ b/include/minicontrol.h
@@ -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_ */
diff --git a/packaging/minicontrol.spec b/packaging/minicontrol.spec
index 0e5a675..858fbd0 100644
--- a/packaging/minicontrol.spec
+++ b/packaging/minicontrol.spec
@@ -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
index 0000000..63d2984
--- /dev/null
+++ b/unittests/CMakeLists.txt
@@ -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
index 0000000..beffc8a
--- /dev/null
+++ b/unittests/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 <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
index 0000000..7931fac
--- /dev/null
+++ b/unittests/minicontrol_test.cc
@@ -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
index 0000000..d581c70
--- /dev/null
+++ b/unittests/mock/elementary_mock.cc
@@ -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
index 0000000..948dc4b
--- /dev/null
+++ b/unittests/mock/elementary_mock.h
@@ -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
index 0000000..6d81cbc
--- /dev/null
+++ b/unittests/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<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
index 0000000..d9dd808
--- /dev/null
+++ b/unittests/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_ \ No newline at end of file
diff --git a/unittests/mock/test_fixture.cc b/unittests/mock/test_fixture.cc
new file mode 100644
index 0000000..85fe344
--- /dev/null
+++ b/unittests/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 <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
index 0000000..8db6123
--- /dev/null
+++ b/unittests/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 <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