if pad has no parent, return NULL as list of internal links
[platform/upstream/gstreamer.git] / tests / check / gst / gstpad.c
1 /* GStreamer
2  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
3  *
4  * gstpad.c: Unit test for GstPad
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 "../gstcheck.h"
23
24 START_TEST (test_link)
25 {
26   GstPad *src, *sink;
27   GstPadTemplate *srct;
28
29   GstPadLinkReturn ret;
30   gchar *name;
31
32   src = gst_pad_new ("source", GST_PAD_SRC);
33   fail_if (src == NULL);
34
35   name = gst_pad_get_name (src);
36   fail_unless (strcmp (name, "source") == 0);
37
38   sink = gst_pad_new ("sink", GST_PAD_SINK);
39   fail_if (sink == NULL);
40
41   /* linking without templates or caps should fail */
42   ret = gst_pad_link (src, sink);
43   fail_unless (ret == GST_PAD_LINK_NOFORMAT);
44
45   ASSERT_CRITICAL (gst_pad_get_pad_template (NULL));
46
47   srct = gst_pad_get_pad_template (src);
48   fail_unless (srct == NULL);
49 }
50
51 END_TEST;
52
53 /* threaded link/unlink */
54 /* use globals */
55 GstPad *src, *sink;
56
57 void
58 thread_link_unlink (gpointer data)
59 {
60   THREAD_START ();
61
62   while (THREAD_TEST_RUNNING ()) {
63     gst_pad_link (src, sink);
64     gst_pad_unlink (src, sink);
65     THREAD_SWITCH ();
66   }
67 }
68
69 START_TEST (test_link_unlink_threaded)
70 {
71   GstCaps *caps;
72   int i;
73
74   src = gst_pad_new ("source", GST_PAD_SRC);
75   fail_if (src == NULL);
76   sink = gst_pad_new ("sink", GST_PAD_SINK);
77   fail_if (sink == NULL);
78
79   caps = gst_caps_from_string ("foo/bar");
80   gst_pad_set_caps (src, caps);
81   gst_pad_set_caps (sink, caps);
82
83   MAIN_START_THREADS (5, thread_link_unlink, NULL);
84   for (i = 0; i < 1000; ++i) {
85     gst_pad_is_linked (src);
86     gst_pad_is_linked (sink);
87     THREAD_SWITCH ();
88   }
89   MAIN_STOP_THREADS ();
90 }
91
92 END_TEST;
93
94 START_TEST (test_refcount)
95 {
96   GstPad *src, *sink;
97   GstCaps *caps;
98   GstPadLinkReturn plr;
99
100   sink = gst_pad_new ("sink", GST_PAD_SINK);
101   fail_if (sink == NULL);
102
103   src = gst_pad_new ("src", GST_PAD_SRC);
104   fail_if (src == NULL);
105
106   caps = gst_caps_from_string ("foo/bar");
107   /* one for me */
108   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
109
110   gst_pad_set_caps (src, caps);
111   gst_pad_set_caps (sink, caps);
112   /* one for me and one for each set_caps */
113   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
114
115   plr = gst_pad_link (src, sink);
116   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
117   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
118
119   gst_pad_unlink (src, sink);
120   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
121
122   /* cleanup */
123   gst_object_unref (src);
124   gst_object_unref (sink);
125   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
126
127   gst_caps_unref (caps);
128 }
129
130 END_TEST;
131
132 START_TEST (test_get_allowed_caps)
133 {
134   GstPad *src, *sink;
135   GstCaps *caps, *gotcaps;
136   GstBuffer *buffer;
137   GstPadLinkReturn plr;
138
139   ASSERT_CRITICAL (gst_pad_get_allowed_caps (NULL));
140
141   buffer = gst_buffer_new ();
142   ASSERT_CRITICAL (gst_pad_get_allowed_caps ((GstPad *) buffer));
143   gst_buffer_unref (buffer);
144
145   sink = gst_pad_new ("sink", GST_PAD_SINK);
146   ASSERT_CRITICAL (gst_pad_get_allowed_caps (sink));
147
148   src = gst_pad_new ("src", GST_PAD_SRC);
149   fail_if (src == NULL);
150   caps = gst_pad_get_allowed_caps (src);
151   fail_unless (caps == NULL);
152
153   caps = gst_caps_from_string ("foo/bar");
154
155   gst_pad_set_caps (src, caps);
156   gst_pad_set_caps (sink, caps);
157   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
158
159   plr = gst_pad_link (src, sink);
160   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
161
162   gotcaps = gst_pad_get_allowed_caps (src);
163   fail_if (gotcaps == NULL);
164   fail_unless (gst_caps_is_equal (gotcaps, caps));
165
166   ASSERT_CAPS_REFCOUNT (gotcaps, "gotcaps", 1);
167   gst_caps_unref (gotcaps);
168
169   gst_pad_unlink (src, sink);
170
171   /* cleanup */
172   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
173   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
174   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
175
176   gst_object_unref (src);
177   gst_object_unref (sink);
178
179   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
180   gst_caps_unref (caps);
181 }
182
183 END_TEST;
184
185 Suite *
186 gst_pad_suite (void)
187 {
188   Suite *s = suite_create ("GstPad");
189   TCase *tc_chain = tcase_create ("general");
190
191   /* turn off timeout */
192   tcase_set_timeout (tc_chain, 60);
193
194   suite_add_tcase (s, tc_chain);
195   tcase_add_test (tc_chain, test_link);
196   tcase_add_test (tc_chain, test_refcount);
197   tcase_add_test (tc_chain, test_get_allowed_caps);
198   tcase_add_test (tc_chain, test_link_unlink_threaded);
199   return s;
200 }
201
202 int
203 main (int argc, char **argv)
204 {
205   int nf;
206
207   Suite *s = gst_pad_suite ();
208   SRunner *sr = srunner_create (s);
209
210   gst_check_init (&argc, &argv);
211
212   srunner_run_all (sr, CK_NORMAL);
213   nf = srunner_ntests_failed (sr);
214   srunner_free (sr);
215
216   return nf;
217 }