2 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
3 * Copyright (C) 2005 Thomas Vander Stichele <thomas at apestaart dot org>
5 * gstbin.c: Unit test for GstBin
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.
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.
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.
23 #include "../gstcheck.h"
26 pop_messages (GstBus * bus, int count)
32 GST_DEBUG ("popping %d messages", count);
33 for (i = 0; i < count; ++i) {
34 fail_unless (gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1)
35 == GST_MESSAGE_STATE_CHANGED, "did not get GST_MESSAGE_STATE_CHANGED");
37 message = gst_bus_pop (bus);
38 gst_message_unref (message);
40 GST_DEBUG ("popped %d messages", count);
43 GST_START_TEST (test_interface)
50 bin = GST_BIN (gst_bin_new (NULL));
51 fail_unless (bin != NULL, "Could not create bin");
53 filesrc = gst_element_factory_make ("filesrc", NULL);
54 fail_unless (filesrc != NULL, "Could not create filesrc");
55 fail_unless (GST_IS_URI_HANDLER (filesrc), "Filesrc not a URI handler");
56 gst_bin_add (bin, filesrc);
58 fail_unless (gst_bin_get_by_interface (bin, GST_TYPE_URI_HANDLER) == filesrc);
59 it = gst_bin_iterate_all_by_interface (bin, GST_TYPE_URI_HANDLER);
60 fail_unless (it != NULL);
61 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
62 fail_unless (item == (gpointer) filesrc);
63 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
64 gst_iterator_free (it);
66 gst_bin_add_many (bin,
67 gst_element_factory_make ("identity", NULL),
68 gst_element_factory_make ("identity", NULL),
69 gst_element_factory_make ("identity", NULL), NULL);
70 fail_unless (gst_bin_get_by_interface (bin, GST_TYPE_URI_HANDLER) == filesrc);
71 it = gst_bin_iterate_all_by_interface (bin, GST_TYPE_URI_HANDLER);
72 fail_unless (it != NULL);
73 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
74 fail_unless (item == (gpointer) filesrc);
75 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
76 gst_iterator_free (it);
79 bin = GST_BIN (gst_bin_new (NULL));
80 fail_unless (bin != NULL);
81 gst_bin_add_many (bin,
82 gst_element_factory_make ("identity", NULL),
83 gst_element_factory_make ("identity", NULL),
84 GST_ELEMENT (bin2), gst_element_factory_make ("identity", NULL), NULL);
85 fail_unless (gst_bin_get_by_interface (bin, GST_TYPE_URI_HANDLER) == filesrc);
86 it = gst_bin_iterate_all_by_interface (bin, GST_TYPE_URI_HANDLER);
87 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
88 fail_unless (item == (gpointer) filesrc);
89 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
90 gst_iterator_free (it);
92 gst_bin_add (bin, gst_element_factory_make ("filesrc", NULL));
93 gst_bin_add (bin2, gst_element_factory_make ("filesrc", NULL));
94 it = gst_bin_iterate_all_by_interface (bin, GST_TYPE_URI_HANDLER);
95 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
96 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
97 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
98 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
99 gst_iterator_free (it);
101 gst_object_unref (bin);
106 GST_START_TEST (test_message_state_changed)
112 bin = GST_BIN (gst_bin_new (NULL));
113 fail_unless (bin != NULL, "Could not create bin");
114 ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
116 bus = GST_ELEMENT_BUS (bin);
118 /* change state, spawning a message, causing an incref on the bin */
119 gst_element_set_state (GST_ELEMENT (bin), GST_STATE_READY);
121 ASSERT_OBJECT_REFCOUNT (bin, "bin", 2);
123 /* get and unref the message, causing a decref on the bin */
124 fail_unless (gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED,
125 -1) == GST_MESSAGE_STATE_CHANGED,
126 "did not get GST_MESSAGE_STATE_CHANGED");
128 message = gst_bus_pop (bus);
129 gst_message_unref (message);
131 ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
134 gst_object_unref (bin);
139 GST_START_TEST (test_message_state_changed_child)
146 bin = GST_BIN (gst_bin_new (NULL));
147 fail_unless (bin != NULL, "Could not create bin");
148 ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
150 src = gst_element_factory_make ("fakesrc", NULL);
151 fail_if (src == NULL, "Could not create fakesrc");
152 gst_bin_add (bin, src);
153 ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
154 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
156 bus = GST_ELEMENT_BUS (bin);
158 /* change state, spawning two messages:
159 * - first for fakesrc, forwarded to bin's bus, causing incref on fakesrc
160 * - second for bin, causing an incref on the bin */
161 GST_DEBUG ("setting bin to READY");
162 fail_unless (gst_element_set_state (GST_ELEMENT (bin), GST_STATE_READY)
163 == GST_STATE_SUCCESS);
165 ASSERT_OBJECT_REFCOUNT (src, "src", 2);
166 ASSERT_OBJECT_REFCOUNT (bin, "bin", 2);
168 /* get and unref the message, causing a decref on the src */
169 fail_unless (gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1)
170 == GST_MESSAGE_STATE_CHANGED, "did not get GST_MESSAGE_STATE_CHANGED");
172 message = gst_bus_pop (bus);
173 fail_unless (message->src == GST_OBJECT (src));
174 gst_message_unref (message);
176 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
177 ASSERT_OBJECT_REFCOUNT (bin, "bin", 2);
179 /* get and unref message 2, causing a decref on the bin */
180 fail_unless (gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1)
181 == GST_MESSAGE_STATE_CHANGED, "did not get GST_MESSAGE_STATE_CHANGED");
183 message = gst_bus_pop (bus);
184 fail_unless (message->src == GST_OBJECT (bin));
185 gst_message_unref (message);
187 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
188 ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
191 gst_object_unref (bin);
196 GST_START_TEST (test_message_state_changed_children)
198 GstPipeline *pipeline;
199 GstElement *src, *sink;
202 pipeline = GST_PIPELINE (gst_pipeline_new (NULL));
203 fail_unless (pipeline != NULL, "Could not create pipeline");
204 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
206 src = gst_element_factory_make ("fakesrc", NULL);
207 fail_if (src == NULL, "Could not create fakesrc");
208 /* need to silence the element as the deep_notify refcounts the
209 * parents while running */
210 g_object_set (G_OBJECT (src), "silent", TRUE, NULL);
211 gst_bin_add (GST_BIN (pipeline), src);
213 sink = gst_element_factory_make ("fakesink", NULL);
214 /* need to silence the element as the deep_notify refcounts the
215 * parents while running */
216 g_object_set (G_OBJECT (sink), "silent", TRUE, NULL);
217 fail_if (sink == NULL, "Could not create fakesink");
218 gst_bin_add (GST_BIN (pipeline), sink);
220 fail_unless (gst_element_link (src, sink), "could not link src and sink");
222 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
223 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
224 ASSERT_OBJECT_REFCOUNT (src, "sink", 1);
226 bus = GST_ELEMENT_BUS (pipeline);
228 /* change state, spawning three times three messages */
229 GST_DEBUG ("setting pipeline to PLAYING");
230 fail_unless (gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING)
231 == GST_STATE_SUCCESS);
233 pop_messages (bus, 9);
235 /* this test is completely bogus as the refcount can change while running */
237 ASSERT_OBJECT_REFCOUNT (bus, "bus", 1);
238 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
239 ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
240 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
243 /* go back to READY, spawning six messages */
244 GST_DEBUG ("setting pipeline to READY");
245 fail_unless (gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_READY)
246 == GST_STATE_SUCCESS);
248 /* each object is referenced by two messages */
249 ASSERT_OBJECT_REFCOUNT (src, "src", 3);
250 ASSERT_OBJECT_REFCOUNT (sink, "sink", 3);
251 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 3);
253 pop_messages (bus, 6);
255 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
256 ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
257 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
259 /* setting pipeline to NULL flushes the bus automatically */
260 fail_unless (gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL)
261 == GST_STATE_SUCCESS);
263 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
264 ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
265 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
268 gst_object_unref (pipeline);
273 /* adding an element with linked pads to a bin unlinks the
275 GST_START_TEST (test_add_linked)
277 GstElement *src, *sink;
278 GstPad *srcpad, *sinkpad;
279 GstElement *pipeline;
281 pipeline = gst_pipeline_new (NULL);
282 fail_unless (pipeline != NULL, "Could not create pipeline");
284 src = gst_element_factory_make ("fakesrc", NULL);
285 fail_if (src == NULL, "Could not create fakesrc");
286 sink = gst_element_factory_make ("fakesink", NULL);
287 fail_if (src == NULL, "Could not create fakesink");
289 srcpad = gst_element_get_pad (src, "src");
290 fail_unless (srcpad != NULL);
291 sinkpad = gst_element_get_pad (sink, "sink");
292 fail_unless (sinkpad != NULL);
294 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK);
296 /* pads are linked now */
297 fail_unless (gst_pad_is_linked (srcpad));
298 fail_unless (gst_pad_is_linked (sinkpad));
300 /* adding element to bin voids hierarchy so pads are unlinked */
301 gst_bin_add (GST_BIN (pipeline), src);
303 /* check if pads really are unlinked */
304 fail_unless (!gst_pad_is_linked (srcpad));
305 fail_unless (!gst_pad_is_linked (sinkpad));
307 /* cannot link pads in wrong hierarchy */
308 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_WRONG_HIERARCHY);
310 /* adding other element to bin as well */
311 gst_bin_add (GST_BIN (pipeline), sink);
313 /* now we can link again */
314 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK);
316 /* check if pads really are linked */
317 fail_unless (gst_pad_is_linked (srcpad));
318 fail_unless (gst_pad_is_linked (sinkpad));
326 Suite *s = suite_create ("GstBin");
327 TCase *tc_chain = tcase_create ("bin tests");
329 suite_add_tcase (s, tc_chain);
330 tcase_add_test (tc_chain, test_interface);
331 tcase_add_test (tc_chain, test_message_state_changed);
332 tcase_add_test (tc_chain, test_message_state_changed_child);
333 tcase_add_test (tc_chain, test_message_state_changed_children);
334 tcase_add_test (tc_chain, test_add_linked);
340 main (int argc, char **argv)
344 Suite *s = gst_bin_suite ();
345 SRunner *sr = srunner_create (s);
347 gst_check_init (&argc, &argv);
349 srunner_run_all (sr, CK_NORMAL);
350 nf = srunner_ntests_failed (sr);