1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tests/gobject/ifaceproperties.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,518 @@
1.4 +/* GObject - GLib Type, Object, Parameter and Signal Library
1.5 + * Copyright (C) 2001, 2003 Red Hat, Inc.
1.6 + * Portions copyright (c) 2006-2009 Nokia Corporation. All rights reserved.
1.7 + * This library is free software; you can redistribute it and/or
1.8 + * modify it under the terms of the GNU Lesser General Public
1.9 + * License as published by the Free Software Foundation; either
1.10 + * version 2 of the License, or (at your option) any later version.
1.11 + *
1.12 + * This library is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.15 + * Lesser General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU Lesser General
1.18 + * Public License along with this library; if not, write to the
1.19 + * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
1.20 + * Boston, MA 02111-1307, USA.
1.21 + */
1.22 +
1.23 +#undef G_LOG_DOMAIN
1.24 +#define G_LOG_DOMAIN "TestIfaceProperties"
1.25 +
1.26 +#undef G_DISABLE_ASSERT
1.27 +#undef G_DISABLE_CHECKS
1.28 +#undef G_DISABLE_CAST_CHECKS
1.29 +
1.30 +#include <string.h>
1.31 +
1.32 +#include <glib-object.h>
1.33 +
1.34 +#include "testcommon.h"
1.35 +#ifdef __SYMBIAN32__
1.36 +#include <gobject_global.h>
1.37 +#include "mrt2_glib2_test.h"
1.38 +#endif /*__SYMBIAN32__*/
1.39 +
1.40 +/* This test tests interface properties, implementing interface
1.41 + * properties and #GParamSpecOverride.
1.42 + *
1.43 + * Four properties are tested:
1.44 + *
1.45 + * prop1: Defined in TestIface, Implemented in BaseObject with a GParamSpecOverride
1.46 + * prop2: Defined in TestIface, Implemented in BaseObject with a new property
1.47 + * prop3: Defined in TestIface, Implemented in BaseObject, Overridden in DerivedObject
1.48 + * prop4: Defined in BaseObject, Overridden in DerivedObject
1.49 + */
1.50 +
1.51 +static GType base_object_get_type (void);
1.52 +static GType derived_object_get_type (void);
1.53 +
1.54 +enum {
1.55 + BASE_PROP_0,
1.56 + BASE_PROP1,
1.57 + BASE_PROP2,
1.58 + BASE_PROP3,
1.59 + BASE_PROP4
1.60 +};
1.61 +
1.62 +enum {
1.63 + DERIVED_PROP_0,
1.64 + DERIVED_PROP3,
1.65 + DERIVED_PROP4
1.66 +};
1.67 +
1.68 +/*
1.69 + * BaseObject, a parent class for DerivedObject
1.70 + */
1.71 +#define BASE_TYPE_OBJECT (base_object_get_type ())
1.72 +#define BASE_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BASE_TYPE_OBJECT, BaseObject))
1.73 +typedef struct _BaseObject BaseObject;
1.74 +typedef struct _BaseObjectClass BaseObjectClass;
1.75 +
1.76 +struct _BaseObject
1.77 +{
1.78 + GObject parent_instance;
1.79 +
1.80 + gint val1;
1.81 + gint val2;
1.82 + gint val3;
1.83 + gint val4;
1.84 +};
1.85 +struct _BaseObjectClass
1.86 +{
1.87 + GObjectClass parent_class;
1.88 +};
1.89 +
1.90 +GObjectClass *base_parent_class;
1.91 +
1.92 +/*
1.93 + * DerivedObject, the child class of DerivedObject
1.94 + */
1.95 +#define DERIVED_TYPE_OBJECT (derived_object_get_type ())
1.96 +typedef struct _DerivedObject DerivedObject;
1.97 +typedef struct _DerivedObjectClass DerivedObjectClass;
1.98 +
1.99 +struct _DerivedObject
1.100 +{
1.101 + BaseObject parent_instance;
1.102 +};
1.103 +struct _DerivedObjectClass
1.104 +{
1.105 + BaseObjectClass parent_class;
1.106 +};
1.107 +
1.108 +/*
1.109 + * The interface
1.110 + */
1.111 +typedef struct _TestIfaceClass TestIfaceClass;
1.112 +
1.113 +struct _TestIfaceClass
1.114 +{
1.115 + GTypeInterface base_iface;
1.116 +};
1.117 +
1.118 +#define TEST_TYPE_IFACE (test_iface_get_type ())
1.119 +
1.120 +/* The paramspecs installed on our interface
1.121 + */
1.122 +static GParamSpec *iface_spec1, *iface_spec2, *iface_spec3;
1.123 +
1.124 +/* The paramspecs inherited by our derived object
1.125 + */
1.126 +static GParamSpec *inherited_spec1, *inherited_spec2, *inherited_spec3, *inherited_spec4;
1.127 +
1.128 +static void
1.129 +test_iface_default_init (TestIfaceClass *iface_vtable)
1.130 +{
1.131 + inherited_spec1 = iface_spec1 = g_param_spec_int ("prop1",
1.132 + "Prop1",
1.133 + "Property 1",
1.134 + G_MININT, /* min */
1.135 + 0xFFFF, /* max */
1.136 + 42, /* default */
1.137 + G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
1.138 + g_object_interface_install_property (iface_vtable, iface_spec1);
1.139 +
1.140 + iface_spec2 = g_param_spec_int ("prop2",
1.141 + "Prop2",
1.142 + "Property 2",
1.143 + G_MININT, /* min */
1.144 + G_MAXINT, /* max */
1.145 + 0, /* default */
1.146 + G_PARAM_WRITABLE);
1.147 + g_object_interface_install_property (iface_vtable, iface_spec2);
1.148 +
1.149 + inherited_spec3 = iface_spec3 = g_param_spec_int ("prop3",
1.150 + "Prop3",
1.151 + "Property 3",
1.152 + G_MININT, /* min */
1.153 + G_MAXINT, /* max */
1.154 + 0, /* default */
1.155 + G_PARAM_READWRITE);
1.156 + g_object_interface_install_property (iface_vtable, iface_spec3);
1.157 +}
1.158 +
1.159 +static DEFINE_IFACE (TestIface, test_iface, NULL, test_iface_default_init)
1.160 +
1.161 +
1.162 +static GObject*
1.163 +base_object_constructor (GType type,
1.164 + guint n_construct_properties,
1.165 + GObjectConstructParam *construct_properties)
1.166 +{
1.167 + /* The constructor is the one place where a GParamSpecOverride is visible
1.168 + * to the outside world, so we do a bunch of checks here
1.169 + */
1.170 + GValue value1 = { 0, };
1.171 + GValue value2 = { 0, };
1.172 + GParamSpec *pspec;
1.173 +
1.174 + g_assert (n_construct_properties == 1);
1.175 +
1.176 + pspec = construct_properties->pspec;
1.177 +
1.178 + /* Check we got the param spec we expected
1.179 + */
1.180 + g_assert (G_IS_PARAM_SPEC_OVERRIDE (pspec));
1.181 + g_assert (pspec->param_id == BASE_PROP1);
1.182 + g_assert (strcmp (g_param_spec_get_name (pspec), "prop1") == 0);
1.183 + g_assert (g_param_spec_get_redirect_target (pspec) == iface_spec1);
1.184 +
1.185 + /* Test redirection of the nick and blurb to the redirect target
1.186 + */
1.187 + g_assert (strcmp (g_param_spec_get_nick (pspec), "Prop1") == 0);
1.188 + g_assert (strcmp (g_param_spec_get_blurb (pspec), "Property 1") == 0);
1.189 +
1.190 + /* Test forwarding of the various GParamSpec methods to the redirect target
1.191 + */
1.192 + g_value_init (&value1, G_TYPE_INT);
1.193 + g_value_init (&value2, G_TYPE_INT);
1.194 +
1.195 + g_param_value_set_default (pspec, &value1);
1.196 + g_assert (g_value_get_int (&value1) == 42);
1.197 +
1.198 + g_value_reset (&value1);
1.199 + g_value_set_int (&value1, 0x10000);
1.200 + g_assert (g_param_value_validate (pspec, &value1));
1.201 + g_assert (g_value_get_int (&value1) == 0xFFFF);
1.202 + g_assert (!g_param_value_validate (pspec, &value1));
1.203 +
1.204 + g_value_reset (&value1);
1.205 + g_value_set_int (&value1, 1);
1.206 + g_value_set_int (&value2, 2);
1.207 + g_assert (g_param_values_cmp (pspec, &value1, &value2) < 0);
1.208 + g_assert (g_param_values_cmp (pspec, &value2, &value1) > 0);
1.209 +
1.210 + g_value_unset (&value1);
1.211 + g_value_unset (&value2);
1.212 +
1.213 + return base_parent_class->constructor (type,
1.214 + n_construct_properties,
1.215 + construct_properties);
1.216 +}
1.217 +
1.218 +static void
1.219 +base_object_set_property (GObject *object,
1.220 + guint prop_id,
1.221 + const GValue *value,
1.222 + GParamSpec *pspec)
1.223 +{
1.224 + BaseObject *base_object = BASE_OBJECT (object);
1.225 +
1.226 + switch (prop_id)
1.227 + {
1.228 + case BASE_PROP1:
1.229 + g_assert (pspec == inherited_spec1);
1.230 + base_object->val1 = g_value_get_int (value);
1.231 + break;
1.232 + case BASE_PROP2:
1.233 + g_assert (pspec == inherited_spec2);
1.234 + base_object->val2 = g_value_get_int (value);
1.235 + break;
1.236 + case BASE_PROP3:
1.237 + g_assert_not_reached ();
1.238 + break;
1.239 + case BASE_PROP4:
1.240 + g_assert_not_reached ();
1.241 + break;
1.242 + default:
1.243 + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1.244 + break;
1.245 + }
1.246 +}
1.247 +
1.248 +static void
1.249 +base_object_get_property (GObject *object,
1.250 + guint prop_id,
1.251 + GValue *value,
1.252 + GParamSpec *pspec)
1.253 +{
1.254 + BaseObject *base_object = BASE_OBJECT (object);
1.255 +
1.256 + switch (prop_id)
1.257 + {
1.258 + case BASE_PROP1:
1.259 + g_assert (pspec == inherited_spec1);
1.260 + g_value_set_int (value, base_object->val1);
1.261 + break;
1.262 + case BASE_PROP2:
1.263 + g_assert (pspec == inherited_spec2);
1.264 + g_value_set_int (value, base_object->val2);
1.265 + break;
1.266 + case BASE_PROP3:
1.267 + g_assert_not_reached ();
1.268 + break;
1.269 + case BASE_PROP4:
1.270 + g_assert_not_reached ();
1.271 + break;
1.272 + default:
1.273 + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1.274 + break;
1.275 + }
1.276 +}
1.277 +
1.278 +static void
1.279 +base_object_notify (GObject *object,
1.280 + GParamSpec *pspec)
1.281 +{
1.282 + /* The property passed to notify is the redirect target, not the
1.283 + * GParamSpecOverride
1.284 + */
1.285 + g_assert (pspec == inherited_spec1 ||
1.286 + pspec == inherited_spec2 ||
1.287 + pspec == inherited_spec3 ||
1.288 + pspec == inherited_spec4);
1.289 +}
1.290 +
1.291 +static void
1.292 +base_object_class_init (BaseObjectClass *class)
1.293 +{
1.294 + GObjectClass *object_class = G_OBJECT_CLASS (class);
1.295 +
1.296 + base_parent_class= g_type_class_peek_parent (class);
1.297 +
1.298 + object_class->constructor = base_object_constructor;
1.299 + object_class->set_property = base_object_set_property;
1.300 + object_class->get_property = base_object_get_property;
1.301 + object_class->notify = base_object_notify;
1.302 +
1.303 + g_object_class_override_property (object_class, BASE_PROP1, "prop1");
1.304 +
1.305 + /* We override this one using a real property, not GParamSpecOverride
1.306 + * We change the flags from READONLY to READWRITE to show that we
1.307 + * can make the flags less restrictive
1.308 + */
1.309 + inherited_spec2 = g_param_spec_int ("prop2",
1.310 + "Prop2",
1.311 + "Property 2",
1.312 + G_MININT, /* min */
1.313 + G_MAXINT, /* max */
1.314 + 0, /* default */
1.315 + G_PARAM_READWRITE);
1.316 + g_object_class_install_property (object_class, BASE_PROP2, inherited_spec2);
1.317 +
1.318 + g_object_class_override_property (object_class, BASE_PROP3, "prop3");
1.319 +
1.320 + inherited_spec4 = g_param_spec_int ("prop4",
1.321 + "Prop4",
1.322 + "Property 4",
1.323 + G_MININT, /* min */
1.324 + G_MAXINT, /* max */
1.325 + 0, /* default */
1.326 + G_PARAM_READWRITE);
1.327 + g_object_class_install_property (object_class, BASE_PROP4, inherited_spec4);
1.328 +}
1.329 +
1.330 +static void
1.331 +base_object_init (BaseObject *base_object)
1.332 +{
1.333 + base_object->val1 = 42;
1.334 +}
1.335 +
1.336 +static DEFINE_TYPE_FULL (BaseObject, base_object,
1.337 + base_object_class_init, NULL, base_object_init,
1.338 + G_TYPE_OBJECT,
1.339 + INTERFACE (NULL, TEST_TYPE_IFACE))
1.340 +
1.341 +static void
1.342 +derived_object_set_property (GObject *object,
1.343 + guint prop_id,
1.344 + const GValue *value,
1.345 + GParamSpec *pspec)
1.346 +{
1.347 + BaseObject *base_object = BASE_OBJECT (object);
1.348 +
1.349 + switch (prop_id)
1.350 + {
1.351 + case DERIVED_PROP3:
1.352 + g_assert (pspec == inherited_spec3);
1.353 + base_object->val3 = g_value_get_int (value);
1.354 + break;
1.355 + case DERIVED_PROP4:
1.356 + g_assert (pspec == inherited_spec4);
1.357 + base_object->val4 = g_value_get_int (value);
1.358 + break;
1.359 + default:
1.360 + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1.361 + break;
1.362 + }
1.363 +}
1.364 +
1.365 +static void
1.366 +derived_object_get_property (GObject *object,
1.367 + guint prop_id,
1.368 + GValue *value,
1.369 + GParamSpec *pspec)
1.370 +{
1.371 + BaseObject *base_object = BASE_OBJECT (object);
1.372 +
1.373 + switch (prop_id)
1.374 + {
1.375 + case DERIVED_PROP3:
1.376 + g_assert (pspec == inherited_spec3);
1.377 + g_value_set_int (value, base_object->val3);
1.378 + break;
1.379 + case DERIVED_PROP4:
1.380 + g_assert (pspec == inherited_spec4);
1.381 + g_value_set_int (value, base_object->val4);
1.382 + break;
1.383 + default:
1.384 + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1.385 + break;
1.386 + }
1.387 +}
1.388 +
1.389 +static void
1.390 +derived_object_class_init (DerivedObjectClass *class)
1.391 +{
1.392 + GObjectClass *object_class = G_OBJECT_CLASS (class);
1.393 +
1.394 + object_class->set_property = derived_object_set_property;
1.395 + object_class->get_property = derived_object_get_property;
1.396 +
1.397 + /* Overriding a property that is itself overridding an interface property */
1.398 + g_object_class_override_property (object_class, DERIVED_PROP3, "prop3");
1.399 +
1.400 + /* Overriding a property not from an interface */
1.401 + g_object_class_override_property (object_class, DERIVED_PROP4, "prop4");
1.402 +}
1.403 +
1.404 +static DEFINE_TYPE (DerivedObject, derived_object,
1.405 + derived_object_class_init, NULL, NULL,
1.406 + BASE_TYPE_OBJECT)
1.407 +
1.408 +/* Helper function for testing ...list_properties()
1.409 + */
1.410 +static void
1.411 +assert_in_properties (GParamSpec *param_spec,
1.412 + GParamSpec **properties,
1.413 + gint n_properties)
1.414 +{
1.415 + gint i;
1.416 + gboolean found = FALSE;
1.417 +
1.418 + for (i = 0; i < n_properties; i++)
1.419 + {
1.420 + if (properties[i] == param_spec)
1.421 + found = TRUE;
1.422 + }
1.423 +
1.424 + g_assert (found);
1.425 +}
1.426 +
1.427 +int
1.428 +main (gint argc,
1.429 + gchar *argv[])
1.430 +{
1.431 + BaseObject *object;
1.432 + GObjectClass *object_class;
1.433 + TestIfaceClass *iface_vtable;
1.434 + GParamSpec **properties;
1.435 + gint n_properties;
1.436 +
1.437 + gint val1, val2, val3, val4;
1.438 + #ifdef __SYMBIAN32__
1.439 + g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
1.440 + g_set_print_handler(mrtPrintHandler);
1.441 + #endif /*__SYMBIAN32__*/
1.442 +
1.443 + g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
1.444 + G_LOG_LEVEL_WARNING |
1.445 + G_LOG_LEVEL_CRITICAL);
1.446 + g_type_init ();
1.447 +
1.448 + object = g_object_new (DERIVED_TYPE_OBJECT, NULL);
1.449 +
1.450 + /* Test setting and getting the properties
1.451 + */
1.452 + g_object_set (object,
1.453 + "prop1", 0x0101,
1.454 + "prop2", 0x0202,
1.455 + "prop3", 0x0303,
1.456 + "prop4", 0x0404,
1.457 + NULL);
1.458 + g_object_get (object,
1.459 + "prop1", &val1,
1.460 + "prop2", &val2,
1.461 + "prop3", &val3,
1.462 + "prop4", &val4,
1.463 + NULL);
1.464 +
1.465 + g_assert (val1 == 0x0101);
1.466 + g_assert (val2 == 0x0202);
1.467 + g_assert (val3 == 0x0303);
1.468 + g_assert (val4 == 0x0404);
1.469 +
1.470 + /* Test that the right spec is passed on explicit notifications
1.471 + */
1.472 + g_object_freeze_notify (G_OBJECT (object));
1.473 + g_object_notify (G_OBJECT (object), "prop1");
1.474 + g_object_notify (G_OBJECT (object), "prop2");
1.475 + g_object_notify (G_OBJECT (object), "prop3");
1.476 + g_object_notify (G_OBJECT (object), "prop4");
1.477 + g_object_thaw_notify (G_OBJECT (object));
1.478 +
1.479 + /* Test g_object_class_find_property() for overridden properties
1.480 + */
1.481 + object_class = G_OBJECT_GET_CLASS (object);
1.482 +
1.483 + g_assert (g_object_class_find_property (object_class, "prop1") == inherited_spec1);
1.484 + g_assert (g_object_class_find_property (object_class, "prop2") == inherited_spec2);
1.485 + g_assert (g_object_class_find_property (object_class, "prop3") == inherited_spec3);
1.486 + g_assert (g_object_class_find_property (object_class, "prop4") == inherited_spec4);
1.487 +
1.488 + /* Test g_object_class_list_properties() for overridden properties
1.489 + */
1.490 + properties = g_object_class_list_properties (object_class,(guint *) &n_properties);
1.491 + g_assert (n_properties == 4);
1.492 + assert_in_properties (inherited_spec1, properties, n_properties);
1.493 + assert_in_properties (inherited_spec2, properties, n_properties);
1.494 + assert_in_properties (inherited_spec3, properties, n_properties);
1.495 + assert_in_properties (inherited_spec4, properties, n_properties);
1.496 + g_free (properties);
1.497 +
1.498 + /* Test g_object_interface_find_property()
1.499 + */
1.500 + iface_vtable = g_type_default_interface_peek (TEST_TYPE_IFACE);
1.501 +
1.502 + g_assert (g_object_interface_find_property (iface_vtable, "prop1") == iface_spec1);
1.503 + g_assert (g_object_interface_find_property (iface_vtable, "prop2") == iface_spec2);
1.504 + g_assert (g_object_interface_find_property (iface_vtable, "prop3") == iface_spec3);
1.505 +
1.506 + /* Test g_object_interface_list_properties()
1.507 + */
1.508 + properties = g_object_interface_list_properties (iface_vtable, (guint *)&n_properties);
1.509 + g_assert (n_properties == 3);
1.510 + assert_in_properties (iface_spec1, properties, n_properties);
1.511 + assert_in_properties (iface_spec2, properties, n_properties);
1.512 + assert_in_properties (iface_spec3, properties, n_properties);
1.513 + g_free (properties);
1.514 +
1.515 + g_object_unref (object);
1.516 + #ifdef __SYMBIAN32__
1.517 + testResultXml("ifaceproperties");
1.518 + #endif /*__SYMBIAN32__*/
1.519 +
1.520 + return 0;
1.521 +}