1 /* GStreamer
2 *
3 * Common code for GStreamer unittests
4 *
5 * Copyright (C) 2004,2006 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 * SECTION:gstcheck
24 * @short_description: Common code for GStreamer unit tests
25 *
26 * These macros and functions are for internal use of the unit tests found
27 * inside the 'check' directories of various GStreamer packages.
28 */
30 #include "gstcheck.h"
32 GST_DEBUG_CATEGORY (check_debug);
34 /* logging function for tests
35 * a test uses g_message() to log a debug line
36 * a gst unit test can be run with GST_TEST_DEBUG env var set to see the
37 * messages
38 */
40 gboolean _gst_check_threads_running = FALSE;
41 GList *thread_list = NULL;
42 GMutex *mutex;
43 GCond *start_cond; /* used to notify main thread of thread startups */
44 GCond *sync_cond; /* used to synchronize all threads and main thread */
46 gboolean _gst_check_debug = FALSE;
47 gboolean _gst_check_raised_critical = FALSE;
48 gboolean _gst_check_raised_warning = FALSE;
49 gboolean _gst_check_expecting_log = FALSE;
51 void gst_check_log_message_func
52 (const gchar * log_domain, GLogLevelFlags log_level,
53 const gchar * message, gpointer user_data)
54 {
55 if (_gst_check_debug) {
56 g_print ("%s", message);
57 }
58 }
60 void gst_check_log_critical_func
61 (const gchar * log_domain, GLogLevelFlags log_level,
62 const gchar * message, gpointer user_data)
63 {
64 if (!_gst_check_expecting_log) {
65 g_print ("\n\nUnexpected critical/warning: %s\n", message);
66 fail ("Unexpected critical/warning: %s", message);
67 }
69 if (_gst_check_debug) {
70 g_print ("\nExpected critical/warning: %s\n", message);
71 }
73 if (log_level & G_LOG_LEVEL_CRITICAL)
74 _gst_check_raised_critical = TRUE;
75 if (log_level & G_LOG_LEVEL_WARNING)
76 _gst_check_raised_warning = TRUE;
77 }
79 /* initialize GStreamer testing */
80 void
81 gst_check_init (int *argc, char **argv[])
82 {
83 gst_init (argc, argv);
85 GST_DEBUG_CATEGORY_INIT (check_debug, "check", 0, "check regression tests");
87 if (g_getenv ("GST_TEST_DEBUG"))
88 _gst_check_debug = TRUE;
90 g_log_set_handler (NULL, G_LOG_LEVEL_MESSAGE, gst_check_log_message_func,
91 NULL);
92 g_log_set_handler (NULL, G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
93 gst_check_log_critical_func, NULL);
94 g_log_set_handler ("GStreamer", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
95 gst_check_log_critical_func, NULL);
96 g_log_set_handler ("GLib-GObject", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
97 gst_check_log_critical_func, NULL);
98 g_log_set_handler ("Gst-Phonon", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
99 gst_check_log_critical_func, NULL);
101 check_cond = g_cond_new ();
102 check_mutex = g_mutex_new ();
103 }
105 /* message checking */
106 void
107 gst_check_message_error (GstMessage * message, GstMessageType type,
108 GQuark domain, gint code)
109 {
110 GError *error;
111 gchar *debug;
113 fail_unless (GST_MESSAGE_TYPE (message) == type,
114 "message is of type %s instead of expected type %s",
115 gst_message_type_get_name (GST_MESSAGE_TYPE (message)),
116 gst_message_type_get_name (type));
117 gst_message_parse_error (message, &error, &debug);
118 fail_unless_equals_int (error->domain, domain);
119 fail_unless_equals_int (error->code, code);
120 g_error_free (error);
121 g_free (debug);
122 }
124 /* helper functions */
125 GstFlowReturn
126 gst_check_chain_func (GstPad * pad, GstBuffer * buffer)
127 {
128 GST_DEBUG ("chain_func: received buffer %p", buffer);
129 buffers = g_list_append (buffers, buffer);
131 g_mutex_lock (check_mutex);
132 g_cond_signal (check_cond);
133 g_mutex_unlock (check_mutex);
135 return GST_FLOW_OK;
136 }
138 /* setup an element for a filter test with mysrcpad and mysinkpad */
139 GstElement *
140 gst_check_setup_element (const gchar * factory)
141 {
142 GstElement *element;
144 GST_DEBUG ("setup_element");
146 element = gst_element_factory_make (factory, factory);
147 fail_if (element == NULL, "Could not create a %s", factory);
148 ASSERT_OBJECT_REFCOUNT (element, factory, 1);
149 return element;
150 }
152 void
153 gst_check_teardown_element (GstElement * element)
154 {
155 GST_DEBUG ("teardown_element");
157 fail_unless (gst_element_set_state (element, GST_STATE_NULL) ==
158 GST_STATE_CHANGE_SUCCESS, "could not set to null");
159 ASSERT_OBJECT_REFCOUNT (element, "element", 1);
160 gst_object_unref (element);
161 }
163 /* FIXME: set_caps isn't that useful
164 */
165 GstPad *
166 gst_check_setup_src_pad (GstElement * element,
167 GstStaticPadTemplate * template, GstCaps * caps)
168 {
169 GstPad *srcpad, *sinkpad;
171 /* sending pad */
172 srcpad = gst_pad_new_from_static_template (template, "src");
173 GST_DEBUG_OBJECT (element, "setting up sending pad %p", srcpad);
174 fail_if (srcpad == NULL, "Could not create a srcpad");
175 ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
177 sinkpad = gst_element_get_pad (element, "sink");
178 fail_if (sinkpad == NULL, "Could not get sink pad from %s",
179 GST_ELEMENT_NAME (element));
180 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
181 if (caps)
182 fail_unless (gst_pad_set_caps (srcpad, caps));
183 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
184 "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
185 gst_object_unref (sinkpad); /* because we got it higher up */
186 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 1);
188 return srcpad;
189 }
191 void
192 gst_check_teardown_src_pad (GstElement * element)
193 {
194 GstPad *srcpad, *sinkpad;
196 /* clean up floating src pad */
197 sinkpad = gst_element_get_pad (element, "sink");
198 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
199 srcpad = gst_pad_get_peer (sinkpad);
201 gst_pad_unlink (srcpad, sinkpad);
203 /* caps could have been set, make sure they get unset */
204 gst_pad_set_caps (srcpad, NULL);
206 /* pad refs held by both creator and this function (through _get) */
207 ASSERT_OBJECT_REFCOUNT (sinkpad, "element sinkpad", 2);
208 gst_object_unref (sinkpad);
209 /* one more ref is held by element itself */
211 /* pad refs held by both creator and this function (through _get_peer) */
212 ASSERT_OBJECT_REFCOUNT (srcpad, "check srcpad", 2);
213 gst_object_unref (srcpad);
214 gst_object_unref (srcpad);
215 }
217 /* FIXME: set_caps isn't that useful; might want to check if fixed,
218 * then use set_use_fixed or somesuch */
219 GstPad *
220 gst_check_setup_sink_pad (GstElement * element, GstStaticPadTemplate * template,
221 GstCaps * caps)
222 {
223 GstPad *srcpad, *sinkpad;
225 /* receiving pad */
226 sinkpad = gst_pad_new_from_static_template (template, "sink");
227 GST_DEBUG_OBJECT (element, "setting up receiving pad %p", sinkpad);
228 fail_if (sinkpad == NULL, "Could not create a sinkpad");
230 srcpad = gst_element_get_pad (element, "src");
231 fail_if (srcpad == NULL, "Could not get source pad from %s",
232 GST_ELEMENT_NAME (element));
233 if (caps)
234 fail_unless (gst_pad_set_caps (sinkpad, caps));
235 gst_pad_set_chain_function (sinkpad, gst_check_chain_func);
237 GST_DEBUG_OBJECT (element, "Linking element src pad and receiving sink pad");
238 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
239 "Could not link %s source and sink pads", GST_ELEMENT_NAME (element));
240 gst_object_unref (srcpad); /* because we got it higher up */
241 ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
243 GST_DEBUG_OBJECT (element, "set up srcpad, refcount is 1");
244 return sinkpad;
245 }
247 void
248 gst_check_teardown_sink_pad (GstElement * element)
249 {
250 GstPad *srcpad, *sinkpad;
252 /* clean up floating sink pad */
253 srcpad = gst_element_get_pad (element, "src");
254 sinkpad = gst_pad_get_peer (srcpad);
256 gst_pad_unlink (srcpad, sinkpad);
258 /* pad refs held by both creator and this function (through _get_pad) */
259 ASSERT_OBJECT_REFCOUNT (srcpad, "element srcpad", 2);
260 gst_object_unref (srcpad);
261 /* one more ref is held by element itself */
263 /* pad refs held by both creator and this function (through _get_peer) */
264 ASSERT_OBJECT_REFCOUNT (sinkpad, "check sinkpad", 2);
265 gst_object_unref (sinkpad);
266 gst_object_unref (sinkpad);
267 }
269 void
270 gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes)
271 {
272 if (have_abi_sizes) {
273 gboolean ok = TRUE;
274 gint i;
276 for (i = 0; list[i].name; i++) {
277 if (list[i].size != list[i].abi_size) {
278 ok = FALSE;
279 g_print ("sizeof(%s) is %d, expected %d\n",
280 list[i].name, list[i].size, list[i].abi_size);
281 }
282 }
283 fail_unless (ok, "failed ABI check");
284 } else {
285 const gchar *fn;
287 if ((fn = g_getenv ("GST_ABI"))) {
288 GError *err = NULL;
289 GString *s;
290 gint i;
292 s = g_string_new ("\nGstCheckABIStruct list[] = {\n");
293 for (i = 0; list[i].name; i++) {
294 g_string_append_printf (s, " {\"%s\", sizeof (%s), %d},\n",
295 list[i].name, list[i].name, list[i].size);
296 }
297 g_string_append (s, " {NULL, 0, 0}\n");
298 g_string_append (s, "};\n");
299 if (!g_file_set_contents (fn, s->str, s->len, &err)) {
300 g_print ("%s", s->str);
301 g_printerr ("\nFailed to write ABI information: %s\n", err->message);
302 } else {
303 g_print ("\nWrote ABI information to '%s'.\n", fn);
304 }
305 g_string_free (s, TRUE);
306 } else {
307 g_print ("No structure size list was generated for this architecture.\n");
308 g_print ("Run with GST_ABI environment variable set to output header.\n");
309 }
310 }
311 }
313 gint
314 gst_check_run_suite (Suite * suite, const gchar * name, const gchar * fname)
315 {
316 gint nf;
318 SRunner *sr = srunner_create (suite);
320 if (g_getenv ("GST_CHECK_XML")) {
321 /* how lucky we are to have __FILE__ end in .c */
322 gchar *xmlfilename = g_strdup_printf ("%sheck.xml", fname);
324 srunner_set_xml (sr, xmlfilename);
325 }
327 srunner_run_all (sr, CK_NORMAL);
328 nf = srunner_ntests_failed (sr);
329 srunner_free (sr);
330 return nf;
331 }