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 */
23 #include "gstcheck.h"
25 GST_DEBUG_CATEGORY (check_debug);
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 */
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 */
39 gboolean _gst_check_debug = FALSE;
40 gboolean _gst_check_raised_critical = FALSE;
41 gboolean _gst_check_expecting_log = FALSE;
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 }
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 }
61 if (_gst_check_debug) {
62 g_print ("\nExpected critical/warning: %s\n", message);
63 }
65 if (log_level & G_LOG_LEVEL_CRITICAL)
66 _gst_check_raised_critical = TRUE;
67 }
69 /* initialize GStreamer testing */
70 void
71 gst_check_init (int *argc, char **argv[])
72 {
73 gst_init (argc, argv);
75 GST_DEBUG_CATEGORY_INIT (check_debug, "check", 0, "check regression tests");
77 if (g_getenv ("GST_TEST_DEBUG"))
78 _gst_check_debug = TRUE;
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 }
90 /* message checking */
91 void
92 gst_check_message_error (GstMessage * message, GstMessageType type,
93 GQuark domain, gint code)
94 {
95 GError *error;
96 gchar *debug;
98 fail_unless_equals_int (GST_MESSAGE_TYPE (message), type);
99 gst_message_parse_error (message, &error, &debug);
100 fail_unless_equals_int (error->domain, domain);
101 fail_unless_equals_int (error->code, code);
102 g_error_free (error);
103 g_free (debug);
104 }
106 /* helper functions */
107 GstFlowReturn
108 gst_check_chain_func (GstPad * pad, GstBuffer * buffer)
109 {
110 GST_DEBUG ("chain_func: received buffer %p", buffer);
111 buffers = g_list_append (buffers, buffer);
113 return GST_FLOW_OK;
114 }
116 /* setup an element for a filter test with mysrcpad and mysinkpad */
117 GstElement *
118 gst_check_setup_element (const gchar * factory)
119 {
120 GstElement *element;
122 GST_DEBUG ("setup_element");
124 element = gst_element_factory_make (factory, factory);
125 fail_if (element == NULL, "Could not create a %s", factory);
126 ASSERT_OBJECT_REFCOUNT (element, factory, 1);
127 return element;
128 }
130 void
131 gst_check_teardown_element (GstElement * element)
132 {
133 GST_DEBUG ("teardown_element");
135 fail_unless (gst_element_set_state (element, GST_STATE_NULL) ==
136 GST_STATE_SUCCESS, "could not set to null");
137 ASSERT_OBJECT_REFCOUNT (element, "element", 1);
138 gst_object_unref (element);
139 }
141 /* FIXME: set_caps isn't that useful
142 */
143 GstPad *
144 gst_check_setup_src_pad (GstElement * element,
145 GstStaticPadTemplate * srctemplate, GstCaps * caps)
146 {
147 GstPad *srcpad, *sinkpad;
149 GST_DEBUG_OBJECT (element, "setting up sending pad");
150 /* sending pad */
151 srcpad =
152 gst_pad_new_from_template (gst_static_pad_template_get (srctemplate),
153 "src");
154 fail_if (srcpad == NULL, "Could not create a srcpad");
155 ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
157 sinkpad = gst_element_get_pad (element, "sink");
158 fail_if (sinkpad == NULL, "Could not get sink pad from %s",
159 GST_ELEMENT_NAME (element));
160 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
161 fail_unless (gst_pad_set_caps (srcpad, caps));
162 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
163 "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
164 gst_object_unref (sinkpad); /* because we got it higher up */
165 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 1);
167 return srcpad;
168 }
170 void
171 gst_check_teardown_src_pad (GstElement * element)
172 {
173 GstPad *srcpad, *sinkpad;
175 /* clean up floating src pad */
176 sinkpad = gst_element_get_pad (element, "sink");
177 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
178 srcpad = gst_pad_get_peer (sinkpad);
180 gst_pad_unlink (srcpad, sinkpad);
182 /* pad refs held by both creator and this function (through _get) */
183 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
184 gst_object_unref (sinkpad);
185 /* one more ref is held by element itself */
187 /* pad refs held by both creator and this function (through _get_peer) */
188 ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 2);
189 gst_object_unref (srcpad);
190 gst_object_unref (srcpad);
191 }
193 /* FIXME: set_caps isn't that useful; might want to check if fixed,
194 * then use set_use_fixed or somesuch */
195 GstPad *
196 gst_check_setup_sink_pad (GstElement * element, GstStaticPadTemplate * template,
197 GstCaps * caps)
198 {
199 GstPad *srcpad, *sinkpad;
201 GST_DEBUG_OBJECT (element, "setting up receiving pad");
202 /* receiving pad */
203 sinkpad =
204 gst_pad_new_from_template (gst_static_pad_template_get (template),
205 "sink");
206 fail_if (sinkpad == NULL, "Could not create a sinkpad");
208 srcpad = gst_element_get_pad (element, "src");
209 fail_if (srcpad == NULL, "Could not get source pad from %s",
210 GST_ELEMENT_NAME (element));
211 fail_unless (gst_pad_set_caps (sinkpad, caps));
212 gst_pad_set_chain_function (sinkpad, gst_check_chain_func);
214 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
215 "Could not link %s source and sink pads", GST_ELEMENT_NAME (element));
216 gst_object_unref (srcpad); /* because we got it higher up */
217 ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
219 return sinkpad;
220 }
222 void
223 gst_check_teardown_sink_pad (GstElement * element)
224 {
225 GstPad *srcpad, *sinkpad;
227 /* clean up floating sink pad */
228 srcpad = gst_element_get_pad (element, "src");
229 sinkpad = gst_pad_get_peer (srcpad);
230 gst_pad_unlink (srcpad, sinkpad);
232 /* pad refs held by both creator and this function (through _get_pad) */
233 ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 2);
234 gst_object_unref (srcpad);
235 /* one more ref is held by element itself */
237 /* pad refs held by both creator and this function (through _get_peer) */
238 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
239 gst_object_unref (sinkpad);
240 gst_object_unref (sinkpad);
241 }