caps: improve _do_simplify
[platform/upstream/gstreamer.git] / tests / check / gst / gstquery.c
1 /* GStreamer
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Library General Public
5  * License as published by the Free Software Foundation; either
6  * version 2 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Library General Public License for more details.
12  *
13  * You should have received a copy of the GNU Library General Public
14  * License along with this library; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 02111-1307, USA.
17  */
18
19
20 #include <gst/check/gstcheck.h>
21 GST_START_TEST (create_queries)
22 {
23   GstQuery *query;
24
25   /* POSITION */
26   {
27     GstFormat format;
28     gint64 position;
29
30     query = gst_query_new_position (GST_FORMAT_TIME);
31     fail_if (query == NULL);
32     fail_unless (GST_QUERY_TYPE (query) == GST_QUERY_POSITION);
33
34     gst_query_parse_position (query, &format, NULL);
35     fail_if (format != GST_FORMAT_TIME);
36
37     gst_query_set_position (query, GST_FORMAT_TIME, 0xdeadbeaf);
38
39     gst_query_parse_position (query, &format, &position);
40     fail_if (format != GST_FORMAT_TIME);
41     fail_if (position != 0xdeadbeaf);
42
43     gst_query_unref (query);
44   }
45   /* DURATION */
46   {
47     GstFormat format;
48     gint64 duration;
49
50     query = gst_query_new_duration (GST_FORMAT_TIME);
51     fail_if (query == NULL);
52     fail_unless (GST_QUERY_TYPE (query) == GST_QUERY_DURATION);
53
54     gst_query_parse_duration (query, &format, NULL);
55     fail_if (format != GST_FORMAT_TIME);
56
57     gst_query_set_duration (query, GST_FORMAT_TIME, 0xdeadbeaf);
58
59     gst_query_parse_duration (query, &format, &duration);
60     fail_if (format != GST_FORMAT_TIME);
61     fail_if (duration != 0xdeadbeaf);
62
63     gst_query_unref (query);
64   }
65   /* BUFFERING RANGES */
66   {
67     gint64 start, stop;
68
69     query = gst_query_new_buffering (GST_FORMAT_PERCENT);
70     fail_if (query == NULL);
71     fail_unless (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
72
73     fail_unless (gst_query_add_buffering_range (query, 0, 20));
74     fail_unless (gst_query_add_buffering_range (query, 25, 30));
75
76     /* check incoherent range insertion */
77     fail_if (gst_query_add_buffering_range (query, 10, 15));
78     fail_if (gst_query_add_buffering_range (query, 50, 40));
79
80     fail_unless (gst_query_get_n_buffering_ranges (query) == 2);
81
82     fail_unless (gst_query_parse_nth_buffering_range (query, 0, &start, &stop));
83     fail_unless (start == 0);
84     fail_unless (stop == 20);
85
86     fail_unless (gst_query_parse_nth_buffering_range (query, 1, &start, &stop));
87     fail_unless (start == 25);
88     fail_unless (stop == 30);
89
90     gst_query_unref (query);
91   }
92   {
93     /* FIXME make tests for:
94      *
95      * LATENCY
96      * JITTER
97      * RATE
98      * SEEKING
99      * SEGMENT
100      * CONVERT
101      */
102   }
103   /* SEGMENT */
104   {
105     gdouble rate;
106     GstFormat format;
107     gint64 start, stop;
108
109     format = GST_FORMAT_BYTES;
110     query = gst_query_new_segment (format);
111
112     fail_if (query == NULL);
113     fail_unless (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);
114
115     gst_query_parse_segment (query, &rate, &format, &start, &stop);
116
117     /* see if empty gives undefined formats */
118     fail_if (rate != 0.0);
119     fail_if (format != GST_FORMAT_BYTES);
120     fail_if (start != -1);
121     fail_if (stop != -1);
122
123     /* change all values */
124     gst_query_set_segment (query, 2.0, GST_FORMAT_TIME, 1 * GST_SECOND,
125         3 * GST_SECOND);
126
127     gst_query_parse_segment (query, &rate, &format, &start, &stop);
128
129     /* see if the values were changed */
130     fail_if (rate != 2.0);
131     fail_if (format != GST_FORMAT_TIME);
132     fail_if (start != 1 * GST_SECOND);
133     fail_if (stop != 3 * GST_SECOND);
134
135     gst_query_unref (query);
136   }
137
138   /* FORMATS */
139   {
140     guint size;
141     GstFormat format;
142
143     query = gst_query_new_formats ();
144     fail_if (query == NULL);
145     fail_unless (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
146
147     /* empty */
148     gst_query_parse_n_formats (query, &size);
149     fail_if (size != 0);
150
151     /* see if empty gives undefined formats */
152     gst_query_parse_nth_format (query, 0, &format);
153     fail_if (format != GST_FORMAT_UNDEFINED);
154     gst_query_parse_nth_format (query, 1, &format);
155     fail_if (format != GST_FORMAT_UNDEFINED);
156
157     /* set 2 formats */
158     gst_query_set_formats (query, 2, GST_FORMAT_TIME, GST_FORMAT_BYTES);
159
160     gst_query_parse_n_formats (query, &size);
161     fail_if (size != 2);
162
163     format = GST_FORMAT_UNDEFINED;
164
165     gst_query_parse_nth_format (query, 0, &format);
166     fail_if (format != GST_FORMAT_TIME);
167     gst_query_parse_nth_format (query, 1, &format);
168     fail_if (format != GST_FORMAT_BYTES);
169
170     /* out of bounds, should return UNDEFINED */
171     gst_query_parse_nth_format (query, 2, &format);
172     fail_if (format != GST_FORMAT_UNDEFINED);
173
174     /* overwrite with 3 formats */
175     gst_query_set_formats (query, 3, GST_FORMAT_TIME, GST_FORMAT_BYTES,
176         GST_FORMAT_PERCENT);
177
178     gst_query_parse_n_formats (query, &size);
179     fail_if (size != 3);
180
181     gst_query_parse_nth_format (query, 2, &format);
182     fail_if (format != GST_FORMAT_PERCENT);
183
184     /* create one from an array */
185     {
186       static GstFormat formats[] = {
187         GST_FORMAT_TIME,
188         GST_FORMAT_BYTES,
189         GST_FORMAT_PERCENT
190       };
191       gst_query_set_formatsv (query, 3, formats);
192
193       gst_query_parse_n_formats (query, &size);
194       fail_if (size != 3);
195
196       gst_query_parse_nth_format (query, 0, &format);
197       fail_if (format != GST_FORMAT_TIME);
198       gst_query_parse_nth_format (query, 2, &format);
199       fail_if (format != GST_FORMAT_PERCENT);
200     }
201     gst_query_unref (query);
202   }
203 }
204
205 GST_END_TEST;
206
207 GST_START_TEST (test_queries)
208 {
209   GstBin *bin;
210   GstElement *src, *sink;
211   GstStateChangeReturn ret;
212   GstPad *pad;
213   GstQuery *dur, *pos;
214
215   fail_unless ((bin = (GstBin *) gst_pipeline_new (NULL)) != NULL,
216       "Could not create pipeline");
217   fail_unless ((src = gst_element_factory_make ("fakesrc", NULL)) != NULL,
218       "Could not create fakesrc");
219   g_object_set (src, "datarate", 200, "sizetype", 2, NULL);
220
221   fail_unless ((sink = gst_element_factory_make ("fakesink", NULL)) != NULL,
222       "Could not create fakesink");
223   g_object_set (sink, "sync", TRUE, NULL);
224   fail_unless ((dur = gst_query_new_duration (GST_FORMAT_BYTES)) != NULL,
225       "Could not prepare duration query");
226   fail_unless ((pos = gst_query_new_position (GST_FORMAT_BYTES)) != NULL,
227       "Could not prepare position query");
228
229   fail_unless (gst_bin_add (bin, src), "Could not add src to bin");
230   fail_unless (gst_bin_add (bin, sink), "Could not add sink to bin");
231   fail_unless (gst_element_link (src, sink), "could not link src and sink");
232
233   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PLAYING);
234   fail_if (ret == GST_STATE_CHANGE_FAILURE, "Failed to set pipeline PLAYING");
235   if (ret == GST_STATE_CHANGE_ASYNC)
236     gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, GST_CLOCK_TIME_NONE);
237
238   /* Query the bin */
239   fail_unless (gst_element_query (GST_ELEMENT (bin), pos),
240       "Could not query pipeline position");
241   fail_unless (gst_element_query (GST_ELEMENT (bin), dur),
242       "Could not query pipeline duration");
243
244   /* Query elements */
245   fail_unless (gst_element_query (GST_ELEMENT (src), pos),
246       "Could not query position of fakesrc");
247   fail_unless (gst_element_query (GST_ELEMENT (src), pos),
248       "Could not query duration of fakesrc");
249
250   fail_unless (gst_element_query (GST_ELEMENT (sink), pos),
251       "Could not query position of fakesink");
252   fail_unless (gst_element_query (GST_ELEMENT (sink), pos),
253       "Could not query duration of fakesink");
254
255   /* Query pads */
256   fail_unless ((pad = gst_element_get_static_pad (src, "src")) != NULL,
257       "Could not get source pad of fakesrc");
258   fail_unless (gst_pad_query (pad, pos),
259       "Could not query position of fakesrc src pad");
260   fail_unless (gst_pad_query (pad, dur),
261       "Could not query duration of fakesrc src pad");
262   gst_object_unref (pad);
263
264   /* We don't query the sink pad of fakesink, it doesn't 
265    * handle downstream queries atm, but it might later, who knows? */
266
267   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
268   fail_if (ret == GST_STATE_CHANGE_FAILURE, "Failed to set pipeline NULL");
269   if (ret == GST_STATE_CHANGE_ASYNC)
270     gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, GST_CLOCK_TIME_NONE);
271
272   gst_query_unref (dur);
273   gst_query_unref (pos);
274   gst_object_unref (bin);
275 }
276
277 GST_END_TEST;
278
279 static Suite *
280 gst_query_suite (void)
281 {
282   Suite *s = suite_create ("GstQuery");
283   TCase *tc_chain = tcase_create ("queries");
284
285   tcase_set_timeout (tc_chain, 20);
286
287   suite_add_tcase (s, tc_chain);
288   tcase_add_test (tc_chain, create_queries);
289   tcase_add_test (tc_chain, test_queries);
290   return s;
291 }
292
293 GST_CHECK_MAIN (gst_query);