Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, 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, NULL);
52   mysinkpad = gst_check_setup_sink_pad (capssetter, &sinktemplate, NULL);
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_pad_set_active (mysrcpad, FALSE);
65   gst_pad_set_active (mysinkpad, FALSE);
66   gst_check_teardown_src_pad (capssetter);
67   gst_check_teardown_sink_pad (capssetter);
68   gst_check_teardown_element (capssetter);
69 }
70
71 static void
72 push_and_test (GstCaps * prop_caps, gboolean join, gboolean replace,
73     GstCaps * in_caps, GstCaps * out_caps)
74 {
75   GstElement *capssetter;
76   GstBuffer *buffer;
77
78   capssetter = setup_capssetter ();
79   fail_unless (gst_element_set_state (capssetter,
80           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
81       "could not set to playing");
82
83   buffer = gst_buffer_new_and_alloc (4);
84   ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1);
85   memcpy (GST_BUFFER_DATA (buffer), "data", 4);
86
87   gst_buffer_set_caps (buffer, in_caps);
88   gst_caps_unref (in_caps);
89
90   g_object_set (capssetter, "join", join, NULL);
91   g_object_set (capssetter, "replace", replace, NULL);
92   g_object_set (capssetter, "caps", prop_caps, NULL);
93   gst_caps_unref (prop_caps);
94
95   /* pushing gives away my reference ... */
96   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK,
97       "Failed pushing buffer to capssetter");
98
99   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()) == TRUE);
100
101   /* ... but it should end up being collected on the global buffer list */
102   fail_unless (g_list_length (buffers) == 1);
103   buffer = g_list_first (buffers)->data;
104   ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1);
105
106   fail_unless (gst_caps_is_equal (out_caps, GST_BUFFER_CAPS (buffer)));
107   gst_caps_unref (out_caps);
108
109   /* cleanup */
110   cleanup_capssetter (capssetter);
111 }
112
113 #define SRC_WIDTH   8
114 #define SRC_HEIGHT 12
115
116 static GstCaps *
117 make_src_caps (void)
118 {
119   return gst_caps_new_simple ("video/x-raw-yuv", "width", G_TYPE_INT, SRC_WIDTH,
120       "height", G_TYPE_INT, SRC_HEIGHT, NULL);
121 }
122
123 /* don't try these caps mutations at home, folks */
124
125 GST_START_TEST (test_n_join_n_replace)
126 {
127   GstCaps *in_caps, *prop_caps, *out_caps;
128
129   in_caps = make_src_caps ();
130   prop_caps = gst_caps_new_simple ("video/x-raw-rgb",
131       "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
132   out_caps = gst_caps_new_simple ("video/x-raw-rgb",
133       "width", G_TYPE_INT, 2 * SRC_WIDTH,
134       "height", G_TYPE_INT, SRC_HEIGHT, NULL);
135   push_and_test (prop_caps, FALSE, FALSE, in_caps, out_caps);
136 }
137
138 GST_END_TEST;
139
140 GST_START_TEST (test_n_join_replace)
141 {
142   GstCaps *in_caps, *prop_caps, *out_caps;
143
144   in_caps = make_src_caps ();
145   prop_caps = gst_caps_new_simple ("video/x-raw-rgb",
146       "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
147   out_caps = gst_caps_copy (prop_caps);
148   push_and_test (prop_caps, FALSE, TRUE, in_caps, out_caps);
149 }
150
151 GST_END_TEST;
152
153 GST_START_TEST (test_join_n_replace_n_match)
154 {
155   GstCaps *in_caps, *prop_caps, *out_caps;
156
157   /* non joining caps */
158   in_caps = make_src_caps ();
159   prop_caps = gst_caps_new_simple ("video/x-raw-rgb",
160       "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
161   out_caps = gst_caps_copy (in_caps);
162   push_and_test (prop_caps, TRUE, FALSE, in_caps, out_caps);
163 }
164
165 GST_END_TEST;
166
167 GST_START_TEST (test_join_n_replace_match)
168 {
169   GstCaps *in_caps, *prop_caps, *out_caps;
170
171   /* joining caps */
172   in_caps = make_src_caps ();
173   prop_caps = gst_caps_new_simple ("video/x-raw-yuv",
174       "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
175   out_caps = gst_caps_new_simple ("video/x-raw-yuv",
176       "width", G_TYPE_INT, 2 * SRC_WIDTH,
177       "height", G_TYPE_INT, SRC_HEIGHT, NULL);
178   push_and_test (prop_caps, TRUE, FALSE, in_caps, out_caps);
179 }
180
181 GST_END_TEST;
182
183 GST_START_TEST (test_join_replace_n_match)
184 {
185   GstCaps *in_caps, *prop_caps, *out_caps;
186
187   /* non joining caps */
188   in_caps = make_src_caps ();
189   prop_caps = gst_caps_new_simple ("video/x-raw-rgb",
190       "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
191   out_caps = gst_caps_copy (in_caps);
192   push_and_test (prop_caps, TRUE, TRUE, in_caps, out_caps);
193 }
194
195 GST_END_TEST;
196
197 GST_START_TEST (test_join_replace_match)
198 {
199   GstCaps *in_caps, *prop_caps, *out_caps;
200
201   /* joining caps */
202   in_caps = make_src_caps ();
203   prop_caps = gst_caps_new_simple ("video/x-raw-yuv",
204       "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
205   out_caps = gst_caps_copy (prop_caps);
206   push_and_test (prop_caps, TRUE, TRUE, in_caps, out_caps);
207 }
208
209 GST_END_TEST;
210
211 static Suite *
212 capssetter_suite (void)
213 {
214   Suite *s = suite_create ("capssetter");
215   TCase *tc_chain = tcase_create ("general");
216
217   suite_add_tcase (s, tc_chain);
218   tcase_add_test (tc_chain, test_n_join_n_replace);
219   tcase_add_test (tc_chain, test_n_join_replace);
220   tcase_add_test (tc_chain, test_join_n_replace_n_match);
221   tcase_add_test (tc_chain, test_join_n_replace_match);
222   tcase_add_test (tc_chain, test_join_replace_n_match);
223   tcase_add_test (tc_chain, test_join_replace_match);
224
225   return s;
226 }
227
228 GST_CHECK_MAIN (capssetter);