tests/check/: use the new macro
[platform/upstream/gstreamer.git] / tests / check / pipelines / simple-launch-lines.c
1 /* GStreamer
2  * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
3  *
4  * simple_launch_lines.c: Unit test for simple pipelines
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
23 #include <gst/check/gstcheck.h>
24
25
26 static GstElement *
27 setup_pipeline (gchar * pipe_descr)
28 {
29   GstElement *pipeline;
30
31   pipeline = gst_parse_launch (pipe_descr, NULL);
32   fail_unless (GST_IS_PIPELINE (pipeline));
33   return pipeline;
34 }
35
36 /*
37  * run_pipeline:
38  * @pipe: the pipeline to run
39  * @desc: the description for use in messages
40  * @message_types: is a mask of expected message_types
41  * @tmessage: is the expected terminal message
42  *
43  * the poll call will time out after half a second.
44  */
45 static void
46 run_pipeline (GstElement * pipeline, gchar * descr,
47     GstMessageType message_types, GstMessageType tmessage)
48 {
49   GstBus *bus;
50   GstMessageType rmessage;
51   GstStateChangeReturn ret;
52
53   fail_if (pipeline == NULL);
54   bus = gst_element_get_bus (pipeline);
55   fail_if (bus == NULL);
56
57   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
58   ret = gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
59
60   if (ret != GST_STATE_CHANGE_SUCCESS) {
61     g_critical ("Couldn't set pipeline to PLAYING");
62     goto done;
63   }
64
65   while (1) {
66     GstMessage *message = gst_bus_poll (bus, GST_MESSAGE_ANY, GST_SECOND / 2);
67
68     if (message) {
69       rmessage = GST_MESSAGE_TYPE (message);
70       gst_message_unref (message);
71     } else {
72       rmessage = GST_MESSAGE_UNKNOWN;
73     }
74
75     if (rmessage == tmessage) {
76       break;
77     } else if (rmessage == GST_MESSAGE_UNKNOWN) {
78       g_critical ("Unexpected timeout in gst_bus_poll, looking for %d: %s",
79           tmessage, descr);
80       break;
81     } else if (rmessage & message_types) {
82       continue;
83     }
84     g_critical ("Unexpected message received of type %d, looking for %d: %s",
85         rmessage, tmessage, descr);
86   }
87
88 done:
89   gst_element_set_state (pipeline, GST_STATE_NULL);
90   gst_object_unref (pipeline);
91   gst_object_unref (bus);
92 }
93
94 GST_START_TEST (test_2_elements)
95 {
96   gchar *s;
97
98   s = "fakesrc can-activate-push=false ! fakesink can-activate-pull=true";
99   run_pipeline (setup_pipeline (s), s,
100       GST_MESSAGE_NEW_CLOCK | GST_MESSAGE_STATE_CHANGED, GST_MESSAGE_UNKNOWN);
101
102   s = "fakesrc can-activate-push=true ! fakesink can-activate-pull=false";
103   run_pipeline (setup_pipeline (s), s,
104       GST_MESSAGE_NEW_CLOCK | GST_MESSAGE_STATE_CHANGED, GST_MESSAGE_UNKNOWN);
105
106   s = "fakesrc can-activate-push=false num-buffers=10 ! fakesink can-activate-pull=true";
107   run_pipeline (setup_pipeline (s), s,
108       GST_MESSAGE_NEW_CLOCK | GST_MESSAGE_STATE_CHANGED, GST_MESSAGE_EOS);
109
110   s = "fakesrc can-activate-push=true num-buffers=10 ! fakesink can-activate-pull=false";
111   run_pipeline (setup_pipeline (s), s,
112       GST_MESSAGE_NEW_CLOCK | GST_MESSAGE_STATE_CHANGED, GST_MESSAGE_EOS);
113
114   s = "fakesrc can-activate-push=false ! fakesink can-activate-pull=false";
115   ASSERT_CRITICAL (run_pipeline (setup_pipeline (s), s,
116           GST_MESSAGE_NEW_CLOCK | GST_MESSAGE_STATE_CHANGED,
117           GST_MESSAGE_UNKNOWN));
118 }
119
120 GST_END_TEST;
121
122 static void
123 got_handoff (GstElement * sink, GstBuffer * buf, GstPad * pad, gpointer unused)
124 {
125   gst_element_post_message
126       (sink, gst_message_new_application (NULL, gst_structure_new ("foo",
127               NULL)));
128 }
129
130 static void
131 assert_live_count (GType type, gint live)
132 {
133   GstAllocTrace *trace;
134   const gchar *name;
135
136   if (gst_alloc_trace_available ()) {
137     name = g_type_name (type);
138     fail_if (name == NULL);
139     trace = gst_alloc_trace_get (name);
140     if (trace) {
141       g_return_if_fail (trace->live == live);
142     }
143   } else {
144     g_print ("\nSkipping live count tests; recompile with traces to enable\n");
145   }
146 }
147
148 GST_START_TEST (test_stop_from_app)
149 {
150   GstElement *fakesrc, *fakesink, *pipeline;
151   GstBus *bus;
152   GstStateChangeReturn ret;
153   GstMessageType rmessage;
154   GstMessage *message;
155
156   assert_live_count (GST_TYPE_BUFFER, 0);
157
158   fakesrc = gst_element_factory_make ("fakesrc", NULL);
159   fakesink = gst_element_factory_make ("fakesink", NULL);
160   pipeline = gst_element_factory_make ("pipeline", NULL);
161
162   fail_unless (fakesrc && fakesink && pipeline);
163
164   gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL);
165   fail_unless (gst_element_link (fakesrc, fakesink) != FALSE);
166
167   g_object_set (fakesink, "signal-handoffs", (gboolean) TRUE, NULL);
168   g_signal_connect (fakesink, "handoff", G_CALLBACK (got_handoff), NULL);
169
170   gst_element_set_state (pipeline, GST_STATE_PAUSED);
171   ret = gst_element_get_state (pipeline, NULL, NULL, 5 * GST_SECOND);
172   switch (ret) {
173     case GST_STATE_CHANGE_FAILURE:
174       g_error ("Failed to change state to PAUSED");
175       break;
176     case GST_STATE_CHANGE_ASYNC:
177       g_error ("Failed to change state to PAUSED within 5 seconds");
178       break;
179     default:
180       break;
181   }
182
183   gst_element_set_state (pipeline, GST_STATE_PLAYING);
184
185   bus = gst_element_get_bus (pipeline);
186   fail_if (bus == NULL);
187
188   /* will time out after half a second */
189   message = gst_bus_poll (bus, GST_MESSAGE_APPLICATION, GST_SECOND / 2);
190   if (message) {
191     rmessage = GST_MESSAGE_TYPE (message);
192     gst_message_unref (message);
193   } else {
194     rmessage = GST_MESSAGE_UNKNOWN;
195   }
196   fail_unless (rmessage == GST_MESSAGE_APPLICATION,
197       "polled message is not APPLICATION but %s",
198       gst_message_type_get_name (rmessage));
199
200   gst_element_set_state (pipeline, GST_STATE_NULL);
201   gst_object_unref (pipeline);
202   gst_object_unref (bus);
203
204   assert_live_count (GST_TYPE_BUFFER, 0);
205 }
206
207 GST_END_TEST;
208
209 Suite *
210 simple_launch_lines_suite (void)
211 {
212   Suite *s = suite_create ("Pipelines");
213   TCase *tc_chain = tcase_create ("linear");
214
215   /* time out after 20s, not the default 3 */
216   tcase_set_timeout (tc_chain, 0);
217
218   suite_add_tcase (s, tc_chain);
219   tcase_add_test (tc_chain, test_2_elements);
220   tcase_add_test (tc_chain, test_stop_from_app);
221   return s;
222 }
223
224 GST_CHECK_MAIN (simple_launch_lines);