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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, 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);
351 gst_element_suite (void)
353 Suite *s = suite_create ("GstElement");
354 TCase *tc_chain = tcase_create ("element tests");
356 suite_add_tcase (s, tc_chain);
357 tcase_add_test (tc_chain, test_add_remove_pad);
358 tcase_add_test (tc_chain, test_add_pad_unref_element);
359 tcase_add_test (tc_chain, test_error_no_bus);
360 tcase_add_test (tc_chain, test_link);
361 tcase_add_test (tc_chain, test_link_no_pads);
362 tcase_add_test (tc_chain, test_pad_templates);
367 GST_CHECK_MAIN (gst_element);