|
1 /* GObject - GLib Type, Object, Parameter and Signal Library |
|
2 * Copyright (C) 2001, 2003 Red Hat, Inc. |
|
3 * Portions copyright (c) 2006-2009 Nokia Corporation. All rights reserved. |
|
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 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, write to the |
|
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
|
17 * Boston, MA 02111-1307, USA. |
|
18 */ |
|
19 |
|
20 #undef G_LOG_DOMAIN |
|
21 #define G_LOG_DOMAIN "TestIfaceProperties" |
|
22 |
|
23 #undef G_DISABLE_ASSERT |
|
24 #undef G_DISABLE_CHECKS |
|
25 #undef G_DISABLE_CAST_CHECKS |
|
26 |
|
27 #include <string.h> |
|
28 |
|
29 #include <glib-object.h> |
|
30 |
|
31 #include "testcommon.h" |
|
32 #ifdef __SYMBIAN32__ |
|
33 #include <gobject_global.h> |
|
34 #include "mrt2_glib2_test.h" |
|
35 #endif /*__SYMBIAN32__*/ |
|
36 |
|
37 /* This test tests interface properties, implementing interface |
|
38 * properties and #GParamSpecOverride. |
|
39 * |
|
40 * Four properties are tested: |
|
41 * |
|
42 * prop1: Defined in TestIface, Implemented in BaseObject with a GParamSpecOverride |
|
43 * prop2: Defined in TestIface, Implemented in BaseObject with a new property |
|
44 * prop3: Defined in TestIface, Implemented in BaseObject, Overridden in DerivedObject |
|
45 * prop4: Defined in BaseObject, Overridden in DerivedObject |
|
46 */ |
|
47 |
|
48 static GType base_object_get_type (void); |
|
49 static GType derived_object_get_type (void); |
|
50 |
|
51 enum { |
|
52 BASE_PROP_0, |
|
53 BASE_PROP1, |
|
54 BASE_PROP2, |
|
55 BASE_PROP3, |
|
56 BASE_PROP4 |
|
57 }; |
|
58 |
|
59 enum { |
|
60 DERIVED_PROP_0, |
|
61 DERIVED_PROP3, |
|
62 DERIVED_PROP4 |
|
63 }; |
|
64 |
|
65 /* |
|
66 * BaseObject, a parent class for DerivedObject |
|
67 */ |
|
68 #define BASE_TYPE_OBJECT (base_object_get_type ()) |
|
69 #define BASE_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BASE_TYPE_OBJECT, BaseObject)) |
|
70 typedef struct _BaseObject BaseObject; |
|
71 typedef struct _BaseObjectClass BaseObjectClass; |
|
72 |
|
73 struct _BaseObject |
|
74 { |
|
75 GObject parent_instance; |
|
76 |
|
77 gint val1; |
|
78 gint val2; |
|
79 gint val3; |
|
80 gint val4; |
|
81 }; |
|
82 struct _BaseObjectClass |
|
83 { |
|
84 GObjectClass parent_class; |
|
85 }; |
|
86 |
|
87 GObjectClass *base_parent_class; |
|
88 |
|
89 /* |
|
90 * DerivedObject, the child class of DerivedObject |
|
91 */ |
|
92 #define DERIVED_TYPE_OBJECT (derived_object_get_type ()) |
|
93 typedef struct _DerivedObject DerivedObject; |
|
94 typedef struct _DerivedObjectClass DerivedObjectClass; |
|
95 |
|
96 struct _DerivedObject |
|
97 { |
|
98 BaseObject parent_instance; |
|
99 }; |
|
100 struct _DerivedObjectClass |
|
101 { |
|
102 BaseObjectClass parent_class; |
|
103 }; |
|
104 |
|
105 /* |
|
106 * The interface |
|
107 */ |
|
108 typedef struct _TestIfaceClass TestIfaceClass; |
|
109 |
|
110 struct _TestIfaceClass |
|
111 { |
|
112 GTypeInterface base_iface; |
|
113 }; |
|
114 |
|
115 #define TEST_TYPE_IFACE (test_iface_get_type ()) |
|
116 |
|
117 /* The paramspecs installed on our interface |
|
118 */ |
|
119 static GParamSpec *iface_spec1, *iface_spec2, *iface_spec3; |
|
120 |
|
121 /* The paramspecs inherited by our derived object |
|
122 */ |
|
123 static GParamSpec *inherited_spec1, *inherited_spec2, *inherited_spec3, *inherited_spec4; |
|
124 |
|
125 static void |
|
126 test_iface_default_init (TestIfaceClass *iface_vtable) |
|
127 { |
|
128 inherited_spec1 = iface_spec1 = g_param_spec_int ("prop1", |
|
129 "Prop1", |
|
130 "Property 1", |
|
131 G_MININT, /* min */ |
|
132 0xFFFF, /* max */ |
|
133 42, /* default */ |
|
134 G_PARAM_READWRITE | G_PARAM_CONSTRUCT); |
|
135 g_object_interface_install_property (iface_vtable, iface_spec1); |
|
136 |
|
137 iface_spec2 = g_param_spec_int ("prop2", |
|
138 "Prop2", |
|
139 "Property 2", |
|
140 G_MININT, /* min */ |
|
141 G_MAXINT, /* max */ |
|
142 0, /* default */ |
|
143 G_PARAM_WRITABLE); |
|
144 g_object_interface_install_property (iface_vtable, iface_spec2); |
|
145 |
|
146 inherited_spec3 = iface_spec3 = g_param_spec_int ("prop3", |
|
147 "Prop3", |
|
148 "Property 3", |
|
149 G_MININT, /* min */ |
|
150 G_MAXINT, /* max */ |
|
151 0, /* default */ |
|
152 G_PARAM_READWRITE); |
|
153 g_object_interface_install_property (iface_vtable, iface_spec3); |
|
154 } |
|
155 |
|
156 static DEFINE_IFACE (TestIface, test_iface, NULL, test_iface_default_init) |
|
157 |
|
158 |
|
159 static GObject* |
|
160 base_object_constructor (GType type, |
|
161 guint n_construct_properties, |
|
162 GObjectConstructParam *construct_properties) |
|
163 { |
|
164 /* The constructor is the one place where a GParamSpecOverride is visible |
|
165 * to the outside world, so we do a bunch of checks here |
|
166 */ |
|
167 GValue value1 = { 0, }; |
|
168 GValue value2 = { 0, }; |
|
169 GParamSpec *pspec; |
|
170 |
|
171 g_assert (n_construct_properties == 1); |
|
172 |
|
173 pspec = construct_properties->pspec; |
|
174 |
|
175 /* Check we got the param spec we expected |
|
176 */ |
|
177 g_assert (G_IS_PARAM_SPEC_OVERRIDE (pspec)); |
|
178 g_assert (pspec->param_id == BASE_PROP1); |
|
179 g_assert (strcmp (g_param_spec_get_name (pspec), "prop1") == 0); |
|
180 g_assert (g_param_spec_get_redirect_target (pspec) == iface_spec1); |
|
181 |
|
182 /* Test redirection of the nick and blurb to the redirect target |
|
183 */ |
|
184 g_assert (strcmp (g_param_spec_get_nick (pspec), "Prop1") == 0); |
|
185 g_assert (strcmp (g_param_spec_get_blurb (pspec), "Property 1") == 0); |
|
186 |
|
187 /* Test forwarding of the various GParamSpec methods to the redirect target |
|
188 */ |
|
189 g_value_init (&value1, G_TYPE_INT); |
|
190 g_value_init (&value2, G_TYPE_INT); |
|
191 |
|
192 g_param_value_set_default (pspec, &value1); |
|
193 g_assert (g_value_get_int (&value1) == 42); |
|
194 |
|
195 g_value_reset (&value1); |
|
196 g_value_set_int (&value1, 0x10000); |
|
197 g_assert (g_param_value_validate (pspec, &value1)); |
|
198 g_assert (g_value_get_int (&value1) == 0xFFFF); |
|
199 g_assert (!g_param_value_validate (pspec, &value1)); |
|
200 |
|
201 g_value_reset (&value1); |
|
202 g_value_set_int (&value1, 1); |
|
203 g_value_set_int (&value2, 2); |
|
204 g_assert (g_param_values_cmp (pspec, &value1, &value2) < 0); |
|
205 g_assert (g_param_values_cmp (pspec, &value2, &value1) > 0); |
|
206 |
|
207 g_value_unset (&value1); |
|
208 g_value_unset (&value2); |
|
209 |
|
210 return base_parent_class->constructor (type, |
|
211 n_construct_properties, |
|
212 construct_properties); |
|
213 } |
|
214 |
|
215 static void |
|
216 base_object_set_property (GObject *object, |
|
217 guint prop_id, |
|
218 const GValue *value, |
|
219 GParamSpec *pspec) |
|
220 { |
|
221 BaseObject *base_object = BASE_OBJECT (object); |
|
222 |
|
223 switch (prop_id) |
|
224 { |
|
225 case BASE_PROP1: |
|
226 g_assert (pspec == inherited_spec1); |
|
227 base_object->val1 = g_value_get_int (value); |
|
228 break; |
|
229 case BASE_PROP2: |
|
230 g_assert (pspec == inherited_spec2); |
|
231 base_object->val2 = g_value_get_int (value); |
|
232 break; |
|
233 case BASE_PROP3: |
|
234 g_assert_not_reached (); |
|
235 break; |
|
236 case BASE_PROP4: |
|
237 g_assert_not_reached (); |
|
238 break; |
|
239 default: |
|
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
|
241 break; |
|
242 } |
|
243 } |
|
244 |
|
245 static void |
|
246 base_object_get_property (GObject *object, |
|
247 guint prop_id, |
|
248 GValue *value, |
|
249 GParamSpec *pspec) |
|
250 { |
|
251 BaseObject *base_object = BASE_OBJECT (object); |
|
252 |
|
253 switch (prop_id) |
|
254 { |
|
255 case BASE_PROP1: |
|
256 g_assert (pspec == inherited_spec1); |
|
257 g_value_set_int (value, base_object->val1); |
|
258 break; |
|
259 case BASE_PROP2: |
|
260 g_assert (pspec == inherited_spec2); |
|
261 g_value_set_int (value, base_object->val2); |
|
262 break; |
|
263 case BASE_PROP3: |
|
264 g_assert_not_reached (); |
|
265 break; |
|
266 case BASE_PROP4: |
|
267 g_assert_not_reached (); |
|
268 break; |
|
269 default: |
|
270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
|
271 break; |
|
272 } |
|
273 } |
|
274 |
|
275 static void |
|
276 base_object_notify (GObject *object, |
|
277 GParamSpec *pspec) |
|
278 { |
|
279 /* The property passed to notify is the redirect target, not the |
|
280 * GParamSpecOverride |
|
281 */ |
|
282 g_assert (pspec == inherited_spec1 || |
|
283 pspec == inherited_spec2 || |
|
284 pspec == inherited_spec3 || |
|
285 pspec == inherited_spec4); |
|
286 } |
|
287 |
|
288 static void |
|
289 base_object_class_init (BaseObjectClass *class) |
|
290 { |
|
291 GObjectClass *object_class = G_OBJECT_CLASS (class); |
|
292 |
|
293 base_parent_class= g_type_class_peek_parent (class); |
|
294 |
|
295 object_class->constructor = base_object_constructor; |
|
296 object_class->set_property = base_object_set_property; |
|
297 object_class->get_property = base_object_get_property; |
|
298 object_class->notify = base_object_notify; |
|
299 |
|
300 g_object_class_override_property (object_class, BASE_PROP1, "prop1"); |
|
301 |
|
302 /* We override this one using a real property, not GParamSpecOverride |
|
303 * We change the flags from READONLY to READWRITE to show that we |
|
304 * can make the flags less restrictive |
|
305 */ |
|
306 inherited_spec2 = g_param_spec_int ("prop2", |
|
307 "Prop2", |
|
308 "Property 2", |
|
309 G_MININT, /* min */ |
|
310 G_MAXINT, /* max */ |
|
311 0, /* default */ |
|
312 G_PARAM_READWRITE); |
|
313 g_object_class_install_property (object_class, BASE_PROP2, inherited_spec2); |
|
314 |
|
315 g_object_class_override_property (object_class, BASE_PROP3, "prop3"); |
|
316 |
|
317 inherited_spec4 = g_param_spec_int ("prop4", |
|
318 "Prop4", |
|
319 "Property 4", |
|
320 G_MININT, /* min */ |
|
321 G_MAXINT, /* max */ |
|
322 0, /* default */ |
|
323 G_PARAM_READWRITE); |
|
324 g_object_class_install_property (object_class, BASE_PROP4, inherited_spec4); |
|
325 } |
|
326 |
|
327 static void |
|
328 base_object_init (BaseObject *base_object) |
|
329 { |
|
330 base_object->val1 = 42; |
|
331 } |
|
332 |
|
333 static DEFINE_TYPE_FULL (BaseObject, base_object, |
|
334 base_object_class_init, NULL, base_object_init, |
|
335 G_TYPE_OBJECT, |
|
336 INTERFACE (NULL, TEST_TYPE_IFACE)) |
|
337 |
|
338 static void |
|
339 derived_object_set_property (GObject *object, |
|
340 guint prop_id, |
|
341 const GValue *value, |
|
342 GParamSpec *pspec) |
|
343 { |
|
344 BaseObject *base_object = BASE_OBJECT (object); |
|
345 |
|
346 switch (prop_id) |
|
347 { |
|
348 case DERIVED_PROP3: |
|
349 g_assert (pspec == inherited_spec3); |
|
350 base_object->val3 = g_value_get_int (value); |
|
351 break; |
|
352 case DERIVED_PROP4: |
|
353 g_assert (pspec == inherited_spec4); |
|
354 base_object->val4 = g_value_get_int (value); |
|
355 break; |
|
356 default: |
|
357 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
|
358 break; |
|
359 } |
|
360 } |
|
361 |
|
362 static void |
|
363 derived_object_get_property (GObject *object, |
|
364 guint prop_id, |
|
365 GValue *value, |
|
366 GParamSpec *pspec) |
|
367 { |
|
368 BaseObject *base_object = BASE_OBJECT (object); |
|
369 |
|
370 switch (prop_id) |
|
371 { |
|
372 case DERIVED_PROP3: |
|
373 g_assert (pspec == inherited_spec3); |
|
374 g_value_set_int (value, base_object->val3); |
|
375 break; |
|
376 case DERIVED_PROP4: |
|
377 g_assert (pspec == inherited_spec4); |
|
378 g_value_set_int (value, base_object->val4); |
|
379 break; |
|
380 default: |
|
381 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
|
382 break; |
|
383 } |
|
384 } |
|
385 |
|
386 static void |
|
387 derived_object_class_init (DerivedObjectClass *class) |
|
388 { |
|
389 GObjectClass *object_class = G_OBJECT_CLASS (class); |
|
390 |
|
391 object_class->set_property = derived_object_set_property; |
|
392 object_class->get_property = derived_object_get_property; |
|
393 |
|
394 /* Overriding a property that is itself overridding an interface property */ |
|
395 g_object_class_override_property (object_class, DERIVED_PROP3, "prop3"); |
|
396 |
|
397 /* Overriding a property not from an interface */ |
|
398 g_object_class_override_property (object_class, DERIVED_PROP4, "prop4"); |
|
399 } |
|
400 |
|
401 static DEFINE_TYPE (DerivedObject, derived_object, |
|
402 derived_object_class_init, NULL, NULL, |
|
403 BASE_TYPE_OBJECT) |
|
404 |
|
405 /* Helper function for testing ...list_properties() |
|
406 */ |
|
407 static void |
|
408 assert_in_properties (GParamSpec *param_spec, |
|
409 GParamSpec **properties, |
|
410 gint n_properties) |
|
411 { |
|
412 gint i; |
|
413 gboolean found = FALSE; |
|
414 |
|
415 for (i = 0; i < n_properties; i++) |
|
416 { |
|
417 if (properties[i] == param_spec) |
|
418 found = TRUE; |
|
419 } |
|
420 |
|
421 g_assert (found); |
|
422 } |
|
423 |
|
424 int |
|
425 main (gint argc, |
|
426 gchar *argv[]) |
|
427 { |
|
428 BaseObject *object; |
|
429 GObjectClass *object_class; |
|
430 TestIfaceClass *iface_vtable; |
|
431 GParamSpec **properties; |
|
432 gint n_properties; |
|
433 |
|
434 gint val1, val2, val3, val4; |
|
435 #ifdef __SYMBIAN32__ |
|
436 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); |
|
437 g_set_print_handler(mrtPrintHandler); |
|
438 #endif /*__SYMBIAN32__*/ |
|
439 |
|
440 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) | |
|
441 G_LOG_LEVEL_WARNING | |
|
442 G_LOG_LEVEL_CRITICAL); |
|
443 g_type_init (); |
|
444 |
|
445 object = g_object_new (DERIVED_TYPE_OBJECT, NULL); |
|
446 |
|
447 /* Test setting and getting the properties |
|
448 */ |
|
449 g_object_set (object, |
|
450 "prop1", 0x0101, |
|
451 "prop2", 0x0202, |
|
452 "prop3", 0x0303, |
|
453 "prop4", 0x0404, |
|
454 NULL); |
|
455 g_object_get (object, |
|
456 "prop1", &val1, |
|
457 "prop2", &val2, |
|
458 "prop3", &val3, |
|
459 "prop4", &val4, |
|
460 NULL); |
|
461 |
|
462 g_assert (val1 == 0x0101); |
|
463 g_assert (val2 == 0x0202); |
|
464 g_assert (val3 == 0x0303); |
|
465 g_assert (val4 == 0x0404); |
|
466 |
|
467 /* Test that the right spec is passed on explicit notifications |
|
468 */ |
|
469 g_object_freeze_notify (G_OBJECT (object)); |
|
470 g_object_notify (G_OBJECT (object), "prop1"); |
|
471 g_object_notify (G_OBJECT (object), "prop2"); |
|
472 g_object_notify (G_OBJECT (object), "prop3"); |
|
473 g_object_notify (G_OBJECT (object), "prop4"); |
|
474 g_object_thaw_notify (G_OBJECT (object)); |
|
475 |
|
476 /* Test g_object_class_find_property() for overridden properties |
|
477 */ |
|
478 object_class = G_OBJECT_GET_CLASS (object); |
|
479 |
|
480 g_assert (g_object_class_find_property (object_class, "prop1") == inherited_spec1); |
|
481 g_assert (g_object_class_find_property (object_class, "prop2") == inherited_spec2); |
|
482 g_assert (g_object_class_find_property (object_class, "prop3") == inherited_spec3); |
|
483 g_assert (g_object_class_find_property (object_class, "prop4") == inherited_spec4); |
|
484 |
|
485 /* Test g_object_class_list_properties() for overridden properties |
|
486 */ |
|
487 properties = g_object_class_list_properties (object_class,(guint *) &n_properties); |
|
488 g_assert (n_properties == 4); |
|
489 assert_in_properties (inherited_spec1, properties, n_properties); |
|
490 assert_in_properties (inherited_spec2, properties, n_properties); |
|
491 assert_in_properties (inherited_spec3, properties, n_properties); |
|
492 assert_in_properties (inherited_spec4, properties, n_properties); |
|
493 g_free (properties); |
|
494 |
|
495 /* Test g_object_interface_find_property() |
|
496 */ |
|
497 iface_vtable = g_type_default_interface_peek (TEST_TYPE_IFACE); |
|
498 |
|
499 g_assert (g_object_interface_find_property (iface_vtable, "prop1") == iface_spec1); |
|
500 g_assert (g_object_interface_find_property (iface_vtable, "prop2") == iface_spec2); |
|
501 g_assert (g_object_interface_find_property (iface_vtable, "prop3") == iface_spec3); |
|
502 |
|
503 /* Test g_object_interface_list_properties() |
|
504 */ |
|
505 properties = g_object_interface_list_properties (iface_vtable, (guint *)&n_properties); |
|
506 g_assert (n_properties == 3); |
|
507 assert_in_properties (iface_spec1, properties, n_properties); |
|
508 assert_in_properties (iface_spec2, properties, n_properties); |
|
509 assert_in_properties (iface_spec3, properties, n_properties); |
|
510 g_free (properties); |
|
511 |
|
512 g_object_unref (object); |
|
513 #ifdef __SYMBIAN32__ |
|
514 testResultXml("ifaceproperties"); |
|
515 #endif /*__SYMBIAN32__*/ |
|
516 |
|
517 return 0; |
|
518 } |