3 * unit test for state changes on all elements
5 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
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.
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.
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.
29 #include <gst/check/gstcheck.h>
31 static GList *elements = NULL;
38 gchar **ignorelist = NULL;
39 const gchar *STATE_IGNORE_ELEMENTS = NULL;
41 GST_DEBUG ("getting elements for package %s", PACKAGE);
42 STATE_IGNORE_ELEMENTS = g_getenv ("GST_STATE_IGNORE_ELEMENTS");
43 if (!g_getenv ("GST_NO_STATE_IGNORE_ELEMENTS") && STATE_IGNORE_ELEMENTS) {
44 GST_DEBUG ("Will ignore element factories: '%s'", STATE_IGNORE_ELEMENTS);
45 ignorelist = g_strsplit (STATE_IGNORE_ELEMENTS, " ", 0);
48 plugins = gst_registry_get_plugin_list (gst_registry_get ());
50 for (p = plugins; p; p = p->next) {
51 GstPlugin *plugin = p->data;
53 if (strcmp (gst_plugin_get_source (plugin), PACKAGE) != 0)
57 gst_registry_get_feature_list_by_plugin (gst_registry_get (),
58 gst_plugin_get_name (plugin));
60 for (f = features; f; f = f->next) {
61 GstPluginFeature *feature = f->data;
63 gboolean ignore = FALSE;
65 if (!GST_IS_ELEMENT_FACTORY (feature))
68 name = GST_OBJECT_NAME (feature);
73 for (s = ignorelist; s && *s; ++s) {
74 if (g_str_has_prefix (name, *s)) {
75 GST_DEBUG ("ignoring element %s", name);
83 GST_DEBUG ("adding element %s", name);
84 elements = g_list_prepend (elements, g_strdup (name));
86 gst_plugin_feature_list_free (features);
88 gst_plugin_list_free (plugins);
89 g_strfreev (ignorelist);
97 for (e = elements; e; e = e->next) {
100 g_list_free (elements);
105 GST_START_TEST (test_state_changes_up_and_down_seq)
110 for (e = elements; e; e = e->next) {
111 const gchar *name = e->data;
113 GST_DEBUG ("testing element %s", name);
114 element = gst_element_factory_make (name, name);
115 fail_if (element == NULL, "Could not make element from factory %s", name);
117 if (GST_IS_PIPELINE (element)) {
118 GST_DEBUG ("element %s is a pipeline", name);
121 gst_element_set_state (element, GST_STATE_READY);
122 gst_element_set_state (element, GST_STATE_PAUSED);
123 gst_element_set_state (element, GST_STATE_PLAYING);
124 gst_element_set_state (element, GST_STATE_PAUSED);
125 gst_element_set_state (element, GST_STATE_READY);
126 gst_element_set_state (element, GST_STATE_NULL);
127 gst_element_set_state (element, GST_STATE_PAUSED);
128 gst_element_set_state (element, GST_STATE_READY);
129 gst_element_set_state (element, GST_STATE_PLAYING);
130 gst_element_set_state (element, GST_STATE_PAUSED);
131 gst_element_set_state (element, GST_STATE_NULL);
132 gst_object_unref (GST_OBJECT (element));
138 GST_START_TEST (test_state_changes_up_seq)
143 for (e = elements; e; e = e->next) {
144 const gchar *name = e->data;
146 GST_DEBUG ("testing element %s", name);
147 element = gst_element_factory_make (name, name);
148 fail_if (element == NULL, "Could not make element from factory %s", name);
150 if (GST_IS_PIPELINE (element)) {
151 GST_DEBUG ("element %s is a pipeline", name);
154 gst_element_set_state (element, GST_STATE_READY);
156 gst_element_set_state (element, GST_STATE_PAUSED);
157 gst_element_set_state (element, GST_STATE_READY);
159 gst_element_set_state (element, GST_STATE_PAUSED);
160 gst_element_set_state (element, GST_STATE_PLAYING);
161 gst_element_set_state (element, GST_STATE_PAUSED);
162 gst_element_set_state (element, GST_STATE_READY);
164 gst_element_set_state (element, GST_STATE_NULL);
165 gst_object_unref (GST_OBJECT (element));
171 GST_START_TEST (test_state_changes_down_seq)
176 for (e = elements; e; e = e->next) {
177 const gchar *name = e->data;
179 GST_DEBUG ("testing element %s", name);
180 element = gst_element_factory_make (name, name);
181 fail_if (element == NULL, "Could not make element from factory %s", name);
183 if (GST_IS_PIPELINE (element)) {
184 GST_DEBUG ("element %s is a pipeline", name);
187 gst_element_set_state (element, GST_STATE_READY);
188 gst_element_set_state (element, GST_STATE_PAUSED);
189 gst_element_set_state (element, GST_STATE_PLAYING);
191 gst_element_set_state (element, GST_STATE_PAUSED);
192 gst_element_set_state (element, GST_STATE_PLAYING);
194 gst_element_set_state (element, GST_STATE_PAUSED);
195 gst_element_set_state (element, GST_STATE_READY);
196 gst_element_set_state (element, GST_STATE_PAUSED);
197 gst_element_set_state (element, GST_STATE_PLAYING);
199 gst_element_set_state (element, GST_STATE_PAUSED);
200 gst_element_set_state (element, GST_STATE_READY);
201 gst_element_set_state (element, GST_STATE_NULL);
202 gst_object_unref (GST_OBJECT (element));
209 element_state_is (GstElement * e, GstState s)
211 GstStateChangeReturn ret;
214 ret = gst_element_get_state (e, &state, NULL, GST_CLOCK_TIME_NONE);
215 return (ret == GST_STATE_CHANGE_SUCCESS && state == s);
218 GST_START_TEST (test_state_changes_up_failure)
224 /* we want at least one before and one after */
225 g_assert (G_N_ELEMENTS (mid) >= 3);
228 bin = gst_element_factory_make ("bin", NULL);
231 for (n = 0; n < G_N_ELEMENTS (mid); ++n) {
232 const char *element = n != 1 ? "identity" : "fakesink";
233 mid[n] = gst_element_factory_make (element, NULL);
234 gst_bin_add (GST_BIN (bin), mid[n]);
236 g_object_set (mid[n], "async", FALSE, NULL);
239 /* This one should work */
240 for (n = 0; n < G_N_ELEMENTS (mid); ++n)
241 fail_unless (element_state_is (mid[n], GST_STATE_NULL));
242 gst_element_set_state (bin, GST_STATE_READY);
243 for (n = 0; n < G_N_ELEMENTS (mid); ++n)
244 fail_unless (element_state_is (mid[n], GST_STATE_READY));
245 gst_element_set_state (bin, GST_STATE_NULL);
246 for (n = 0; n < G_N_ELEMENTS (mid); ++n)
247 fail_unless (element_state_is (mid[n], GST_STATE_NULL));
249 /* make the middle element fail to switch up */
250 g_object_set (mid[1], "state-error", 1 /* null-to-ready */ , NULL);
252 /* This one should not */
253 for (n = 0; n < G_N_ELEMENTS (mid); ++n)
254 fail_unless (element_state_is (mid[n], GST_STATE_NULL));
255 gst_element_set_state (bin, GST_STATE_READY);
256 for (n = 0; n < G_N_ELEMENTS (mid); ++n)
257 fail_unless (element_state_is (mid[n], GST_STATE_NULL));
258 gst_element_set_state (bin, GST_STATE_NULL);
259 for (n = 0; n < G_N_ELEMENTS (mid); ++n)
260 fail_unless (element_state_is (mid[n], GST_STATE_NULL));
263 gst_object_unref (bin);
272 Suite *s = suite_create ("states");
273 TCase *tc_chain = tcase_create ("general");
275 suite_add_tcase (s, tc_chain);
276 tcase_add_checked_fixture (tc_chain, setup, teardown);
277 tcase_add_test (tc_chain, test_state_changes_up_and_down_seq);
278 tcase_add_test (tc_chain, test_state_changes_up_seq);
279 tcase_add_test (tc_chain, test_state_changes_down_seq);
280 tcase_add_test (tc_chain, test_state_changes_up_failure);
285 GST_CHECK_MAIN (states);