251f433146677edc6feff02f0a3647aca3c89ab9
[platform/upstream/gstreamer.git] / tests / check / generic / states.c
1 /* GStreamer
2  *
3  * unit test for state changes on all elements
4  *
5  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <unistd.h>
28
29 #include <gst/check/gstcheck.h>
30
31 static GList *elements = NULL;
32
33 static void
34 setup (void)
35 {
36   GList *features, *f;
37   GList *plugins, *p;
38   gchar **ignorelist = NULL;
39   const gchar *STATE_IGNORE_ELEMENTS = NULL;
40
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);
46   }
47
48   plugins = gst_registry_get_plugin_list (gst_registry_get ());
49
50   for (p = plugins; p; p = p->next) {
51     GstPlugin *plugin = p->data;
52
53     if (strcmp (gst_plugin_get_source (plugin), PACKAGE) != 0)
54       continue;
55
56     features =
57         gst_registry_get_feature_list_by_plugin (gst_registry_get (),
58         gst_plugin_get_name (plugin));
59
60     for (f = features; f; f = f->next) {
61       GstPluginFeature *feature = f->data;
62       const gchar *name;
63       gboolean ignore = FALSE;
64
65       if (!GST_IS_ELEMENT_FACTORY (feature))
66         continue;
67
68       name = GST_OBJECT_NAME (feature);
69
70       if (ignorelist) {
71         gchar **s;
72
73         for (s = ignorelist; s && *s; ++s) {
74           if (g_str_has_prefix (name, *s)) {
75             GST_DEBUG ("ignoring element %s", name);
76             ignore = TRUE;
77           }
78         }
79         if (ignore)
80           continue;
81       }
82
83       GST_DEBUG ("adding element %s", name);
84       elements = g_list_prepend (elements, g_strdup (name));
85     }
86     gst_plugin_feature_list_free (features);
87   }
88   gst_plugin_list_free (plugins);
89   g_strfreev (ignorelist);
90 }
91
92 static void
93 teardown (void)
94 {
95   GList *e;
96
97   for (e = elements; e; e = e->next) {
98     g_free (e->data);
99   }
100   g_list_free (elements);
101   elements = NULL;
102 }
103
104
105 GST_START_TEST (test_state_changes_up_and_down_seq)
106 {
107   GstElement *element;
108   GList *e;
109
110   for (e = elements; e; e = e->next) {
111     const gchar *name = e->data;
112
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);
116
117     if (GST_IS_PIPELINE (element)) {
118       GST_DEBUG ("element %s is a pipeline", name);
119     }
120
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));
133   }
134 }
135
136 GST_END_TEST;
137
138 GST_START_TEST (test_state_changes_up_seq)
139 {
140   GstElement *element;
141   GList *e;
142
143   for (e = elements; e; e = e->next) {
144     const gchar *name = e->data;
145
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);
149
150     if (GST_IS_PIPELINE (element)) {
151       GST_DEBUG ("element %s is a pipeline", name);
152     }
153
154     gst_element_set_state (element, GST_STATE_READY);
155
156     gst_element_set_state (element, GST_STATE_PAUSED);
157     gst_element_set_state (element, GST_STATE_READY);
158
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);
163
164     gst_element_set_state (element, GST_STATE_NULL);
165     gst_object_unref (GST_OBJECT (element));
166   }
167 }
168
169 GST_END_TEST;
170
171 GST_START_TEST (test_state_changes_down_seq)
172 {
173   GstElement *element;
174   GList *e;
175
176   for (e = elements; e; e = e->next) {
177     const gchar *name = e->data;
178
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);
182
183     if (GST_IS_PIPELINE (element)) {
184       GST_DEBUG ("element %s is a pipeline", name);
185     }
186
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);
190
191     gst_element_set_state (element, GST_STATE_PAUSED);
192     gst_element_set_state (element, GST_STATE_PLAYING);
193
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);
198
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));
203   }
204 }
205
206 GST_END_TEST;
207
208
209 static Suite *
210 states_suite (void)
211 {
212   Suite *s = suite_create ("states");
213   TCase *tc_chain = tcase_create ("general");
214
215   suite_add_tcase (s, tc_chain);
216   tcase_add_checked_fixture (tc_chain, setup, teardown);
217   tcase_add_test (tc_chain, test_state_changes_up_and_down_seq);
218   tcase_add_test (tc_chain, test_state_changes_up_seq);
219   tcase_add_test (tc_chain, test_state_changes_down_seq);
220
221   return s;
222 }
223
224 GST_CHECK_MAIN (states);