fix buffer leaks in tests
[platform/upstream/gst-plugins-good.git] / tests / check / elements / capssetter.c
1 /* GStreamer
2  *
3  * unit test for capssetter
4  *
5  * Copyright (C) <2009> Mark Nauwelaerts <mnauw@users.sourceforge.net>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #include <unistd.h>
24
25 #include <gst/check/gstcheck.h>
26
27
28 /* For ease of programming we use globals to keep refs for our floating
29  * src and sink pads we create; otherwise we always have to do get_pad,
30  * get_peer, and then remove references in every test function */
31 static GstPad *mysrcpad, *mysinkpad;
32
33
34 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
35     GST_PAD_SINK,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS_ANY);
38 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
39     GST_PAD_SRC,
40     GST_PAD_ALWAYS,
41     GST_STATIC_CAPS_ANY);
42
43 static GstElement *
44 setup_capssetter (void)
45 {
46   GstElement *capssetter;
47
48   GST_DEBUG ("setup_capssetter");
49
50   capssetter = gst_check_setup_element ("capssetter");
51   mysrcpad = gst_check_setup_src_pad (capssetter, &srctemplate);
52   mysinkpad = gst_check_setup_sink_pad (capssetter, &sinktemplate);
53   gst_pad_set_active (mysrcpad, TRUE);
54   gst_pad_set_active (mysinkpad, TRUE);
55
56   return capssetter;
57 }
58
59 static void
60 cleanup_capssetter (GstElement * capssetter)
61 {
62   GST_DEBUG ("cleanup_capssetter");
63
64   gst_check_drop_buffers ();
65   gst_pad_set_active (mysrcpad, FALSE);
66   gst_pad_set_active (mysinkpad, FALSE);
67   gst_check_teardown_src_pad (capssetter);
68   gst_check_teardown_sink_pad (capssetter);
69   gst_check_teardown_element (capssetter);
70 }
71
72 static void
73 push_and_test (GstCaps * prop_caps, gboolean join, gboolean replace,
74     GstCaps * in_caps, GstCaps * out_caps)
75 {
76   GstElement *capssetter;
77   GstBuffer *buffer;
78   GstCaps *current_out;
79
80   capssetter = setup_capssetter ();
81   fail_unless (gst_element_set_state (capssetter,
82           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
83       "could not set to playing");
84
85   g_object_set (capssetter, "join", join, NULL);
86   g_object_set (capssetter, "replace", replace, NULL);
87   g_object_set (capssetter, "caps", prop_caps, NULL);
88   gst_caps_unref (prop_caps);
89
90   buffer = gst_buffer_new_and_alloc (4);
91   ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1);
92   gst_buffer_fill (buffer, 0, "data", 4);
93
94   gst_check_setup_events (mysrcpad, capssetter, in_caps, GST_FORMAT_TIME);
95   gst_caps_unref (in_caps);
96
97   /* pushing gives away my reference ... */
98   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK,
99       "Failed pushing buffer to capssetter");
100
101   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()) == TRUE);
102
103   /* ... but it should end up being collected on the global buffer list */
104   fail_unless (g_list_length (buffers) == 1);
105   buffer = g_list_first (buffers)->data;
106   ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1);
107
108   current_out = gst_pad_get_current_caps (mysinkpad);
109   fail_unless (gst_caps_is_equal (out_caps, current_out));
110   gst_caps_unref (current_out);
111   gst_caps_unref (out_caps);
112
113   /* cleanup */
114   cleanup_capssetter (capssetter);
115 }
116
117 #define SRC_WIDTH   8
118 #define SRC_HEIGHT 12
119
120 static GstCaps *
121 make_src_caps (void)
122 {
123   return gst_caps_new_simple ("video/x-foo", "width", G_TYPE_INT, SRC_WIDTH,
124       "height", G_TYPE_INT, SRC_HEIGHT, NULL);
125 }
126
127 /* don't try these caps mutations at home, folks */
128
129 GST_START_TEST (test_n_join_n_replace)
130 {
131   GstCaps *in_caps, *prop_caps, *out_caps;
132
133   in_caps = make_src_caps ();
134   prop_caps = gst_caps_new_simple ("video/x-bar",
135       "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
136   out_caps = gst_caps_new_simple ("video/x-bar",
137       "width", G_TYPE_INT, 2 * SRC_WIDTH,
138       "height", G_TYPE_INT, SRC_HEIGHT, NULL);
139   push_and_test (prop_caps, FALSE, FALSE, in_caps, out_caps);
140 }
141
142 GST_END_TEST;
143
144 GST_START_TEST (test_n_join_replace)
145 {
146   GstCaps *in_caps, *prop_caps, *out_caps;
147
148   in_caps = make_src_caps ();
149   prop_caps = gst_caps_new_simple ("video/x-bar",
150       "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
151   out_caps = gst_caps_copy (prop_caps);
152   push_and_test (prop_caps, FALSE, TRUE, in_caps, out_caps);
153 }
154
155 GST_END_TEST;
156
157 GST_START_TEST (test_join_n_replace_n_match)
158 {
159   GstCaps *in_caps, *prop_caps, *out_caps;
160
161   /* non joining caps */
162   in_caps = make_src_caps ();
163   prop_caps = gst_caps_new_simple ("video/x-bar",
164       "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
165   out_caps = gst_caps_copy (in_caps);
166   push_and_test (prop_caps, TRUE, FALSE, in_caps, out_caps);
167 }
168
169 GST_END_TEST;
170
171 GST_START_TEST (test_join_n_replace_match)
172 {
173   GstCaps *in_caps, *prop_caps, *out_caps;
174
175   /* joining caps */
176   in_caps = make_src_caps ();
177   prop_caps = gst_caps_new_simple ("video/x-foo",
178       "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
179   out_caps = gst_caps_new_simple ("video/x-foo",
180       "width", G_TYPE_INT, 2 * SRC_WIDTH,
181       "height", G_TYPE_INT, SRC_HEIGHT, NULL);
182   push_and_test (prop_caps, TRUE, FALSE, in_caps, out_caps);
183 }
184
185 GST_END_TEST;
186
187 GST_START_TEST (test_join_replace_n_match)
188 {
189   GstCaps *in_caps, *prop_caps, *out_caps;
190
191   /* non joining caps */
192   in_caps = make_src_caps ();
193   prop_caps = gst_caps_new_simple ("video/x-bar",
194       "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
195   out_caps = gst_caps_copy (in_caps);
196   push_and_test (prop_caps, TRUE, TRUE, in_caps, out_caps);
197 }
198
199 GST_END_TEST;
200
201 GST_START_TEST (test_join_replace_match)
202 {
203   GstCaps *in_caps, *prop_caps, *out_caps;
204
205   /* joining caps */
206   in_caps = make_src_caps ();
207   prop_caps = gst_caps_new_simple ("video/x-foo",
208       "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
209   out_caps = gst_caps_copy (prop_caps);
210   push_and_test (prop_caps, TRUE, TRUE, in_caps, out_caps);
211 }
212
213 GST_END_TEST;
214
215 static Suite *
216 capssetter_suite (void)
217 {
218   Suite *s = suite_create ("capssetter");
219   TCase *tc_chain = tcase_create ("general");
220
221   suite_add_tcase (s, tc_chain);
222   tcase_add_test (tc_chain, test_n_join_n_replace);
223   tcase_add_test (tc_chain, test_n_join_replace);
224   tcase_add_test (tc_chain, test_join_n_replace_n_match);
225   tcase_add_test (tc_chain, test_join_n_replace_match);
226   tcase_add_test (tc_chain, test_join_replace_n_match);
227   tcase_add_test (tc_chain, test_join_replace_match);
228
229   return s;
230 }
231
232 GST_CHECK_MAIN (capssetter);