gst/gstutils.c: fix a bug in the case where neither element has a pad
[platform/upstream/gstreamer.git] / tests / check / gst / gstelement.c
1 /* GStreamer
2  * Copyright (C) 2005 Thomas Vander Stichele <thomas at apestaart dot org>
3  *
4  * gstelement.c: Unit test for GstElement
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include <gst/check/gstcheck.h>
23
24 GST_START_TEST (test_add_remove_pad)
25 {
26   GstElement *e;
27   GstPad *p;
28
29   /* getting an existing element class is cheating, but easier */
30   e = gst_element_factory_make ("fakesrc", "source");
31
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 */
36   gst_object_ref (p);
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);
41
42   /* removing it reduces the refcount */
43   gst_element_remove_pad (e, p);
44   ASSERT_OBJECT_REFCOUNT (p, "pad", 1);
45
46   /* clean up our own reference */
47   gst_object_unref (p);
48 }
49
50 GST_END_TEST;
51
52 GST_START_TEST (test_add_pad_unref_element)
53 {
54   GstElement *e;
55   GstPad *p;
56
57   /* getting an existing element class is cheating, but easier */
58   e = gst_element_factory_make ("fakesrc", "source");
59
60   /* create a new floating pad with refcount 1 */
61   p = gst_pad_new ("source", GST_PAD_SRC);
62   ASSERT_OBJECT_REFCOUNT (p, "pad", 1);
63   /* ref it for ourselves */
64   gst_object_ref (p);
65   ASSERT_OBJECT_REFCOUNT (p, "pad", 2);
66   /* adding it sinks the pad -> not floating, same refcount */
67   gst_element_add_pad (e, p);
68   ASSERT_OBJECT_REFCOUNT (p, "pad", 2);
69
70   /* unreffing the element should clean it up */
71   gst_object_unref (GST_OBJECT (e));
72
73   ASSERT_OBJECT_REFCOUNT (p, "pad", 1);
74
75   /* clean up our own reference */
76   gst_object_unref (p);
77 }
78
79 GST_END_TEST;
80
81 GST_START_TEST (test_error_no_bus)
82 {
83   GstElement *e;
84
85   e = gst_element_factory_make ("fakesrc", "source");
86
87   /* I don't want errors shown */
88   gst_debug_set_default_threshold (GST_LEVEL_NONE);
89
90   GST_ELEMENT_ERROR (e, RESOURCE, OPEN_READ, ("I could not read"), ("debug"));
91
92   gst_object_unref (e);
93 }
94
95 GST_END_TEST;
96
97 /* link and run two elements without putting them in a pipeline */
98 GST_START_TEST (test_link)
99 {
100   GstElement *src, *sink;
101
102   src = gst_element_factory_make ("fakesrc", "source");
103   sink = gst_element_factory_make ("fakesink", "sink");
104
105   fail_unless (gst_element_link_pads (src, "src", sink, "sink"));
106
107   /* do sink to source state change */
108   gst_element_set_state (sink, GST_STATE_PAUSED);
109   gst_element_set_state (src, GST_STATE_PAUSED);
110
111   /* wait for preroll */
112   gst_element_get_state (sink, NULL, NULL, NULL);
113
114   /* play some more */
115   gst_element_set_state (sink, GST_STATE_PLAYING);
116   gst_element_set_state (src, GST_STATE_PLAYING);
117
118   g_usleep (G_USEC_PER_SEC);
119
120   /* and stop */
121   gst_element_set_state (sink, GST_STATE_PAUSED);
122   gst_element_set_state (src, GST_STATE_PAUSED);
123
124   /* wait for preroll */
125   gst_element_get_state (sink, NULL, NULL, NULL);
126
127   gst_element_set_state (sink, GST_STATE_NULL);
128   gst_element_set_state (src, GST_STATE_NULL);
129 }
130
131 GST_END_TEST;
132
133 /* linking two elements without pads should fail */
134 GST_START_TEST (test_link_no_pads)
135 {
136   GstElement *src, *sink;
137
138   src = gst_bin_new ("src");
139   sink = gst_bin_new ("sink");
140
141   fail_if (gst_element_link (src, sink));
142
143   gst_object_unref (src);
144   gst_object_unref (sink);
145 }
146
147 GST_END_TEST;
148
149 Suite *
150 gst_element_suite (void)
151 {
152   Suite *s = suite_create ("GstElement");
153   TCase *tc_chain = tcase_create ("element tests");
154
155   suite_add_tcase (s, tc_chain);
156   tcase_add_test (tc_chain, test_add_remove_pad);
157   tcase_add_test (tc_chain, test_add_pad_unref_element);
158   tcase_add_test (tc_chain, test_error_no_bus);
159   tcase_add_test (tc_chain, test_link);
160   tcase_add_test (tc_chain, test_link_no_pads);
161
162   return s;
163 }
164
165 int
166 main (int argc, char **argv)
167 {
168   int nf;
169
170   Suite *s = gst_element_suite ();
171   SRunner *sr = srunner_create (s);
172
173   gst_check_init (&argc, &argv);
174
175   srunner_run_all (sr, CK_NORMAL);
176   nf = srunner_ntests_failed (sr);
177   srunner_free (sr);
178
179   return nf;
180 }