diff options
author | Matthias Clasen <mclasen@redhat.com> | 2010-12-27 23:37:21 -0500 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2010-12-27 23:40:28 -0500 |
commit | 2a5e0cf9e0d67ea35b2f5fdf8bb2297354bcb7e6 (patch) | |
tree | 4b7f5a757a8362d40128382da1bbaa8ad0dc3b0e /tests | |
parent | 58e36daf29aa1cf042a1141b9bbb91b4e794bfc7 (diff) |
Convert ifaceproperties.c to test framework
Also add a test that checks warnings for failure to implement
interface properties.
See https://bugzilla.gnome.org/show_bug.cgi?id=637738
Diffstat (limited to 'tests')
-rw-r--r-- | tests/gobject/ifaceproperties.c | 379 |
1 files changed, 260 insertions, 119 deletions
diff --git a/tests/gobject/ifaceproperties.c b/tests/gobject/ifaceproperties.c index 3b758ccbc..14762fb6e 100644 --- a/tests/gobject/ifaceproperties.c +++ b/tests/gobject/ifaceproperties.c @@ -17,13 +17,7 @@ * Boston, MA 02111-1307, USA. */ -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "TestIfaceProperties" - -#undef G_DISABLE_ASSERT -#undef G_DISABLE_CHECKS -#undef G_DISABLE_CAST_CHECKS - +#include <stdlib.h> #include <string.h> #include <glib-object.h> @@ -62,7 +56,7 @@ enum { * BaseObject, a parent class for DerivedObject */ #define BASE_TYPE_OBJECT (base_object_get_type ()) -#define BASE_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BASE_TYPE_OBJECT, BaseObject)) +#define BASE_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BASE_TYPE_OBJECT, BaseObject)) typedef struct _BaseObject BaseObject; typedef struct _BaseObjectClass BaseObjectClass; @@ -122,30 +116,30 @@ static void test_iface_default_init (TestIfaceClass *iface_vtable) { inherited_spec1 = iface_spec1 = g_param_spec_int ("prop1", - "Prop1", - "Property 1", - G_MININT, /* min */ - 0xFFFF, /* max */ - 42, /* default */ - G_PARAM_READWRITE | G_PARAM_CONSTRUCT); + "Prop1", + "Property 1", + G_MININT, /* min */ + 0xFFFF, /* max */ + 42, /* default */ + G_PARAM_READWRITE | G_PARAM_CONSTRUCT); g_object_interface_install_property (iface_vtable, iface_spec1); iface_spec2 = g_param_spec_int ("prop2", - "Prop2", - "Property 2", - G_MININT, /* min */ - G_MAXINT, /* max */ - 0, /* default */ - G_PARAM_WRITABLE); + "Prop2", + "Property 2", + G_MININT, /* min */ + G_MAXINT, /* max */ + 0, /* default */ + G_PARAM_WRITABLE); g_object_interface_install_property (iface_vtable, iface_spec2); inherited_spec3 = iface_spec3 = g_param_spec_int ("prop3", - "Prop3", - "Property 3", - G_MININT, /* min */ - G_MAXINT, /* max */ - 0, /* default */ - G_PARAM_READWRITE); + "Prop3", + "Property 3", + G_MININT, /* min */ + G_MAXINT, /* max */ + 0, /* default */ + G_PARAM_READWRITE); g_object_interface_install_property (iface_vtable, iface_spec3); } @@ -153,9 +147,9 @@ static DEFINE_IFACE (TestIface, test_iface, NULL, test_iface_default_init) static GObject* -base_object_constructor (GType type, - guint n_construct_properties, - GObjectConstructParam *construct_properties) +base_object_constructor (GType type, + guint n_construct_properties, + GObjectConstructParam *construct_properties) { /* The constructor is the one place where a GParamSpecOverride is visible * to the outside world, so we do a bunch of checks here @@ -184,7 +178,7 @@ base_object_constructor (GType type, */ g_value_init (&value1, G_TYPE_INT); g_value_init (&value2, G_TYPE_INT); - + g_param_value_set_default (pspec, &value1); g_assert (g_value_get_int (&value1) == 42); @@ -193,29 +187,29 @@ base_object_constructor (GType type, g_assert (g_param_value_validate (pspec, &value1)); g_assert (g_value_get_int (&value1) == 0xFFFF); g_assert (!g_param_value_validate (pspec, &value1)); - + g_value_reset (&value1); g_value_set_int (&value1, 1); g_value_set_int (&value2, 2); g_assert (g_param_values_cmp (pspec, &value1, &value2) < 0); g_assert (g_param_values_cmp (pspec, &value2, &value1) > 0); - + g_value_unset (&value1); g_value_unset (&value2); return base_parent_class->constructor (type, - n_construct_properties, - construct_properties); + n_construct_properties, + construct_properties); } static void base_object_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec) + guint prop_id, + const GValue *value, + GParamSpec *pspec) { BaseObject *base_object = BASE_OBJECT (object); - + switch (prop_id) { case BASE_PROP1: @@ -239,10 +233,10 @@ base_object_set_property (GObject *object, } static void -base_object_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec) +base_object_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) { BaseObject *base_object = BASE_OBJECT (object); @@ -270,15 +264,15 @@ base_object_get_property (GObject *object, static void base_object_notify (GObject *object, - GParamSpec *pspec) + GParamSpec *pspec) { /* The property passed to notify is the redirect target, not the * GParamSpecOverride */ g_assert (pspec == inherited_spec1 || - pspec == inherited_spec2 || - pspec == inherited_spec3 || - pspec == inherited_spec4); + pspec == inherited_spec2 || + pspec == inherited_spec3 || + pspec == inherited_spec4); } static void @@ -300,23 +294,23 @@ base_object_class_init (BaseObjectClass *class) * can make the flags less restrictive */ inherited_spec2 = g_param_spec_int ("prop2", - "Prop2", - "Property 2", - G_MININT, /* min */ - G_MAXINT, /* max */ - 0, /* default */ - G_PARAM_READWRITE); + "Prop2", + "Property 2", + G_MININT, /* min */ + G_MAXINT, /* max */ + 0, /* default */ + G_PARAM_READWRITE); g_object_class_install_property (object_class, BASE_PROP2, inherited_spec2); g_object_class_override_property (object_class, BASE_PROP3, "prop3"); - + inherited_spec4 = g_param_spec_int ("prop4", - "Prop4", - "Property 4", - G_MININT, /* min */ - G_MAXINT, /* max */ - 0, /* default */ - G_PARAM_READWRITE); + "Prop4", + "Property 4", + G_MININT, /* min */ + G_MAXINT, /* max */ + 0, /* default */ + G_PARAM_READWRITE); g_object_class_install_property (object_class, BASE_PROP4, inherited_spec4); } @@ -327,15 +321,15 @@ base_object_init (BaseObject *base_object) } static DEFINE_TYPE_FULL (BaseObject, base_object, - base_object_class_init, NULL, base_object_init, - G_TYPE_OBJECT, - INTERFACE (NULL, TEST_TYPE_IFACE)) + base_object_class_init, NULL, base_object_init, + G_TYPE_OBJECT, + INTERFACE (NULL, TEST_TYPE_IFACE)) static void derived_object_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec) + guint prop_id, + const GValue *value, + GParamSpec *pspec) { BaseObject *base_object = BASE_OBJECT (object); @@ -356,10 +350,10 @@ derived_object_set_property (GObject *object, } static void -derived_object_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec) +derived_object_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) { BaseObject *base_object = BASE_OBJECT (object); @@ -395,15 +389,14 @@ derived_object_class_init (DerivedObjectClass *class) } static DEFINE_TYPE (DerivedObject, derived_object, - derived_object_class_init, NULL, NULL, - BASE_TYPE_OBJECT) + derived_object_class_init, NULL, NULL, + BASE_TYPE_OBJECT) -/* Helper function for testing ...list_properties() - */ +/* Helper function for testing ...list_properties() */ static void assert_in_properties (GParamSpec *param_spec, - GParamSpec **properties, - gint n_properties) + GParamSpec **properties, + gint n_properties) { gint i; gboolean found = FALSE; @@ -411,53 +404,50 @@ assert_in_properties (GParamSpec *param_spec, for (i = 0; i < n_properties; i++) { if (properties[i] == param_spec) - found = TRUE; + found = TRUE; } g_assert (found); } -int -main (gint argc, - gchar *argv[]) +/* Test setting and getting the properties */ +static void +test_set (void) { BaseObject *object; - GObjectClass *object_class; - TestIfaceClass *iface_vtable; - GParamSpec **properties; - guint n_properties; - gint val1, val2, val3, val4; - - g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) | - G_LOG_LEVEL_WARNING | - G_LOG_LEVEL_CRITICAL); - g_type_init (); object = g_object_new (DERIVED_TYPE_OBJECT, NULL); - /* Test setting and getting the properties - */ g_object_set (object, - "prop1", 0x0101, - "prop2", 0x0202, - "prop3", 0x0303, - "prop4", 0x0404, - NULL); + "prop1", 0x0101, + "prop2", 0x0202, + "prop3", 0x0303, + "prop4", 0x0404, + NULL); g_object_get (object, - "prop1", &val1, - "prop2", &val2, - "prop3", &val3, - "prop4", &val4, - NULL); + "prop1", &val1, + "prop2", &val2, + "prop3", &val3, + "prop4", &val4, + NULL); g_assert (val1 == 0x0101); g_assert (val2 == 0x0202); g_assert (val3 == 0x0303); g_assert (val4 == 0x0404); - /* Test that the right spec is passed on explicit notifications - */ + g_object_unref (object); +} + +/* Test that the right spec is passed on explicit notifications */ +static void +test_notify (void) +{ + BaseObject *object; + + object = g_object_new (DERIVED_TYPE_OBJECT, NULL); + g_object_freeze_notify (G_OBJECT (object)); g_object_notify (G_OBJECT (object), "prop1"); g_object_notify (G_OBJECT (object), "prop2"); @@ -465,17 +455,33 @@ main (gint argc, g_object_notify (G_OBJECT (object), "prop4"); g_object_thaw_notify (G_OBJECT (object)); - /* Test g_object_class_find_property() for overridden properties - */ - object_class = G_OBJECT_GET_CLASS (object); + g_object_unref (object); +} + +/* Test g_object_class_find_property() for overridden properties */ +static void +test_find_overridden (void) +{ + GObjectClass *object_class; + + object_class = g_type_class_peek (DERIVED_TYPE_OBJECT); g_assert (g_object_class_find_property (object_class, "prop1") == inherited_spec1); g_assert (g_object_class_find_property (object_class, "prop2") == inherited_spec2); g_assert (g_object_class_find_property (object_class, "prop3") == inherited_spec3); g_assert (g_object_class_find_property (object_class, "prop4") == inherited_spec4); +} + +/* Test g_object_class_list_properties() for overridden properties */ +static void +test_list_overridden (void) +{ + GObjectClass *object_class; + GParamSpec **properties; + guint n_properties; + + object_class = g_type_class_peek (DERIVED_TYPE_OBJECT); - /* Test g_object_class_list_properties() for overridden properties - */ properties = g_object_class_list_properties (object_class, &n_properties); g_assert (n_properties == 4); assert_in_properties (inherited_spec1, properties, n_properties); @@ -483,25 +489,160 @@ main (gint argc, assert_in_properties (inherited_spec3, properties, n_properties); assert_in_properties (inherited_spec4, properties, n_properties); g_free (properties); +} - /* Test g_object_interface_find_property() - */ - iface_vtable = g_type_default_interface_peek (TEST_TYPE_IFACE); +/* Test g_object_interface_find_property() */ +static void +test_find_interface (void) +{ + TestIfaceClass *iface; - g_assert (g_object_interface_find_property (iface_vtable, "prop1") == iface_spec1); - g_assert (g_object_interface_find_property (iface_vtable, "prop2") == iface_spec2); - g_assert (g_object_interface_find_property (iface_vtable, "prop3") == iface_spec3); + iface = g_type_default_interface_peek (TEST_TYPE_IFACE); - /* Test g_object_interface_list_properties() - */ - properties = g_object_interface_list_properties (iface_vtable, &n_properties); + g_assert (g_object_interface_find_property (iface, "prop1") == iface_spec1); + g_assert (g_object_interface_find_property (iface, "prop2") == iface_spec2); + g_assert (g_object_interface_find_property (iface, "prop3") == iface_spec3); +} + +/* Test g_object_interface_list_properties() */ +static void +test_list_interface (void) +{ + TestIfaceClass *iface; + GParamSpec **properties; + guint n_properties; + + iface = g_type_default_interface_peek (TEST_TYPE_IFACE); + + properties = g_object_interface_list_properties (iface, &n_properties); g_assert (n_properties == 3); assert_in_properties (iface_spec1, properties, n_properties); assert_in_properties (iface_spec2, properties, n_properties); assert_in_properties (iface_spec3, properties, n_properties); g_free (properties); +} - g_object_unref (object); +/* Base2Object, which implements the interface but fails + * to override some of its properties + */ +#define BASE2_TYPE_OBJECT (base2_object_get_type ()) +#define BASE2_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BASE2_TYPE_OBJECT, Base2Object)) + +typedef struct _Base2Object Base2Object; +typedef struct _Base2ObjectClass Base2ObjectClass; + +static void +base2_object_test_iface_init (TestIfaceClass *iface) +{ +} + +enum { + BASE2_PROP_0, + BASE2_PROP1, + BASE2_PROP2 +}; + +struct _Base2Object +{ + GObject parent_instance; +}; + +struct _Base2ObjectClass +{ + GObjectClass parent_class; +}; + +G_DEFINE_TYPE_WITH_CODE (Base2Object, base2_object, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE, + base2_object_test_iface_init)) + +static void +base2_object_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + switch (prop_id) + { + case BASE2_PROP1: + g_value_set_int (value, 0); + break; + case BASE2_PROP2: + g_value_set_int (value, 0); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +base2_object_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + switch (prop_id) + { + case BASE2_PROP1: + break; + case BASE2_PROP2: + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +base2_object_class_init (Base2ObjectClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + + object_class->set_property = base2_object_set_property; + object_class->get_property = base2_object_get_property; + + g_object_class_override_property (object_class, BASE2_PROP1, "prop1"); + g_object_class_override_property (object_class, BASE2_PROP2, "prop2"); +} + +static void +base2_object_init (Base2Object *object) +{ +} + +static void +test_not_overridden (void) +{ + g_test_bug ("637738"); + + if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT|G_TEST_TRAP_SILENCE_STDERR)) + { + Base2Object *object; + + object = g_object_new (BASE2_TYPE_OBJECT, NULL); + g_object_unref (object); + exit (0); + } + g_test_trap_assert_failed (); + g_test_trap_assert_stderr ("*Base2Object doesn't implement property 'prop3' from interface 'TestIface'*"); +} + +int +main (int argc, char *argv[]) +{ + g_type_init (); + + g_test_init (&argc, &argv, NULL); + g_test_bug_base ("http://bugzilla.gnome.org/"); + + g_test_add_func ("/interface/properties/set", test_set); + g_test_add_func ("/interface/properties/notify", test_notify); + g_test_add_func ("/interface/properties/find-overridden", test_find_overridden); + g_test_add_func ("/interface/properties/list-overridden", test_list_overridden); + g_test_add_func ("/interface/properties/find-interface", test_find_interface); + g_test_add_func ("/interface/properties/list-interface", test_list_interface); + g_test_add_func ("/interface/properties/not-overridden", test_not_overridden); - return 0; + return g_test_run (); } |