2 * Copyright (C) 2005 Thomas Vander Stichele <thomas at apestaart dot org>
4 * gstelement.c: Unit test for GstElement
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #include <gst/check/gstcheck.h>
24 GST_START_TEST (test_add_remove_pad)
29 /* getting an existing element class is cheating, but easier */
30 e = gst_element_factory_make ("fakesrc", "source");
32 /* create a new floating pad with refcount 1 */
33 p = gst_pad_new ("source", GST_PAD_SRC);
34 ASSERT_OBJECT_REFCOUNT (p, "pad", 1);
35 /* ref it for ourselves */
37 ASSERT_OBJECT_REFCOUNT (p, "pad", 2);
38 /* adding it sinks the pad -> not floating, same refcount */
39 gst_element_add_pad (e, p);
40 ASSERT_OBJECT_REFCOUNT (p, "pad", 2);
42 /* removing it reduces the refcount */
43 gst_element_remove_pad (e, p);
44 ASSERT_OBJECT_REFCOUNT (p, "pad", 1);
46 /* clean up our own reference */
53 GST_START_TEST (test_add_pad_unref_element)
58 /* getting an existing element class is cheating, but easier */
59 e = gst_element_factory_make ("fakesrc", "source");
61 /* create a new floating pad with refcount 1 */
62 p = gst_pad_new ("source", GST_PAD_SRC);
63 ASSERT_OBJECT_REFCOUNT (p, "pad", 1);
64 /* ref it for ourselves */
66 ASSERT_OBJECT_REFCOUNT (p, "pad", 2);
67 /* adding it sinks the pad -> not floating, same refcount */
68 gst_element_add_pad (e, p);
69 ASSERT_OBJECT_REFCOUNT (p, "pad", 2);
71 /* unreffing the element should clean it up */
72 gst_object_unref (GST_OBJECT (e));
74 ASSERT_OBJECT_REFCOUNT (p, "pad", 1);
76 /* clean up our own reference */
82 GST_START_TEST (test_error_no_bus)
87 e = gst_element_factory_make ("fakesrc", "source");
89 /* get the bus, should be NULL */
90 bus = gst_element_get_bus (e);
91 fail_if (bus != NULL);
93 /* I don't want errors shown */
94 gst_debug_set_default_threshold (GST_LEVEL_NONE);
96 GST_ELEMENT_ERROR (e, RESOURCE, OPEN_READ, ("I could not read"), ("debug"));
103 /* link and run two elements without putting them in a pipeline */
104 GST_START_TEST (test_link)
106 GstElement *src, *sink;
108 src = gst_element_factory_make ("fakesrc", "source");
109 sink = gst_element_factory_make ("fakesink", "sink");
111 fail_unless (gst_element_link_pads (src, "src", sink, "sink"));
113 /* do sink to source state change */
114 gst_element_set_state (sink, GST_STATE_PAUSED);
115 gst_element_set_state (src, GST_STATE_PAUSED);
117 /* wait for preroll */
118 gst_element_get_state (sink, NULL, NULL, GST_CLOCK_TIME_NONE);
121 gst_element_set_state (sink, GST_STATE_PLAYING);
122 gst_element_set_state (src, GST_STATE_PLAYING);
124 g_usleep (G_USEC_PER_SEC);
127 gst_element_set_state (sink, GST_STATE_PAUSED);
128 gst_element_set_state (src, GST_STATE_PAUSED);
130 /* wait for preroll */
131 gst_element_get_state (sink, NULL, NULL, GST_CLOCK_TIME_NONE);
133 gst_element_set_state (sink, GST_STATE_NULL);
134 gst_element_set_state (src, GST_STATE_NULL);
136 gst_element_get_state (sink, NULL, NULL, GST_CLOCK_TIME_NONE);
137 g_usleep (G_USEC_PER_SEC / 2);
139 ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
140 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
141 gst_element_unlink_pads (src, "src", sink, "sink");
142 ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
143 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
145 gst_object_unref (src);
146 gst_object_unref (sink);
151 /* linking two elements without pads should fail */
152 GST_START_TEST (test_link_no_pads)
154 GstElement *src, *sink;
156 src = gst_bin_new ("src");
157 sink = gst_bin_new ("sink");
159 fail_if (gst_element_link (src, sink));
161 gst_object_unref (src);
162 gst_object_unref (sink);
167 typedef struct _GstTestElement
173 typedef struct _GstTestElementClass
175 GstElementClass parent;
177 } GstTestElementClass;
180 gst_test_element_class_init (GstTestElementClass * klass)
182 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
183 GstPadTemplate *templ;
185 gst_element_class_set_metadata (element_class, "Test element",
186 "Element", "Does nothing", "Foo Bar <foo@bar.com>");
188 fail_unless_equals_int (g_list_length (gst_element_class_get_pad_template_list
189 (element_class)), 0);
191 fail_unless (gst_element_class_get_pad_template (element_class,
194 gst_element_class_add_pad_template (element_class,
195 gst_pad_template_new ("test", GST_PAD_SRC, GST_PAD_ALWAYS, GST_CAPS_ANY));
197 fail_unless_equals_int (g_list_length (gst_element_class_get_pad_template_list
198 (element_class)), 1);
200 fail_unless ((templ =
201 gst_element_class_get_pad_template (element_class, "test")) != NULL);
202 fail_unless (gst_caps_is_any (templ->caps));
204 gst_element_class_add_pad_template (element_class,
205 gst_pad_template_new ("test2", GST_PAD_SRC, GST_PAD_ALWAYS,
208 fail_unless_equals_int (g_list_length (gst_element_class_get_pad_template_list
209 (element_class)), 2);
211 fail_unless ((templ =
212 gst_element_class_get_pad_template (element_class, "test2")) != NULL);
213 fail_unless (gst_caps_is_any (templ->caps));
215 /* Add "test" again, with NONE caps this time */
216 gst_element_class_add_pad_template (element_class,
217 gst_pad_template_new ("test", GST_PAD_SRC, GST_PAD_ALWAYS,
220 fail_unless_equals_int (g_list_length (gst_element_class_get_pad_template_list
221 (element_class)), 2);
223 fail_unless ((templ =
224 gst_element_class_get_pad_template (element_class, "test")) != NULL);
225 fail_unless (gst_caps_is_empty (templ->caps));
229 gst_test_element_get_type (void)
231 static GType gst_test_element_type = G_TYPE_NONE;
233 if (gst_test_element_type == G_TYPE_NONE) {
234 static const GTypeInfo gst_test_element_info = {
235 sizeof (GstTestElementClass),
236 NULL, /* base_init */
237 NULL, /* base_finalize */
238 (GClassInitFunc) gst_test_element_class_init,
241 sizeof (GstTestElement),
243 NULL, /* instance_init */
247 gst_test_element_type = g_type_register_static (GST_TYPE_ELEMENT,
248 "GstTestElement", &gst_test_element_info, 0);
250 return gst_test_element_type;
253 typedef struct _GstTestElement2
255 GstTestElement parent;
259 typedef struct _GstTestElement2Class
261 GstTestElementClass parent;
263 } GstTestElement2Class;
266 gst_test_element2_class_init (GstTestElement2Class * klass)
268 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
269 GstPadTemplate *templ;
271 gst_element_class_set_metadata (element_class, "Test element 2",
272 "Element", "Does nothing", "Foo Bar <foo@bar.com>");
274 fail_unless_equals_int (g_list_length (gst_element_class_get_pad_template_list
275 (element_class)), 2);
277 fail_unless ((templ =
278 gst_element_class_get_pad_template (element_class, "test")) != NULL);
279 fail_unless (gst_caps_is_empty (templ->caps));
281 fail_unless ((templ =
282 gst_element_class_get_pad_template (element_class, "test2")) != NULL);
283 fail_unless (gst_caps_is_any (templ->caps));
285 /* Add "test" pad with ANY caps, should have "test" pad with EMPTY caps before */
286 gst_element_class_add_pad_template (element_class,
287 gst_pad_template_new ("test", GST_PAD_SRC, GST_PAD_ALWAYS, GST_CAPS_ANY));
289 fail_unless_equals_int (g_list_length (gst_element_class_get_pad_template_list
290 (element_class)), 2);
292 fail_unless ((templ =
293 gst_element_class_get_pad_template (element_class, "test")) != NULL);
294 fail_unless (gst_caps_is_any (templ->caps));
297 gst_element_class_add_pad_template (element_class,
298 gst_pad_template_new ("test4", GST_PAD_SRC, GST_PAD_ALWAYS,
301 fail_unless_equals_int (g_list_length (gst_element_class_get_pad_template_list
302 (element_class)), 3);
304 fail_unless ((templ =
305 gst_element_class_get_pad_template (element_class, "test4")) != NULL);
306 fail_unless (gst_caps_is_any (templ->caps));
310 gst_test_element2_get_type (void)
312 static GType gst_test_element2_type = G_TYPE_NONE;
314 if (gst_test_element2_type == G_TYPE_NONE) {
315 static const GTypeInfo gst_test_element2_info = {
316 sizeof (GstTestElement2Class),
317 NULL, /* base_init */
318 NULL, /* base_finalize */
319 (GClassInitFunc) gst_test_element2_class_init,
322 sizeof (GstTestElement2),
324 NULL, /* instance_init */
328 gst_test_element2_type =
329 g_type_register_static (gst_test_element_get_type (), "GstTestElement2",
330 &gst_test_element2_info, 0);
332 return gst_test_element2_type;
336 GST_START_TEST (test_pad_templates)
338 GstTestElement *test;
339 GstTestElement2 *test2;
341 test = g_object_new (gst_test_element_get_type (), NULL);
342 test2 = g_object_new (gst_test_element2_get_type (), NULL);
344 g_object_unref (test);
345 g_object_unref (test2);
350 /* need to return the message here because object, property name and value
351 * are only valid as long as we keep the message alive */
353 bus_wait_for_notify_message (GstBus * bus, GstElement ** obj,
354 const gchar ** prop_name, const GValue ** val)
359 msg = gst_bus_timed_pop_filtered (bus, -1, GST_MESSAGE_ANY);
360 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_PROPERTY_NOTIFY)
362 gst_message_unref (msg);
365 gst_message_parse_property_notify (msg, (GstObject **) obj, prop_name, val);
369 GST_START_TEST (test_property_notify_message)
371 GstElement *pipeline, *identity;
372 gulong watch_id0, watch_id1, watch_id2, deep_watch_id1, deep_watch_id2;
375 pipeline = gst_pipeline_new (NULL);
376 identity = gst_element_factory_make ("identity", NULL);
377 gst_bin_add (GST_BIN (pipeline), identity);
379 bus = GST_ELEMENT_BUS (pipeline);
381 /* need to set state to READY, otherwise bus will be flushing and discard
383 gst_element_set_state (pipeline, GST_STATE_READY);
385 watch_id0 = gst_element_add_property_notify_watch (identity, NULL, FALSE);
387 watch_id1 = gst_element_add_property_notify_watch (identity, "sync", FALSE);
389 watch_id2 = gst_element_add_property_notify_watch (identity, "silent", TRUE);
392 gst_element_add_property_deep_notify_watch (pipeline, NULL, TRUE);
395 gst_element_add_property_deep_notify_watch (pipeline, "silent", FALSE);
397 /* Now test property changes and if we get the messages we expect. We rely
398 * on the signals being fired in the order that they were set up here. */
405 /* A - This should be picked up by... */
406 g_object_set (identity, "dump", TRUE, NULL);
407 /* 1) the catch-all notify on the element (no value) */
408 msg = bus_wait_for_notify_message (bus, &obj, &name, &val);
409 fail_unless (obj == identity);
410 fail_unless_equals_string (name, "dump");
411 fail_unless (val == NULL);
412 gst_message_unref (msg);
413 /* 2) the catch-all deep-notify on the pipeline (with value) */
414 msg = bus_wait_for_notify_message (bus, &obj, &name, &val);
415 fail_unless_equals_string (name, "dump");
416 fail_unless (obj == identity);
417 fail_unless (G_VALUE_HOLDS_BOOLEAN (val));
418 fail_unless_equals_int (g_value_get_boolean (val), TRUE);
419 gst_message_unref (msg);
421 /* B - This should be picked up by... */
422 g_object_set (identity, "sync", TRUE, NULL);
423 /* 1) the catch-all notify on the element (no value) */
424 msg = bus_wait_for_notify_message (bus, &obj, &name, &val);
425 fail_unless (obj == identity);
426 fail_unless_equals_string (name, "sync");
427 fail_unless (val == NULL);
428 gst_message_unref (msg);
429 /* 2) the "sync" notify on the element (no value) */
430 msg = bus_wait_for_notify_message (bus, &obj, &name, &val);
431 fail_unless (obj == identity);
432 fail_unless_equals_string (name, "sync");
433 fail_unless (val == NULL);
434 gst_message_unref (msg);
435 /* 3) the catch-all deep-notify on the pipeline (with value) */
436 msg = bus_wait_for_notify_message (bus, &obj, &name, &val);
437 fail_unless_equals_string (name, "sync");
438 fail_unless (obj == identity);
439 fail_unless (G_VALUE_HOLDS_BOOLEAN (val));
440 fail_unless_equals_int (g_value_get_boolean (val), TRUE);
441 gst_message_unref (msg);
443 /* C - This should be picked up by... */
444 g_object_set (identity, "silent", FALSE, NULL);
445 /* 1) the catch-all notify on the element (no value) */
446 msg = bus_wait_for_notify_message (bus, &obj, &name, &val);
447 fail_unless (obj == identity);
448 fail_unless_equals_string (name, "silent");
449 fail_unless (val == NULL);
450 gst_message_unref (msg);
451 /* 2) the "silent" notify on the element (with value) */
452 msg = bus_wait_for_notify_message (bus, &obj, &name, &val);
453 fail_unless (obj == identity);
454 fail_unless_equals_string (name, "silent");
455 fail_unless (val != NULL);
456 fail_unless (G_VALUE_HOLDS_BOOLEAN (val));
457 fail_unless_equals_int (g_value_get_boolean (val), FALSE);
458 gst_message_unref (msg);
459 /* 3) the catch-all deep-notify on the pipeline (with value) */
460 msg = bus_wait_for_notify_message (bus, &obj, &name, &val);
461 fail_unless_equals_string (name, "silent");
462 fail_unless (obj == identity);
463 fail_unless (G_VALUE_HOLDS_BOOLEAN (val));
464 fail_unless_equals_int (g_value_get_boolean (val), FALSE);
465 gst_message_unref (msg);
466 /* 4) the "silent" deep-notify on the pipeline (without value) */
467 msg = bus_wait_for_notify_message (bus, &obj, &name, &val);
468 fail_unless_equals_string (name, "silent");
469 fail_unless (obj == identity);
470 fail_unless (val == NULL);
471 gst_message_unref (msg);
474 gst_element_remove_property_notify_watch (identity, watch_id0);
475 gst_element_remove_property_notify_watch (identity, watch_id1);
476 gst_element_remove_property_notify_watch (identity, watch_id2);
477 gst_element_remove_property_notify_watch (pipeline, deep_watch_id1);
478 gst_element_remove_property_notify_watch (pipeline, deep_watch_id2);
479 gst_element_set_state (pipeline, GST_STATE_NULL);
480 gst_object_unref (pipeline);
486 gst_element_suite (void)
488 Suite *s = suite_create ("GstElement");
489 TCase *tc_chain = tcase_create ("element tests");
491 suite_add_tcase (s, tc_chain);
492 tcase_add_test (tc_chain, test_add_remove_pad);
493 tcase_add_test (tc_chain, test_add_pad_unref_element);
494 tcase_add_test (tc_chain, test_error_no_bus);
495 tcase_add_test (tc_chain, test_link);
496 tcase_add_test (tc_chain, test_link_no_pads);
497 tcase_add_test (tc_chain, test_pad_templates);
498 tcase_add_test (tc_chain, test_property_notify_message);
503 GST_CHECK_MAIN (gst_element);