tests/check/: use the new macro
[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   {
66     /* FIXME make tests for:
67      *
68      * LATENCY
69      * JITTER
70      * RATE
71      * SEEKING
72      * SEGMENT
73      * CONVERT
74      */
75   }
76
77   /* FORMATS */
78   {
79     guint size;
80     GstFormat format;
81
82     query = gst_query_new_formats ();
83     fail_if (query == NULL);
84     fail_unless (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
85
86     /* empty */
87     gst_query_parse_formats_length (query, &size);
88     fail_if (size != 0);
89
90     /* see if empty gives undefined formats */
91     gst_query_parse_formats_nth (query, 0, &format);
92     fail_if (format != GST_FORMAT_UNDEFINED);
93     gst_query_parse_formats_nth (query, 1, &format);
94     fail_if (format != GST_FORMAT_UNDEFINED);
95
96     /* set 2 formats */
97     gst_query_set_formats (query, 2, GST_FORMAT_TIME, GST_FORMAT_BYTES);
98
99     gst_query_parse_formats_length (query, &size);
100     fail_if (size != 2);
101
102     format = GST_FORMAT_UNDEFINED;
103
104     gst_query_parse_formats_nth (query, 0, &format);
105     fail_if (format != GST_FORMAT_TIME);
106     gst_query_parse_formats_nth (query, 1, &format);
107     fail_if (format != GST_FORMAT_BYTES);
108
109     /* out of bounds, should return UNDEFINED */
110     gst_query_parse_formats_nth (query, 2, &format);
111     fail_if (format != GST_FORMAT_UNDEFINED);
112
113     /* overwrite with 3 formats */
114     gst_query_set_formats (query, 3, GST_FORMAT_TIME, GST_FORMAT_BYTES,
115         GST_FORMAT_PERCENT);
116
117     gst_query_parse_formats_length (query, &size);
118     fail_if (size != 3);
119
120     gst_query_parse_formats_nth (query, 2, &format);
121     fail_if (format != GST_FORMAT_PERCENT);
122
123     /* create one from an array */
124     {
125       static GstFormat formats[] = {
126         GST_FORMAT_TIME,
127         GST_FORMAT_BYTES,
128         GST_FORMAT_PERCENT
129       };
130       gst_query_set_formatsv (query, 3, formats);
131
132       gst_query_parse_formats_length (query, &size);
133       fail_if (size != 3);
134
135       gst_query_parse_formats_nth (query, 0, &format);
136       fail_if (format != GST_FORMAT_TIME);
137       gst_query_parse_formats_nth (query, 2, &format);
138       fail_if (format != GST_FORMAT_PERCENT);
139     }
140     gst_query_unref (query);
141   }
142 }
143
144 GST_END_TEST;
145
146 GST_START_TEST (test_queries)
147 {
148   GstBin *bin;
149   GstElement *src, *sink;
150   GstStateChangeReturn ret;
151   GstPad *pad;
152   GstQuery *dur, *pos;
153
154   fail_unless ((bin = (GstBin *) gst_pipeline_new (NULL)) != NULL,
155       "Could not create pipeline");
156   fail_unless ((src = gst_element_factory_make ("fakesrc", NULL)) != NULL,
157       "Could not create fakesrc");
158   g_object_set (src, "datarate", 200, "sizetype", 2, NULL);
159
160   fail_unless ((sink = gst_element_factory_make ("fakesink", NULL)) != NULL,
161       "Could not create fakesink");
162   g_object_set (sink, "sync", TRUE, NULL);
163   fail_unless ((dur = gst_query_new_duration (GST_FORMAT_BYTES)) != NULL,
164       "Could not prepare duration query");
165   fail_unless ((pos = gst_query_new_position (GST_FORMAT_BYTES)) != NULL,
166       "Could not prepare position query");
167
168   fail_unless (gst_bin_add (bin, src), "Could not add src to bin");
169   fail_unless (gst_bin_add (bin, sink), "Could not add sink to bin");
170   fail_unless (gst_element_link (src, sink), "could not link src and sink");
171
172   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PLAYING);
173   fail_if (ret == GST_STATE_CHANGE_FAILURE, "Failed to set pipeline PLAYING");
174   if (ret == GST_STATE_CHANGE_ASYNC)
175     gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, GST_CLOCK_TIME_NONE);
176
177   /* Query the bin */
178   fail_unless (gst_element_query (GST_ELEMENT (bin), pos),
179       "Could not query pipeline position");
180   fail_unless (gst_element_query (GST_ELEMENT (bin), dur),
181       "Could not query pipeline duration");
182
183   /* Query elements */
184   fail_unless (gst_element_query (GST_ELEMENT (src), pos),
185       "Could not query position of fakesrc");
186   fail_unless (gst_element_query (GST_ELEMENT (src), pos),
187       "Could not query duration of fakesrc");
188
189   fail_unless (gst_element_query (GST_ELEMENT (sink), pos),
190       "Could not query position of fakesink");
191   fail_unless (gst_element_query (GST_ELEMENT (sink), pos),
192       "Could not query duration of fakesink");
193
194   /* Query pads */
195   fail_unless ((pad = gst_element_get_pad (src, "src")) != NULL,
196       "Could not get source pad of fakesrc");
197   fail_unless (gst_pad_query (pad, pos),
198       "Could not query position of fakesrc src pad");
199   fail_unless (gst_pad_query (pad, dur),
200       "Could not query duration of fakesrc src pad");
201   gst_object_unref (pad);
202
203   /* We don't query the sink pad of fakesink, it doesn't 
204    * handle downstream queries atm, but it might later, who knows? */
205
206   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
207   fail_if (ret == GST_STATE_CHANGE_FAILURE, "Failed to set pipeline NULL");
208   if (ret == GST_STATE_CHANGE_ASYNC)
209     gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, GST_CLOCK_TIME_NONE);
210
211   gst_query_unref (dur);
212   gst_query_unref (pos);
213   gst_object_unref (bin);
214 }
215
216 GST_END_TEST;
217
218 Suite *
219 gst_query_suite (void)
220 {
221   Suite *s = suite_create ("GstQuery");
222   TCase *tc_chain = tcase_create ("queries");
223
224   tcase_set_timeout (tc_chain, 20);
225
226   suite_add_tcase (s, tc_chain);
227   tcase_add_test (tc_chain, create_queries);
228   tcase_add_test (tc_chain, test_queries);
229   return s;
230 }
231
232 GST_CHECK_MAIN (gst_query);