tests/check/: use the new macro
[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
86   e = gst_element_factory_make ("fakesrc", "source");
87
88   /* I don't want errors shown */
89   gst_debug_set_default_threshold (GST_LEVEL_NONE);
90
91   GST_ELEMENT_ERROR (e, RESOURCE, OPEN_READ, ("I could not read"), ("debug"));
92
93   gst_object_unref (e);
94 }
95
96 GST_END_TEST;
97
98 /* link and run two elements without putting them in a pipeline */
99 GST_START_TEST (test_link)
100 {
101   GstElement *src, *sink;
102
103   src = gst_element_factory_make ("fakesrc", "source");
104   sink = gst_element_factory_make ("fakesink", "sink");
105
106   fail_unless (gst_element_link_pads (src, "src", sink, "sink"));
107
108   /* do sink to source state change */
109   gst_element_set_state (sink, GST_STATE_PAUSED);
110   gst_element_set_state (src, GST_STATE_PAUSED);
111
112   /* wait for preroll */
113   gst_element_get_state (sink, NULL, NULL, GST_CLOCK_TIME_NONE);
114
115   /* play some more */
116   gst_element_set_state (sink, GST_STATE_PLAYING);
117   gst_element_set_state (src, GST_STATE_PLAYING);
118
119   g_usleep (G_USEC_PER_SEC);
120
121   /* and stop */
122   gst_element_set_state (sink, GST_STATE_PAUSED);
123   gst_element_set_state (src, GST_STATE_PAUSED);
124
125   /* wait for preroll */
126   gst_element_get_state (sink, NULL, NULL, GST_CLOCK_TIME_NONE);
127
128   gst_element_set_state (sink, GST_STATE_NULL);
129   gst_element_set_state (src, GST_STATE_NULL);
130
131   gst_element_get_state (sink, NULL, NULL, GST_CLOCK_TIME_NONE);
132   g_usleep (G_USEC_PER_SEC / 2);
133
134   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
135   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
136   gst_element_unlink_pads (src, "src", sink, "sink");
137   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
138   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
139
140   gst_object_unref (src);
141   gst_object_unref (sink);
142 }
143
144 GST_END_TEST;
145
146 /* linking two elements without pads should fail */
147 GST_START_TEST (test_link_no_pads)
148 {
149   GstElement *src, *sink;
150
151   src = gst_bin_new ("src");
152   sink = gst_bin_new ("sink");
153
154   fail_if (gst_element_link (src, sink));
155
156   gst_object_unref (src);
157   gst_object_unref (sink);
158 }
159
160 GST_END_TEST;
161
162 /* check if the elementfactory of a class is filled (see #131079) */
163 GST_START_TEST (test_class)
164 {
165   GstElementClass *klass;
166   GstElementFactory *factory, *tmp;
167   GType type;
168
169   GST_DEBUG ("finding factory for queue");
170   factory = gst_element_factory_find ("queue");
171   fail_if (factory == NULL);
172
173   GST_DEBUG ("getting the type");
174   /* feature is not loaded, should return 0 as the type */
175   type = gst_element_factory_get_element_type (factory);
176   fail_if (type != 0);
177
178   GST_DEBUG ("now loading the plugin");
179   tmp =
180       GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
181           (factory)));
182   fail_if (tmp == NULL);
183
184   gst_object_unref (factory);
185   factory = tmp;
186
187   /* feature is now loaded */
188   type = gst_element_factory_get_element_type (factory);
189   fail_if (type == 0);
190
191   klass = g_type_class_ref (factory->type);
192   fail_if (klass == NULL);
193
194   GST_DEBUG ("checking the element factory class field");
195   /* and elementfactory is filled in */
196   fail_if (klass->elementfactory == NULL);
197   fail_if (klass->elementfactory != factory);
198
199 }
200
201 GST_END_TEST;
202
203 Suite *
204 gst_element_suite (void)
205 {
206   Suite *s = suite_create ("GstElement");
207   TCase *tc_chain = tcase_create ("element tests");
208
209   suite_add_tcase (s, tc_chain);
210   tcase_add_test (tc_chain, test_add_remove_pad);
211   tcase_add_test (tc_chain, test_add_pad_unref_element);
212   tcase_add_test (tc_chain, test_error_no_bus);
213   tcase_add_test (tc_chain, test_link);
214   tcase_add_test (tc_chain, test_link_no_pads);
215   tcase_add_test (tc_chain, test_class);
216
217   return s;
218 }
219
220 GST_CHECK_MAIN (gst_element);