3 * unit test for the controller library
5 * Copyright (C) <2005> Stefan Kost <ensonic at users dot sf dot net>
6 * Copyright (C) <2006-2007> Sebastian Dröge <slomo@circular-chaos.org>
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.
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.
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., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
28 #include <gst/check/gstcheck.h>
31 /* local test element */
45 #define GST_TYPE_TEST_OBJ (gst_test_obj_get_type ())
46 #define GST_TEST_OBJ(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_TEST_OBJ, GstTestObj))
47 #define GST_TEST_OBJ_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_TEST_OBJ, GstTestObjClass))
48 #define GST_IS_TEST_OBJ(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_TEST_OBJ))
49 #define GST_IS_TEST_OBJ_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_TEST_OBJ))
50 #define GST_TEST_OBJ_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_TEST_OBJ, GstTestObjClass))
52 typedef struct _GstTestObj GstTestObj;
53 typedef struct _GstTestObjClass GstTestObjClass;
63 struct _GstTestObjClass
65 GstElementClass parent_class;
68 static GType gst_test_obj_get_type (void);
71 gst_test_obj_get_property (GObject * object,
72 guint property_id, GValue * value, GParamSpec * pspec)
74 GstTestObj *self = GST_TEST_OBJ (object);
76 switch (property_id) {
78 g_value_set_int (value, self->val_int);
81 g_value_set_float (value, self->val_float);
84 g_value_set_double (value, self->val_double);
87 g_value_set_boolean (value, self->val_boolean);
90 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
96 gst_test_obj_set_property (GObject * object,
97 guint property_id, const GValue * value, GParamSpec * pspec)
99 GstTestObj *self = GST_TEST_OBJ (object);
101 switch (property_id) {
103 self->val_int = g_value_get_int (value);
104 GST_DEBUG ("test value int=%d", self->val_int);
107 self->val_float = g_value_get_float (value);
108 GST_DEBUG ("test value float=%f", self->val_float);
111 self->val_double = g_value_get_double (value);
112 GST_DEBUG ("test value double=%lf", self->val_double);
115 self->val_boolean = g_value_get_boolean (value);
116 GST_DEBUG ("test value boolean=%d", self->val_boolean);
118 case PROP_CONSTRUCTONLY:
121 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
127 gst_test_obj_class_init (GstTestObjClass * klass)
129 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
130 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
132 gobject_class->set_property = gst_test_obj_set_property;
133 gobject_class->get_property = gst_test_obj_get_property;
135 g_object_class_install_property (gobject_class, PROP_INT,
136 g_param_spec_int ("int",
138 "int number parameter",
139 0, 100, 0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
141 g_object_class_install_property (gobject_class, PROP_FLOAT,
142 g_param_spec_float ("float",
144 "float number parameter",
145 0.0, 100.0, 0.0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
147 g_object_class_install_property (gobject_class, PROP_DOUBLE,
148 g_param_spec_double ("double",
150 "double number parameter",
151 0.0, 100.0, 0.0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
153 g_object_class_install_property (gobject_class, PROP_BOOLEAN,
154 g_param_spec_boolean ("boolean",
157 FALSE, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
159 g_object_class_install_property (gobject_class, PROP_READONLY,
160 g_param_spec_int ("readonly",
162 "readonly parameter",
163 0, G_MAXINT, 0, G_PARAM_READABLE | GST_PARAM_CONTROLLABLE));
165 g_object_class_install_property (gobject_class, PROP_STATIC,
166 g_param_spec_int ("static",
168 "static parameter", 0, G_MAXINT, 0, G_PARAM_READWRITE));
170 g_object_class_install_property (gobject_class, PROP_CONSTRUCTONLY,
171 g_param_spec_int ("construct-only",
172 "construct-only prop",
173 "construct-only parameter",
174 0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
176 gst_element_class_set_metadata (element_class,
177 "test object for unit tests",
178 "Test", "Use in unit tests", "Stefan Sauer <ensonic@users.sf.net>");
182 gst_test_obj_get_type (void)
184 static volatile gsize test_obj_type = 0;
186 if (g_once_init_enter (&test_obj_type)) {
188 static const GTypeInfo info = {
189 (guint16) sizeof (GstTestObjClass),
191 NULL, // base_finalize
192 (GClassInitFunc) gst_test_obj_class_init, // class_init
193 NULL, // class_finalize
195 (guint16) sizeof (GstTestObj),
197 NULL, // instance_init
200 type = g_type_register_static (GST_TYPE_ELEMENT, "GstTestObj", &info, 0);
201 g_once_init_leave (&test_obj_type, type);
203 return test_obj_type;
206 /* test control source */
208 #define GST_TYPE_TEST_CONTROL_SOURCE (gst_test_control_source_get_type ())
209 #define GST_TEST_CONTROL_SOURCE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_TEST_CONTROL_SOURCE, GstTestControlSource))
210 #define GST_TEST_CONTROL_SOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_TEST_CONTROL_SOURCE, GstTestControlSourceClass))
211 #define GST_IS_TEST_CONTROL_SOURCE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_TEST_CONTROL_SOURCE))
212 #define GST_IS_TEST_CONTROL_SOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_TEST_CONTROL_SOURCE))
213 #define GST_TEST_CONTROL_SOURCE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_TEST_CONTROL_SOURCE, GstTestControlSourceClass))
215 typedef struct _GstTestControlSource GstTestControlSource;
216 typedef struct _GstTestControlSourceClass GstTestControlSourceClass;
218 struct _GstTestControlSource
220 GstControlSource parent;
224 struct _GstTestControlSourceClass
226 GstControlSourceClass parent_class;
229 static GType gst_test_control_source_get_type (void);
231 static GstTestControlSource *
232 gst_test_control_source_new (void)
234 GstTestControlSource *csource =
235 g_object_new (GST_TYPE_TEST_CONTROL_SOURCE, NULL);
237 /* Clear floating flag */
238 gst_object_ref_sink (csource);
244 gst_test_control_source_get (GstTestControlSource * self,
245 GstClockTime timestamp, gdouble * value)
247 *value = self->value;
252 gst_test_control_source_get_value_array (GstTestControlSource * self,
253 GstClockTime timestamp, GstClockTime interval, guint n_values,
258 for (i = 0; i < n_values; i++) {
259 *values = self->value;
266 gst_test_control_source_init (GstTestControlSource * self)
268 GstControlSource *cs = (GstControlSource *) self;
270 cs->get_value = (GstControlSourceGetValue) gst_test_control_source_get;
271 cs->get_value_array = (GstControlSourceGetValueArray)
272 gst_test_control_source_get_value_array;
277 gst_test_control_source_get_type (void)
279 static volatile gsize test_countrol_source_type = 0;
281 if (g_once_init_enter (&test_countrol_source_type)) {
283 static const GTypeInfo info = {
284 (guint16) sizeof (GstTestControlSourceClass),
286 NULL, // base_finalize
288 NULL, // class_finalize
290 (guint16) sizeof (GstTestControlSource),
292 (GInstanceInitFunc) gst_test_control_source_init, // instance_init
296 g_type_register_static (GST_TYPE_CONTROL_SOURCE, "GstTestControlSource",
298 g_once_init_leave (&test_countrol_source_type, type);
300 return test_countrol_source_type;
303 /* test control binding */
310 #define GST_TYPE_TEST_CONTROL_BINDING (gst_test_control_binding_get_type ())
311 #define GST_TEST_CONTROL_BINDING(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_TEST_CONTROL_BINDING, GstTestControlBinding))
312 #define GST_TEST_CONTROL_BINDING_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_TEST_CONTROL_BINDING, GstTestControlBindingClass))
313 #define GST_IS_TEST_CONTROL_BINDING(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_TEST_CONTROL_BINDING))
314 #define GST_IS_TEST_CONTROL_BINDING_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_TEST_CONTROL_BINDING))
315 #define GST_TEST_CONTROL_BINDING_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_TEST_CONTROL_BINDING, GstTestControlBindingClass))
317 typedef struct _GstTestControlBinding GstTestControlBinding;
318 typedef struct _GstTestControlBindingClass GstTestControlBindingClass;
320 struct _GstTestControlBinding
322 GstControlBinding parent;
324 GstControlSource *cs;
326 struct _GstTestControlBindingClass
328 GstControlBindingClass parent_class;
331 static GType gst_test_control_binding_get_type (void);
332 static GstControlBindingClass *gst_test_control_binding_parent_class = NULL;
334 static GstControlBinding *
335 gst_test_control_binding_new (GstObject * object, const gchar * property_name,
336 GstControlSource * cs)
338 GstTestControlBinding *self;
339 self = (GstTestControlBinding *) g_object_new (GST_TYPE_TEST_CONTROL_BINDING,
340 "object", object, "name", property_name, NULL);
342 self->cs = gst_object_ref (cs);
344 return (GstControlBinding *) self;
348 gst_test_control_binding_get_property (GObject * object,
349 guint property_id, GValue * value, GParamSpec * pspec)
351 GstTestControlBinding *self = GST_TEST_CONTROL_BINDING (object);
353 switch (property_id) {
355 g_value_set_object (value, self->cs);
358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
364 gst_test_control_binding_set_property (GObject * object,
365 guint property_id, const GValue * value, GParamSpec * pspec)
367 GstTestControlBinding *self = GST_TEST_CONTROL_BINDING (object);
369 switch (property_id) {
371 self->cs = g_value_dup_object (value);
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
380 gst_test_control_binding_finalize (GObject * obj)
382 GstTestControlBinding *self = GST_TEST_CONTROL_BINDING (obj);
384 gst_object_unref (self->cs);
386 G_OBJECT_CLASS (gst_test_control_binding_parent_class)->finalize (obj);
390 gst_test_control_binding_class_init (gpointer klass, gpointer class_data)
392 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
394 gst_test_control_binding_parent_class = g_type_class_peek_parent (klass);
396 gobject_class->set_property = gst_test_control_binding_set_property;
397 gobject_class->get_property = gst_test_control_binding_get_property;
398 gobject_class->finalize = gst_test_control_binding_finalize;
400 g_object_class_install_property (gobject_class, PROP_CS,
401 g_param_spec_object ("control-source", "ControlSource",
402 "The control source",
403 GST_TYPE_CONTROL_SOURCE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
407 gst_test_control_binding_get_type (void)
409 static volatile gsize test_countrol_binding_type = 0;
411 if (g_once_init_enter (&test_countrol_binding_type)) {
413 static const GTypeInfo info = {
414 (guint16) sizeof (GstTestControlBindingClass),
416 NULL, // base_finalize
417 gst_test_control_binding_class_init, // class_init
418 NULL, // class_finalize
420 (guint16) sizeof (GstTestControlBinding),
422 NULL, // instance_init
426 g_type_register_static (GST_TYPE_CONTROL_BINDING,
427 "GstTestControlBinding", &info, 0);
428 g_once_init_leave (&test_countrol_binding_type, type);
430 return test_countrol_binding_type;
437 gst_element_register (NULL, "testobj", GST_RANK_NONE, GST_TYPE_TEST_OBJ);
448 /* tests for an element with no controlled params */
449 GST_START_TEST (controller_new_fail1)
452 GstTestControlSource *cs;
453 GstControlBinding *cb;
455 elem = gst_element_factory_make ("testobj", NULL);
456 cs = gst_test_control_source_new ();
458 /* that property should not exist */
459 cb = gst_test_control_binding_new (GST_OBJECT (elem), "_schrompf_",
460 GST_CONTROL_SOURCE (cs));
461 fail_unless (GST_CONTROL_BINDING_PSPEC (cb) == NULL, NULL);
463 gst_object_unref (cb);
464 gst_object_unref (cs);
465 gst_object_unref (elem);
470 /* tests for readonly params */
471 GST_START_TEST (controller_new_fail2)
474 GstTestControlSource *cs;
475 GstControlBinding *cb;
477 elem = gst_element_factory_make ("testobj", NULL);
478 cs = gst_test_control_source_new ();
480 /* that property should exist and but is readonly */
481 cb = gst_test_control_binding_new (GST_OBJECT (elem), "readonly",
482 GST_CONTROL_SOURCE (cs));
483 fail_unless (GST_CONTROL_BINDING_PSPEC (cb) == NULL, NULL);
485 gst_object_unref (cb);
486 gst_object_unref (cs);
487 gst_object_unref (elem);
492 /* tests for static params */
493 GST_START_TEST (controller_new_fail3)
496 GstTestControlSource *cs;
497 GstControlBinding *cb;
499 elem = gst_element_factory_make ("testobj", NULL);
500 cs = gst_test_control_source_new ();
502 /* that property should exist and but is not controlable */
503 cb = gst_test_control_binding_new (GST_OBJECT (elem), "static",
504 GST_CONTROL_SOURCE (cs));
505 fail_unless (GST_CONTROL_BINDING_PSPEC (cb) == NULL, NULL);
507 gst_object_unref (cb);
508 gst_object_unref (cs);
509 gst_object_unref (elem);
514 /* tests for construct-only params */
515 GST_START_TEST (controller_new_fail4)
518 GstTestControlSource *cs;
519 GstControlBinding *cb;
521 elem = gst_element_factory_make ("testobj", NULL);
522 cs = gst_test_control_source_new ();
524 /* that property should exist and but is construct-only */
525 cb = gst_test_control_binding_new (GST_OBJECT (elem), "construct-only",
526 GST_CONTROL_SOURCE (cs));
527 fail_unless (GST_CONTROL_BINDING_PSPEC (cb) == NULL, NULL);
529 gst_object_unref (cb);
530 gst_object_unref (cs);
531 gst_object_unref (elem);
537 /* tests for an element with controlled params */
538 GST_START_TEST (controller_new_okay1)
541 GstTestControlSource *cs;
542 GstControlBinding *cb;
544 elem = gst_element_factory_make ("testobj", NULL);
545 cs = gst_test_control_source_new ();
547 /* that property should exist and should be controllable */
548 cb = gst_test_control_binding_new (GST_OBJECT (elem), "int",
549 GST_CONTROL_SOURCE (cs));
550 fail_unless (GST_CONTROL_BINDING_PSPEC (cb) != NULL, NULL);
552 gst_object_unref (cb);
553 gst_object_unref (cs);
554 gst_object_unref (elem);
559 /* tests for an element with several controlled params */
560 GST_START_TEST (controller_new_okay2)
563 GstTestControlSource *cs1, *cs2;
564 GstControlBinding *cb1, *cb2;
566 elem = gst_element_factory_make ("testobj", NULL);
567 cs1 = gst_test_control_source_new ();
568 cs2 = gst_test_control_source_new ();
570 /* these properties should exist and should be controllable */
571 cb1 = gst_test_control_binding_new (GST_OBJECT (elem), "int",
572 GST_CONTROL_SOURCE (cs1));
573 fail_unless (GST_CONTROL_BINDING_PSPEC (cb1) != NULL, NULL);
575 cb2 = gst_test_control_binding_new (GST_OBJECT (elem), "boolean",
576 GST_CONTROL_SOURCE (cs2));
577 fail_unless (GST_CONTROL_BINDING_PSPEC (cb2) != NULL, NULL);
579 gst_object_unref (cb1);
580 gst_object_unref (cb2);
581 gst_object_unref (cs1);
582 gst_object_unref (cs2);
583 gst_object_unref (elem);
588 /* controlling a param twice should be handled */
589 GST_START_TEST (controller_param_twice)
592 GstTestControlSource *cs;
593 GstControlBinding *cb;
596 elem = gst_element_factory_make ("testobj", NULL);
597 cs = gst_test_control_source_new ();
599 /* that property should exist and should be controllable */
600 cb = gst_test_control_binding_new (GST_OBJECT (elem), "int",
601 GST_CONTROL_SOURCE (cs));
602 fail_unless (GST_CONTROL_BINDING_PSPEC (cb) != NULL, NULL);
603 cb = gst_object_ref (cb);
605 res = gst_object_add_control_binding (GST_OBJECT (elem), cb);
606 fail_unless (res, NULL);
608 /* setting it again will just unset the old and set it again
609 * this might cause some trouble with binding the control source again
611 res = gst_object_add_control_binding (GST_OBJECT (elem), cb);
612 fail_unless (res, NULL);
614 /* it should have been added now, let remove it */
615 res = gst_object_remove_control_binding (GST_OBJECT (elem), cb);
616 fail_unless (res, NULL);
618 /* removing it again should not work */
619 res = gst_object_remove_control_binding (GST_OBJECT (elem), cb);
620 fail_unless (!res, NULL);
622 gst_object_unref (cb);
623 gst_object_unref (cs);
624 gst_object_unref (elem);
629 /* tests if we can run controller methods against any GObject */
630 GST_START_TEST (controller_any_gobject)
635 elem = gst_element_factory_make ("bin", "test_elem");
637 /* that element is not controllable */
638 res = gst_object_sync_values (GST_OBJECT (elem), 0LL);
639 /* Syncing should still succeed as there's nothing to sync */
640 fail_unless (res == TRUE, NULL);
642 gst_object_unref (elem);
647 /* tests if we cleanup properly */
648 GST_START_TEST (controller_controlsource_refcounts)
651 GstControlBinding *cb, *test_cb;
652 GstControlSource *cs, *test_cs;
654 elem = gst_element_factory_make ("testobj", NULL);
656 cs = (GstControlSource *) gst_test_control_source_new ();
657 fail_unless (cs != NULL, NULL);
659 fail_unless_equals_int (G_OBJECT (cs)->ref_count, 1);
661 cb = gst_test_control_binding_new (GST_OBJECT (elem), "int", cs);
662 fail_unless (GST_CONTROL_BINDING_PSPEC (cb) != NULL, NULL);
663 fail_unless_equals_int (G_OBJECT (cs)->ref_count, 2);
664 fail_unless (gst_object_add_control_binding (GST_OBJECT (elem), cb));
666 test_cb = gst_object_get_control_binding (GST_OBJECT (elem), "int");
667 fail_unless (test_cb != NULL, NULL);
669 g_object_get (test_cb, "control-source", &test_cs, NULL);
670 fail_unless (test_cs != NULL, NULL);
671 fail_unless (test_cs == cs);
672 fail_unless_equals_int (G_OBJECT (cs)->ref_count, 3);
673 gst_object_unref (test_cs);
674 gst_object_unref (test_cb);
675 gst_object_unref (cs);
677 gst_object_unref (elem);
682 /* tests if we can bind a control source twice */
683 GST_START_TEST (controller_bind_twice)
686 GstControlSource *cs;
687 GstControlBinding *cb1, *cb2;
689 elem = gst_element_factory_make ("testobj", NULL);
691 cs = (GstControlSource *) gst_test_control_source_new ();
692 fail_unless (cs != NULL, NULL);
694 cb1 = gst_test_control_binding_new (GST_OBJECT (elem), "int", cs);
695 fail_unless (GST_CONTROL_BINDING_PSPEC (cb1) != NULL, NULL);
696 cb2 = gst_test_control_binding_new (GST_OBJECT (elem), "double", cs);
697 fail_unless (GST_CONTROL_BINDING_PSPEC (cb2) != NULL, NULL);
699 gst_object_unref (cb1);
700 gst_object_unref (cb2);
701 gst_object_unref (cs);
702 gst_object_unref (elem);
709 gst_controller_suite (void)
711 Suite *s = suite_create ("Controller");
712 TCase *tc = tcase_create ("general");
714 suite_add_tcase (s, tc);
715 tcase_add_checked_fixture (tc, setup, teardown);
716 tcase_add_test (tc, controller_new_fail1);
717 tcase_add_test (tc, controller_new_fail2);
718 tcase_add_test (tc, controller_new_fail3);
719 tcase_add_test (tc, controller_new_fail4);
720 tcase_add_test (tc, controller_new_okay1);
721 tcase_add_test (tc, controller_new_okay2);
722 tcase_add_test (tc, controller_param_twice);
723 tcase_add_test (tc, controller_any_gobject);
724 tcase_add_test (tc, controller_controlsource_refcounts);
725 tcase_add_test (tc, controller_bind_twice);
730 GST_CHECK_MAIN (gst_controller);