plugin: add release datetime field to GstPluginDesc and set it if GST_PACKAGE_RELEASE...
[platform/upstream/gstreamer.git] / tests / check / gst / gstplugin.c
1 /* GStreamer
2  *
3  * unit test for GstPlugin
4  *
5  * Copyright 2004 Thomas Vander Stichele <thomas at apestaart dot org>
6  * Copyright 2005 David Schleef <ds@schleef.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include "config.h"
25
26 #include <gst/check/gstcheck.h>
27
28 #ifdef GST_DISABLE_DEPRECATED
29 void _gst_plugin_register_static (GstPluginDesc * desc);
30 #endif
31
32 /* keep in sync with GST_GNUC_CONSTRUCTOR in gstmacros.h (ideally we'd just
33  * do it there, but I don't want to touch that now, and also we really want
34  * to deprecate this macro in the long run, I think) */
35 #if defined (__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4))
36 #define GST_GNUC_CONSTRUCTOR_DEFINED
37 #else
38 #undef GST_GNUC_CONSTRUCTOR_DEFINED
39 #endif
40
41 #ifdef GST_GNUC_CONSTRUCTOR_DEFINED
42 /* ------------------------------------------------------------------------- */
43 /* To make sure the old and deprecated GST_PLUGIN_DEFINE_STATIC still works  */
44
45 static guint plugin_init_counter;       /* 0 */
46
47 static gboolean
48 plugin1_init (GstPlugin * plugin)
49 {
50   ++plugin_init_counter;
51   return TRUE;
52 }
53
54 static gboolean
55 plugin2_init (GstPlugin * plugin)
56 {
57   ++plugin_init_counter;
58   return TRUE;
59 }
60
61 static gboolean
62 plugin3_init (GstPlugin * plugin)
63 {
64   ++plugin_init_counter;
65   return TRUE;
66 }
67
68 GST_PLUGIN_DEFINE_STATIC (GST_VERSION_MAJOR, GST_VERSION_MINOR, "plugin-1",
69     "some static elements 1", plugin1_init, VERSION, GST_LICENSE, PACKAGE,
70     GST_PACKAGE_ORIGIN);
71
72 GST_PLUGIN_DEFINE_STATIC (GST_VERSION_MAJOR, GST_VERSION_MINOR, "plugin-2",
73     "some static elements 2", plugin2_init, VERSION, GST_LICENSE, PACKAGE,
74     GST_PACKAGE_ORIGIN);
75
76 GST_PLUGIN_DEFINE_STATIC (GST_VERSION_MAJOR, GST_VERSION_MINOR, "plugin-3",
77     "some static elements 3", plugin3_init, VERSION, GST_LICENSE, PACKAGE,
78     GST_PACKAGE_ORIGIN);
79
80 GST_START_TEST (test_old_register_static)
81 {
82   fail_unless (plugin_init_counter == 3);
83 }
84
85 GST_END_TEST;
86
87 #endif /* GST_GNUC_CONSTRUCTOR_DEFINED */
88
89
90 static gboolean
91 register_check_elements (GstPlugin * plugin)
92 {
93   return TRUE;
94 }
95
96 static GstPluginDesc plugin_desc = {
97   GST_VERSION_MAJOR,
98   GST_VERSION_MINOR,
99   "check elements",
100   "check elements",
101   register_check_elements,
102   VERSION,
103   GST_LICENSE,
104   PACKAGE,
105   GST_PACKAGE_NAME,
106   GST_PACKAGE_ORIGIN,
107   NULL,
108   GST_PADDING_INIT
109 };
110
111 GST_START_TEST (test_register_static)
112 {
113   GstPlugin *plugin;
114
115   _gst_plugin_register_static (&plugin_desc);
116   fail_unless (gst_plugin_register_static (GST_VERSION_MAJOR,
117           GST_VERSION_MINOR, "more-elements", "more-elements",
118           register_check_elements, VERSION, GST_LICENSE, PACKAGE,
119           GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN));
120
121   plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
122
123   gst_object_unref (plugin);
124 }
125
126 GST_END_TEST;
127
128 GST_START_TEST (test_registry)
129 {
130   GList *g;
131   GstRegistry *registry;
132
133   registry = gst_registry_get_default ();
134
135   for (g = registry->plugins; g; g = g->next) {
136     GstPlugin *plugin = GST_PLUGIN (g->data);
137
138     ASSERT_OBJECT_REFCOUNT (plugin, "plugin in registry", 1);
139     GST_DEBUG ("refcount %d %s", GST_OBJECT_REFCOUNT_VALUE (plugin),
140         plugin->desc.name);
141   }
142   for (g = registry->features; g; g = g->next) {
143     GstPluginFeature *feature = GST_PLUGIN_FEATURE (g->data);
144
145     fail_if (GST_OBJECT_REFCOUNT_VALUE (feature) != 1,
146         "Feature in registry should have refcount of 1");
147     GST_DEBUG ("refcount %d %s", GST_OBJECT_REFCOUNT_VALUE (feature),
148         feature->name);
149   }
150 }
151
152 GST_END_TEST;
153
154 GST_START_TEST (test_load_coreelements)
155 {
156   GstPlugin *unloaded_plugin;
157   GstPlugin *loaded_plugin;
158
159   unloaded_plugin = gst_default_registry_find_plugin ("coreelements");
160   fail_if (unloaded_plugin == NULL, "Failed to find coreelements plugin");
161   fail_if (GST_OBJECT_REFCOUNT_VALUE (unloaded_plugin) != 2,
162       "Refcount of unloaded plugin in registry initially should be 2");
163   GST_DEBUG ("refcount %d", GST_OBJECT_REFCOUNT_VALUE (unloaded_plugin));
164
165   loaded_plugin = gst_plugin_load (unloaded_plugin);
166   fail_if (loaded_plugin == NULL, "Failed to load plugin");
167
168   if (loaded_plugin != unloaded_plugin) {
169     fail_if (GST_OBJECT_REFCOUNT_VALUE (loaded_plugin) != 2,
170         "Refcount of loaded plugin in registry should be 2");
171     GST_DEBUG ("refcount %d", GST_OBJECT_REFCOUNT_VALUE (loaded_plugin));
172     fail_if (GST_OBJECT_REFCOUNT_VALUE (unloaded_plugin) != 1,
173         "Refcount of replaced plugin should be 1");
174     GST_DEBUG ("refcount %d", GST_OBJECT_REFCOUNT_VALUE (unloaded_plugin));
175   }
176
177   gst_object_unref (unloaded_plugin);
178   gst_object_unref (loaded_plugin);
179 }
180
181 GST_END_TEST;
182
183 GST_START_TEST (test_registry_get_plugin_list)
184 {
185   GList *list;
186   GstPlugin *plugin;
187
188   plugin = gst_default_registry_find_plugin ("coreelements");
189   fail_if (GST_OBJECT_REFCOUNT_VALUE (plugin) != 2,
190       "Refcount of plugin in registry should be 2");
191
192   list = gst_registry_get_plugin_list (gst_registry_get_default ());
193
194   fail_if (GST_OBJECT_REFCOUNT_VALUE (plugin) != 3,
195       "Refcount of plugin in registry+list should be 3");
196
197   gst_plugin_list_free (list);
198
199   fail_if (GST_OBJECT_REFCOUNT_VALUE (plugin) != 2,
200       "Refcount of plugin in after list free should be 2");
201
202   gst_object_unref (plugin);
203 }
204
205 GST_END_TEST;
206
207 GST_START_TEST (test_find_plugin)
208 {
209   GstPlugin *plugin;
210
211   plugin = gst_registry_find_plugin (gst_registry_get_default (),
212       "coreelements");
213   fail_if (plugin == NULL, "Failed to find coreelements plugin");
214   ASSERT_OBJECT_REFCOUNT (plugin, "plugin", 2);
215
216   fail_unless_equals_string (plugin->desc.version, VERSION);
217   fail_unless_equals_string (plugin->desc.license, "LGPL");
218   fail_unless_equals_string (plugin->desc.source, "gstreamer");
219   fail_unless_equals_string (plugin->desc.package, GST_PACKAGE_NAME);
220   fail_unless_equals_string (plugin->desc.origin, GST_PACKAGE_ORIGIN);
221
222   gst_object_unref (plugin);
223 }
224
225 GST_END_TEST;
226
227
228 GST_START_TEST (test_find_feature)
229 {
230   GstPluginFeature *feature;
231
232   feature = gst_registry_find_feature (gst_registry_get_default (),
233       "identity", GST_TYPE_ELEMENT_FACTORY);
234   fail_if (feature == NULL, "Failed to find identity element factory");
235   fail_if (strcmp (feature->plugin_name, "coreelements"),
236       "Expected identity to be from coreelements plugin");
237
238   fail_if (GST_OBJECT_REFCOUNT_VALUE (feature) != 2,
239       "Refcount of feature should be 2");
240   GST_DEBUG ("refcount %d", GST_OBJECT_REFCOUNT_VALUE (feature));
241
242   gst_object_unref (feature);
243 }
244
245 GST_END_TEST;
246
247 GST_START_TEST (test_find_element)
248 {
249   GstElementFactory *element_factory;
250
251   element_factory = gst_element_factory_find ("identity");
252   fail_if (element_factory == NULL, "Failed to find identity element factory");
253
254   fail_if (GST_OBJECT_REFCOUNT_VALUE (element_factory) != 2,
255       "Refcount of plugin in registry+feature should be 2");
256
257   gst_object_unref (element_factory);
258 }
259
260 GST_END_TEST;
261
262 #if 0
263 guint8 *
264 peek (gpointer data, gint64 offset, guint size)
265 {
266   return NULL;
267 }
268
269 void
270 suggest (gpointer data, guint probability, const GstCaps * caps)
271 {
272
273 }
274
275 GST_START_TEST (test_typefind)
276 {
277   GstPlugin *plugin;
278   GstPluginFeature *feature;
279   GstTypeFind typefind = {
280     peek,
281     suggest,
282     NULL,
283     NULL,
284     GST_PADDING_INIT
285   };
286
287   plugin = gst_default_registry_find_plugin ("typefindfunctions");
288   fail_if (plugin == NULL, "Failed to find typefind functions");
289   fail_if (GST_OBJECT_REFCOUNT_VALUE (plugin) != 2,
290       "Refcount of plugin in registry should be 2");
291   fail_if (gst_plugin_is_loaded (plugin), "Expected plugin to be unloaded");
292
293   feature = gst_registry_find_feature (gst_registry_get_default (),
294       "audio/x-au", GST_TYPE_TYPE_FIND_FACTORY);
295   fail_if (feature == NULL, "Failed to find audio/x-aw typefind factory");
296   fail_if (feature->plugin != plugin,
297       "Expected identity to be from coreelements plugin");
298
299   fail_if (GST_OBJECT_REFCOUNT_VALUE (plugin) != 3,
300       "Refcount of plugin in registry+feature should be 3");
301
302   gst_type_find_factory_call_function (GST_TYPE_FIND_FACTORY (feature),
303       &typefind);
304
305   gst_object_unref (feature->plugin);
306
307   fail_if (GST_OBJECT_REFCOUNT_VALUE (plugin) != 1,
308       "Refcount of plugin in after list free should be 1");
309
310   gst_object_unref (plugin);
311 }
312
313 GST_END_TEST;
314 #endif
315
316 GST_START_TEST (test_version_checks)
317 {
318   fail_if (gst_default_registry_check_feature_version ("identity",
319           GST_VERSION_MAJOR, GST_VERSION_MINOR, GST_VERSION_MICRO) == FALSE,
320       "Unexpected version check result");
321
322   fail_if (gst_default_registry_check_feature_version ("identity",
323           GST_VERSION_MAJOR + 1, GST_VERSION_MINOR, GST_VERSION_MICRO) == TRUE,
324       "Unexpected version check result");
325
326   fail_if (gst_default_registry_check_feature_version ("identity",
327           GST_VERSION_MAJOR, GST_VERSION_MINOR + 1, GST_VERSION_MICRO) == TRUE,
328       "Unexpected version check result");
329
330   /* If the nano is set, then we expect that X.Y.Z-1.x >= X.Y.Z, so that a
331    * devel plugin is valid against an upcoming release */
332   if (GST_VERSION_NANO > 0) {
333     fail_unless (gst_default_registry_check_feature_version ("identity",
334             GST_VERSION_MAJOR, GST_VERSION_MINOR, GST_VERSION_MICRO + 1) ==
335         TRUE, "Unexpected version check result");
336   } else {
337     fail_if (gst_default_registry_check_feature_version ("identity",
338             GST_VERSION_MAJOR, GST_VERSION_MINOR, GST_VERSION_MICRO + 1) ==
339         TRUE, "Unexpected version check result");
340   }
341
342   if (GST_VERSION_MAJOR > 0) {
343     fail_if (gst_default_registry_check_feature_version ("identity",
344             GST_VERSION_MAJOR - 1, GST_VERSION_MINOR,
345             GST_VERSION_MICRO) == FALSE, "Unexpected version check result");
346   }
347
348   if (GST_VERSION_MINOR > 0) {
349     fail_if (gst_default_registry_check_feature_version ("identity",
350             GST_VERSION_MAJOR, GST_VERSION_MINOR - 1,
351             GST_VERSION_MICRO) == FALSE, "Unexpected version check result");
352   }
353
354   if (GST_VERSION_MICRO > 0) {
355     fail_if (gst_default_registry_check_feature_version ("identity",
356             GST_VERSION_MAJOR, GST_VERSION_MINOR,
357             GST_VERSION_MICRO - 1) == FALSE, "Unexpected version check result");
358   }
359
360   fail_if (gst_default_registry_check_feature_version ("entityid",
361           GST_VERSION_MAJOR, GST_VERSION_MINOR, GST_VERSION_MICRO) == TRUE,
362       "Unexpected version check result");
363 }
364
365 GST_END_TEST;
366
367 static Suite *
368 gst_plugin_suite (void)
369 {
370   Suite *s = suite_create ("GstPlugin");
371   TCase *tc_chain = tcase_create ("general");
372
373   /* turn off timeout */
374   tcase_set_timeout (tc_chain, 60);
375
376   suite_add_tcase (s, tc_chain);
377 #ifdef GST_GNUC_CONSTRUCTOR_DEFINED
378   tcase_add_test (tc_chain, test_old_register_static);
379 #endif
380   tcase_add_test (tc_chain, test_register_static);
381   tcase_add_test (tc_chain, test_registry);
382   tcase_add_test (tc_chain, test_load_coreelements);
383   tcase_add_test (tc_chain, test_registry_get_plugin_list);
384   tcase_add_test (tc_chain, test_find_plugin);
385   tcase_add_test (tc_chain, test_find_feature);
386   tcase_add_test (tc_chain, test_find_element);
387   tcase_add_test (tc_chain, test_version_checks);
388   //tcase_add_test (tc_chain, test_typefind);
389
390   return s;
391 }
392
393 GST_CHECK_MAIN (gst_plugin);