Branch data Line data Source code
1 : : #include <stdlib.h>
2 : : #include <gstdio.h>
3 : : #include <glib-object.h>
4 : :
5 : : typedef struct _TestObject {
6 : : GObject parent_instance;
7 : : gint foo;
8 : : gboolean bar;
9 : : gchar *baz;
10 : : GVariant *var; /* (nullable) (owned) */
11 : : gchar *quux;
12 : : } TestObject;
13 : :
14 : : typedef struct _TestObjectClass {
15 : : GObjectClass parent_class;
16 : : } TestObjectClass;
17 : :
18 : : enum { PROP_0, PROP_FOO, PROP_BAR, PROP_BAZ, PROP_VAR, PROP_QUUX, N_PROPERTIES };
19 : :
20 : : static GParamSpec *properties[N_PROPERTIES] = { NULL, };
21 : :
22 : : static GType test_object_get_type (void);
23 : 12 : G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
24 : :
25 : : static void
26 : 25 : test_object_set_foo (TestObject *obj,
27 : : gint foo)
28 : : {
29 : 25 : if (obj->foo != foo)
30 : : {
31 : 23 : obj->foo = foo;
32 : :
33 : 23 : g_assert_nonnull (properties[PROP_FOO]);
34 : 23 : g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_FOO]);
35 : : }
36 : 25 : }
37 : :
38 : : static void
39 : 5 : test_object_set_bar (TestObject *obj,
40 : : gboolean bar)
41 : : {
42 : 5 : bar = !!bar;
43 : :
44 : 5 : if (obj->bar != bar)
45 : : {
46 : 1 : obj->bar = bar;
47 : :
48 : 1 : g_assert_nonnull (properties[PROP_BAR]);
49 : 1 : g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_BAR]);
50 : : }
51 : 5 : }
52 : :
53 : : static void
54 : 7 : test_object_set_baz (TestObject *obj,
55 : : const gchar *baz)
56 : : {
57 : 7 : if (g_strcmp0 (obj->baz, baz) != 0)
58 : : {
59 : 6 : g_free (obj->baz);
60 : 6 : obj->baz = g_strdup (baz);
61 : :
62 : 6 : g_assert_nonnull (properties[PROP_BAZ]);
63 : 6 : g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_BAZ]);
64 : : }
65 : 7 : }
66 : :
67 : : static void
68 : 1 : test_object_set_var (TestObject *obj,
69 : : GVariant *var)
70 : : {
71 : 1 : GVariant *new_var = NULL;
72 : :
73 : 1 : if (var == NULL || obj->var == NULL ||
74 : 0 : !g_variant_equal (var, obj->var))
75 : : {
76 : : /* Note: We deliberately don’t sink @var here, to make sure that
77 : : * properties_set_property_variant_floating() is testing that GObject
78 : : * internally sinks variants. */
79 : 1 : new_var = g_variant_ref (var);
80 : 1 : g_clear_pointer (&obj->var, g_variant_unref);
81 : 1 : obj->var = g_steal_pointer (&new_var);
82 : :
83 : 1 : g_assert_nonnull (properties[PROP_VAR]);
84 : 1 : g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_VAR]);
85 : : }
86 : 1 : }
87 : :
88 : : static void
89 : 4 : test_object_set_quux (TestObject *obj,
90 : : const gchar *quux)
91 : : {
92 : 4 : if (g_strcmp0 (obj->quux, quux) != 0)
93 : : {
94 : 3 : g_free (obj->quux);
95 : 3 : obj->quux = g_strdup (quux);
96 : :
97 : 3 : g_assert_nonnull (properties[PROP_QUUX]);
98 : 3 : g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_QUUX]);
99 : : }
100 : 4 : }
101 : :
102 : : static void
103 : 10 : test_object_finalize (GObject *gobject)
104 : : {
105 : 10 : TestObject *self = (TestObject *) gobject;
106 : :
107 : 10 : g_free (self->baz);
108 : 10 : g_clear_pointer (&self->var, g_variant_unref);
109 : 10 : g_free (self->quux);
110 : :
111 : : /* When the ref_count of an object is zero it is still
112 : : * possible to notify the property, but it should do
113 : : * nothing and silently quit (bug #705570)
114 : : */
115 : 10 : g_object_notify (gobject, "foo");
116 : 10 : g_object_notify_by_pspec (gobject, properties[PROP_BAR]);
117 : :
118 : 10 : G_OBJECT_CLASS (test_object_parent_class)->finalize (gobject);
119 : 10 : }
120 : :
121 : : static void
122 : 42 : test_object_set_property (GObject *gobject,
123 : : guint prop_id,
124 : : const GValue *value,
125 : : GParamSpec *pspec)
126 : : {
127 : 42 : TestObject *tobj = (TestObject *) gobject;
128 : :
129 : 42 : g_assert_cmpint (prop_id, !=, 0);
130 : 42 : g_assert_true (prop_id < N_PROPERTIES && pspec == properties[prop_id]);
131 : :
132 : 42 : switch (prop_id)
133 : : {
134 : 25 : case PROP_FOO:
135 : 25 : test_object_set_foo (tobj, g_value_get_int (value));
136 : 25 : break;
137 : :
138 : 5 : case PROP_BAR:
139 : 5 : test_object_set_bar (tobj, g_value_get_boolean (value));
140 : 5 : break;
141 : :
142 : 7 : case PROP_BAZ:
143 : 7 : test_object_set_baz (tobj, g_value_get_string (value));
144 : 7 : break;
145 : :
146 : 1 : case PROP_VAR:
147 : 1 : test_object_set_var (tobj, g_value_get_variant (value));
148 : 1 : break;
149 : :
150 : 4 : case PROP_QUUX:
151 : 4 : test_object_set_quux (tobj, g_value_get_string (value));
152 : 4 : break;
153 : :
154 : 0 : default:
155 : : g_assert_not_reached ();
156 : : }
157 : 42 : }
158 : :
159 : : static void
160 : 27 : test_object_get_property (GObject *gobject,
161 : : guint prop_id,
162 : : GValue *value,
163 : : GParamSpec *pspec)
164 : : {
165 : 27 : TestObject *tobj = (TestObject *) gobject;
166 : :
167 : 27 : g_assert_cmpint (prop_id, !=, 0);
168 : 27 : g_assert_true (prop_id < N_PROPERTIES && pspec == properties[prop_id]);
169 : :
170 : 27 : switch (prop_id)
171 : : {
172 : 7 : case PROP_FOO:
173 : 7 : g_value_set_int (value, tobj->foo);
174 : 7 : break;
175 : :
176 : 8 : case PROP_BAR:
177 : 8 : g_value_set_boolean (value, tobj->bar);
178 : 8 : break;
179 : :
180 : 6 : case PROP_BAZ:
181 : 6 : g_value_set_string (value, tobj->baz);
182 : 6 : break;
183 : :
184 : 1 : case PROP_VAR:
185 : 1 : g_value_set_variant (value, tobj->var);
186 : 1 : break;
187 : :
188 : 5 : case PROP_QUUX:
189 : 5 : g_value_set_string (value, tobj->quux);
190 : 5 : break;
191 : :
192 : 0 : default:
193 : : g_assert_not_reached ();
194 : : }
195 : 27 : }
196 : :
197 : : static void
198 : 1 : test_object_class_init (TestObjectClass *klass)
199 : : {
200 : 1 : GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
201 : :
202 : 1 : properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "Foo",
203 : : -1, G_MAXINT,
204 : : 0,
205 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
206 : 1 : properties[PROP_BAR] = g_param_spec_boolean ("bar", "Bar", "Bar",
207 : : FALSE,
208 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
209 : 1 : properties[PROP_BAZ] = g_param_spec_string ("baz", "Baz", "Baz",
210 : : NULL,
211 : : G_PARAM_READWRITE);
212 : 1 : properties[PROP_VAR] = g_param_spec_variant ("var", "Var", "Var",
213 : : G_VARIANT_TYPE_STRING, NULL,
214 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
215 : :
216 : 1 : gobject_class->set_property = test_object_set_property;
217 : 1 : gobject_class->get_property = test_object_get_property;
218 : 1 : gobject_class->finalize = test_object_finalize;
219 : :
220 : 1 : g_object_class_install_properties (gobject_class, N_PROPERTIES - 1, properties);
221 : :
222 : : /* We intentionally install this property separately, to test
223 : : * that that works, and that property lookup works regardless
224 : : * how the property was installed.
225 : : */
226 : 1 : properties[PROP_QUUX] = g_param_spec_string ("quux", "quux", "quux",
227 : : NULL,
228 : : G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
229 : :
230 : 1 : g_object_class_install_property (gobject_class, PROP_QUUX, properties[PROP_QUUX]);
231 : 1 : }
232 : :
233 : : static void
234 : 10 : test_object_init (TestObject *self)
235 : : {
236 : 10 : self->foo = 42;
237 : 10 : self->bar = TRUE;
238 : 10 : self->baz = g_strdup ("Hello");
239 : 10 : self->quux = NULL;
240 : 10 : }
241 : :
242 : : static void
243 : 1 : properties_install (void)
244 : : {
245 : 1 : TestObject *obj = g_object_new (test_object_get_type (), NULL);
246 : : GParamSpec *pspec;
247 : : char *name;
248 : :
249 : 1 : g_assert_nonnull (properties[PROP_FOO]);
250 : :
251 : 1 : pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), "foo");
252 : 1 : g_assert_true (properties[PROP_FOO] == pspec);
253 : :
254 : 1 : name = g_strdup ("bar");
255 : 1 : pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), name);
256 : 1 : g_assert_true (properties[PROP_BAR] == pspec);
257 : 1 : g_free (name);
258 : :
259 : 1 : pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), "baz");
260 : 1 : g_assert_true (properties[PROP_BAZ] == pspec);
261 : :
262 : 1 : pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), "var");
263 : 1 : g_assert_true (properties[PROP_VAR] == pspec);
264 : :
265 : 1 : pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), "quux");
266 : 1 : g_assert_true (properties[PROP_QUUX] == pspec);
267 : :
268 : 1 : g_object_unref (obj);
269 : 1 : }
270 : :
271 : : typedef struct {
272 : : GObject parent_instance;
273 : : int value[16];
274 : : } ManyProps;
275 : :
276 : : typedef GObjectClass ManyPropsClass;
277 : :
278 : : static GParamSpec *props[16];
279 : :
280 : : GType many_props_get_type (void) G_GNUC_CONST;
281 : :
282 : 3 : G_DEFINE_TYPE(ManyProps, many_props, G_TYPE_OBJECT)
283 : :
284 : : static void
285 : 1 : many_props_init (ManyProps *self)
286 : : {
287 : 1 : }
288 : :
289 : : static void
290 : 0 : get_prop (GObject *object,
291 : : guint prop_id,
292 : : GValue *value,
293 : : GParamSpec *pspec)
294 : : {
295 : 0 : ManyProps *mp = (ManyProps *) object;
296 : :
297 : 0 : if (prop_id > 0 && prop_id < 13)
298 : 0 : g_value_set_int (value, mp->value[prop_id]);
299 : : else
300 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301 : 0 : }
302 : :
303 : : static void
304 : 0 : set_prop (GObject *object,
305 : : guint prop_id,
306 : : const GValue *value,
307 : : GParamSpec *pspec)
308 : : {
309 : 0 : ManyProps *mp = (ManyProps *) object;
310 : :
311 : 0 : if (prop_id > 0 && prop_id < 13)
312 : 0 : mp->value[prop_id] = g_value_get_int (value);
313 : : else
314 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
315 : 0 : }
316 : :
317 : : static void
318 : 1 : many_props_class_init (ManyPropsClass *class)
319 : : {
320 : 1 : G_OBJECT_CLASS (class)->get_property = get_prop;
321 : 1 : G_OBJECT_CLASS (class)->set_property = set_prop;
322 : :
323 : 1 : props[1] = g_param_spec_int ("one", NULL, NULL,
324 : : 0, G_MAXINT, 0,
325 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
326 : 1 : props[2] = g_param_spec_int ("two", NULL, NULL,
327 : : 0, G_MAXINT, 0,
328 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
329 : 1 : props[3] = g_param_spec_int ("three", NULL, NULL,
330 : : 0, G_MAXINT, 0,
331 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
332 : 1 : props[4] = g_param_spec_int ("four", NULL, NULL,
333 : : 0, G_MAXINT, 0,
334 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
335 : 1 : props[5] = g_param_spec_int ("five", NULL, NULL,
336 : : 0, G_MAXINT, 0,
337 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
338 : 1 : props[6] = g_param_spec_int ("six", NULL, NULL,
339 : : 0, G_MAXINT, 0,
340 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
341 : 1 : props[7] = g_param_spec_int ("seven", NULL, NULL,
342 : : 0, G_MAXINT, 0,
343 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
344 : 1 : props[8] = g_param_spec_int ("eight", NULL, NULL,
345 : : 0, G_MAXINT, 0,
346 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
347 : 1 : props[9] = g_param_spec_int ("nine", NULL, NULL,
348 : : 0, G_MAXINT, 0,
349 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
350 : 1 : props[10] = g_param_spec_int ("ten", NULL, NULL,
351 : : 0, G_MAXINT, 0,
352 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
353 : 1 : props[11] = g_param_spec_int ("eleven", NULL, NULL,
354 : : 0, G_MAXINT, 0,
355 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
356 : 1 : props[12] = g_param_spec_int ("twelve", NULL, NULL,
357 : : 0, G_MAXINT, 0,
358 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
359 : 1 : g_object_class_install_properties (G_OBJECT_CLASS (class), 12, props);
360 : 1 : }
361 : :
362 : : static void
363 : 1 : properties_install_many (void)
364 : : {
365 : 1 : ManyProps *obj = g_object_new (many_props_get_type (), NULL);
366 : : GParamSpec *pspec;
367 : :
368 : 1 : pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), "one");
369 : 1 : g_assert_true (props[1] == pspec);
370 : :
371 : 1 : pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), "ten");
372 : 1 : g_assert_true (props[10] == pspec);
373 : :
374 : 1 : g_object_unref (obj);
375 : 1 : }
376 : :
377 : : typedef struct {
378 : : const gchar *name;
379 : : GParamSpec *pspec;
380 : : gboolean fired;
381 : : } TestNotifyClosure;
382 : :
383 : : static void
384 : 4 : on_notify (GObject *gobject,
385 : : GParamSpec *pspec,
386 : : TestNotifyClosure *closure)
387 : : {
388 : 4 : g_assert_true (closure->pspec == pspec);
389 : 4 : g_assert_cmpstr (closure->name, ==, pspec->name);
390 : 4 : closure->fired = TRUE;
391 : 4 : }
392 : :
393 : : static void
394 : 1 : properties_notify (void)
395 : : {
396 : 1 : TestObject *obj = g_object_new (test_object_get_type (), NULL);
397 : : TestNotifyClosure closure;
398 : :
399 : 1 : g_assert_nonnull (properties[PROP_FOO]);
400 : 1 : g_assert_nonnull (properties[PROP_QUUX]);
401 : 1 : g_signal_connect (obj, "notify", G_CALLBACK (on_notify), &closure);
402 : :
403 : 1 : closure.name = "foo";
404 : 1 : closure.pspec = properties[PROP_FOO];
405 : :
406 : 1 : closure.fired = FALSE;
407 : 1 : g_object_set (obj, "foo", 47, NULL);
408 : 1 : g_assert_true (closure.fired);
409 : :
410 : 1 : closure.name = "baz";
411 : 1 : closure.pspec = properties[PROP_BAZ];
412 : :
413 : 1 : closure.fired = FALSE;
414 : 1 : g_object_set (obj, "baz", "something new", NULL);
415 : 1 : g_assert_true (closure.fired);
416 : :
417 : : /* baz lacks explicit notify, so we will see this twice */
418 : 1 : closure.fired = FALSE;
419 : 1 : g_object_set (obj, "baz", "something new", NULL);
420 : 1 : g_assert_true (closure.fired);
421 : :
422 : : /* quux on the other hand, ... */
423 : 1 : closure.name = "quux";
424 : 1 : closure.pspec = properties[PROP_QUUX];
425 : :
426 : 1 : closure.fired = FALSE;
427 : 1 : g_object_set (obj, "quux", "something new", NULL);
428 : 1 : g_assert_true (closure.fired);
429 : :
430 : : /* no change; no notify */
431 : 1 : closure.fired = FALSE;
432 : 1 : g_object_set (obj, "quux", "something new", NULL);
433 : 1 : g_assert_false (closure.fired);
434 : :
435 : :
436 : 1 : g_object_unref (obj);
437 : 1 : }
438 : :
439 : : typedef struct {
440 : : GParamSpec *pspec[3];
441 : : gint pos;
442 : : } Notifys;
443 : :
444 : : static void
445 : 6 : on_notify2 (GObject *gobject,
446 : : GParamSpec *pspec,
447 : : Notifys *n)
448 : : {
449 : 6 : g_assert_true (n->pspec[n->pos] == pspec);
450 : 6 : n->pos++;
451 : 6 : }
452 : :
453 : : static void
454 : 1 : properties_notify_queue (void)
455 : : {
456 : 1 : TestObject *obj = g_object_new (test_object_get_type (), NULL);
457 : : Notifys n;
458 : :
459 : 1 : g_assert_nonnull (properties[PROP_FOO]);
460 : :
461 : 1 : n.pspec[0] = properties[PROP_BAZ];
462 : 1 : n.pspec[1] = properties[PROP_BAR];
463 : 1 : n.pspec[2] = properties[PROP_FOO];
464 : 1 : n.pos = 0;
465 : :
466 : 1 : g_signal_connect (obj, "notify", G_CALLBACK (on_notify2), &n);
467 : :
468 : 1 : g_object_freeze_notify (G_OBJECT (obj));
469 : 1 : g_object_set (obj, "foo", 47, NULL);
470 : 1 : g_object_set (obj, "bar", TRUE, "foo", 42, "baz", "abc", NULL);
471 : 1 : g_object_thaw_notify (G_OBJECT (obj));
472 : 1 : g_assert_cmpint (n.pos, ==, 3);
473 : :
474 : 1 : g_object_unref (obj);
475 : 1 : }
476 : :
477 : : static void
478 : 1 : test_properties_notify_too_frozen (void)
479 : : {
480 : 1 : if (g_test_subprocess ())
481 : : {
482 : 0 : TestObject *obj = g_object_new (test_object_get_type (), NULL);
483 : :
484 : 0 : for (unsigned int i = 0; i < 1000000; i++)
485 : 0 : g_object_freeze_notify (G_OBJECT (obj));
486 : :
487 : 0 : g_object_unref (obj);
488 : : }
489 : :
490 : 1 : g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
491 : 1 : g_test_trap_assert_failed ();
492 : 1 : g_test_trap_assert_stderr ("*CRITICAL*called g_object_freeze_notify() too often*");
493 : 1 : }
494 : :
495 : : static void
496 : 1 : properties_construct (void)
497 : : {
498 : : TestObject *obj;
499 : : gint val;
500 : : gboolean b;
501 : : gchar *s;
502 : :
503 : 1 : g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=630357");
504 : :
505 : : /* more than 16 args triggers a realloc in g_object_new_valist() */
506 : 1 : obj = g_object_new (test_object_get_type (),
507 : : "foo", 1,
508 : : "foo", 2,
509 : : "foo", 3,
510 : : "foo", 4,
511 : : "foo", 5,
512 : : "bar", FALSE,
513 : : "foo", 6,
514 : : "foo", 7,
515 : : "foo", 8,
516 : : "foo", 9,
517 : : "foo", 10,
518 : : "baz", "boo",
519 : : "foo", 11,
520 : : "foo", 12,
521 : : "foo", 13,
522 : : "foo", 14,
523 : : "foo", 15,
524 : : "foo", 16,
525 : : "foo", 17,
526 : : "foo", 18,
527 : : NULL);
528 : :
529 : 1 : g_object_get (obj, "foo", &val, NULL);
530 : 1 : g_assert_cmpint (val, ==, 18);
531 : 1 : g_object_get (obj, "bar", &b, NULL);
532 : 1 : g_assert_false (b);
533 : 1 : g_object_get (obj, "baz", &s, NULL);
534 : 1 : g_assert_cmpstr (s, ==, "boo");
535 : 1 : g_free (s);
536 : :
537 : 1 : g_object_unref (obj);
538 : 1 : }
539 : :
540 : : static void
541 : 1 : properties_testv_with_no_properties (void)
542 : : {
543 : : TestObject *test_obj;
544 : 1 : const char *prop_names[4] = { "foo", "bar", "baz", "quux" };
545 : 1 : GValue values_out[4] = { G_VALUE_INIT };
546 : : guint i;
547 : :
548 : : /* Test newv_with_properties && getv */
549 : 1 : test_obj = (TestObject *) g_object_new_with_properties (
550 : : test_object_get_type (), 0, NULL, NULL);
551 : 1 : g_object_getv (G_OBJECT (test_obj), 4, prop_names, values_out);
552 : :
553 : : /* It should have init values */
554 : 1 : g_assert_cmpint (g_value_get_int (&values_out[0]), ==, 42);
555 : 1 : g_assert_true (g_value_get_boolean (&values_out[1]));
556 : 1 : g_assert_cmpstr (g_value_get_string (&values_out[2]), ==, "Hello");
557 : 1 : g_assert_cmpstr (g_value_get_string (&values_out[3]), ==, NULL);
558 : :
559 : 5 : for (i = 0; i < 4; i++)
560 : 4 : g_value_unset (&values_out[i]);
561 : 1 : g_object_unref (test_obj);
562 : 1 : }
563 : :
564 : : static void
565 : 1 : properties_testv_with_valid_properties (void)
566 : : {
567 : : TestObject *test_obj;
568 : 1 : const char *prop_names[4] = { "foo", "bar", "baz", "quux" };
569 : :
570 : 1 : GValue values_in[4] = { G_VALUE_INIT };
571 : 1 : GValue values_out[4] = { G_VALUE_INIT };
572 : : guint i;
573 : :
574 : 1 : g_value_init (&(values_in[0]), G_TYPE_INT);
575 : 1 : g_value_set_int (&(values_in[0]), 100);
576 : :
577 : 1 : g_value_init (&(values_in[1]), G_TYPE_BOOLEAN);
578 : 1 : g_value_set_boolean (&(values_in[1]), TRUE);
579 : :
580 : 1 : g_value_init (&(values_in[2]), G_TYPE_STRING);
581 : 1 : g_value_set_string (&(values_in[2]), "pigs");
582 : :
583 : 1 : g_value_init (&(values_in[3]), G_TYPE_STRING);
584 : 1 : g_value_set_string (&(values_in[3]), "fly");
585 : :
586 : : /* Test newv_with_properties && getv */
587 : 1 : test_obj = (TestObject *) g_object_new_with_properties (
588 : : test_object_get_type (), 4, prop_names, values_in);
589 : 1 : g_object_getv (G_OBJECT (test_obj), 4, prop_names, values_out);
590 : :
591 : 1 : g_assert_cmpint (g_value_get_int (&values_out[0]), ==, 100);
592 : 1 : g_assert_true (g_value_get_boolean (&values_out[1]));
593 : 1 : g_assert_cmpstr (g_value_get_string (&values_out[2]), ==, "pigs");
594 : 1 : g_assert_cmpstr (g_value_get_string (&values_out[3]), ==, "fly");
595 : :
596 : 5 : for (i = 0; i < G_N_ELEMENTS (values_out); i++)
597 : 4 : g_value_unset (&values_out[i]);
598 : :
599 : : /* Test newv2 && getv */
600 : 1 : g_value_set_string (&(values_in[2]), "Elmo knows");
601 : 1 : g_value_set_string (&(values_in[3]), "where you live");
602 : 1 : g_object_setv (G_OBJECT (test_obj), 4, prop_names, values_in);
603 : :
604 : 1 : g_object_getv (G_OBJECT (test_obj), 4, prop_names, values_out);
605 : :
606 : 1 : g_assert_cmpint (g_value_get_int (&values_out[0]), ==, 100);
607 : 1 : g_assert_true (g_value_get_boolean (&values_out[1]));
608 : :
609 : 1 : g_assert_cmpstr (g_value_get_string (&values_out[2]), ==, "Elmo knows");
610 : 1 : g_assert_cmpstr (g_value_get_string (&values_out[3]), ==, "where you live");
611 : :
612 : 5 : for (i = 0; i < G_N_ELEMENTS (values_in); i++)
613 : 4 : g_value_unset (&values_in[i]);
614 : 5 : for (i = 0; i < G_N_ELEMENTS (values_out); i++)
615 : 4 : g_value_unset (&values_out[i]);
616 : :
617 : 1 : g_object_unref (test_obj);
618 : 1 : }
619 : :
620 : : static void
621 : 1 : properties_testv_with_invalid_property_type (void)
622 : : {
623 : 1 : if (g_test_subprocess ())
624 : : {
625 : : TestObject *test_obj;
626 : 0 : const char *invalid_prop_names[1] = { "foo" };
627 : 0 : GValue values_in[1] = { G_VALUE_INIT };
628 : :
629 : 0 : g_value_init (&(values_in[0]), G_TYPE_STRING);
630 : 0 : g_value_set_string (&(values_in[0]), "fly");
631 : :
632 : 0 : test_obj = (TestObject *) g_object_new_with_properties (
633 : : test_object_get_type (), 1, invalid_prop_names, values_in);
634 : : /* should give a warning */
635 : :
636 : 0 : g_object_unref (test_obj);
637 : : }
638 : 1 : g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
639 : 1 : g_test_trap_assert_failed ();
640 : 1 : g_test_trap_assert_stderr ("*CRITICAL*foo*gint*gchararray*");
641 : 1 : }
642 : :
643 : :
644 : : static void
645 : 1 : properties_testv_with_invalid_property_names (void)
646 : : {
647 : 1 : if (g_test_subprocess ())
648 : : {
649 : : TestObject *test_obj;
650 : 0 : const char *invalid_prop_names[4] = { "foo", "boo", "moo", "poo" };
651 : 0 : GValue values_in[4] = { G_VALUE_INIT };
652 : :
653 : 0 : g_value_init (&(values_in[0]), G_TYPE_INT);
654 : 0 : g_value_set_int (&(values_in[0]), 100);
655 : :
656 : 0 : g_value_init (&(values_in[1]), G_TYPE_BOOLEAN);
657 : 0 : g_value_set_boolean (&(values_in[1]), TRUE);
658 : :
659 : 0 : g_value_init (&(values_in[2]), G_TYPE_STRING);
660 : 0 : g_value_set_string (&(values_in[2]), "pigs");
661 : :
662 : 0 : g_value_init (&(values_in[3]), G_TYPE_STRING);
663 : 0 : g_value_set_string (&(values_in[3]), "fly");
664 : :
665 : 0 : test_obj = (TestObject *) g_object_new_with_properties (
666 : : test_object_get_type (), 4, invalid_prop_names, values_in);
667 : : /* This call should give 3 Critical warnings. Actually, a critical warning
668 : : * shouldn't make g_object_new_with_properties to fail when a bad named
669 : : * property is given, because, it will just ignore that property. However,
670 : : * for test purposes, it is considered that the test doesn't pass.
671 : : */
672 : :
673 : 0 : g_object_unref (test_obj);
674 : : }
675 : :
676 : 1 : g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
677 : 1 : g_test_trap_assert_failed ();
678 : 1 : g_test_trap_assert_stderr ("*CRITICAL*g_object_new_is_valid_property*boo*");
679 : 1 : }
680 : :
681 : : static void
682 : 1 : properties_testv_getv (void)
683 : : {
684 : : TestObject *test_obj;
685 : 1 : const char *prop_names[4] = { "foo", "bar", "baz", "quux" };
686 : 1 : GValue values_out_initialized[4] = { G_VALUE_INIT };
687 : 1 : GValue values_out_uninitialized[4] = { G_VALUE_INIT };
688 : : guint i;
689 : :
690 : 1 : g_value_init (&(values_out_initialized[0]), G_TYPE_INT);
691 : 1 : g_value_init (&(values_out_initialized[1]), G_TYPE_BOOLEAN);
692 : 1 : g_value_init (&(values_out_initialized[2]), G_TYPE_STRING);
693 : 1 : g_value_init (&(values_out_initialized[3]), G_TYPE_STRING);
694 : :
695 : 1 : test_obj = (TestObject *) g_object_new_with_properties (
696 : : test_object_get_type (), 0, NULL, NULL);
697 : :
698 : : /* Test g_object_getv for an initialized values array */
699 : 1 : g_object_getv (G_OBJECT (test_obj), 4, prop_names, values_out_initialized);
700 : : /* It should have init values */
701 : 1 : g_assert_cmpint (g_value_get_int (&values_out_initialized[0]), ==, 42);
702 : 1 : g_assert_true (g_value_get_boolean (&values_out_initialized[1]));
703 : 1 : g_assert_cmpstr (g_value_get_string (&values_out_initialized[2]), ==, "Hello");
704 : 1 : g_assert_cmpstr (g_value_get_string (&values_out_initialized[3]), ==, NULL);
705 : :
706 : : /* Test g_object_getv for an uninitialized values array */
707 : 1 : g_object_getv (G_OBJECT (test_obj), 4, prop_names, values_out_uninitialized);
708 : : /* It should have init values */
709 : 1 : g_assert_cmpint (g_value_get_int (&values_out_uninitialized[0]), ==, 42);
710 : 1 : g_assert_true (g_value_get_boolean (&values_out_uninitialized[1]));
711 : 1 : g_assert_cmpstr (g_value_get_string (&values_out_uninitialized[2]), ==, "Hello");
712 : 1 : g_assert_cmpstr (g_value_get_string (&values_out_uninitialized[3]), ==, NULL);
713 : :
714 : 5 : for (i = 0; i < 4; i++)
715 : : {
716 : 4 : g_value_unset (&values_out_initialized[i]);
717 : 4 : g_value_unset (&values_out_uninitialized[i]);
718 : : }
719 : 1 : g_object_unref (test_obj);
720 : 1 : }
721 : :
722 : : static void
723 : 1 : properties_get_property (void)
724 : : {
725 : : TestObject *test_obj;
726 : : struct {
727 : : const char *name;
728 : : GType gtype;
729 : : GValue value;
730 : 1 : } test_props[] = {
731 : : { "foo", G_TYPE_INT, G_VALUE_INIT },
732 : : { "bar", G_TYPE_INVALID, G_VALUE_INIT },
733 : : { "bar", G_TYPE_STRING, G_VALUE_INIT },
734 : : };
735 : : gsize i;
736 : :
737 : 1 : g_test_summary ("g_object_get_property() accepts uninitialized, "
738 : : "initialized, and transformable values");
739 : :
740 : 4 : for (i = 0; i < G_N_ELEMENTS (test_props); i++)
741 : : {
742 : 3 : if (test_props[i].gtype != G_TYPE_INVALID)
743 : 2 : g_value_init (&(test_props[i].value), test_props[i].gtype);
744 : : }
745 : :
746 : 1 : test_obj = (TestObject *) g_object_new_with_properties (test_object_get_type (), 0, NULL, NULL);
747 : :
748 : 1 : g_test_message ("Test g_object_get_property with an initialized value");
749 : 1 : g_object_get_property (G_OBJECT (test_obj), test_props[0].name, &(test_props[0].value));
750 : 1 : g_assert_cmpint (g_value_get_int (&(test_props[0].value)), ==, 42);
751 : :
752 : 1 : g_test_message ("Test g_object_get_property with an uninitialized value");
753 : 1 : g_object_get_property (G_OBJECT (test_obj), test_props[1].name, &(test_props[1].value));
754 : 1 : g_assert_true (g_value_get_boolean (&(test_props[1].value)));
755 : :
756 : 1 : g_test_message ("Test g_object_get_property with a transformable value");
757 : 1 : g_object_get_property (G_OBJECT (test_obj), test_props[2].name, &(test_props[2].value));
758 : 1 : g_assert_true (G_VALUE_HOLDS_STRING (&(test_props[2].value)));
759 : 1 : g_assert_cmpstr (g_value_get_string (&(test_props[2].value)), ==, "TRUE");
760 : :
761 : 4 : for (i = 0; i < G_N_ELEMENTS (test_props); i++)
762 : 3 : g_value_unset (&(test_props[i].value));
763 : :
764 : 1 : g_object_unref (test_obj);
765 : 1 : }
766 : :
767 : : static void
768 : 1 : properties_set_property_variant_floating (void)
769 : : {
770 : 1 : TestObject *test_obj = NULL;
771 : 1 : GVariant *owned_floating_variant = NULL;
772 : 1 : GVariant *floating_variant_ptr = NULL;
773 : 1 : GVariant *got_variant = NULL;
774 : :
775 : 1 : g_test_summary ("Test that setting a property to a floating variant consumes the reference");
776 : :
777 : 1 : test_obj = (TestObject *) g_object_new (test_object_get_type (), NULL);
778 : :
779 : 1 : owned_floating_variant = floating_variant_ptr = g_variant_new_string ("this variant has only one floating ref");
780 : 1 : g_assert_true (g_variant_is_floating (floating_variant_ptr));
781 : :
782 : 1 : g_object_set (test_obj, "var", g_steal_pointer (&owned_floating_variant), NULL);
783 : :
784 : : /* This assumes that the GObject implementation refs, rather than copies and destroys, the incoming variant */
785 : 1 : g_assert_false (g_variant_is_floating (floating_variant_ptr));
786 : :
787 : 1 : g_object_get (test_obj, "var", &got_variant, NULL);
788 : 1 : g_assert_false (g_variant_is_floating (got_variant));
789 : 1 : g_assert_cmpvariant (got_variant, floating_variant_ptr);
790 : :
791 : 1 : g_variant_unref (got_variant);
792 : 1 : g_object_unref (test_obj);
793 : 1 : }
794 : :
795 : : static void
796 : 1 : properties_testv_notify_queue (void)
797 : : {
798 : : TestObject *test_obj;
799 : 1 : const char *prop_names[3] = { "foo", "bar", "baz" };
800 : 1 : GValue values_in[3] = { G_VALUE_INIT };
801 : : Notifys n;
802 : : guint i;
803 : :
804 : 1 : g_value_init (&(values_in[0]), G_TYPE_INT);
805 : 1 : g_value_set_int (&(values_in[0]), 100);
806 : :
807 : 1 : g_value_init (&(values_in[1]), G_TYPE_BOOLEAN);
808 : 1 : g_value_set_boolean (&(values_in[1]), TRUE);
809 : :
810 : 1 : g_value_init (&(values_in[2]), G_TYPE_STRING);
811 : 1 : g_value_set_string (&(values_in[2]), "");
812 : :
813 : : /* Test newv_with_properties && getv */
814 : 1 : test_obj = (TestObject *) g_object_new_with_properties (
815 : : test_object_get_type (), 0, NULL, NULL);
816 : :
817 : 1 : g_assert_nonnull (properties[PROP_FOO]);
818 : :
819 : 1 : n.pspec[0] = properties[PROP_BAZ];
820 : 1 : n.pspec[1] = properties[PROP_BAR];
821 : 1 : n.pspec[2] = properties[PROP_FOO];
822 : 1 : n.pos = 0;
823 : :
824 : 1 : g_signal_connect (test_obj, "notify", G_CALLBACK (on_notify2), &n);
825 : :
826 : 1 : g_object_freeze_notify (G_OBJECT (test_obj));
827 : : {
828 : 1 : g_object_setv (G_OBJECT (test_obj), 3, prop_names, values_in);
829 : :
830 : : /* Set "foo" to 70 */
831 : 1 : g_value_set_int (&(values_in[0]), 100);
832 : 1 : g_object_setv (G_OBJECT (test_obj), 1, prop_names, values_in);
833 : : }
834 : 1 : g_object_thaw_notify (G_OBJECT (test_obj));
835 : 1 : g_assert_cmpint (n.pos, ==, 3);
836 : :
837 : 4 : for (i = 0; i < 3; i++)
838 : 3 : g_value_unset (&values_in[i]);
839 : 1 : g_object_unref (test_obj);
840 : 1 : }
841 : :
842 : : int
843 : 1 : main (int argc, char *argv[])
844 : : {
845 : 1 : g_test_init (&argc, &argv, NULL);
846 : :
847 : 1 : g_test_add_func ("/properties/install", properties_install);
848 : 1 : g_test_add_func ("/properties/install-many", properties_install_many);
849 : 1 : g_test_add_func ("/properties/notify", properties_notify);
850 : 1 : g_test_add_func ("/properties/notify-queue", properties_notify_queue);
851 : 1 : g_test_add_func ("/properties/notify/too-many-freezes", test_properties_notify_too_frozen);
852 : 1 : g_test_add_func ("/properties/construct", properties_construct);
853 : 1 : g_test_add_func ("/properties/get-property", properties_get_property);
854 : 1 : g_test_add_func ("/properties/set-property/variant/floating", properties_set_property_variant_floating);
855 : :
856 : 1 : g_test_add_func ("/properties/testv_with_no_properties",
857 : : properties_testv_with_no_properties);
858 : 1 : g_test_add_func ("/properties/testv_with_valid_properties",
859 : : properties_testv_with_valid_properties);
860 : 1 : g_test_add_func ("/properties/testv_with_invalid_property_type",
861 : : properties_testv_with_invalid_property_type);
862 : 1 : g_test_add_func ("/properties/testv_with_invalid_property_names",
863 : : properties_testv_with_invalid_property_names);
864 : 1 : g_test_add_func ("/properties/testv_getv", properties_testv_getv);
865 : 1 : g_test_add_func ("/properties/testv_notify_queue",
866 : : properties_testv_notify_queue);
867 : :
868 : 1 : return g_test_run ();
869 : : }
|