elementfactory: fix factory leak in test
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 #include "../../gst/gst_private.h"
22
23 #include <gst/check/gstcheck.h>
24
25 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
26     GST_PAD_SINK,
27     GST_PAD_ALWAYS,
28     GST_STATIC_CAPS ("audio/x-raw, " "channels = (int) [ 1, 6 ]")
29     );
30 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
31     GST_PAD_SRC,
32     GST_PAD_ALWAYS,
33     GST_STATIC_CAPS ("audio/x-raw, " "channels = (int) [ 1, 6 ]")
34     );
35
36 static void
37 setup_pad_template (GstElementFactory * factory, GstStaticPadTemplate * tmpl)
38 {
39   GstStaticPadTemplate *template;
40
41   template = g_slice_dup (GstStaticPadTemplate, tmpl);
42   factory->staticpadtemplates = g_list_append (factory->staticpadtemplates,
43       template);
44   factory->numpadtemplates++;
45 }
46
47 static GstElementFactory *
48 setup_factory (void)
49 {
50   GstPluginFeature *feature;
51   GstElementFactory *factory;
52
53   feature = g_object_newv (GST_TYPE_ELEMENT_FACTORY, 0, NULL);
54   gst_plugin_feature_set_name (feature, "test");
55
56   factory = GST_ELEMENT_FACTORY_CAST (feature);
57 #if 0
58   gst_element_class_set_metadata (factory, "test", "test", "test", "test");
59 #endif
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 /* test if the factory can accept some caps */
81 GST_START_TEST (test_can_sink_any_caps)
82 {
83   GstElementFactory *factory;
84   GstCaps *caps;
85   gboolean res;
86
87   factory = setup_factory ();
88   fail_if (factory == NULL);
89
90   caps = gst_caps_new_empty_simple ("audio/x-raw");
91   fail_if (caps == NULL);
92   res = gst_element_factory_can_sink_any_caps (factory, caps);
93   fail_if (!res);
94   gst_caps_unref (caps);
95
96   g_object_unref (factory);
97 }
98
99 GST_END_TEST;
100
101 /* test if the factory is compatible with some caps */
102 GST_START_TEST (test_can_sink_all_caps)
103 {
104   GstElementFactory *factory;
105   GstCaps *caps;
106   gboolean res;
107
108   factory = setup_factory ();
109   fail_if (factory == NULL);
110
111   caps = gst_caps_new_empty_simple ("audio/x-raw");
112   fail_if (caps == NULL);
113   res = gst_element_factory_can_sink_all_caps (factory, caps);
114   fail_if (res);
115   gst_caps_unref (caps);
116
117   g_object_unref (factory);
118 }
119
120 GST_END_TEST;
121
122 /* check if the elementfactory of a class is filled (see #131079) */
123 GST_START_TEST (test_class)
124 {
125   GstElementClass *klass;
126   GstElementFactory *factory, *tmp;
127   GType type;
128
129   GST_DEBUG ("finding factory for queue");
130   factory = gst_element_factory_find ("queue");
131   fail_if (factory == NULL);
132
133   /* it may already be loaded if check is being run with CK_FORK=no */
134   if (!GST_PLUGIN_FEATURE (factory)->loaded) {
135     GST_DEBUG ("getting the type");
136     /* feature is not loaded, should return 0 as the type */
137     type = gst_element_factory_get_element_type (factory);
138     fail_if (type != 0);
139   }
140
141   GST_DEBUG ("now loading the plugin");
142   tmp =
143       GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
144           (factory)));
145   fail_if (tmp == NULL);
146
147   gst_object_unref (factory);
148   factory = tmp;
149
150   /* feature is now loaded */
151   type = gst_element_factory_get_element_type (factory);
152   fail_if (type == 0);
153
154   klass = g_type_class_ref (factory->type);
155   fail_if (klass == NULL);
156
157   GST_DEBUG ("checking the element factory class field");
158   /* and elementfactory is filled in */
159   fail_if (klass->elementfactory == NULL);
160   fail_if (klass->elementfactory != factory);
161
162   gst_object_unref (factory);
163 }
164
165 GST_END_TEST;
166
167
168 static Suite *
169 gst_element_factory_suite (void)
170 {
171   Suite *s = suite_create ("GstElementFactory");
172   TCase *tc_chain = tcase_create ("element-factory tests");
173
174   suite_add_tcase (s, tc_chain);
175   tcase_add_test (tc_chain, test_class);
176   tcase_add_test (tc_chain, test_create);
177   tcase_add_test (tc_chain, test_can_sink_any_caps);
178   tcase_add_test (tc_chain, test_can_sink_all_caps);
179
180   return s;
181 }
182
183 GST_CHECK_MAIN (gst_element_factory);