move check stuff to its own library to be used by other modules
[platform/upstream/gstreamer.git] / gst / check / gstcheck.c
1 /* GStreamer
2  *
3  * Common code for GStreamer unittests
4  *
5  * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include "gstcheck.h"
24
25 GST_DEBUG_CATEGORY (check_debug);
26
27 /* logging function for tests
28  * a test uses g_message() to log a debug line
29  * a gst unit test can be run with GST_TEST_DEBUG env var set to see the
30  * messages
31  */
32
33 gboolean _gst_check_threads_running = FALSE;
34 GList *thread_list = NULL;
35 GMutex *mutex;
36 GCond *start_cond;              /* used to notify main thread of thread startups */
37 GCond *sync_cond;               /* used to synchronize all threads and main thread */
38
39 gboolean _gst_check_debug = FALSE;
40 gboolean _gst_check_raised_critical = FALSE;
41 gboolean _gst_check_expecting_log = FALSE;
42
43 void gst_check_log_message_func
44     (const gchar * log_domain, GLogLevelFlags log_level,
45     const gchar * message, gpointer user_data)
46 {
47   if (_gst_check_debug) {
48     g_print (message);
49   }
50 }
51
52 void gst_check_log_critical_func
53     (const gchar * log_domain, GLogLevelFlags log_level,
54     const gchar * message, gpointer user_data)
55 {
56   if (!_gst_check_expecting_log) {
57     g_print ("\n\nUnexpected critical/warning: %s\n", message);
58     fail ("Unexpected critical/warning: %s", message);
59   }
60
61   if (_gst_check_debug) {
62     g_print ("\nExpected critical/warning: %s\n", message);
63   }
64
65   if (log_level & G_LOG_LEVEL_CRITICAL)
66     _gst_check_raised_critical = TRUE;
67 }
68
69 /* initialize GStreamer testing */
70 void
71 gst_check_init (int *argc, char **argv[])
72 {
73   gst_init (argc, argv);
74
75   GST_DEBUG_CATEGORY_INIT (check_debug, "check", 0, "check regression tests");
76
77   if (g_getenv ("GST_TEST_DEBUG"))
78     _gst_check_debug = TRUE;
79
80   g_log_set_handler (NULL, G_LOG_LEVEL_MESSAGE, gst_check_log_message_func,
81       NULL);
82   g_log_set_handler (NULL, G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
83       gst_check_log_critical_func, NULL);
84   g_log_set_handler ("GStreamer", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
85       gst_check_log_critical_func, NULL);
86   g_log_set_handler ("GLib-GObject", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
87       gst_check_log_critical_func, NULL);
88 }