diff options
author | mk5004.lee <mk5004.lee@samsung.com> | 2020-07-13 16:40:39 +0900 |
---|---|---|
committer | mk5004.lee <mk5004.lee@samsung.com> | 2020-07-23 14:06:08 +0900 |
commit | e9351459e318122b34bdc7ed5b80e7815b7dea2f (patch) | |
tree | 8e0a73b77da240d08e5560485957900ba00b5c90 | |
parent | 3ee298f2e8047bcca3ca2280271fa934d8312e2e (diff) | |
download | badge-e9351459e318122b34bdc7ed5b80e7815b7dea2f.tar.gz badge-e9351459e318122b34bdc7ed5b80e7815b7dea2f.tar.bz2 badge-e9351459e318122b34bdc7ed5b80e7815b7dea2f.zip |
Add unittest
Change-Id: Id3bd7887cbd6697cb1f9394a0c673144d697b9d9
Signed-off-by: mk5004.lee <mk5004.lee@samsung.com>
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | packaging/badge.spec | 42 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 8 | ||||
-rw-r--r-- | tests/mock/aul_mock.cc | 23 | ||||
-rw-r--r-- | tests/mock/aul_mock.h | 32 | ||||
-rw-r--r-- | tests/mock/gio_mock.cc | 98 | ||||
-rw-r--r-- | tests/mock/gio_mock.h | 67 | ||||
-rw-r--r-- | tests/mock/mock_hook.h | 42 | ||||
-rw-r--r-- | tests/mock/module_mock.h | 25 | ||||
-rw-r--r-- | tests/mock/package_manager_mock.cc | 24 | ||||
-rw-r--r-- | tests/mock/package_manager_mock.h | 33 | ||||
-rw-r--r-- | tests/mock/system_info_mock.cc | 23 | ||||
-rw-r--r-- | tests/mock/system_info_mock.h | 32 | ||||
-rw-r--r-- | tests/mock/test_fixture.cc | 21 | ||||
-rw-r--r-- | tests/mock/test_fixture.h | 53 | ||||
-rw-r--r-- | tests/unit_tests/CMakeLists.txt | 48 | ||||
-rw-r--r-- | tests/unit_tests/src/test_badge.cc | 52 | ||||
-rw-r--r-- | tests/unit_tests/src/test_main.cc | 37 |
18 files changed, 660 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index dd932e7..2398072 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,3 +62,5 @@ INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/include/ INSTALL(FILES ${CMAKE_BINARY_DIR}/11_badge-add.post DESTINATION ${SYSCONF_INSTALL_DIR}/gumd/useradd.d/) INSTALL(TARGETS badge_init DESTINATION bin) + +ADD_SUBDIRECTORY(tests) diff --git a/packaging/badge.spec b/packaging/badge.spec index 0f2f7e4..a6e8564 100644 --- a/packaging/badge.spec +++ b/packaging/badge.spec @@ -15,6 +15,14 @@ BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(pkgmgr-info) BuildRequires: pkgconfig(libsmack) BuildRequires: pkgconfig(capi-system-info) +BuildRequires: pkgconfig(gio-2.0) +BuildRequires: pkgconfig(gmock) + +%if 0%{?gcov:1} +BuildRequires: lcov +BuildRequires: zip +%endif + BuildRequires: cmake Requires(post): /sbin/ldconfig requires(postun): /sbin/ldconfig @@ -38,6 +46,20 @@ Requires: %{name} = %{version}-%{release} %description service-devel Development files needed to build badge service +################################################# +# badge-unittests +################################################# +%package -n badge_unittests +Summary: GTest for badge API +Group: Development/Libraries +Requires: %{name} + +%description -n badge_unittests +GTest for badge API + +################################################# +# gcov +################################################# %if 0%{?gcov:1} %package gcov Summary: Badge library (gcov) @@ -71,6 +93,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 badge.info +genhtml badge.info -o badge.out +zip -r badge.zip badge.out +install -m 0644 badge.zip %{buildroot}%{_datadir}/gcov/badge.zip +%endif + %install %make_install @@ -104,7 +135,6 @@ fi %attr(755,root,root) %{_sysconfdir}/gumd/useradd.d/11_badge-add.post %{_bindir}/badge_init - %files devel %defattr(-,root,root,-) %{_includedir}/badge/badge.h @@ -117,9 +147,17 @@ fi %{_libdir}/pkgconfig/%{name}.pc %{_libdir}/libbadge.so +################################################# +# badge_unittests +################################################# +%files -n badge_unittests +%{_bindir}/badge_unittests +################################################# +# badge-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 index 0000000..848994b --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,8 @@ +IF(NOT DEFINED MINIMUM_BUILD) +ENABLE_TESTING() +SET(BADGE_UNIT_TESTS badge_unittests) +ADD_TEST(NAME ${BADGE_UNIT_TESTS} COMMAND ${BADGE_UNIT_TESTS}) + +ADD_SUBDIRECTORY(unit_tests) +ADD_DEPENDENCIES(${BADGE_UNIT_TESTS} badge) +ENDIF(NOT DEFINED MINIMUM_BUILD) diff --git a/tests/mock/aul_mock.cc b/tests/mock/aul_mock.cc new file mode 100644 index 0000000..15952dd --- /dev/null +++ b/tests/mock/aul_mock.cc @@ -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 "aul_mock.h" +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" int aul_app_get_pkgname_bypid(int arg0, char* arg1, int arg2) { + return MOCK_HOOK_P3(AulMock, aul_app_get_pkgname_bypid, arg0, arg1, arg2); +} diff --git a/tests/mock/aul_mock.h b/tests/mock/aul_mock.h new file mode 100644 index 0000000..46bceef --- /dev/null +++ b/tests/mock/aul_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_AUL_MOCK_H_ +#define MOCK_AUL_MOCK_H_ + +#include <aul.h> +#include <gmock/gmock.h> + +#include "module_mock.h" + +class AulMock : public virtual ModuleMock { + public: + virtual ~AulMock() {} + + MOCK_METHOD3(aul_app_get_pkgname_bypid, int(int, char*, int)); +}; + +#endif // MOCK_AUL_MOCK_H_ diff --git a/tests/mock/gio_mock.cc b/tests/mock/gio_mock.cc new file mode 100644 index 0000000..cd176fb --- /dev/null +++ b/tests/mock/gio_mock.cc @@ -0,0 +1,98 @@ +/* + * 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 <gio/gio.h> + +#include "gio_mock.h" +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" GDBusConnection* g_bus_get_sync(GBusType type, + GCancellable* cancellable, GError** error) { + return MOCK_HOOK_P3(GioMock, g_bus_get_sync, type, cancellable, error); +} + +extern "C" GDBusMessage* g_dbus_connection_send_message_with_reply_sync( + GDBusConnection* conn, GDBusMessage* msg, GDBusSendMessageFlags flags, + gint timeout, volatile guint32* out_serial, GCancellable* cancellable, + GError** error) { + return MOCK_HOOK_P7(GioMock, g_dbus_connection_send_message_with_reply_sync, + conn, msg, flags, timeout, out_serial, cancellable, error); +} + +extern "C" GDBusMessage* g_dbus_message_new_method_call(const gchar* arg0, + const gchar* arg1, const gchar* arg2, const gchar* arg3) { + return MOCK_HOOK_P4(GioMock, g_dbus_message_new_method_call, arg0, arg1, arg2, + arg3); +} + +extern "C" void g_dbus_message_set_body(GDBusMessage* arg0, GVariant* arg1) { + return MOCK_HOOK_P2(GioMock, g_dbus_message_set_body, arg0, arg1); +} + +extern "C" GVariant* g_dbus_message_get_body(GDBusMessage* arg0) { + return MOCK_HOOK_P1(GioMock, g_dbus_message_get_body, arg0); +} + +extern "C" gboolean g_dbus_connection_emit_signal(GDBusConnection* arg0, + const gchar* arg1, const gchar* arg2, const gchar* arg3, const gchar* arg4, + GVariant* arg5, GError** arg6) { + return MOCK_HOOK_P7(GioMock, g_dbus_connection_emit_signal, + arg0, arg1, arg2, arg3, arg4, arg5, arg6); +} + +extern "C" GDBusNodeInfo* g_dbus_node_info_new_for_xml( + const gchar* arg0, GError** arg1) { + return MOCK_HOOK_P2(GioMock, g_dbus_node_info_new_for_xml, arg0, arg1); +} + +extern "C" guint g_dbus_connection_register_object(GDBusConnection* arg0, + const gchar* arg1, GDBusInterfaceInfo* arg2, + const GDBusInterfaceVTable* arg3, gpointer arg4, + GDestroyNotify arg5, GError** arg6) { + return MOCK_HOOK_P7(GioMock, g_dbus_connection_register_object, + arg0, arg1, arg2, arg3, arg4, arg5, arg6); +} + +extern "C" guint g_bus_own_name_on_connection(GDBusConnection* arg0, + const gchar* arg1, GBusNameOwnerFlags arg2, + GBusNameAcquiredCallback arg3, GBusNameLostCallback arg4, + gpointer arg5, GDestroyNotify arg6) { + return MOCK_HOOK_P7(GioMock, g_bus_own_name_on_connection, + arg0, arg1, arg2, arg3, arg4, arg5, arg6); +} + +extern "C" guint g_dbus_connection_signal_subscribe(GDBusConnection* arg0, + const gchar* arg1, const gchar* arg2, const gchar* arg3, const gchar* arg4, + const gchar* arg5, GDBusSignalFlags arg6, GDBusSignalCallback arg7, + gpointer arg8, GDestroyNotify arg9) { + return MOCK_HOOK_P10(GioMock, g_dbus_connection_signal_subscribe, + arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); +} + +extern "C" void g_dbus_connection_send_message_with_reply(GDBusConnection* arg0, + GDBusMessage* arg1, GDBusSendMessageFlags arg2, gint arg3, + volatile guint32* arg4, GCancellable* arg5, GAsyncReadyCallback arg6, + gpointer arg7) { + return MOCK_HOOK_P8(GioMock, g_dbus_connection_send_message_with_reply, + arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7); +} + +extern "C" GDBusMessage* g_dbus_connection_send_message_with_reply_finish( + GDBusConnection* arg0, GAsyncResult* arg1, GError** arg2) { + return MOCK_HOOK_P3(GioMock, g_dbus_connection_send_message_with_reply_finish, + arg0, arg1, arg2); +} diff --git a/tests/mock/gio_mock.h b/tests/mock/gio_mock.h new file mode 100644 index 0000000..ab343d0 --- /dev/null +++ b/tests/mock/gio_mock.h @@ -0,0 +1,67 @@ +/* + * 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_GIO_MOCK_H_ +#define MOCK_GIO_MOCK_H_ + +#include <gio/gio.h> +#include <gmock/gmock.h> + +#include "module_mock.h" + +class GioMock : public virtual ModuleMock { + public: + virtual ~GioMock() {} + + MOCK_METHOD3(g_bus_get_sync, + GDBusConnection*(GBusType, GCancellable*, GError**)); + MOCK_METHOD7(g_bus_own_name_on_connection, + guint(GDBusConnection*, const gchar*, GBusNameOwnerFlags, + GBusNameAcquiredCallback, GBusNameLostCallback, + gpointer, GDestroyNotify)); + + MOCK_METHOD4(g_dbus_message_new_method_call, + GDBusMessage*(const gchar*, const gchar*, const gchar*, const gchar*)); + MOCK_METHOD2(g_dbus_message_set_body, void(GDBusMessage*, GVariant*)); + MOCK_METHOD1(g_dbus_message_get_body, GVariant*(GDBusMessage*)); + + MOCK_METHOD7(g_dbus_connection_send_message_with_reply_sync, + GDBusMessage*(GDBusConnection*, GDBusMessage*, GDBusSendMessageFlags, + gint, volatile guint32*, GCancellable*, GError**)); + MOCK_METHOD7(g_dbus_connection_emit_signal, + gboolean(GDBusConnection*, const gchar*, const gchar*, const gchar*, + const gchar*, GVariant*, GError**)); + MOCK_METHOD7(g_dbus_connection_register_object, + guint(GDBusConnection*, const gchar*, GDBusInterfaceInfo*, + const GDBusInterfaceVTable*, gpointer, GDestroyNotify, GError**)); + MOCK_METHOD10(g_dbus_connection_signal_subscribe, + guint(GDBusConnection*, const gchar*, const gchar*, const gchar*, + const gchar*, const gchar*, GDBusSignalFlags, GDBusSignalCallback, + gpointer, GDestroyNotify)); + MOCK_METHOD8(g_dbus_connection_send_message_with_reply, + void(GDBusConnection*, GDBusMessage*, GDBusSendMessageFlags, gint, + volatile guint32*, GCancellable*, GAsyncReadyCallback, gpointer)); + MOCK_METHOD3(g_dbus_connection_send_message_with_reply_finish, + GDBusMessage*(GDBusConnection*, GAsyncResult*, GError**)); + + MOCK_METHOD2(g_dbus_node_info_new_for_xml, + GDBusNodeInfo*(const gchar*, GError**)); + + MOCK_METHOD2(g_dbus_method_invocation_return_value, + GDBusNodeInfo*(GDBusMethodInvocation*, GVariant*)); +}; + +#endif // MOCK_GIO_MOCK_H_ 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<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 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/package_manager_mock.cc b/tests/mock/package_manager_mock.cc new file mode 100644 index 0000000..f49ea96 --- /dev/null +++ b/tests/mock/package_manager_mock.cc @@ -0,0 +1,24 @@ +/* + * 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 "package_manager_mock.h" +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" int package_manager_compare_app_cert_info(const char* arg0, const char* arg1, package_manager_compare_result_type_e* arg2) { + return MOCK_HOOK_P3(PackageManagerMock, package_manager_compare_app_cert_info, arg0, arg1, arg2); +} + diff --git a/tests/mock/package_manager_mock.h b/tests/mock/package_manager_mock.h new file mode 100644 index 0000000..4552053 --- /dev/null +++ b/tests/mock/package_manager_mock.h @@ -0,0 +1,33 @@ +/* + * 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_PACKAGE_MANAGER_MOCK_H_ +#define MOCK_PACKAGE_MANAGER_MOCK_H_ + +#include <package_manager.h> +#include <gmock/gmock.h> + +#include "module_mock.h" + +class PackageManagerMock : public virtual ModuleMock { + public: + virtual ~PackageManagerMock() {} + + MOCK_METHOD3(package_manager_compare_app_cert_info, int(const char*, const char*, package_manager_compare_result_type_e*)); +}; + +#endif // MOCK_PACKAGE_MANAGER_MOCK_H_ + diff --git a/tests/mock/system_info_mock.cc b/tests/mock/system_info_mock.cc new file mode 100644 index 0000000..cd6917e --- /dev/null +++ b/tests/mock/system_info_mock.cc @@ -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 index 0000000..eecbc52 --- /dev/null +++ b/tests/mock/system_info_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_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 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 <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 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 <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 index 0000000..74d7b75 --- /dev/null +++ b/tests/unit_tests/CMakeLists.txt @@ -0,0 +1,48 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.8) +PROJECT(badge_unittests) + +INCLUDE(FindPkgConfig) +PKG_CHECK_MODULES(badge_unittests REQUIRED + gmock + aul + dlog + dbus-1 + vconf + capi-appfw-package-manager + capi-system-info + db-util + libtzplatform-config + pkgmgr-info + libsmack + gio-2.0 +) + +FOREACH(flag ${badge_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 SOURCES) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../mock MOCK_SOURCES) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../src LIB_SOURCES) +list(REMOVE_ITEM LIB_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/../../src/badge_init.c") + +ADD_EXECUTABLE(${PROJECT_NAME} + ${SOURCES} + ${MOCK_SOURCES} + ${LIB_SOURCES} +) + +TARGET_LINK_LIBRARIES(${PROJECT_NAME} + ${badge_unittests_LDFLAGS} +) + +INSTALL(TARGETS ${PROJECT_NAME} DESTINATION /usr/bin/) diff --git a/tests/unit_tests/src/test_badge.cc b/tests/unit_tests/src/test_badge.cc new file mode 100644 index 0000000..cc531d5 --- /dev/null +++ b/tests/unit_tests/src/test_badge.cc @@ -0,0 +1,52 @@ +/* + * 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 <badge.h> + +#include <memory> + +#include "system_info_mock.h" +#include "gio_mock.h" +#include "aul_mock.h" +#include "package_manager_mock.h" +#include "test_fixture.h" + +using ::testing::_; +using ::testing::DoAll; +using ::testing::Return; +using ::testing::SetArgPointee; +using ::testing::Invoke; +using ::testing::SetArrayArgument; + +class Mocks : public ::testing::NiceMock<SystemInfoMock>, + ::testing::NiceMock<GioMock>, + ::testing::NiceMock<AulMock>, + ::testing::NiceMock<PackageManagerMock> { +}; + +class BadgeTest : public TestFixture { + public: + BadgeTest() : TestFixture(std::make_unique<Mocks>()) {} + virtual ~BadgeTest() {} + + virtual void SetUp() { + } + + virtual void TearDown() { + } +}; 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 <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; +} |