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