tests: add test to create a factory
[platform/upstream/gstreamer.git] / tests / check / gst / gstelementfactory.c
1 /* GStreamer
2  * Copyright (C) 2011 Stefan Kost <ensonic@users.sf.net>
3  *
4  * gstelementfactory.c: Unit test for GstElementFactory
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 #include <gst/check/gstcheck.h>
23
24 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
25     GST_PAD_SINK,
26     GST_PAD_ALWAYS,
27     GST_STATIC_CAPS ("audio/x-raw-int, " "channels = (int) [ 1, 6 ]")
28     );
29 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
30     GST_PAD_SRC,
31     GST_PAD_ALWAYS,
32     GST_STATIC_CAPS ("audio/x-raw-int, " "channels = (int) [ 1, 6 ]")
33     );
34
35 static void
36 setup_pad_template (GstElementFactory * factory, GstStaticPadTemplate * tmpl)
37 {
38   GstStaticPadTemplate *template;
39
40   template = g_slice_dup (GstStaticPadTemplate, tmpl);
41   factory->staticpadtemplates = g_list_append (factory->staticpadtemplates,
42       template);
43   factory->numpadtemplates++;
44 }
45
46 static GstElementFactory *
47 setup_factory (void)
48 {
49   GstPluginFeature *feature;
50   GstElementFactory *factory;
51
52   feature = g_object_newv (GST_TYPE_ELEMENT_FACTORY, 0, NULL);
53   gst_plugin_feature_set_name (feature, "test");
54
55   factory = GST_ELEMENT_FACTORY_CAST (feature);
56   factory->details.longname = g_strdup ("test");
57   factory->details.klass = g_strdup ("test");
58   factory->details.description = g_strdup ("test");
59   factory->details.author = g_strdup ("test");
60
61   setup_pad_template (factory, &sink_template);
62   setup_pad_template (factory, &src_template);
63
64   return factory;
65 }
66
67 /* create a basic factory */
68 GST_START_TEST (test_create)
69 {
70   GstElementFactory *factory;
71
72   factory = setup_factory ();
73   fail_if (factory == NULL);
74
75   g_object_unref (factory);
76 }
77
78 GST_END_TEST;
79
80 /* check if the elementfactory of a class is filled (see #131079) */
81 GST_START_TEST (test_class)
82 {
83   GstElementClass *klass;
84   GstElementFactory *factory, *tmp;
85   GType type;
86
87   GST_DEBUG ("finding factory for queue");
88   factory = gst_element_factory_find ("queue");
89   fail_if (factory == NULL);
90
91   /* it may already be loaded if check is being run with CK_FORK=no */
92   if (!GST_PLUGIN_FEATURE (factory)->loaded) {
93     GST_DEBUG ("getting the type");
94     /* feature is not loaded, should return 0 as the type */
95     type = gst_element_factory_get_element_type (factory);
96     fail_if (type != 0);
97   }
98
99   GST_DEBUG ("now loading the plugin");
100   tmp =
101       GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
102           (factory)));
103   fail_if (tmp == NULL);
104
105   gst_object_unref (factory);
106   factory = tmp;
107
108   /* feature is now loaded */
109   type = gst_element_factory_get_element_type (factory);
110   fail_if (type == 0);
111
112   klass = g_type_class_ref (factory->type);
113   fail_if (klass == NULL);
114
115   GST_DEBUG ("checking the element factory class field");
116   /* and elementfactory is filled in */
117   fail_if (klass->elementfactory == NULL);
118   fail_if (klass->elementfactory != factory);
119
120 }
121
122 GST_END_TEST;
123
124
125 static Suite *
126 gst_element_factory_suite (void)
127 {
128   Suite *s = suite_create ("GstElementFactory");
129   TCase *tc_chain = tcase_create ("element-factory tests");
130
131   suite_add_tcase (s, tc_chain);
132   tcase_add_test (tc_chain, test_class);
133   tcase_add_test (tc_chain, test_create);
134
135   return s;
136 }
137
138 GST_CHECK_MAIN (gst_element_factory);