Git init
[framework/multimedia/gstreamer0.10.git] / tests / check / gst / gstpreset.c
1 /* GStreamer
2  * Copyright (C) 2008 Nokia Corporation and its subsidary(-ies)
3  *               contact: <stefan.kost@nokia.com>
4  *
5  * gstpreset.c: Unit test for GstPreset
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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <glib.h>
28 #include <glib/gstdio.h>
29 #include <gst/check/gstcheck.h>
30
31 #include <unistd.h>
32
33 static GType gst_preset_test_get_type (void);
34
35 #define GST_TYPE_PRESET_TEST            (gst_preset_test_get_type ())
36 #define GST_PRESET_TEST(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_PRESET_TEST, GstPresetTest))
37 #define GST_PRESET_TEST_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_PRESET_TEST, GstPresetTestClass))
38 #define GST_IS_PRESET_TEST(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_PRESET_TEST))
39 #define GST_IS_PRESET_TEST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_PRESET_TEST))
40 #define GST_PRESET_TEST_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PRESET_TEST, GstPresetTestClass))
41 #define GST_PRESET_TEST_NAME            "preset-test"
42
43 enum
44 {
45   PROP_TEST = 1,
46 };
47
48 typedef struct _GstPresetTest
49 {
50   GstElement parent;
51
52   gint test;
53 } GstPresetTest;
54
55 typedef struct _GstPresetTestClass
56 {
57   GstElementClass parent_class;
58 } GstPresetTestClass;
59
60 static void
61 gst_preset_test_get_property (GObject * object, guint property_id,
62     GValue * value, GParamSpec * pspec)
63 {
64   GstPresetTest *self = GST_PRESET_TEST (object);
65
66   switch (property_id) {
67     case PROP_TEST:
68       g_value_set_int (value, self->test);
69       break;
70     default:
71       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
72       break;
73   }
74 }
75
76 static void
77 gst_preset_test_set_property (GObject * object, guint property_id,
78     const GValue * value, GParamSpec * pspec)
79 {
80   GstPresetTest *self = GST_PRESET_TEST (object);
81
82   switch (property_id) {
83     case PROP_TEST:
84       self->test = g_value_get_int (value);
85       break;
86   }
87 }
88
89 static void
90 gst_preset_test_class_init (GObjectClass * klass)
91 {
92   klass->set_property = gst_preset_test_set_property;
93   klass->get_property = gst_preset_test_get_property;
94
95   g_object_class_install_property (klass, PROP_TEST,
96       g_param_spec_int ("test",
97           "test prop",
98           "test parameter for preset test",
99           G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
100 }
101
102 static void
103 gst_preset_test_base_init (GstPresetTestClass * klass)
104 {
105   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
106
107   gst_element_class_set_details_simple (element_class,
108       "Element for unit tests",
109       "Testing", "Use in unit tests", "Stefan Kost <stefan.kost@nokia.com>");
110 }
111
112 static GType
113 gst_preset_test_get_type (void)
114 {
115   static volatile gsize preset_test_type = 0;
116
117   if (g_once_init_enter (&preset_test_type)) {
118     GType type;
119     const GTypeInfo info = {
120       sizeof (GstPresetTestClass),
121       (GBaseInitFunc) gst_preset_test_base_init,        /* base_init */
122       NULL,                     /* base_finalize */
123       (GClassInitFunc) gst_preset_test_class_init,      /* class_init */
124       NULL,                     /* class_finalize */
125       NULL,                     /* class_data */
126       sizeof (GstPresetTest),
127       0,                        /* n_preallocs */
128       NULL,                     /* instance_init */
129       NULL                      /* value_table */
130     };
131     const GInterfaceInfo preset_interface_info = {
132       NULL,                     /* interface_init */
133       NULL,                     /* interface_finalize */
134       NULL                      /* interface_data */
135     };
136     type = g_type_register_static (GST_TYPE_ELEMENT, "GstPresetTest", &info, 0);
137     g_type_add_interface_static (type, GST_TYPE_PRESET, &preset_interface_info);
138     g_once_init_leave (&preset_test_type, type);
139   }
140   return preset_test_type;
141 }
142
143 static gboolean
144 gst_preset_test_plugin_init (GstPlugin * plugin)
145 {
146   gst_element_register (plugin, GST_PRESET_TEST_NAME, GST_RANK_NONE,
147       GST_TYPE_PRESET_TEST);
148   return TRUE;
149 }
150
151
152 GST_START_TEST (test_check)
153 {
154   GstElement *elem;
155
156   elem = gst_element_factory_make (GST_PRESET_TEST_NAME, NULL);
157   fail_unless (GST_IS_PRESET (elem));
158
159   gst_object_unref (elem);
160 }
161
162 GST_END_TEST;
163
164 GST_START_TEST (test_load)
165 {
166   GstElement *elem;
167   gboolean res;
168
169   elem = gst_element_factory_make (GST_PRESET_TEST_NAME, NULL);
170   res = gst_preset_load_preset (GST_PRESET (elem), "does-not-exist");
171   fail_unless (!res);
172
173   gst_object_unref (elem);
174 }
175
176 GST_END_TEST;
177
178 GST_START_TEST (test_add)
179 {
180   GstElement *elem;
181   gboolean res;
182   gint val;
183
184   elem = gst_element_factory_make (GST_PRESET_TEST_NAME, NULL);
185   g_object_set (elem, "test", 5, NULL);
186
187   res = gst_preset_save_preset (GST_PRESET (elem), "test");
188   fail_unless (res);
189
190   res = gst_preset_load_preset (GST_PRESET (elem), "test");
191   fail_unless (res);
192   g_object_get (elem, "test", &val, NULL);
193   fail_unless (val == 5);
194
195   gst_object_unref (elem);
196 }
197
198 GST_END_TEST;
199
200
201 GST_START_TEST (test_del)
202 {
203   GstElement *elem;
204   gboolean res;
205
206   elem = gst_element_factory_make (GST_PRESET_TEST_NAME, NULL);
207   res = gst_preset_save_preset (GST_PRESET (elem), "test");
208   fail_unless (res);
209
210   res = gst_preset_delete_preset (GST_PRESET (elem), "test");
211   fail_unless (res);
212
213   res = gst_preset_load_preset (GST_PRESET (elem), "test");
214   fail_unless (!res);
215
216   gst_object_unref (elem);
217 }
218
219 GST_END_TEST;
220
221 GST_START_TEST (test_two_instances)
222 {
223   GstElement *elem1, *elem2;
224   gboolean res;
225   gint val;
226
227   elem1 = gst_element_factory_make (GST_PRESET_TEST_NAME, NULL);
228   g_object_set (elem1, "test", 5, NULL);
229
230   res = gst_preset_save_preset (GST_PRESET (elem1), "test");
231   fail_unless (res);
232
233   elem2 = gst_element_factory_make (GST_PRESET_TEST_NAME, NULL);
234   res = gst_preset_load_preset (GST_PRESET (elem2), "test");
235   fail_unless (res);
236   g_object_get (elem2, "test", &val, NULL);
237   fail_unless (val == 5);
238
239   gst_object_unref (elem1);
240   gst_object_unref (elem2);
241 }
242
243 GST_END_TEST;
244
245
246 static void
247 remove_preset_file (void)
248 {
249   gchar *preset_file_name;
250
251   preset_file_name = g_build_filename (g_get_home_dir (),
252       ".gstreamer-" GST_MAJORMINOR, "presets", "GstPresetTest.prs", NULL);
253   g_unlink (preset_file_name);
254   g_free (preset_file_name);
255 }
256
257 static void
258 test_setup (void)
259 {
260   remove_preset_file ();
261   gst_plugin_register_static (GST_VERSION_MAJOR,
262       GST_VERSION_MINOR,
263       "gst-test",
264       "preset test plugin",
265       gst_preset_test_plugin_init,
266       VERSION, GST_LICENSE, PACKAGE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
267 }
268
269 static void
270 test_teardown (void)
271 {
272   remove_preset_file ();
273 }
274
275
276 static Suite *
277 gst_preset_suite (void)
278 {
279   Suite *s = suite_create ("GstPreset");
280   TCase *tc = tcase_create ("preset");
281   gchar *gst_dir;
282   gboolean can_write = FALSE;
283
284   /* cehck if we can create presets */
285   gst_dir = g_build_filename (g_get_home_dir (),
286       ".gstreamer-" GST_MAJORMINOR, NULL);
287   can_write = (g_access (gst_dir, R_OK | W_OK | X_OK) == 0);
288   g_free (gst_dir);
289
290   suite_add_tcase (s, tc);
291   tcase_add_test (tc, test_check);
292   tcase_add_test (tc, test_load);
293   if (can_write) {
294     tcase_add_test (tc, test_add);
295     tcase_add_test (tc, test_del);
296     tcase_add_test (tc, test_two_instances);
297   }
298   tcase_add_unchecked_fixture (tc, test_setup, test_teardown);
299
300   return s;
301 }
302
303 GST_CHECK_MAIN (gst_preset);