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