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