And correct even more valid sparse warnings.
[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   gst_object_unref (e);
49 }
50
51 GST_END_TEST;
52
53 GST_START_TEST (test_add_pad_unref_element)
54 {
55   GstElement *e;
56   GstPad *p;
57
58   /* getting an existing element class is cheating, but easier */
59   e = gst_element_factory_make ("fakesrc", "source");
60
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 */
65   gst_object_ref (p);
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);
70
71   /* unreffing the element should clean it up */
72   gst_object_unref (GST_OBJECT (e));
73
74   ASSERT_OBJECT_REFCOUNT (p, "pad", 1);
75
76   /* clean up our own reference */
77   gst_object_unref (p);
78 }
79
80 GST_END_TEST;
81
82 GST_START_TEST (test_error_no_bus)
83 {
84   GstElement *e;
85   GstBus *bus;
86
87   e = gst_element_factory_make ("fakesrc", "source");
88
89   /* get the bus, should be NULL */
90   bus = gst_element_get_bus (e);
91   fail_if (bus != NULL);
92
93   /* I don't want errors shown */
94   gst_debug_set_default_threshold (GST_LEVEL_NONE);
95
96   GST_ELEMENT_ERROR (e, RESOURCE, OPEN_READ, ("I could not read"), ("debug"));
97
98   gst_object_unref (e);
99 }
100
101 GST_END_TEST;
102
103 /* link and run two elements without putting them in a pipeline */
104 GST_START_TEST (test_link)
105 {
106   GstElement *src, *sink;
107
108   src = gst_element_factory_make ("fakesrc", "source");
109   sink = gst_element_factory_make ("fakesink", "sink");
110
111   fail_unless (gst_element_link_pads (src, "src", sink, "sink"));
112
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);
116
117   /* wait for preroll */
118   gst_element_get_state (sink, NULL, NULL, GST_CLOCK_TIME_NONE);
119
120   /* play some more */
121   gst_element_set_state (sink, GST_STATE_PLAYING);
122   gst_element_set_state (src, GST_STATE_PLAYING);
123
124   g_usleep (G_USEC_PER_SEC);
125
126   /* and stop */
127   gst_element_set_state (sink, GST_STATE_PAUSED);
128   gst_element_set_state (src, GST_STATE_PAUSED);
129
130   /* wait for preroll */
131   gst_element_get_state (sink, NULL, NULL, GST_CLOCK_TIME_NONE);
132
133   gst_element_set_state (sink, GST_STATE_NULL);
134   gst_element_set_state (src, GST_STATE_NULL);
135
136   gst_element_get_state (sink, NULL, NULL, GST_CLOCK_TIME_NONE);
137   g_usleep (G_USEC_PER_SEC / 2);
138
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);
144
145   gst_object_unref (src);
146   gst_object_unref (sink);
147 }
148
149 GST_END_TEST;
150
151 /* linking two elements without pads should fail */
152 GST_START_TEST (test_link_no_pads)
153 {
154   GstElement *src, *sink;
155
156   src = gst_bin_new ("src");
157   sink = gst_bin_new ("sink");
158
159   fail_if (gst_element_link (src, sink));
160
161   gst_object_unref (src);
162   gst_object_unref (sink);
163 }
164
165 GST_END_TEST;
166
167 /* check if the elementfactory of a class is filled (see #131079) */
168 GST_START_TEST (test_class)
169 {
170   GstElementClass *klass;
171   GstElementFactory *factory, *tmp;
172   GType type;
173
174   GST_DEBUG ("finding factory for queue");
175   factory = gst_element_factory_find ("queue");
176   fail_if (factory == NULL);
177
178   GST_DEBUG ("getting the type");
179   /* feature is not loaded, should return 0 as the type */
180   type = gst_element_factory_get_element_type (factory);
181   fail_if (type != 0);
182
183   GST_DEBUG ("now loading the plugin");
184   tmp =
185       GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
186           (factory)));
187   fail_if (tmp == NULL);
188
189   gst_object_unref (factory);
190   factory = tmp;
191
192   /* feature is now loaded */
193   type = gst_element_factory_get_element_type (factory);
194   fail_if (type == 0);
195
196   klass = g_type_class_ref (factory->type);
197   fail_if (klass == NULL);
198
199   GST_DEBUG ("checking the element factory class field");
200   /* and elementfactory is filled in */
201   fail_if (klass->elementfactory == NULL);
202   fail_if (klass->elementfactory != factory);
203
204 }
205
206 GST_END_TEST;
207
208 static Suite *
209 gst_element_suite (void)
210 {
211   Suite *s = suite_create ("GstElement");
212   TCase *tc_chain = tcase_create ("element tests");
213
214   suite_add_tcase (s, tc_chain);
215   tcase_add_test (tc_chain, test_add_remove_pad);
216   tcase_add_test (tc_chain, test_add_pad_unref_element);
217   tcase_add_test (tc_chain, test_error_no_bus);
218   tcase_add_test (tc_chain, test_link);
219   tcase_add_test (tc_chain, test_link_no_pads);
220   tcase_add_test (tc_chain, test_class);
221
222   return s;
223 }
224
225 GST_CHECK_MAIN (gst_element);