1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
|
/* GObject - GLib Type, Object, Parameter and Signal Library
* Copyright (C) 2001, 2003 Red Hat, Inc.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <glib-object.h>
#include "testcommon.h"
/* This test tests interface properties, implementing interface
* properties and #GParamSpecOverride.
*
* Four properties are tested:
*
* prop1: Defined in TestIface, Implemented in BaseObject with a GParamSpecOverride
* prop2: Defined in TestIface, Implemented in BaseObject with a new property
* prop3: Defined in TestIface, Implemented in BaseObject, Overridden in DerivedObject
* prop4: Defined in BaseObject, Overridden in DerivedObject
*/
static GType base_object_get_type (void);
static GType derived_object_get_type (void);
enum {
BASE_PROP_0,
BASE_PROP1,
BASE_PROP2,
BASE_PROP3,
BASE_PROP4
};
enum {
DERIVED_PROP_0,
DERIVED_PROP3,
DERIVED_PROP4
};
/*
* 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))
typedef struct _BaseObject BaseObject;
typedef struct _BaseObjectClass BaseObjectClass;
struct _BaseObject
{
GObject parent_instance;
gint val1;
gint val2;
gint val3;
gint val4;
};
struct _BaseObjectClass
{
GObjectClass parent_class;
};
GObjectClass *base_parent_class;
/*
* DerivedObject, the child class of DerivedObject
*/
#define DERIVED_TYPE_OBJECT (derived_object_get_type ())
typedef struct _DerivedObject DerivedObject;
typedef struct _DerivedObjectClass DerivedObjectClass;
struct _DerivedObject
{
BaseObject parent_instance;
};
struct _DerivedObjectClass
{
BaseObjectClass parent_class;
};
/*
* The interface
*/
typedef struct _TestIfaceClass TestIfaceClass;
struct _TestIfaceClass
{
GTypeInterface base_iface;
};
#define TEST_TYPE_IFACE (test_iface_get_type ())
/* The paramspecs installed on our interface
*/
static GParamSpec *iface_spec1, *iface_spec2, *iface_spec3;
/* The paramspecs inherited by our derived object
*/
static GParamSpec *inherited_spec1, *inherited_spec2, *inherited_spec3, *inherited_spec4;
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);
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);
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);
g_object_interface_install_property (iface_vtable, iface_spec3);
}
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)
{
/* The constructor is the one place where a GParamSpecOverride is visible
* to the outside world, so we do a bunch of checks here
*/
GValue value1 = G_VALUE_INIT;
GValue value2 = G_VALUE_INIT;
GParamSpec *pspec;
g_assert (n_construct_properties == 1);
pspec = construct_properties->pspec;
/* Check we got the param spec we expected
*/
g_assert (G_IS_PARAM_SPEC_OVERRIDE (pspec));
g_assert (pspec->param_id == BASE_PROP1);
g_assert (strcmp (g_param_spec_get_name (pspec), "prop1") == 0);
g_assert (g_param_spec_get_redirect_target (pspec) == iface_spec1);
/* Test redirection of the nick and blurb to the redirect target
*/
g_assert (strcmp (g_param_spec_get_nick (pspec), "Prop1") == 0);
g_assert (strcmp (g_param_spec_get_blurb (pspec), "Property 1") == 0);
/* Test forwarding of the various GParamSpec methods to the redirect target
*/
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);
g_value_reset (&value1);
g_value_set_int (&value1, 0x10000);
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);
}
static void
base_object_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
BaseObject *base_object = BASE_OBJECT (object);
switch (prop_id)
{
case BASE_PROP1:
g_assert (pspec == inherited_spec1);
base_object->val1 = g_value_get_int (value);
break;
case BASE_PROP2:
g_assert (pspec == inherited_spec2);
base_object->val2 = g_value_get_int (value);
break;
case BASE_PROP3:
g_assert_not_reached ();
break;
case BASE_PROP4:
g_assert_not_reached ();
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
base_object_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
BaseObject *base_object = BASE_OBJECT (object);
switch (prop_id)
{
case BASE_PROP1:
g_assert (pspec == inherited_spec1);
g_value_set_int (value, base_object->val1);
break;
case BASE_PROP2:
g_assert (pspec == inherited_spec2);
g_value_set_int (value, base_object->val2);
break;
case BASE_PROP3:
g_assert_not_reached ();
break;
case BASE_PROP4:
g_assert_not_reached ();
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
base_object_notify (GObject *object,
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);
}
static void
base_object_class_init (BaseObjectClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
base_parent_class= g_type_class_peek_parent (class);
object_class->constructor = base_object_constructor;
object_class->set_property = base_object_set_property;
object_class->get_property = base_object_get_property;
object_class->notify = base_object_notify;
g_object_class_override_property (object_class, BASE_PROP1, "prop1");
/* We override this one using a real property, not GParamSpecOverride
* We change the flags from READONLY to READWRITE to show that we
* 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);
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);
g_object_class_install_property (object_class, BASE_PROP4, inherited_spec4);
}
static void
base_object_init (BaseObject *base_object)
{
base_object->val1 = 42;
}
static DEFINE_TYPE_FULL (BaseObject, base_object,
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)
{
BaseObject *base_object = BASE_OBJECT (object);
switch (prop_id)
{
case DERIVED_PROP3:
g_assert (pspec == inherited_spec3);
base_object->val3 = g_value_get_int (value);
break;
case DERIVED_PROP4:
g_assert (pspec == inherited_spec4);
base_object->val4 = g_value_get_int (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
derived_object_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
BaseObject *base_object = BASE_OBJECT (object);
switch (prop_id)
{
case DERIVED_PROP3:
g_assert (pspec == inherited_spec3);
g_value_set_int (value, base_object->val3);
break;
case DERIVED_PROP4:
g_assert (pspec == inherited_spec4);
g_value_set_int (value, base_object->val4);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
derived_object_class_init (DerivedObjectClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->set_property = derived_object_set_property;
object_class->get_property = derived_object_get_property;
/* Overriding a property that is itself overriding an interface property */
g_object_class_override_property (object_class, DERIVED_PROP3, "prop3");
/* Overriding a property not from an interface */
g_object_class_override_property (object_class, DERIVED_PROP4, "prop4");
}
static DEFINE_TYPE (DerivedObject, derived_object,
derived_object_class_init, NULL, NULL,
BASE_TYPE_OBJECT)
/* Helper function for testing ...list_properties() */
static void
assert_in_properties (GParamSpec *param_spec,
GParamSpec **properties,
gint n_properties)
{
gint i;
gboolean found = FALSE;
for (i = 0; i < n_properties; i++)
{
if (properties[i] == param_spec)
found = TRUE;
}
g_assert (found);
}
/* Test setting and getting the properties */
static void
test_set (void)
{
BaseObject *object;
gint val1, val2, val3, val4;
object = g_object_new (DERIVED_TYPE_OBJECT, NULL);
g_object_set (object,
"prop1", 0x0101,
"prop2", 0x0202,
"prop3", 0x0303,
"prop4", 0x0404,
NULL);
g_object_get (object,
"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);
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");
g_object_notify (G_OBJECT (object), "prop3");
g_object_notify (G_OBJECT (object), "prop4");
g_object_thaw_notify (G_OBJECT (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);
properties = g_object_class_list_properties (object_class, &n_properties);
g_assert (n_properties == 4);
assert_in_properties (inherited_spec1, properties, n_properties);
assert_in_properties (inherited_spec2, properties, n_properties);
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() */
static void
test_find_interface (void)
{
TestIfaceClass *iface;
iface = g_type_default_interface_peek (TEST_TYPE_IFACE);
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);
}
/* 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;
};
static GType base2_object_get_type (void);
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)
{
Base2Object *object;
if (!g_test_undefined ())
return;
g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=637738");
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"*Base2Object doesn't implement property 'prop3' from interface 'TestIface'*");
object = g_object_new (BASE2_TYPE_OBJECT, NULL);
g_test_assert_expected_messages ();
g_object_unref (object);
}
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
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 g_test_run ();
}
|