Adding initial structure for unittest 79/193479/4
authorjiyong.min <jiyong.min@samsung.com>
Wed, 21 Nov 2018 07:07:54 +0000 (16:07 +0900)
committerjiyong.min <jiyong.min@samsung.com>
Mon, 26 Nov 2018 05:34:18 +0000 (14:34 +0900)
- It has testcases for libmedia_utils of unittest
- It would be enabled by gtest build with '--define "gtests 1"'
- It would be executed by only user-mode

Change-Id: Iccb00c69fb39de5e25bae50089f56ae1f0184370

Makefile.am [changed mode: 0755->0644]
configure.ac
packaging/media-server.spec
unittest/Makefile.am [new file with mode: 0644]
unittest/media_server_unittest.cpp [new file with mode: 0644]
unittest/media_server_unittest.h [new file with mode: 0644]

old mode 100755 (executable)
new mode 100644 (file)
index 082c084..3b038a4
@@ -2,6 +2,9 @@ ACLOCAL_AMFLAGS = -I m4
 AUTOMAKE_OPTIONS = subdir-objects
 
 SUBDIRS = .
+if IS_TESTS
+SUBDIRS += unittest
+endif
 
 AM_CPPFLAGS = $(FMS_DEBUG_FLAGS)
 
index d7f5c99..23b803e 100644 (file)
@@ -15,6 +15,7 @@ AC_CONFIG_MACRO_DIR([m4])
 m4_ifdef([AM_PROG_AR], [AM_PROG_AR])
 AC_PROG_CC
 AM_PROG_CC_C_O
+AC_PROG_CXX
 AC_PROG_INSTALL
 AC_PROG_MAKE_SET
 AC_PROG_LIBTOOL
@@ -155,6 +156,24 @@ if test "x$USE_PRODUCT_TV" = "xyes"; then
 fi
 AM_CONDITIONAL(USE_PRODUCT_TV, test "x$USE_PRODUCT_TV" = "xyes")
 
+# for gtests
+AC_ARG_ENABLE(tests, AC_HELP_STRING([--enable-tests], [unittest build]),
+        [
+               case "${enableval}" in
+               yes) IS_TESTS=yes ;;
+               no)  IS_TESTS=no ;;
+                 *)   AC_MSG_ERROR(bad value ${enableval} for --enable-tests) ;;
+               esac
+       ],
+[IS_TESTS=no])
+AM_CONDITIONAL([IS_TESTS], [test "x$IS_TESTS" = "xyes"])
+
+AS_IF([test "x$enable_tests" = "xyes"], [
+       PKG_CHECK_MODULES(GTESTS, gmock)
+       AC_SUBST(GTESTS_CFLAGS)
+       AC_SUBST(GTESTS_LIBS)
+])
+
 #Checks for header files.
 AC_HEADER_DIRENT
 AC_HEADER_STDC
@@ -167,6 +186,7 @@ AC_C_CONST
 AC_TYPE_SIZE_T
 AC_HEADER_TIME
 AC_STRUCT_TM
+AC_SUBST(GCC_CXXFLAGS)
 
 # Checks for library functions.
 AC_FUNC_CLOSEDIR_VOID
@@ -177,6 +197,7 @@ AC_FUNC_MALLOC
 AC_CHECK_FUNCS([gettimeofday memset mkdir strcasecmp strstr])
 
 AC_CONFIG_FILES([Makefile
-                                libmedia-utils.pc
-                                ])
+                unittest/Makefile
+                libmedia-utils.pc
+])
 AC_OUTPUT
index 4498ec3..b1291c7 100755 (executable)
@@ -25,7 +25,9 @@ BuildRequires:  pkgconfig(cynara-client)
 BuildRequires:  pkgconfig(cynara-session)
 BuildRequires:  pkgconfig(capi-system-info)
 BuildRequires:  pkgconfig(capi-system-device)
-
+%if 0%{?gtests:1}
+BuildRequires:  pkgconfig(gmock)
+%endif
 %if "%{tizen_profile_name}" == "tv"
 BuildRequires:  pkgconfig(capi-system-usbdevice)
 BuildRequires:  pkgconfig(power-defs)
@@ -77,6 +79,9 @@ rm -rf autom4te.cache
 rm -f aclocal.m4 ltmain.sh
 mkdir -p m4
 %reconfigure --prefix=%{_prefix} \
+%if 0%{?gtests:1}
+       --enable-tests \
+%endif
 %if 0%{?product_tv}
        --enable-product-tv \
 %else
@@ -134,6 +139,9 @@ chmod 755 /etc/gumd/useradd.d/30_media-server-add.post
 %{_bindir}/media-scanner
 %{_bindir}/media-scanner-v2
 %{_bindir}/mediadb-update
+%if 0%{?gtests:1}
+%{_bindir}/gtest*
+%endif
 %{_unitdir}/media-server.service
 %{_unitdir}/multi-user.target.wants/media-server.service
 %if !0%{?product_tv}
diff --git a/unittest/Makefile.am b/unittest/Makefile.am
new file mode 100644 (file)
index 0000000..176529a
--- /dev/null
@@ -0,0 +1,18 @@
+# Unit tests
+bin_PROGRAMS = gtest-media-server
+
+gtest_media_server_SOURCES  = media_server_unittest.cpp
+
+gtest_media_server_CXXFLAGS =  -I$(top_srcdir)/lib/include \
+                       $(GLIB_CFLAGS) \
+                       $(GTESTS_CFLAGS) \
+                       $(TZ_PLATFORM_CONFIG_CFLAGS) \
+                       -Werror -Wno-deprecated -Wno-deprecated-declarations -Wno-cpp
+
+gtest_media_server_DEPENDENCIES = $(top_srcdir)/.libs/libmedia-utils.la
+
+gtest_media_server_LDADD = \
+                       $(GLIB_LIBS) \
+                       $(GTESTS_LIBS) \
+                       $(TZ_PLATFORM_CONFIG_LIBS) \
+                       $(top_srcdir)/.libs/libmedia-utils.la
diff --git a/unittest/media_server_unittest.cpp b/unittest/media_server_unittest.cpp
new file mode 100644 (file)
index 0000000..b51e773
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 <glib.h>
+#include <tzplatform_config.h>
+
+#include "media_server_unittest.h"
+#include "media-util.h"
+
+#define        TIMEOUT_SECONDS 5000
+
+//Add helper function declarations here
+static GMainLoop *g_ms_mainloop = NULL;
+static gboolean g_async_result = FALSE;
+gboolean timeout_func(gpointer data)
+{
+       g_main_loop_quit((GMainLoop *) data);
+
+       return FALSE;
+}
+
+static void wait_async_cb(void)
+{
+       int timeout_id = 0;
+       g_ms_mainloop = g_main_loop_new(NULL, FALSE);
+
+       // init
+       g_async_result = FALSE;
+
+       timeout_id = g_timeout_add(TIMEOUT_SECONDS, timeout_func, g_ms_mainloop);
+       g_main_loop_run(g_ms_mainloop);
+       g_source_remove(timeout_id);
+       g_main_loop_unref(g_ms_mainloop);
+       g_ms_mainloop = NULL;
+}
+
+void signal_async(gboolean async_result)
+{
+       g_async_result = async_result;
+
+       if (g_ms_mainloop != NULL) {
+               g_main_loop_quit(g_ms_mainloop);
+       }
+}
+
+void _scan_complete_cb(media_request_result_s *result, void *user_data)
+{
+       signal_async(TRUE);
+}
+
+using ::testing::InitGoogleTest;
+using ::testing::Test;
+using ::testing::TestCase;
+
+class media_server_Test : public ::testing::Test {
+       protected:
+               void SetUp() {
+                       std::cout << "SetUp()" << std::endl;
+               }
+
+               void TearDown() {
+                       std::cout << "TearDown()" << std::endl;
+               }
+};
+
+TEST(media_server_Test, media_directory_scanning_async_p)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+
+       ret = media_directory_scanning_async(tzplatform_getenv(TZ_USER_CONTENT), "media", false, _scan_complete_cb, NULL, getuid());
+       EXPECT_EQ(ret, MS_MEDIA_ERR_NONE);
+
+       wait_async_cb();
+       EXPECT_TRUE(g_async_result);
+}
+
+TEST(media_server_Test, media_directory_scanning_async_n)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+
+       ret = media_directory_scanning_async(NULL, NULL, false, NULL, NULL, getuid());
+       EXPECT_EQ(ret, MS_MEDIA_ERR_INVALID_PATH);
+}
+
+int main(int argc, char **argv)
+{
+       InitGoogleTest(&argc, argv);
+
+       return RUN_ALL_TESTS();
+}
diff --git a/unittest/media_server_unittest.h b/unittest/media_server_unittest.h
new file mode 100644 (file)
index 0000000..e790934
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 __MEDIA_SERVER_UNITTEST_H__
+#define __MEDIA_SERVER_UNITTEST_H__
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#undef LOG_TAG
+#define LOG_TAG                "GTEST_MEDIA_SERVER"
+
+#endif /*__MEDIA_SERVER_UNITTEST_H__*/