6 #define G_SETTINGS_ENABLE_BACKEND
7 #include <gio/gsettingsbackend.h>
11 static const gchar
*locale_dir
= ".";
13 static gboolean backend_set
;
15 /* These tests rely on the schemas in org.gtk.test.gschema.xml
16 * to be compiled and installed in the same directory.
20 check_and_free (GVariant
*value
,
21 const gchar
*expected
)
25 printed
= g_variant_print (value
, TRUE
);
26 g_assert_cmpstr (printed
, ==, expected
);
29 g_variant_unref (value
);
33 /* Just to get warmed up: Read and set a string, and
34 * verify that can read the changed string back
42 gboolean has_unapplied
;
46 settings
= g_settings_new ("org.gtk.test");
48 g_object_get (settings
,
52 "has-unapplied", &has_unapplied
,
53 "delay-apply", &delay_apply
,
55 g_assert_cmpstr (str
, ==, "org.gtk.test");
57 g_assert_cmpstr (path
, ==, "/tests/");
58 g_assert (!has_unapplied
);
59 g_assert (!delay_apply
);
64 g_settings_get (settings
, "greeting", "s", &str
);
65 g_assert_cmpstr (str
, ==, "Hello, earthlings");
68 g_settings_set (settings
, "greeting", "s", "goodbye world");
69 g_settings_get (settings
, "greeting", "s", &str
);
70 g_assert_cmpstr (str
, ==, "goodbye world");
74 if (!backend_set
&& g_test_undefined ())
76 GSettings
*tmp_settings
= g_settings_new ("org.gtk.test");
78 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
,
79 "*g_settings_set_value*expects type*");
80 g_settings_set (tmp_settings
, "greeting", "i", 555);
81 g_test_assert_expected_messages ();
83 g_object_unref (tmp_settings
);
86 g_settings_get (settings
, "greeting", "s", &str
);
87 g_assert_cmpstr (str
, ==, "goodbye world");
91 g_settings_reset (settings
, "greeting");
92 str
= g_settings_get_string (settings
, "greeting");
93 g_assert_cmpstr (str
, ==, "Hello, earthlings");
96 g_settings_set (settings
, "greeting", "s", "this is the end");
97 g_object_unref (settings
);
100 /* Check that we get an error when getting a key
101 * that is not in the schema
104 test_unknown_key (void)
106 if (!g_test_undefined ())
109 if (g_test_subprocess ())
114 settings
= g_settings_new ("org.gtk.test");
115 value
= g_settings_get_value (settings
, "no_such_key");
117 g_assert (value
== NULL
);
119 g_object_unref (settings
);
122 g_test_trap_subprocess (NULL
, 0, 0);
123 g_test_trap_assert_failed ();
124 g_test_trap_assert_stderr ("*does not contain*");
127 /* Check that we get an error when the schema
128 * has not been installed
131 test_no_schema (void)
133 if (!g_test_undefined ())
136 if (g_test_subprocess ())
140 settings
= g_settings_new ("no.such.schema");
142 g_assert (settings
== NULL
);
145 g_test_trap_subprocess (NULL
, 0, 0);
146 g_test_trap_assert_failed ();
147 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
150 /* Check that we get an error when passing a type string
151 * that does not match the schema
154 test_wrong_type (void)
159 if (!g_test_undefined ())
162 settings
= g_settings_new ("org.gtk.test");
164 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL
,
165 "*given value has a type of*");
166 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL
,
167 "*valid_format_string*");
168 g_settings_get (settings
, "greeting", "o", &str
);
169 g_test_assert_expected_messages ();
171 g_assert (str
== NULL
);
173 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
,
174 "*expects type 's'*");
175 g_settings_set (settings
, "greeting", "o", "/a/path");
176 g_test_assert_expected_messages ();
178 g_object_unref (settings
);
181 /* Check errors with explicit paths */
183 test_wrong_path (void)
185 if (!g_test_undefined ())
188 if (g_test_subprocess ())
190 GSettings
*settings G_GNUC_UNUSED
;
192 settings
= g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
195 g_test_trap_subprocess (NULL
, 0, 0);
196 g_test_trap_assert_failed ();
197 g_test_trap_assert_stderr ("*but path * specified by schema*");
203 if (!g_test_undefined ())
206 if (g_test_subprocess ())
208 GSettings
*settings G_GNUC_UNUSED
;
210 settings
= g_settings_new ("org.gtk.test.no-path");
213 g_test_trap_subprocess (NULL
, 0, 0);
214 g_test_trap_assert_failed ();
215 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
219 /* Check that we can successfully read and set the full
220 * range of all basic types
223 test_basic_types (void)
237 settings
= g_settings_new ("org.gtk.test.basic-types");
239 g_settings_get (settings
, "test-boolean", "b", &b
);
240 g_assert_cmpint (b
, ==, 1);
242 g_settings_set (settings
, "test-boolean", "b", 0);
243 g_settings_get (settings
, "test-boolean", "b", &b
);
244 g_assert_cmpint (b
, ==, 0);
246 g_settings_get (settings
, "test-byte", "y", &byte
);
247 g_assert_cmpint (byte
, ==, 25);
249 g_settings_set (settings
, "test-byte", "y", G_MAXUINT8
);
250 g_settings_get (settings
, "test-byte", "y", &byte
);
251 g_assert_cmpint (byte
, ==, G_MAXUINT8
);
253 g_settings_get (settings
, "test-int16", "n", &i16
);
254 g_assert_cmpint (i16
, ==, -1234);
256 g_settings_set (settings
, "test-int16", "n", G_MININT16
);
257 g_settings_get (settings
, "test-int16", "n", &i16
);
258 g_assert_cmpint (i16
, ==, G_MININT16
);
260 g_settings_set (settings
, "test-int16", "n", G_MAXINT16
);
261 g_settings_get (settings
, "test-int16", "n", &i16
);
262 g_assert_cmpint (i16
, ==, G_MAXINT16
);
264 g_settings_get (settings
, "test-uint16", "q", &u16
);
265 g_assert_cmpuint (u16
, ==, 1234);
267 g_settings_set (settings
, "test-uint16", "q", G_MAXUINT16
);
268 g_settings_get (settings
, "test-uint16", "q", &u16
);
269 g_assert_cmpuint (u16
, ==, G_MAXUINT16
);
271 g_settings_get (settings
, "test-int32", "i", &i32
);
272 g_assert_cmpint (i32
, ==, -123456);
274 g_settings_set (settings
, "test-int32", "i", G_MININT32
);
275 g_settings_get (settings
, "test-int32", "i", &i32
);
276 g_assert_cmpint (i32
, ==, G_MININT32
);
278 g_settings_set (settings
, "test-int32", "i", G_MAXINT32
);
279 g_settings_get (settings
, "test-int32", "i", &i32
);
280 g_assert_cmpint (i32
, ==, G_MAXINT32
);
282 g_settings_get (settings
, "test-uint32", "u", &u32
);
283 g_assert_cmpuint (u32
, ==, 123456);
285 g_settings_set (settings
, "test-uint32", "u", G_MAXUINT32
);
286 g_settings_get (settings
, "test-uint32", "u", &u32
);
287 g_assert_cmpuint (u32
, ==, G_MAXUINT32
);
289 g_settings_get (settings
, "test-int64", "x", &i64
);
290 g_assert_cmpuint (i64
, ==, -123456789);
292 g_settings_set (settings
, "test-int64", "x", G_MININT64
);
293 g_settings_get (settings
, "test-int64", "x", &i64
);
294 g_assert_cmpuint (i64
, ==, G_MININT64
);
296 g_settings_set (settings
, "test-int64", "x", G_MAXINT64
);
297 g_settings_get (settings
, "test-int64", "x", &i64
);
298 g_assert_cmpuint (i64
, ==, G_MAXINT64
);
300 g_settings_get (settings
, "test-uint64", "t", &u64
);
301 g_assert_cmpuint (u64
, ==, 123456789);
303 g_settings_set (settings
, "test-uint64", "t", G_MAXUINT64
);
304 g_settings_get (settings
, "test-uint64", "t", &u64
);
305 g_assert_cmpuint (u64
, ==, G_MAXUINT64
);
307 g_settings_get (settings
, "test-double", "d", &d
);
308 g_assert_cmpfloat (d
, ==, 123.456);
310 g_settings_set (settings
, "test-double", "d", G_MINDOUBLE
);
311 g_settings_get (settings
, "test-double", "d", &d
);
312 g_assert_cmpfloat (d
, ==, G_MINDOUBLE
);
314 g_settings_set (settings
, "test-double", "d", G_MAXDOUBLE
);
315 g_settings_get (settings
, "test-double", "d", &d
);
316 g_assert_cmpfloat (d
, ==, G_MAXDOUBLE
);
318 g_settings_get (settings
, "test-string", "s", &str
);
319 g_assert_cmpstr (str
, ==, "a string, it seems");
323 g_settings_get (settings
, "test-objectpath", "o", &str
);
324 g_assert_cmpstr (str
, ==, "/a/object/path");
325 g_object_unref (settings
);
330 /* Check that we can read an set complex types like
331 * tuples, arrays and dictionaries
334 test_complex_types (void)
339 GVariantIter
*iter
= NULL
;
342 settings
= g_settings_new ("org.gtk.test.complex-types");
344 g_settings_get (settings
, "test-tuple", "(s(ii))", &s
, &i1
, &i2
);
345 g_assert_cmpstr (s
, ==, "one");
346 g_assert_cmpint (i1
,==, 2);
347 g_assert_cmpint (i2
,==, 3);
351 g_settings_set (settings
, "test-tuple", "(s(ii))", "none", 0, 0);
352 g_settings_get (settings
, "test-tuple", "(s(ii))", &s
, &i1
, &i2
);
353 g_assert_cmpstr (s
, ==, "none");
354 g_assert_cmpint (i1
,==, 0);
355 g_assert_cmpint (i2
,==, 0);
359 g_settings_get (settings
, "test-array", "ai", &iter
);
360 g_assert_cmpint (g_variant_iter_n_children (iter
), ==, 6);
361 g_assert (g_variant_iter_next (iter
, "i", &i1
));
362 g_assert_cmpint (i1
, ==, 0);
363 g_assert (g_variant_iter_next (iter
, "i", &i1
));
364 g_assert_cmpint (i1
, ==, 1);
365 g_assert (g_variant_iter_next (iter
, "i", &i1
));
366 g_assert_cmpint (i1
, ==, 2);
367 g_assert (g_variant_iter_next (iter
, "i", &i1
));
368 g_assert_cmpint (i1
, ==, 3);
369 g_assert (g_variant_iter_next (iter
, "i", &i1
));
370 g_assert_cmpint (i1
, ==, 4);
371 g_assert (g_variant_iter_next (iter
, "i", &i1
));
372 g_assert_cmpint (i1
, ==, 5);
373 g_assert (!g_variant_iter_next (iter
, "i", &i1
));
374 g_variant_iter_free (iter
);
376 g_settings_get (settings
, "test-dict", "a{sau}", &iter
);
377 g_assert_cmpint (g_variant_iter_n_children (iter
), ==, 2);
378 g_assert (g_variant_iter_next (iter
, "{&s@au}", &s
, &v
));
379 g_assert_cmpstr (s
, ==, "AC");
380 g_assert_cmpstr ((char *)g_variant_get_type (v
), ==, "au");
382 g_assert (g_variant_iter_next (iter
, "{&s@au}", &s
, &v
));
383 g_assert_cmpstr (s
, ==, "IV");
384 g_assert_cmpstr ((char *)g_variant_get_type (v
), ==, "au");
386 g_variant_iter_free (iter
);
388 v
= g_settings_get_value (settings
, "test-dict");
389 g_assert_cmpstr ((char *)g_variant_get_type (v
), ==, "a{sau}");
392 g_object_unref (settings
);
395 static gboolean changed_cb_called
;
398 changed_cb (GSettings
*settings
,
402 changed_cb_called
= TRUE
;
404 g_assert_cmpstr (key
, ==, data
);
407 /* Test that basic change notification with the changed signal works.
413 GSettings
*settings2
;
415 settings
= g_settings_new ("org.gtk.test");
417 g_signal_connect (settings
, "changed",
418 G_CALLBACK (changed_cb
), "greeting");
420 changed_cb_called
= FALSE
;
422 g_settings_set (settings
, "greeting", "s", "new greeting");
423 g_assert (changed_cb_called
);
425 settings2
= g_settings_new ("org.gtk.test");
427 changed_cb_called
= FALSE
;
429 g_settings_set (settings2
, "greeting", "s", "hi");
430 g_assert (changed_cb_called
);
432 g_object_unref (settings2
);
433 g_object_unref (settings
);
436 static gboolean changed_cb_called2
;
439 changed_cb2 (GSettings
*settings
,
448 /* Test that changes done to a delay-mode instance
449 * don't appear to the outside world until apply. Also
450 * check that we get change notification when they are
452 * Also test that the has-unapplied property is properly
456 test_delay_apply (void)
459 GSettings
*settings2
;
465 settings
= g_settings_new ("org.gtk.test");
466 settings2
= g_settings_new ("org.gtk.test");
468 g_settings_set (settings2
, "greeting", "s", "top o' the morning");
470 changed_cb_called
= FALSE
;
471 changed_cb_called2
= FALSE
;
473 g_signal_connect (settings
, "changed",
474 G_CALLBACK (changed_cb2
), &changed_cb_called
);
475 g_signal_connect (settings2
, "changed",
476 G_CALLBACK (changed_cb2
), &changed_cb_called2
);
478 g_settings_delay (settings
);
480 g_settings_set (settings
, "greeting", "s", "greetings from test_delay_apply");
482 g_assert (changed_cb_called
);
483 g_assert (!changed_cb_called2
);
485 writable
= g_settings_is_writable (settings
, "greeting");
488 g_settings_get (settings
, "greeting", "s", &str
);
489 g_assert_cmpstr (str
, ==, "greetings from test_delay_apply");
493 v
= g_settings_get_user_value (settings
, "greeting");
494 s
= g_variant_get_string (v
, NULL
);
495 g_assert_cmpstr (s
, ==, "greetings from test_delay_apply");
498 g_settings_get (settings2
, "greeting", "s", &str
);
499 g_assert_cmpstr (str
, ==, "top o' the morning");
503 g_assert (g_settings_get_has_unapplied (settings
));
504 g_assert (!g_settings_get_has_unapplied (settings2
));
506 changed_cb_called
= FALSE
;
507 changed_cb_called2
= FALSE
;
509 g_settings_apply (settings
);
511 g_assert (!changed_cb_called
);
512 g_assert (changed_cb_called2
);
514 g_settings_get (settings
, "greeting", "s", &str
);
515 g_assert_cmpstr (str
, ==, "greetings from test_delay_apply");
519 g_settings_get (settings2
, "greeting", "s", &str
);
520 g_assert_cmpstr (str
, ==, "greetings from test_delay_apply");
524 g_assert (!g_settings_get_has_unapplied (settings
));
525 g_assert (!g_settings_get_has_unapplied (settings2
));
527 g_settings_reset (settings
, "greeting");
528 g_settings_apply (settings
);
530 g_settings_get (settings
, "greeting", "s", &str
);
531 g_assert_cmpstr (str
, ==, "Hello, earthlings");
534 g_object_unref (settings2
);
535 g_object_unref (settings
);
538 /* Test that reverting unapplied changes in a delay-apply
539 * settings instance works.
542 test_delay_revert (void)
545 GSettings
*settings2
;
548 settings
= g_settings_new ("org.gtk.test");
549 settings2
= g_settings_new ("org.gtk.test");
551 g_settings_set (settings2
, "greeting", "s", "top o' the morning");
553 g_settings_get (settings
, "greeting", "s", &str
);
554 g_assert_cmpstr (str
, ==, "top o' the morning");
557 g_settings_delay (settings
);
559 g_settings_set (settings
, "greeting", "s", "greetings from test_delay_revert");
561 g_settings_get (settings
, "greeting", "s", &str
);
562 g_assert_cmpstr (str
, ==, "greetings from test_delay_revert");
566 g_settings_get (settings2
, "greeting", "s", &str
);
567 g_assert_cmpstr (str
, ==, "top o' the morning");
571 g_assert (g_settings_get_has_unapplied (settings
));
573 g_settings_revert (settings
);
575 g_assert (!g_settings_get_has_unapplied (settings
));
577 g_settings_get (settings
, "greeting", "s", &str
);
578 g_assert_cmpstr (str
, ==, "top o' the morning");
582 g_settings_get (settings2
, "greeting", "s", &str
);
583 g_assert_cmpstr (str
, ==, "top o' the morning");
587 g_object_unref (settings2
);
588 g_object_unref (settings
);
592 test_delay_child (void)
600 base
= g_settings_new ("org.gtk.test.basic-types");
601 g_settings_set (base
, "test-byte", "y", 36);
603 settings
= g_settings_new ("org.gtk.test");
604 g_settings_delay (settings
);
605 g_object_get (settings
, "delay-apply", &delay
, NULL
);
608 child
= g_settings_get_child (settings
, "basic-types");
609 g_assert (child
!= NULL
);
611 g_object_get (child
, "delay-apply", &delay
, NULL
);
614 g_settings_get (child
, "test-byte", "y", &byte
);
615 g_assert_cmpuint (byte
, ==, 36);
617 g_settings_set (child
, "test-byte", "y", 42);
619 /* make sure the child was delayed too */
620 g_settings_get (base
, "test-byte", "y", &byte
);
621 g_assert_cmpuint (byte
, ==, 36);
623 g_object_unref (child
);
624 g_object_unref (settings
);
625 g_object_unref (base
);
629 keys_changed_cb (GSettings
*settings
,
635 g_assert_cmpint (n_keys
, ==, 2);
637 g_assert ((keys
[0] == g_quark_from_static_string ("greeting") &&
638 keys
[1] == g_quark_from_static_string ("farewell")) ||
639 (keys
[1] == g_quark_from_static_string ("greeting") &&
640 keys
[0] == g_quark_from_static_string ("farewell")));
642 g_settings_get (settings
, "greeting", "s", &str
);
643 g_assert_cmpstr (str
, ==, "greetings from test_atomic");
647 g_settings_get (settings
, "farewell", "s", &str
);
648 g_assert_cmpstr (str
, ==, "atomic bye-bye");
653 /* Check that delay-applied changes appear atomically.
654 * More specifically, verify that all changed keys appear
655 * with their new value while handling the change-event signal.
661 GSettings
*settings2
;
664 settings
= g_settings_new ("org.gtk.test");
665 settings2
= g_settings_new ("org.gtk.test");
667 g_settings_set (settings2
, "greeting", "s", "top o' the morning");
669 changed_cb_called
= FALSE
;
670 changed_cb_called2
= FALSE
;
672 g_signal_connect (settings2
, "change-event",
673 G_CALLBACK (keys_changed_cb
), NULL
);
675 g_settings_delay (settings
);
677 g_settings_set (settings
, "greeting", "s", "greetings from test_atomic");
678 g_settings_set (settings
, "farewell", "s", "atomic bye-bye");
680 g_settings_apply (settings
);
682 g_settings_get (settings
, "greeting", "s", &str
);
683 g_assert_cmpstr (str
, ==, "greetings from test_atomic");
687 g_settings_get (settings
, "farewell", "s", &str
);
688 g_assert_cmpstr (str
, ==, "atomic bye-bye");
692 g_settings_get (settings2
, "greeting", "s", &str
);
693 g_assert_cmpstr (str
, ==, "greetings from test_atomic");
697 g_settings_get (settings2
, "farewell", "s", &str
);
698 g_assert_cmpstr (str
, ==, "atomic bye-bye");
702 g_object_unref (settings2
);
703 g_object_unref (settings
);
706 /* On Windows the interaction between the C library locale and libintl
707 * (from GNU gettext) is not like on POSIX, so just skip these tests
710 * There are several issues:
712 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
713 * in libintl (defined in its <libintl.h>).
715 * 2) The locale names that setlocale() accepts and returns aren't in
716 * the "de_DE" style, but like "German_Germany".
718 * 3) libintl looks at the Win32 thread locale and not the C library
719 * locale. (And even if libintl would use the C library's locale, as
720 * there are several alternative C library DLLs, libintl might be
721 * linked to a different one than the application code, so they
722 * wouldn't have the same C library locale anyway.)
725 /* Test that translations work for schema defaults.
727 * This test relies on the de.po file in the same directory
728 * to be compiled into ./de/LC_MESSAGES/test.mo
737 bindtextdomain ("test", locale_dir
);
738 bind_textdomain_codeset ("test", "UTF-8");
740 locale
= g_strdup (setlocale (LC_MESSAGES
, NULL
));
742 settings
= g_settings_new ("org.gtk.test.localized");
744 g_setenv ("LC_MESSAGES", "C", TRUE
);
745 setlocale (LC_MESSAGES
, "C");
746 str
= g_settings_get_string (settings
, "error-message");
747 g_setenv ("LC_MESSAGES", locale
, TRUE
);
748 setlocale (LC_MESSAGES
, locale
);
750 g_assert_cmpstr (str
, ==, "Unnamed");
754 g_setenv ("LC_MESSAGES", "de_DE.UTF-8", TRUE
);
755 setlocale (LC_MESSAGES
, "de_DE.UTF-8");
756 /* Only do the test if translation is actually working... */
757 if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
759 str
= g_settings_get_string (settings
, "error-message");
761 g_assert_cmpstr (str
, ==, "Unbenannt");
766 g_printerr ("warning: translation is not working... skipping test. ");
768 g_setenv ("LC_MESSAGES", locale
, TRUE
);
769 setlocale (LC_MESSAGES
, locale
);
771 g_object_unref (settings
);
774 /* Test that message context works as expected with translated
775 * schema defaults. Also, verify that non-ASCII UTF-8 content
778 * This test relies on the de.po file in the same directory
779 * to be compiled into ./de/LC_MESSAGES/test.mo
782 test_l10n_context (void)
788 bindtextdomain ("test", locale_dir
);
789 bind_textdomain_codeset ("test", "UTF-8");
791 locale
= g_strdup (setlocale (LC_MESSAGES
, NULL
));
793 settings
= g_settings_new ("org.gtk.test.localized");
795 g_setenv ("LC_MESSAGES", "C", TRUE
);
796 setlocale (LC_MESSAGES
, "C");
797 g_settings_get (settings
, "backspace", "s", &str
);
798 g_setenv ("LC_MESSAGES", locale
, TRUE
);
799 setlocale (LC_MESSAGES
, locale
);
801 g_assert_cmpstr (str
, ==, "BackSpace");
805 g_setenv ("LC_MESSAGES", "de_DE.UTF-8", TRUE
);
806 setlocale (LC_MESSAGES
, "de_DE.UTF-8");
807 /* Only do the test if translation is actually working... */
808 if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
810 g_settings_get (settings
, "backspace", "s", &str
);
812 g_assert_cmpstr (str
, ==, "Löschen");
817 g_printerr ("warning: translation is not working... skipping test. ");
819 g_setenv ("LC_MESSAGES", locale
, TRUE
);
820 setlocale (LC_MESSAGES
, locale
);
822 g_object_unref (settings
);
848 GObject parent_instance
;
851 gboolean anti_bool_prop
;
862 gchar
*no_write_prop
;
870 GObjectClass parent_class
;
873 static GType
test_object_get_type (void);
874 G_DEFINE_TYPE (TestObject
, test_object
, G_TYPE_OBJECT
)
877 test_object_init (TestObject
*object
)
882 test_object_finalize (GObject
*object
)
884 TestObject
*testo
= (TestObject
*)object
;
885 g_strfreev (testo
->strv_prop
);
886 g_free (testo
->string_prop
);
887 G_OBJECT_CLASS (test_object_parent_class
)->finalize (object
);
891 test_object_get_property (GObject
*object
,
896 TestObject
*test_object
= (TestObject
*)object
;
901 g_value_set_boolean (value
, test_object
->bool_prop
);
904 g_value_set_boolean (value
, test_object
->anti_bool_prop
);
907 g_value_set_schar (value
, test_object
->byte_prop
);
910 g_value_set_uint (value
, test_object
->uint16_prop
);
913 g_value_set_int (value
, test_object
->int16_prop
);
916 g_value_set_int (value
, test_object
->int_prop
);
919 g_value_set_uint (value
, test_object
->uint_prop
);
922 g_value_set_int64 (value
, test_object
->int64_prop
);
925 g_value_set_uint64 (value
, test_object
->uint64_prop
);
928 g_value_set_double (value
, test_object
->double_prop
);
931 g_value_set_string (value
, test_object
->string_prop
);
934 g_value_set_string (value
, test_object
->no_write_prop
);
937 g_value_set_boxed (value
, test_object
->strv_prop
);
940 g_value_set_enum (value
, test_object
->enum_prop
);
943 g_value_set_flags (value
, test_object
->flags_prop
);
946 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
952 test_object_set_property (GObject
*object
,
957 TestObject
*test_object
= (TestObject
*)object
;
962 test_object
->bool_prop
= g_value_get_boolean (value
);
965 test_object
->anti_bool_prop
= g_value_get_boolean (value
);
968 test_object
->byte_prop
= g_value_get_schar (value
);
971 test_object
->int16_prop
= g_value_get_int (value
);
974 test_object
->uint16_prop
= g_value_get_uint (value
);
977 test_object
->int_prop
= g_value_get_int (value
);
980 test_object
->uint_prop
= g_value_get_uint (value
);
983 test_object
->int64_prop
= g_value_get_int64 (value
);
986 test_object
->uint64_prop
= g_value_get_uint64 (value
);
989 test_object
->double_prop
= g_value_get_double (value
);
992 g_free (test_object
->string_prop
);
993 test_object
->string_prop
= g_value_dup_string (value
);
996 g_free (test_object
->no_read_prop
);
997 test_object
->no_read_prop
= g_value_dup_string (value
);
1000 g_strfreev (test_object
->strv_prop
);
1001 test_object
->strv_prop
= g_value_dup_boxed (value
);
1004 test_object
->enum_prop
= g_value_get_enum (value
);
1007 test_object
->flags_prop
= g_value_get_flags (value
);
1010 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
1016 test_enum_get_type (void)
1018 static volatile gsize define_type_id
= 0;
1020 if (g_once_init_enter (&define_type_id
))
1022 static const GEnumValue values
[] = {
1023 { TEST_ENUM_FOO
, "TEST_ENUM_FOO", "foo" },
1024 { TEST_ENUM_BAR
, "TEST_ENUM_BAR", "bar" },
1025 { TEST_ENUM_BAZ
, "TEST_ENUM_BAZ", "baz" },
1026 { TEST_ENUM_QUUX
, "TEST_ENUM_QUUX", "quux" },
1030 GType type_id
= g_enum_register_static ("TestEnum", values
);
1031 g_once_init_leave (&define_type_id
, type_id
);
1034 return define_type_id
;
1038 test_flags_get_type (void)
1040 static volatile gsize define_type_id
= 0;
1042 if (g_once_init_enter (&define_type_id
))
1044 static const GFlagsValue values
[] = {
1045 { TEST_FLAGS_NONE
, "TEST_FLAGS_NONE", "none" },
1046 { TEST_FLAGS_MOURNING
, "TEST_FLAGS_MOURNING", "mourning" },
1047 { TEST_FLAGS_LAUGHING
, "TEST_FLAGS_LAUGHING", "laughing" },
1048 { TEST_FLAGS_WALKING
, "TEST_FLAGS_WALKING", "walking" },
1052 GType type_id
= g_flags_register_static ("TestFlags", values
);
1053 g_once_init_leave (&define_type_id
, type_id
);
1056 return define_type_id
;
1060 test_object_class_init (TestObjectClass
*class)
1062 GObjectClass
*gobject_class
= G_OBJECT_CLASS (class);
1064 gobject_class
->get_property
= test_object_get_property
;
1065 gobject_class
->set_property
= test_object_set_property
;
1066 gobject_class
->finalize
= test_object_finalize
;
1068 g_object_class_install_property (gobject_class
, PROP_BOOL
,
1069 g_param_spec_boolean ("bool", "", "", FALSE
, G_PARAM_READWRITE
));
1070 g_object_class_install_property (gobject_class
, PROP_ANTI_BOOL
,
1071 g_param_spec_boolean ("anti-bool", "", "", FALSE
, G_PARAM_READWRITE
));
1072 g_object_class_install_property (gobject_class
, PROP_BYTE
,
1073 g_param_spec_char ("byte", "", "", G_MININT8
, G_MAXINT8
, 0, G_PARAM_READWRITE
));
1074 g_object_class_install_property (gobject_class
, PROP_INT16
,
1075 g_param_spec_int ("int16", "", "", -G_MAXINT16
, G_MAXINT16
, 0, G_PARAM_READWRITE
));
1076 g_object_class_install_property (gobject_class
, PROP_UINT16
,
1077 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16
, 0, G_PARAM_READWRITE
));
1078 g_object_class_install_property (gobject_class
, PROP_INT
,
1079 g_param_spec_int ("int", "", "", G_MININT
, G_MAXINT
, 0, G_PARAM_READWRITE
));
1080 g_object_class_install_property (gobject_class
, PROP_UINT
,
1081 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT
, 0, G_PARAM_READWRITE
));
1082 g_object_class_install_property (gobject_class
, PROP_INT64
,
1083 g_param_spec_int64 ("int64", "", "", G_MININT64
, G_MAXINT64
, 0, G_PARAM_READWRITE
));
1084 g_object_class_install_property (gobject_class
, PROP_UINT64
,
1085 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64
, 0, G_PARAM_READWRITE
));
1086 g_object_class_install_property (gobject_class
, PROP_DOUBLE
,
1087 g_param_spec_double ("double", "", "", -G_MAXDOUBLE
, G_MAXDOUBLE
, 0.0, G_PARAM_READWRITE
));
1088 g_object_class_install_property (gobject_class
, PROP_STRING
,
1089 g_param_spec_string ("string", "", "", NULL
, G_PARAM_READWRITE
));
1090 g_object_class_install_property (gobject_class
, PROP_NO_WRITE
,
1091 g_param_spec_string ("no-write", "", "", NULL
, G_PARAM_READABLE
));
1092 g_object_class_install_property (gobject_class
, PROP_NO_READ
,
1093 g_param_spec_string ("no-read", "", "", NULL
, G_PARAM_WRITABLE
));
1094 g_object_class_install_property (gobject_class
, PROP_STRV
,
1095 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV
, G_PARAM_READWRITE
));
1096 g_object_class_install_property (gobject_class
, PROP_ENUM
,
1097 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO
, G_PARAM_READWRITE
));
1098 g_object_class_install_property (gobject_class
, PROP_FLAGS
,
1099 g_param_spec_flags ("flags", "", "", test_flags_get_type (), TEST_FLAGS_NONE
, G_PARAM_READWRITE
));
1103 test_object_new (void)
1105 return (TestObject
*)g_object_new (test_object_get_type (), NULL
);
1108 /* Test basic binding functionality for simple types.
1109 * Verify that with bidirectional bindings, changes on either side
1110 * are notified on the other end.
1113 test_simple_binding (void)
1116 GSettings
*settings
;
1132 settings
= g_settings_new ("org.gtk.test.binding");
1133 obj
= test_object_new ();
1135 g_settings_bind (settings
, "bool", obj
, "bool", G_SETTINGS_BIND_DEFAULT
);
1136 g_object_set (obj
, "bool", TRUE
, NULL
);
1137 g_assert_cmpint (g_settings_get_boolean (settings
, "bool"), ==, TRUE
);
1139 g_settings_set_boolean (settings
, "bool", FALSE
);
1141 g_object_get (obj
, "bool", &b
, NULL
);
1142 g_assert_cmpint (b
, ==, FALSE
);
1144 g_settings_bind (settings
, "anti-bool", obj
, "anti-bool",
1145 G_SETTINGS_BIND_INVERT_BOOLEAN
);
1146 g_object_set (obj
, "anti-bool", FALSE
, NULL
);
1147 g_assert_cmpint (g_settings_get_boolean (settings
, "anti-bool"), ==, TRUE
);
1149 g_settings_set_boolean (settings
, "anti-bool", FALSE
);
1151 g_object_get (obj
, "anti-bool", &b
, NULL
);
1152 g_assert_cmpint (b
, ==, TRUE
);
1154 g_settings_bind (settings
, "byte", obj
, "byte", G_SETTINGS_BIND_DEFAULT
);
1156 g_object_set (obj
, "byte", 123, NULL
);
1158 g_settings_get (settings
, "byte", "y", &y
);
1159 g_assert_cmpint (y
, ==, 123);
1161 g_settings_set (settings
, "byte", "y", 54);
1163 g_object_get (obj
, "byte", &y
, NULL
);
1164 g_assert_cmpint (y
, ==, 54);
1166 g_settings_bind (settings
, "int16", obj
, "int16", G_SETTINGS_BIND_DEFAULT
);
1168 g_object_set (obj
, "int16", 1234, NULL
);
1170 g_settings_get (settings
, "int16", "n", &n
);
1171 g_assert_cmpint (n
, ==, 1234);
1173 g_settings_set (settings
, "int16", "n", 4321);
1175 g_object_get (obj
, "int16", &n2
, NULL
);
1176 g_assert_cmpint (n2
, ==, 4321);
1178 g_settings_bind (settings
, "uint16", obj
, "uint16", G_SETTINGS_BIND_DEFAULT
);
1180 g_object_set (obj
, "uint16", (guint16
) G_MAXUINT16
, NULL
);
1182 g_settings_get (settings
, "uint16", "q", &q
);
1183 g_assert_cmpuint (q
, ==, G_MAXUINT16
);
1185 g_settings_set (settings
, "uint16", "q", (guint16
) G_MAXINT16
);
1187 g_object_get (obj
, "uint16", &q2
, NULL
);
1188 g_assert_cmpuint (q2
, ==, (guint16
) G_MAXINT16
);
1190 g_settings_bind (settings
, "int", obj
, "int", G_SETTINGS_BIND_DEFAULT
);
1192 g_object_set (obj
, "int", 12345, NULL
);
1193 g_assert_cmpint (g_settings_get_int (settings
, "int"), ==, 12345);
1195 g_settings_set_int (settings
, "int", 54321);
1197 g_object_get (obj
, "int", &i
, NULL
);
1198 g_assert_cmpint (i
, ==, 54321);
1200 g_settings_bind (settings
, "uint", obj
, "uint", G_SETTINGS_BIND_DEFAULT
);
1202 g_object_set (obj
, "uint", 12345, NULL
);
1203 g_assert_cmpuint (g_settings_get_uint (settings
, "uint"), ==, 12345);
1205 g_settings_set_uint (settings
, "uint", 54321);
1207 g_object_get (obj
, "uint", &u
, NULL
);
1208 g_assert_cmpuint (u
, ==, 54321);
1210 g_settings_bind (settings
, "uint64", obj
, "uint64", G_SETTINGS_BIND_DEFAULT
);
1212 g_object_set (obj
, "uint64", (guint64
) 12345, NULL
);
1213 g_assert_cmpuint (g_settings_get_uint64 (settings
, "uint64"), ==, 12345);
1215 g_settings_set_uint64 (settings
, "uint64", 54321);
1217 g_object_get (obj
, "uint64", &u64
, NULL
);
1218 g_assert_cmpuint (u64
, ==, 54321);
1220 g_settings_bind (settings
, "int64", obj
, "int64", G_SETTINGS_BIND_DEFAULT
);
1222 g_object_set (obj
, "int64", (gint64
) G_MAXINT64
, NULL
);
1224 g_settings_get (settings
, "int64", "x", &i64
);
1225 g_assert_cmpint (i64
, ==, G_MAXINT64
);
1227 g_settings_set (settings
, "int64", "x", (gint64
) G_MININT64
);
1229 g_object_get (obj
, "int64", &i64
, NULL
);
1230 g_assert_cmpint (i64
, ==, G_MININT64
);
1232 g_settings_bind (settings
, "uint64", obj
, "uint64", G_SETTINGS_BIND_DEFAULT
);
1234 g_object_set (obj
, "uint64", (guint64
) G_MAXUINT64
, NULL
);
1236 g_settings_get (settings
, "uint64", "t", &u64
);
1237 g_assert_cmpuint (u64
, ==, G_MAXUINT64
);
1239 g_settings_set (settings
, "uint64", "t", (guint64
) G_MAXINT64
);
1241 g_object_get (obj
, "uint64", &u64
, NULL
);
1242 g_assert_cmpuint (u64
, ==, (guint64
) G_MAXINT64
);
1244 g_settings_bind (settings
, "string", obj
, "string", G_SETTINGS_BIND_DEFAULT
);
1246 g_object_set (obj
, "string", "bu ba", NULL
);
1247 s
= g_settings_get_string (settings
, "string");
1248 g_assert_cmpstr (s
, ==, "bu ba");
1251 g_settings_set_string (settings
, "string", "bla bla");
1252 g_object_get (obj
, "string", &s
, NULL
);
1253 g_assert_cmpstr (s
, ==, "bla bla");
1256 g_settings_bind (settings
, "chararray", obj
, "string", G_SETTINGS_BIND_DEFAULT
);
1258 g_object_set (obj
, "string", "non-unicode:\315", NULL
);
1259 value
= g_settings_get_value (settings
, "chararray");
1260 g_assert_cmpstr (g_variant_get_bytestring (value
), ==, "non-unicode:\315");
1261 g_variant_unref (value
);
1263 g_settings_bind (settings
, "double", obj
, "double", G_SETTINGS_BIND_DEFAULT
);
1265 g_object_set (obj
, "double", G_MAXFLOAT
, NULL
);
1266 g_assert_cmpfloat (g_settings_get_double (settings
, "double"), ==, G_MAXFLOAT
);
1268 g_settings_set_double (settings
, "double", G_MINFLOAT
);
1270 g_object_get (obj
, "double", &d
, NULL
);
1271 g_assert_cmpfloat (d
, ==, G_MINFLOAT
);
1273 g_object_set (obj
, "double", G_MAXDOUBLE
, NULL
);
1274 g_assert_cmpfloat (g_settings_get_double (settings
, "double"), ==, G_MAXDOUBLE
);
1276 g_settings_set_double (settings
, "double", -G_MINDOUBLE
);
1278 g_object_get (obj
, "double", &d
, NULL
);
1279 g_assert_cmpfloat (d
, ==, -G_MINDOUBLE
);
1281 strv
= g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1282 g_settings_bind (settings
, "strv", obj
, "strv", G_SETTINGS_BIND_DEFAULT
);
1283 g_object_set (obj
, "strv", strv
, NULL
);
1285 strv
= g_settings_get_strv (settings
, "strv");
1286 s
= g_strjoinv (",", strv
);
1287 g_assert_cmpstr (s
, ==, "plastic bag,middle class,polyethylene");
1290 strv
= g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1291 g_settings_set_strv (settings
, "strv", (const gchar
**) strv
);
1293 g_object_get (obj
, "strv", &strv
, NULL
);
1294 s
= g_strjoinv (",", strv
);
1295 g_assert_cmpstr (s
, ==, "decaffeinate,unleaded,keep all surfaces clean");
1298 g_settings_set_strv (settings
, "strv", NULL
);
1299 g_object_get (obj
, "strv", &strv
, NULL
);
1300 g_assert (strv
!= NULL
);
1301 g_assert_cmpint (g_strv_length (strv
), ==, 0);
1304 g_settings_bind (settings
, "enum", obj
, "enum", G_SETTINGS_BIND_DEFAULT
);
1305 g_object_set (obj
, "enum", TEST_ENUM_BAZ
, NULL
);
1306 s
= g_settings_get_string (settings
, "enum");
1307 g_assert_cmpstr (s
, ==, "baz");
1309 g_assert_cmpint (g_settings_get_enum (settings
, "enum"), ==, TEST_ENUM_BAZ
);
1311 g_settings_set_enum (settings
, "enum", TEST_ENUM_QUUX
);
1313 g_object_get (obj
, "enum", &i
, NULL
);
1314 g_assert_cmpint (i
, ==, TEST_ENUM_QUUX
);
1316 g_settings_set_string (settings
, "enum", "baz");
1318 g_object_get (obj
, "enum", &i
, NULL
);
1319 g_assert_cmpint (i
, ==, TEST_ENUM_BAZ
);
1321 g_settings_bind (settings
, "flags", obj
, "flags", G_SETTINGS_BIND_DEFAULT
);
1322 g_object_set (obj
, "flags", TEST_FLAGS_MOURNING
, NULL
);
1323 strv
= g_settings_get_strv (settings
, "flags");
1324 g_assert_cmpint (g_strv_length (strv
), ==, 1);
1325 g_assert_cmpstr (strv
[0], ==, "mourning");
1328 g_assert_cmpint (g_settings_get_flags (settings
, "flags"), ==, TEST_FLAGS_MOURNING
);
1330 g_settings_set_flags (settings
, "flags", TEST_FLAGS_MOURNING
| TEST_FLAGS_WALKING
);
1332 g_object_get (obj
, "flags", &i
, NULL
);
1333 g_assert_cmpint (i
, ==, TEST_FLAGS_MOURNING
| TEST_FLAGS_WALKING
);
1335 g_settings_bind (settings
, "uint", obj
, "uint", G_SETTINGS_BIND_DEFAULT
);
1337 g_object_set (obj
, "uint", 12345, NULL
);
1338 g_assert_cmpuint (g_settings_get_uint (settings
, "uint"), ==, 12345);
1340 g_settings_set_uint (settings
, "uint", 54321);
1342 g_object_get (obj
, "uint", &u
, NULL
);
1343 g_assert_cmpuint (u
, ==, 54321);
1345 g_settings_bind (settings
, "range", obj
, "uint", G_SETTINGS_BIND_DEFAULT
);
1346 g_object_set (obj
, "uint", 22, NULL
);
1348 g_assert_cmpuint (g_settings_get_uint (settings
, "range"), ==, 22);
1349 g_object_get (obj
, "uint", &u
, NULL
);
1350 g_assert_cmpuint (u
, ==, 22);
1352 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
,
1353 "* is out of schema-specified range for*");
1354 g_object_set (obj
, "uint", 45, NULL
);
1355 g_test_assert_expected_messages ();
1357 g_object_get (obj
, "uint", &u
, NULL
);
1358 g_assert_cmpuint (g_settings_get_uint (settings
, "range"), ==, 22);
1359 /* The value of the object is currently not reset back to its initial value
1360 g_assert_cmpuint (u, ==, 22); */
1362 g_object_unref (obj
);
1363 g_object_unref (settings
);
1370 GSettings
*settings
;
1372 settings
= g_settings_new ("org.gtk.test.binding");
1373 obj
= test_object_new ();
1375 g_settings_bind (settings
, "int", obj
, "int", G_SETTINGS_BIND_DEFAULT
);
1377 g_object_set (obj
, "int", 12345, NULL
);
1378 g_assert_cmpint (g_settings_get_int (settings
, "int"), ==, 12345);
1380 g_settings_unbind (obj
, "int");
1382 g_object_set (obj
, "int", 54321, NULL
);
1383 g_assert_cmpint (g_settings_get_int (settings
, "int"), ==, 12345);
1385 g_object_unref (obj
);
1386 g_object_unref (settings
);
1390 test_bind_writable (void)
1393 GSettings
*settings
;
1396 settings
= g_settings_new ("org.gtk.test.binding");
1397 obj
= test_object_new ();
1399 g_object_set (obj
, "bool", FALSE
, NULL
);
1401 g_settings_bind_writable (settings
, "int", obj
, "bool", FALSE
);
1403 g_object_get (obj
, "bool", &b
, NULL
);
1406 g_settings_unbind (obj
, "bool");
1408 g_settings_bind_writable (settings
, "int", obj
, "bool", TRUE
);
1410 g_object_get (obj
, "bool", &b
, NULL
);
1413 g_object_unref (obj
);
1414 g_object_unref (settings
);
1417 /* Test one-way bindings.
1418 * Verify that changes on one side show up on the other,
1419 * but not vice versa
1422 test_directional_binding (void)
1425 GSettings
*settings
;
1429 settings
= g_settings_new ("org.gtk.test.binding");
1430 obj
= test_object_new ();
1432 g_object_set (obj
, "bool", FALSE
, NULL
);
1433 g_settings_set_boolean (settings
, "bool", FALSE
);
1435 g_settings_bind (settings
, "bool", obj
, "bool", G_SETTINGS_BIND_GET
);
1437 g_settings_set_boolean (settings
, "bool", TRUE
);
1438 g_object_get (obj
, "bool", &b
, NULL
);
1439 g_assert_cmpint (b
, ==, TRUE
);
1441 g_object_set (obj
, "bool", FALSE
, NULL
);
1442 g_assert_cmpint (g_settings_get_boolean (settings
, "bool"), ==, TRUE
);
1444 g_object_set (obj
, "int", 20, NULL
);
1445 g_settings_set_int (settings
, "int", 20);
1447 g_settings_bind (settings
, "int", obj
, "int", G_SETTINGS_BIND_SET
);
1449 g_object_set (obj
, "int", 32, NULL
);
1450 g_assert_cmpint (g_settings_get_int (settings
, "int"), ==, 32);
1452 g_settings_set_int (settings
, "int", 20);
1453 g_object_get (obj
, "int", &i
, NULL
);
1454 g_assert_cmpint (i
, ==, 32);
1456 g_object_unref (obj
);
1457 g_object_unref (settings
);
1460 /* Test that type mismatch is caught when creating a binding */
1462 test_typesafe_binding (void)
1464 if (!g_test_undefined ())
1467 if (g_test_subprocess ())
1470 GSettings
*settings
;
1472 settings
= g_settings_new ("org.gtk.test.binding");
1473 obj
= test_object_new ();
1475 g_settings_bind (settings
, "string", obj
, "int", G_SETTINGS_BIND_DEFAULT
);
1477 g_object_unref (obj
);
1478 g_object_unref (settings
);
1481 g_test_trap_subprocess (NULL
, 0, 0);
1482 g_test_trap_assert_failed ();
1483 g_test_trap_assert_stderr ("*not compatible*");
1487 string_to_bool (GValue
*value
,
1493 s
= g_variant_get_string (variant
, NULL
);
1494 g_value_set_boolean (value
, g_strcmp0 (s
, "true") == 0);
1500 bool_to_string (const GValue
*value
,
1501 const GVariantType
*expected_type
,
1504 if (g_value_get_boolean (value
))
1505 return g_variant_new_string ("true");
1507 return g_variant_new_string ("false");
1511 bool_to_bool (const GValue
*value
,
1512 const GVariantType
*expected_type
,
1515 return g_variant_new_boolean (g_value_get_boolean (value
));
1518 /* Test custom bindings.
1519 * Translate strings to booleans and back
1522 test_custom_binding (void)
1525 GSettings
*settings
;
1529 settings
= g_settings_new ("org.gtk.test.binding");
1530 obj
= test_object_new ();
1532 g_settings_set_string (settings
, "string", "true");
1534 g_settings_bind_with_mapping (settings
, "string",
1536 G_SETTINGS_BIND_DEFAULT
,
1541 g_settings_set_string (settings
, "string", "false");
1542 g_object_get (obj
, "bool", &b
, NULL
);
1543 g_assert_cmpint (b
, ==, FALSE
);
1545 g_settings_set_string (settings
, "string", "not true");
1546 g_object_get (obj
, "bool", &b
, NULL
);
1547 g_assert_cmpint (b
, ==, FALSE
);
1549 g_object_set (obj
, "bool", TRUE
, NULL
);
1550 s
= g_settings_get_string (settings
, "string");
1551 g_assert_cmpstr (s
, ==, "true");
1554 g_settings_bind_with_mapping (settings
, "string",
1556 G_SETTINGS_BIND_DEFAULT
,
1557 string_to_bool
, bool_to_bool
,
1559 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
,
1560 "*binding mapping function for key 'string' returned"
1561 " GVariant of type 'b' when type 's' was requested*");
1562 g_object_set (obj
, "bool", FALSE
, NULL
);
1563 g_test_assert_expected_messages ();
1565 g_object_unref (obj
);
1566 g_object_unref (settings
);
1569 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1570 * initial settings value is transported to the object
1571 * side, but later settings changes do not affect the
1575 test_no_change_binding (void)
1578 GSettings
*settings
;
1581 settings
= g_settings_new ("org.gtk.test.binding");
1582 obj
= test_object_new ();
1584 g_object_set (obj
, "bool", TRUE
, NULL
);
1585 g_settings_set_boolean (settings
, "bool", FALSE
);
1587 g_settings_bind (settings
, "bool", obj
, "bool", G_SETTINGS_BIND_GET_NO_CHANGES
);
1589 g_object_get (obj
, "bool", &b
, NULL
);
1590 g_assert_cmpint (b
, ==, FALSE
);
1592 g_settings_set_boolean (settings
, "bool", TRUE
);
1593 g_object_get (obj
, "bool", &b
, NULL
);
1594 g_assert_cmpint (b
, ==, FALSE
);
1596 g_settings_set_boolean (settings
, "bool", FALSE
);
1597 g_object_set (obj
, "bool", TRUE
, NULL
);
1598 b
= g_settings_get_boolean (settings
, "bool");
1599 g_assert_cmpint (b
, ==, TRUE
);
1601 g_object_unref (obj
);
1602 g_object_unref (settings
);
1605 /* Test that binding a non-readable property only
1606 * works in 'GET' mode.
1609 test_no_read_binding_fail (void)
1612 GSettings
*settings
;
1614 settings
= g_settings_new ("org.gtk.test.binding");
1615 obj
= test_object_new ();
1617 g_settings_bind (settings
, "string", obj
, "no-read", 0);
1621 test_no_read_binding_pass (void)
1624 GSettings
*settings
;
1626 settings
= g_settings_new ("org.gtk.test.binding");
1627 obj
= test_object_new ();
1629 g_settings_bind (settings
, "string", obj
, "no-read", G_SETTINGS_BIND_GET
);
1635 test_no_read_binding (void)
1637 if (g_test_undefined ())
1639 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/fail", 0, 0);
1640 g_test_trap_assert_failed ();
1641 g_test_trap_assert_stderr ("*property*is not readable*");
1644 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/pass", 0, 0);
1645 g_test_trap_assert_passed ();
1648 /* Test that binding a non-writable property only
1649 * works in 'SET' mode.
1652 test_no_write_binding_fail (void)
1655 GSettings
*settings
;
1657 settings
= g_settings_new ("org.gtk.test.binding");
1658 obj
= test_object_new ();
1660 g_settings_bind (settings
, "string", obj
, "no-write", 0);
1664 test_no_write_binding_pass (void)
1667 GSettings
*settings
;
1669 settings
= g_settings_new ("org.gtk.test.binding");
1670 obj
= test_object_new ();
1672 g_settings_bind (settings
, "string", obj
, "no-write", G_SETTINGS_BIND_SET
);
1678 test_no_write_binding (void)
1680 if (g_test_undefined ())
1682 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/fail", 0, 0);
1683 g_test_trap_assert_failed ();
1684 g_test_trap_assert_stderr ("*property*is not writable*");
1687 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/pass", 0, 0);
1688 g_test_trap_assert_passed ();
1692 key_changed_cb (GSettings
*settings
, const gchar
*key
, gpointer data
)
1699 * Test that using a keyfile works
1704 GSettingsBackend
*kf_backend
;
1705 GSettings
*settings
;
1709 GError
*error
= NULL
;
1712 gboolean called
= FALSE
;
1714 g_remove ("keyfile/gsettings.store");
1715 g_rmdir ("keyfile");
1717 kf_backend
= g_keyfile_settings_backend_new ("keyfile/gsettings.store", "/", "root");
1718 settings
= g_settings_new_with_backend ("org.gtk.test", kf_backend
);
1719 g_object_unref (kf_backend
);
1721 g_settings_reset (settings
, "greeting");
1722 str
= g_settings_get_string (settings
, "greeting");
1723 g_assert_cmpstr (str
, ==, "Hello, earthlings");
1726 writable
= g_settings_is_writable (settings
, "greeting");
1727 g_assert (writable
);
1728 g_settings_set (settings
, "greeting", "s", "see if this works");
1730 str
= g_settings_get_string (settings
, "greeting");
1731 g_assert_cmpstr (str
, ==, "see if this works");
1734 g_settings_delay (settings
);
1735 g_settings_set (settings
, "farewell", "s", "cheerio");
1736 g_settings_apply (settings
);
1738 keyfile
= g_key_file_new ();
1739 g_assert (g_key_file_load_from_file (keyfile
, "keyfile/gsettings.store", 0, NULL
));
1741 str
= g_key_file_get_string (keyfile
, "tests", "greeting", NULL
);
1742 g_assert_cmpstr (str
, ==, "'see if this works'");
1745 str
= g_key_file_get_string (keyfile
, "tests", "farewell", NULL
);
1746 g_assert_cmpstr (str
, ==, "'cheerio'");
1748 g_key_file_free (keyfile
);
1750 g_settings_reset (settings
, "greeting");
1751 g_settings_apply (settings
);
1752 keyfile
= g_key_file_new ();
1753 g_assert (g_key_file_load_from_file (keyfile
, "keyfile/gsettings.store", 0, NULL
));
1755 str
= g_key_file_get_string (keyfile
, "tests", "greeting", NULL
);
1756 g_assert (str
== NULL
);
1759 g_signal_connect (settings
, "changed::greeting", G_CALLBACK (key_changed_cb
), &called
);
1761 g_key_file_set_string (keyfile
, "tests", "greeting", "'howdy'");
1762 data
= g_key_file_to_data (keyfile
, &len
, NULL
);
1763 g_file_set_contents ("keyfile/gsettings.store", data
, len
, &error
);
1764 g_assert_no_error (error
);
1766 g_main_context_iteration (NULL
, FALSE
);
1767 g_signal_handlers_disconnect_by_func (settings
, key_changed_cb
, &called
);
1769 str
= g_settings_get_string (settings
, "greeting");
1770 g_assert_cmpstr (str
, ==, "howdy");
1773 g_settings_set (settings
, "farewell", "s", "cheerio");
1776 g_signal_connect (settings
, "writable-changed::greeting", G_CALLBACK (key_changed_cb
), &called
);
1778 g_chmod ("keyfile", 0500);
1780 g_main_context_iteration (NULL
, FALSE
);
1781 g_signal_handlers_disconnect_by_func (settings
, key_changed_cb
, &called
);
1783 writable
= g_settings_is_writable (settings
, "greeting");
1784 g_assert (!writable
);
1786 g_key_file_free (keyfile
);
1789 g_object_unref (settings
);
1790 g_chmod ("keyfile", 0777);
1793 /* Test that getting child schemas works
1796 test_child_schema (void)
1798 GSettings
*settings
;
1802 /* first establish some known conditions */
1803 settings
= g_settings_new ("org.gtk.test.basic-types");
1804 g_settings_set (settings
, "test-byte", "y", 36);
1806 g_settings_get (settings
, "test-byte", "y", &byte
);
1807 g_assert_cmpint (byte
, ==, 36);
1809 g_object_unref (settings
);
1811 settings
= g_settings_new ("org.gtk.test");
1812 child
= g_settings_get_child (settings
, "basic-types");
1813 g_assert (child
!= NULL
);
1815 g_settings_get (child
, "test-byte", "y", &byte
);
1816 g_assert_cmpint (byte
, ==, 36);
1818 g_object_unref (child
);
1819 g_object_unref (settings
);
1822 #include "../strinfo.c"
1827 /* "foo" has a value of 1
1828 * "bar" has a value of 2
1829 * "baz" is an alias for "bar"
1832 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1833 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1835 const guint32
*strinfo
= (guint32
*) array
;
1836 guint length
= sizeof array
/ 4;
1840 /* build it and compare */
1843 builder
= g_string_new (NULL
);
1844 strinfo_builder_append_item (builder
, "foo", 1);
1845 strinfo_builder_append_item (builder
, "bar", 2);
1846 g_assert (strinfo_builder_append_alias (builder
, "baz", "bar"));
1847 g_assert_cmpmem (builder
->str
, builder
->len
, strinfo
, length
* 4);
1848 g_string_free (builder
, TRUE
);
1851 g_assert_cmpstr (strinfo_string_from_alias (strinfo
, length
, "foo"),
1853 g_assert_cmpstr (strinfo_string_from_alias (strinfo
, length
, "bar"),
1855 g_assert_cmpstr (strinfo_string_from_alias (strinfo
, length
, "baz"),
1857 g_assert_cmpstr (strinfo_string_from_alias (strinfo
, length
, "quux"),
1860 g_assert (strinfo_enum_from_string (strinfo
, length
, "foo", &result
));
1861 g_assert_cmpint (result
, ==, 1);
1862 g_assert (strinfo_enum_from_string (strinfo
, length
, "bar", &result
));
1863 g_assert_cmpint (result
, ==, 2);
1864 g_assert (!strinfo_enum_from_string (strinfo
, length
, "baz", &result
));
1865 g_assert (!strinfo_enum_from_string (strinfo
, length
, "quux", &result
));
1867 g_assert_cmpstr (strinfo_string_from_enum (strinfo
, length
, 0), ==, NULL
);
1868 g_assert_cmpstr (strinfo_string_from_enum (strinfo
, length
, 1), ==, "foo");
1869 g_assert_cmpstr (strinfo_string_from_enum (strinfo
, length
, 2), ==, "bar");
1870 g_assert_cmpstr (strinfo_string_from_enum (strinfo
, length
, 3), ==, NULL
);
1872 g_assert (strinfo_is_string_valid (strinfo
, length
, "foo"));
1873 g_assert (strinfo_is_string_valid (strinfo
, length
, "bar"));
1874 g_assert (!strinfo_is_string_valid (strinfo
, length
, "baz"));
1875 g_assert (!strinfo_is_string_valid (strinfo
, length
, "quux"));
1879 test_enums_non_enum_key (void)
1883 direct
= g_settings_new ("org.gtk.test.enums.direct");
1884 g_settings_get_enum (direct
, "test");
1885 g_assert_not_reached ();
1889 test_enums_non_enum_value (void)
1891 GSettings
*settings
;
1893 settings
= g_settings_new ("org.gtk.test.enums");
1894 g_settings_set_enum (settings
, "test", 42);
1895 g_assert_not_reached ();
1899 test_enums_range (void)
1901 GSettings
*settings
;
1903 settings
= g_settings_new ("org.gtk.test.enums");
1904 g_settings_set_string (settings
, "test", "qux");
1905 g_assert_not_reached ();
1909 test_enums_non_flags (void)
1911 GSettings
*settings
;
1913 settings
= g_settings_new ("org.gtk.test.enums");
1914 g_settings_get_flags (settings
, "test");
1915 g_assert_not_reached ();
1921 GSettings
*settings
, *direct
;
1924 settings
= g_settings_new ("org.gtk.test.enums");
1925 direct
= g_settings_new ("org.gtk.test.enums.direct");
1927 if (g_test_undefined () && !backend_set
)
1929 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-key", 0, 0);
1930 g_test_trap_assert_failed ();
1931 g_test_trap_assert_stderr ("*not associated with an enum*");
1933 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-value", 0, 0);
1934 g_test_trap_assert_failed ();
1935 g_test_trap_assert_stderr ("*invalid enum value 42*");
1937 g_test_trap_subprocess ("/gsettings/enums/subprocess/range", 0, 0);
1938 g_test_trap_assert_failed ();
1939 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1941 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-flags", 0, 0);
1942 g_test_trap_assert_failed ();
1943 g_test_trap_assert_stderr ("*not associated with a flags*");
1946 str
= g_settings_get_string (settings
, "test");
1947 g_assert_cmpstr (str
, ==, "bar");
1950 g_settings_set_enum (settings
, "test", TEST_ENUM_FOO
);
1952 str
= g_settings_get_string (settings
, "test");
1953 g_assert_cmpstr (str
, ==, "foo");
1956 g_assert_cmpint (g_settings_get_enum (settings
, "test"), ==, TEST_ENUM_FOO
);
1958 g_settings_set_string (direct
, "test", "qux");
1960 str
= g_settings_get_string (direct
, "test");
1961 g_assert_cmpstr (str
, ==, "qux");
1964 str
= g_settings_get_string (settings
, "test");
1965 g_assert_cmpstr (str
, ==, "quux");
1968 g_assert_cmpint (g_settings_get_enum (settings
, "test"), ==, TEST_ENUM_QUUX
);
1970 g_object_unref (direct
);
1971 g_object_unref (settings
);
1975 test_flags_non_flags_key (void)
1979 direct
= g_settings_new ("org.gtk.test.enums.direct");
1980 g_settings_get_flags (direct
, "test");
1981 g_assert_not_reached ();
1985 test_flags_non_flags_value (void)
1987 GSettings
*settings
;
1989 settings
= g_settings_new ("org.gtk.test.enums");
1990 g_settings_set_flags (settings
, "f-test", 0x42);
1991 g_assert_not_reached ();
1995 test_flags_range (void)
1997 GSettings
*settings
;
1999 settings
= g_settings_new ("org.gtk.test.enums");
2000 g_settings_set_strv (settings
, "f-test",
2001 (const gchar
**) g_strsplit ("rock", ",", 0));
2002 g_assert_not_reached ();
2006 test_flags_non_enum (void)
2008 GSettings
*settings
;
2010 settings
= g_settings_new ("org.gtk.test.enums");
2011 g_settings_get_enum (settings
, "f-test");
2012 g_assert_not_reached ();
2018 GSettings
*settings
, *direct
;
2022 settings
= g_settings_new ("org.gtk.test.enums");
2023 direct
= g_settings_new ("org.gtk.test.enums.direct");
2025 if (g_test_undefined () && !backend_set
)
2027 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-key", 0, 0);
2028 g_test_trap_assert_failed ();
2029 g_test_trap_assert_stderr ("*not associated with a flags*");
2031 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-value", 0, 0);
2032 g_test_trap_assert_failed ();
2033 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
2035 g_test_trap_subprocess ("/gsettings/flags/subprocess/range", 0, 0);
2036 g_test_trap_assert_failed ();
2037 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2039 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-enum", 0, 0);
2040 g_test_trap_assert_failed ();
2041 g_test_trap_assert_stderr ("*not associated with an enum*");
2044 strv
= g_settings_get_strv (settings
, "f-test");
2045 str
= g_strjoinv (",", strv
);
2046 g_assert_cmpstr (str
, ==, "");
2050 g_settings_set_flags (settings
, "f-test",
2051 TEST_FLAGS_WALKING
| TEST_FLAGS_TALKING
);
2053 strv
= g_settings_get_strv (settings
, "f-test");
2054 str
= g_strjoinv (",", strv
);
2055 g_assert_cmpstr (str
, ==, "talking,walking");
2059 g_assert_cmpint (g_settings_get_flags (settings
, "f-test"), ==,
2060 TEST_FLAGS_WALKING
| TEST_FLAGS_TALKING
);
2062 strv
= g_strsplit ("speaking,laughing", ",", 0);
2063 g_settings_set_strv (direct
, "f-test", (const gchar
**) strv
);
2066 strv
= g_settings_get_strv (direct
, "f-test");
2067 str
= g_strjoinv (",", strv
);
2068 g_assert_cmpstr (str
, ==, "speaking,laughing");
2072 strv
= g_settings_get_strv (settings
, "f-test");
2073 str
= g_strjoinv (",", strv
);
2074 g_assert_cmpstr (str
, ==, "talking,laughing");
2078 g_assert_cmpint (g_settings_get_flags (settings
, "f-test"), ==,
2079 TEST_FLAGS_TALKING
| TEST_FLAGS_LAUGHING
);
2081 g_object_unref (direct
);
2082 g_object_unref (settings
);
2086 test_range_high (void)
2088 GSettings
*settings
;
2090 settings
= g_settings_new ("org.gtk.test.range");
2091 g_settings_set_int (settings
, "val", 45);
2092 g_assert_not_reached ();
2096 test_range_low (void)
2098 GSettings
*settings
;
2100 settings
= g_settings_new ("org.gtk.test.range");
2101 g_settings_set_int (settings
, "val", 1);
2102 g_assert_not_reached ();
2108 GSettings
*settings
, *direct
;
2111 settings
= g_settings_new ("org.gtk.test.range");
2112 direct
= g_settings_new ("org.gtk.test.range.direct");
2114 if (g_test_undefined () && !backend_set
)
2116 g_test_trap_subprocess ("/gsettings/range/subprocess/high", 0, 0);
2117 g_test_trap_assert_failed ();
2118 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2120 g_test_trap_subprocess ("/gsettings/range/subprocess/low", 0, 0);
2121 g_test_trap_assert_failed ();
2122 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2125 g_assert_cmpint (g_settings_get_int (settings
, "val"), ==, 33);
2126 g_settings_set_int (direct
, "val", 22);
2127 g_assert_cmpint (g_settings_get_int (direct
, "val"), ==, 22);
2128 g_assert_cmpint (g_settings_get_int (settings
, "val"), ==, 22);
2129 g_settings_set_int (direct
, "val", 45);
2130 g_assert_cmpint (g_settings_get_int (direct
, "val"), ==, 45);
2131 g_assert_cmpint (g_settings_get_int (settings
, "val"), ==, 33);
2132 g_settings_set_int (direct
, "val", 1);
2133 g_assert_cmpint (g_settings_get_int (direct
, "val"), ==, 1);
2134 g_assert_cmpint (g_settings_get_int (settings
, "val"), ==, 33);
2136 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2137 value
= g_variant_new_int32 (1);
2138 g_assert (!g_settings_range_check (settings
, "val", value
));
2139 g_variant_unref (value
);
2140 value
= g_variant_new_int32 (33);
2141 g_assert (g_settings_range_check (settings
, "val", value
));
2142 g_variant_unref (value
);
2143 value
= g_variant_new_int32 (45);
2144 g_assert (!g_settings_range_check (settings
, "val", value
));
2145 g_variant_unref (value
);
2146 G_GNUC_END_IGNORE_DEPRECATIONS
2148 g_object_unref (direct
);
2149 g_object_unref (settings
);
2153 strv_has_string (gchar
**haystack
,
2154 const gchar
*needle
)
2158 for (n
= 0; haystack
!= NULL
&& haystack
[n
] != NULL
; n
++)
2160 if (g_strcmp0 (haystack
[n
], needle
) == 0)
2167 strv_set_equal (gchar
**strv
, ...)
2176 va_start (list
, strv
);
2179 str
= va_arg (list
, const gchar
*);
2182 if (!strv_has_string (strv
, str
))
2192 res
= g_strv_length ((gchar
**)strv
) == count
;
2198 test_list_items (void)
2200 GSettingsSchema
*schema
;
2201 GSettings
*settings
;
2205 settings
= g_settings_new ("org.gtk.test");
2206 g_object_get (settings
, "settings-schema", &schema
, NULL
);
2207 children
= g_settings_list_children (settings
);
2208 keys
= g_settings_schema_list_keys (schema
);
2210 g_assert (strv_set_equal (children
, "basic-types", "complex-types", "localized", NULL
));
2211 g_assert (strv_set_equal (keys
, "greeting", "farewell", NULL
));
2213 g_strfreev (children
);
2216 g_settings_schema_unref (schema
);
2217 g_object_unref (settings
);
2221 test_list_schemas (void)
2223 const gchar
* const *schemas
;
2224 const gchar
* const *relocs
;
2226 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2227 relocs
= g_settings_list_relocatable_schemas ();
2228 schemas
= g_settings_list_schemas ();
2229 G_GNUC_END_IGNORE_DEPRECATIONS
2231 g_assert (strv_set_equal ((gchar
**)relocs
,
2232 "org.gtk.test.no-path",
2233 "org.gtk.test.extends.base",
2234 "org.gtk.test.extends.extended",
2237 g_assert (strv_set_equal ((gchar
**)schemas
,
2239 "org.gtk.test.basic-types",
2240 "org.gtk.test.complex-types",
2241 "org.gtk.test.localized",
2242 "org.gtk.test.binding",
2243 "org.gtk.test.enums",
2244 "org.gtk.test.enums.direct",
2245 "org.gtk.test.range",
2246 "org.gtk.test.range.direct",
2247 "org.gtk.test.mapped",
2248 "org.gtk.test.descriptions",
2249 "org.gtk.test.per-desktop",
2254 map_func (GVariant
*value
,
2258 gint
*state
= user_data
;
2262 v
= g_variant_get_int32 (value
);
2268 g_assert_cmpint (v
, ==, 1);
2272 else if (*state
== 1)
2274 g_assert_cmpint (v
, ==, 0);
2280 g_assert (value
== NULL
);
2281 *result
= g_variant_new_int32 (5);
2287 test_get_mapped (void)
2289 GSettings
*settings
;
2294 settings
= g_settings_new ("org.gtk.test.mapped");
2295 g_settings_set_int (settings
, "val", 1);
2298 p
= g_settings_get_mapped (settings
, "val", map_func
, &state
);
2299 val
= g_variant_get_int32 ((GVariant
*)p
);
2300 g_assert_cmpint (val
, ==, 5);
2302 g_variant_unref (p
);
2303 g_object_unref (settings
);
2307 test_get_range (void)
2309 GSettings
*settings
;
2312 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2313 settings
= g_settings_new ("org.gtk.test.range");
2314 range
= g_settings_get_range (settings
, "val");
2315 check_and_free (range
, "('range', <(2, 44)>)");
2316 g_object_unref (settings
);
2318 settings
= g_settings_new ("org.gtk.test.enums");
2319 range
= g_settings_get_range (settings
, "test");
2320 check_and_free (range
, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
2321 g_object_unref (settings
);
2323 settings
= g_settings_new ("org.gtk.test.enums");
2324 range
= g_settings_get_range (settings
, "f-test");
2325 check_and_free (range
, "('flags', "
2326 "<['mourning', 'laughing', 'talking', 'walking']>)");
2327 g_object_unref (settings
);
2329 settings
= g_settings_new ("org.gtk.test");
2330 range
= g_settings_get_range (settings
, "greeting");
2331 check_and_free (range
, "('type', <@as []>)");
2332 g_object_unref (settings
);
2333 G_GNUC_END_IGNORE_DEPRECATIONS
2337 test_schema_source (void)
2339 GSettingsSchemaSource
*parent
;
2340 GSettingsSchemaSource
*source
;
2341 GSettingsBackend
*backend
;
2342 GSettingsSchema
*schema
;
2343 GError
*error
= NULL
;
2344 GSettings
*settings
;
2347 backend
= g_settings_backend_get_default ();
2349 /* make sure it fails properly */
2350 parent
= g_settings_schema_source_get_default ();
2351 source
= g_settings_schema_source_new_from_directory ("/path/that/does/not/exist", parent
, TRUE
, &error
);
2352 g_assert (source
== NULL
);
2353 g_assert_error (error
, G_FILE_ERROR
, G_FILE_ERROR_NOENT
);
2354 g_clear_error (&error
);
2356 /* create a source with the parent */
2357 source
= g_settings_schema_source_new_from_directory ("schema-source", parent
, TRUE
, &error
);
2358 g_assert_no_error (error
);
2359 g_assert (source
!= NULL
);
2361 /* check recursive lookups are working */
2362 schema
= g_settings_schema_source_lookup (source
, "org.gtk.test", TRUE
);
2363 g_assert (schema
!= NULL
);
2364 g_settings_schema_unref (schema
);
2366 /* check recursive lookups for non-existent schemas */
2367 schema
= g_settings_schema_source_lookup (source
, "org.gtk.doesnotexist", TRUE
);
2368 g_assert (schema
== NULL
);
2370 /* check non-recursive for schema that only exists in lower layers */
2371 schema
= g_settings_schema_source_lookup (source
, "org.gtk.test", FALSE
);
2372 g_assert (schema
== NULL
);
2374 /* check non-recursive lookup for non-existent */
2375 schema
= g_settings_schema_source_lookup (source
, "org.gtk.doesnotexist", FALSE
);
2376 g_assert (schema
== NULL
);
2378 /* check non-recursive for schema that exists in toplevel */
2379 schema
= g_settings_schema_source_lookup (source
, "org.gtk.schemasourcecheck", FALSE
);
2380 g_assert (schema
!= NULL
);
2381 g_settings_schema_unref (schema
);
2383 /* check recursive for schema that exists in toplevel */
2384 schema
= g_settings_schema_source_lookup (source
, "org.gtk.schemasourcecheck", TRUE
);
2385 g_assert (schema
!= NULL
);
2387 /* try to use it for something */
2388 settings
= g_settings_new_full (schema
, backend
, g_settings_schema_get_path (schema
));
2389 g_settings_schema_unref (schema
);
2391 g_settings_get (settings
, "enabled", "b", &enabled
);
2393 g_object_unref (settings
);
2395 g_settings_schema_source_unref (source
);
2397 /* try again, but with no parent */
2398 source
= g_settings_schema_source_new_from_directory ("schema-source", NULL
, FALSE
, NULL
);
2399 g_assert (source
!= NULL
);
2401 /* should not find it this time, even if recursive... */
2402 schema
= g_settings_schema_source_lookup (source
, "org.gtk.test", FALSE
);
2403 g_assert (schema
== NULL
);
2404 schema
= g_settings_schema_source_lookup (source
, "org.gtk.test", TRUE
);
2405 g_assert (schema
== NULL
);
2407 /* should still find our own... */
2408 schema
= g_settings_schema_source_lookup (source
, "org.gtk.schemasourcecheck", TRUE
);
2409 g_assert (schema
!= NULL
);
2410 g_settings_schema_unref (schema
);
2411 schema
= g_settings_schema_source_lookup (source
, "org.gtk.schemasourcecheck", FALSE
);
2412 g_assert (schema
!= NULL
);
2413 g_settings_schema_unref (schema
);
2415 g_settings_schema_source_unref (source
);
2419 test_schema_list_keys (void)
2422 GSettingsSchemaSource
*src
= g_settings_schema_source_get_default ();
2423 GSettingsSchema
*schema
= g_settings_schema_source_lookup (src
, "org.gtk.test", TRUE
);
2424 g_assert (schema
!= NULL
);
2426 keys
= g_settings_schema_list_keys (schema
);
2428 g_assert (strv_set_equal ((gchar
**)keys
,
2434 g_settings_schema_unref (schema
);
2440 GAction
*string
, *toggle
;
2441 gboolean c1
, c2
, c3
;
2442 GSettings
*settings
;
2444 GVariantType
*param_type
;
2446 GVariantType
*state_type
;
2449 settings
= g_settings_new ("org.gtk.test.basic-types");
2450 string
= g_settings_create_action (settings
, "test-string");
2451 toggle
= g_settings_create_action (settings
, "test-boolean");
2452 g_object_unref (settings
); /* should be held by the actions */
2454 g_signal_connect (settings
, "changed", G_CALLBACK (changed_cb2
), &c1
);
2455 g_signal_connect (string
, "notify::state", G_CALLBACK (changed_cb2
), &c2
);
2456 g_signal_connect (toggle
, "notify::state", G_CALLBACK (changed_cb2
), &c3
);
2458 c1
= c2
= c3
= FALSE
;
2459 g_settings_set_string (settings
, "test-string", "hello world");
2460 check_and_free (g_action_get_state (string
), "'hello world'");
2461 g_assert (c1
&& c2
&& !c3
);
2462 c1
= c2
= c3
= FALSE
;
2464 g_action_activate (string
, g_variant_new_string ("hihi"));
2465 check_and_free (g_settings_get_value (settings
, "test-string"), "'hihi'");
2466 g_assert (c1
&& c2
&& !c3
);
2467 c1
= c2
= c3
= FALSE
;
2469 g_action_change_state (string
, g_variant_new_string ("kthxbye"));
2470 check_and_free (g_settings_get_value (settings
, "test-string"), "'kthxbye'");
2471 g_assert (c1
&& c2
&& !c3
);
2472 c1
= c2
= c3
= FALSE
;
2474 g_action_change_state (toggle
, g_variant_new_boolean (TRUE
));
2475 g_assert (g_settings_get_boolean (settings
, "test-boolean"));
2476 g_assert (c1
&& !c2
&& c3
);
2477 c1
= c2
= c3
= FALSE
;
2479 g_action_activate (toggle
, NULL
);
2480 g_assert (!g_settings_get_boolean (settings
, "test-boolean"));
2481 g_assert (c1
&& !c2
&& c3
);
2483 g_object_get (string
,
2485 "parameter-type", ¶m_type
,
2486 "enabled", &enabled
,
2487 "state-type", &state_type
,
2491 g_assert_cmpstr (name
, ==, "test-string");
2492 g_assert (g_variant_type_equal (param_type
, G_VARIANT_TYPE_STRING
));
2494 g_assert (g_variant_type_equal (state_type
, G_VARIANT_TYPE_STRING
));
2495 g_assert_cmpstr (g_variant_get_string (state
, NULL
), ==, "kthxbye");
2498 g_variant_type_free (param_type
);
2499 g_variant_type_free (state_type
);
2500 g_variant_unref (state
);
2502 g_object_unref (string
);
2503 g_object_unref (toggle
);
2507 test_null_backend (void)
2509 GSettingsBackend
*backend
;
2510 GSettings
*settings
;
2514 backend
= g_null_settings_backend_new ();
2515 settings
= g_settings_new_with_backend_and_path ("org.gtk.test", backend
, "/tests/");
2517 g_object_get (settings
, "schema-id", &str
, NULL
);
2518 g_assert_cmpstr (str
, ==, "org.gtk.test");
2521 g_settings_get (settings
, "greeting", "s", &str
);
2522 g_assert_cmpstr (str
, ==, "Hello, earthlings");
2525 g_settings_set (settings
, "greeting", "s", "goodbye world");
2526 g_settings_get (settings
, "greeting", "s", &str
);
2527 g_assert_cmpstr (str
, ==, "Hello, earthlings");
2530 writable
= g_settings_is_writable (settings
, "greeting");
2531 g_assert (!writable
);
2533 g_settings_reset (settings
, "greeting");
2535 g_settings_delay (settings
);
2536 g_settings_set (settings
, "greeting", "s", "goodbye world");
2537 g_settings_apply (settings
);
2538 g_settings_get (settings
, "greeting", "s", &str
);
2539 g_assert_cmpstr (str
, ==, "Hello, earthlings");
2542 g_object_unref (settings
);
2543 g_object_unref (backend
);
2547 test_memory_backend (void)
2549 GSettingsBackend
*backend
;
2551 backend
= g_memory_settings_backend_new ();
2552 g_assert (G_IS_SETTINGS_BACKEND (backend
));
2553 g_object_unref (backend
);
2557 test_read_descriptions (void)
2559 GSettingsSchema
*schema
;
2560 GSettingsSchemaKey
*key
;
2561 GSettings
*settings
;
2563 settings
= g_settings_new ("org.gtk.test");
2564 g_object_get (settings
, "settings-schema", &schema
, NULL
);
2565 key
= g_settings_schema_get_key (schema
, "greeting");
2567 g_assert_cmpstr (g_settings_schema_key_get_summary (key
), ==, "A greeting");
2568 g_assert_cmpstr (g_settings_schema_key_get_description (key
), ==, "Greeting of the invading martians");
2570 g_settings_schema_key_unref (key
);
2571 g_settings_schema_unref (schema
);
2573 g_object_unref (settings
);
2575 settings
= g_settings_new ("org.gtk.test.descriptions");
2576 g_object_get (settings
, "settings-schema", &schema
, NULL
);
2577 key
= g_settings_schema_get_key (schema
, "a");
2579 g_assert_cmpstr (g_settings_schema_key_get_summary (key
), ==,
2581 "with some whitespace.\n\n"
2582 "because not everyone has a great editor.\n\n"
2583 "lots of space is as one.");
2585 g_settings_schema_key_unref (key
);
2586 g_settings_schema_unref (schema
);
2588 g_object_unref (settings
);
2592 test_default_value (void)
2594 GSettings
*settings
;
2595 GSettingsSchema
*schema
;
2596 GSettingsSchemaKey
*key
;
2601 settings
= g_settings_new ("org.gtk.test");
2602 g_object_get (settings
, "settings-schema", &schema
, NULL
);
2603 key
= g_settings_schema_get_key (schema
, "greeting");
2604 g_settings_schema_unref (schema
);
2605 g_settings_schema_key_ref (key
);
2607 g_assert (g_variant_type_equal (g_settings_schema_key_get_value_type (key
), G_VARIANT_TYPE_STRING
));
2609 v
= g_settings_schema_key_get_default_value (key
);
2610 str
= g_variant_get_string (v
, NULL
);
2611 g_assert_cmpstr (str
, ==, "Hello, earthlings");
2612 g_variant_unref (v
);
2614 g_settings_schema_key_unref (key
);
2615 g_settings_schema_key_unref (key
);
2617 g_settings_set (settings
, "greeting", "s", "goodbye world");
2619 v
= g_settings_get_user_value (settings
, "greeting");
2620 str
= g_variant_get_string (v
, NULL
);
2621 g_assert_cmpstr (str
, ==, "goodbye world");
2622 g_variant_unref (v
);
2624 v
= g_settings_get_default_value (settings
, "greeting");
2625 str
= g_variant_get_string (v
, NULL
);
2626 g_assert_cmpstr (str
, ==, "Hello, earthlings");
2627 g_variant_unref (v
);
2629 g_settings_reset (settings
, "greeting");
2631 v
= g_settings_get_user_value (settings
, "greeting");
2634 s
= g_settings_get_string (settings
, "greeting");
2635 g_assert_cmpstr (s
, ==, "Hello, earthlings");
2638 g_object_unref (settings
);
2642 string_map_func (GVariant
*value
,
2648 str
= g_variant_get_string (value
, NULL
);
2649 *result
= g_variant_new_string (str
);
2654 /* Test that per-desktop values from org.gtk.test.gschema.override
2655 * does not change default value if current desktop is not listed in
2656 * $XDG_CURRENT_DESKTOP.
2659 test_per_desktop (void)
2661 GSettings
*settings
;
2666 settings
= g_settings_new ("org.gtk.test.per-desktop");
2667 obj
= test_object_new ();
2669 if (!g_test_subprocess ())
2671 g_test_trap_subprocess ("/gsettings/per-desktop/subprocess", 0, 0);
2672 g_test_trap_assert_passed ();
2675 str
= g_settings_get_string (settings
, "desktop");
2676 g_assert_cmpstr (str
, ==, "GNOME");
2679 p
= g_settings_get_mapped (settings
, "desktop", string_map_func
, NULL
);
2681 str
= g_variant_dup_string (p
, NULL
);
2682 g_assert_cmpstr (str
, ==, "GNOME");
2685 g_variant_unref (p
);
2687 g_settings_bind (settings
, "desktop", obj
, "string", G_SETTINGS_BIND_DEFAULT
);
2689 g_object_get (obj
, "string", &str
, NULL
);
2690 g_assert_cmpstr (str
, ==, "GNOME");
2693 g_object_unref (settings
);
2694 g_object_unref (obj
);
2697 /* Test that per-desktop values from org.gtk.test.gschema.override
2698 * are successfully loaded based on the value of $XDG_CURRENT_DESKTOP.
2701 test_per_desktop_subprocess (void)
2703 GSettings
*settings
;
2708 g_setenv ("XDG_CURRENT_DESKTOP", "GNOME-Classic:GNOME", TRUE
);
2710 settings
= g_settings_new ("org.gtk.test.per-desktop");
2711 obj
= test_object_new ();
2713 str
= g_settings_get_string (settings
, "desktop");
2714 g_assert_cmpstr (str
, ==, "GNOME Classic");
2717 p
= g_settings_get_mapped (settings
, "desktop", string_map_func
, NULL
);
2719 str
= g_variant_dup_string (p
, NULL
);
2720 g_assert_cmpstr (str
, ==, "GNOME Classic");
2723 g_variant_unref (p
);
2725 g_settings_bind (settings
, "desktop", obj
, "string", G_SETTINGS_BIND_DEFAULT
);
2727 g_object_get (obj
, "string", &str
, NULL
);
2728 g_assert_cmpstr (str
, ==, "GNOME Classic");
2731 g_object_unref (settings
);
2732 g_object_unref (obj
);
2736 test_extended_schema (void)
2738 GSettingsSchema
*schema
;
2739 GSettings
*settings
;
2742 settings
= g_settings_new_with_path ("org.gtk.test.extends.extended", "/test/extendes/");
2743 g_object_get (settings
, "settings-schema", &schema
, NULL
);
2744 keys
= g_settings_schema_list_keys (schema
);
2745 g_assert (strv_set_equal (keys
, "int32", "string", "another-int32", NULL
));
2747 g_object_unref (settings
);
2748 g_settings_schema_unref (schema
);
2752 main (int argc
, char *argv
[])
2755 gchar
*override_text
;
2759 /* Meson build sets this */
2760 #ifdef TEST_LOCALE_PATH
2761 if (g_str_has_suffix (TEST_LOCALE_PATH
, "LC_MESSAGES"))
2763 locale_dir
= TEST_LOCALE_PATH G_DIR_SEPARATOR_S
".." G_DIR_SEPARATOR_S
"..";
2767 setlocale (LC_ALL
, "");
2769 g_test_init (&argc
, &argv
, NULL
);
2771 if (!g_test_subprocess ())
2773 backend_set
= g_getenv ("GSETTINGS_BACKEND") != NULL
;
2775 g_setenv ("XDG_DATA_DIRS", ".", TRUE
);
2776 g_setenv ("XDG_DATA_HOME", ".", TRUE
);
2777 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE
);
2778 g_setenv ("XDG_CURRENT_DESKTOP", "", TRUE
);
2781 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE
);
2783 /* Meson build defines this, autotools build does not */
2784 #ifndef GLIB_MKENUMS
2785 #define GLIB_MKENUMS "../../gobject/glib-mkenums"
2788 g_remove ("org.gtk.test.enums.xml");
2789 g_assert (g_spawn_command_line_sync (GLIB_MKENUMS
" "
2790 "--template " SRCDIR
"/enums.xml.template "
2791 SRCDIR
"/testenum.h",
2792 &enums
, NULL
, &result
, NULL
));
2793 g_assert (result
== 0);
2794 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums
, -1, NULL
));
2797 g_assert (g_file_get_contents (SRCDIR
"/org.gtk.test.gschema.xml.orig", &schema_text
, NULL
, NULL
));
2798 g_assert (g_file_set_contents ("org.gtk.test.gschema.xml", schema_text
, -1, NULL
));
2799 g_free (schema_text
);
2801 g_assert (g_file_get_contents (SRCDIR
"/org.gtk.test.gschema.override.orig", &override_text
, NULL
, NULL
));
2802 g_assert (g_file_set_contents ("org.gtk.test.gschema.override", override_text
, -1, NULL
));
2803 g_free (override_text
);
2805 /* Meson build defines this, autotools build does not */
2806 #ifndef GLIB_COMPILE_SCHEMAS
2807 #define GLIB_COMPILE_SCHEMAS "../glib-compile-schemas"
2810 g_remove ("gschemas.compiled");
2811 g_assert (g_spawn_command_line_sync (GLIB_COMPILE_SCHEMAS
" --targetdir=. "
2812 "--schema-file=org.gtk.test.enums.xml "
2813 "--schema-file=org.gtk.test.gschema.xml "
2814 "--override-file=org.gtk.test.gschema.override",
2815 NULL
, NULL
, &result
, NULL
));
2816 g_assert (result
== 0);
2818 g_remove ("schema-source/gschemas.compiled");
2819 g_mkdir ("schema-source", 0777);
2820 g_assert (g_spawn_command_line_sync (GLIB_COMPILE_SCHEMAS
" --targetdir=schema-source "
2821 "--schema-file=" SRCDIR
"/org.gtk.schemasourcecheck.gschema.xml",
2822 NULL
, NULL
, &result
, NULL
));
2823 g_assert (result
== 0);
2826 g_test_add_func ("/gsettings/basic", test_basic
);
2830 g_test_add_func ("/gsettings/no-schema", test_no_schema
);
2831 g_test_add_func ("/gsettings/unknown-key", test_unknown_key
);
2832 g_test_add_func ("/gsettings/wrong-type", test_wrong_type
);
2833 g_test_add_func ("/gsettings/wrong-path", test_wrong_path
);
2834 g_test_add_func ("/gsettings/no-path", test_no_path
);
2837 g_test_add_func ("/gsettings/basic-types", test_basic_types
);
2838 g_test_add_func ("/gsettings/complex-types", test_complex_types
);
2839 g_test_add_func ("/gsettings/changes", test_changes
);
2841 g_test_add_func ("/gsettings/l10n", test_l10n
);
2842 g_test_add_func ("/gsettings/l10n-context", test_l10n_context
);
2844 g_test_add_func ("/gsettings/delay-apply", test_delay_apply
);
2845 g_test_add_func ("/gsettings/delay-revert", test_delay_revert
);
2846 g_test_add_func ("/gsettings/delay-child", test_delay_child
);
2847 g_test_add_func ("/gsettings/atomic", test_atomic
);
2849 g_test_add_func ("/gsettings/simple-binding", test_simple_binding
);
2850 g_test_add_func ("/gsettings/directional-binding", test_directional_binding
);
2851 g_test_add_func ("/gsettings/custom-binding", test_custom_binding
);
2852 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding
);
2853 g_test_add_func ("/gsettings/unbinding", test_unbind
);
2854 g_test_add_func ("/gsettings/writable-binding", test_bind_writable
);
2858 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding
);
2859 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding
);
2860 g_test_add_func ("/gsettings/no-read-binding/subprocess/fail", test_no_read_binding_fail
);
2861 g_test_add_func ("/gsettings/no-read-binding/subprocess/pass", test_no_read_binding_pass
);
2862 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding
);
2863 g_test_add_func ("/gsettings/no-write-binding/subprocess/fail", test_no_write_binding_fail
);
2864 g_test_add_func ("/gsettings/no-write-binding/subprocess/pass", test_no_write_binding_pass
);
2867 g_test_add_func ("/gsettings/keyfile", test_keyfile
);
2868 g_test_add_func ("/gsettings/child-schema", test_child_schema
);
2869 g_test_add_func ("/gsettings/strinfo", test_strinfo
);
2870 g_test_add_func ("/gsettings/enums", test_enums
);
2871 g_test_add_func ("/gsettings/enums/subprocess/non-enum-key", test_enums_non_enum_key
);
2872 g_test_add_func ("/gsettings/enums/subprocess/non-enum-value", test_enums_non_enum_value
);
2873 g_test_add_func ("/gsettings/enums/subprocess/range", test_enums_range
);
2874 g_test_add_func ("/gsettings/enums/subprocess/non-flags", test_enums_non_flags
);
2875 g_test_add_func ("/gsettings/flags", test_flags
);
2876 g_test_add_func ("/gsettings/flags/subprocess/non-flags-key", test_flags_non_flags_key
);
2877 g_test_add_func ("/gsettings/flags/subprocess/non-flags-value", test_flags_non_flags_value
);
2878 g_test_add_func ("/gsettings/flags/subprocess/range", test_flags_range
);
2879 g_test_add_func ("/gsettings/flags/subprocess/non-enum", test_flags_non_enum
);
2880 g_test_add_func ("/gsettings/range", test_range
);
2881 g_test_add_func ("/gsettings/range/subprocess/high", test_range_high
);
2882 g_test_add_func ("/gsettings/range/subprocess/low", test_range_low
);
2883 g_test_add_func ("/gsettings/list-items", test_list_items
);
2884 g_test_add_func ("/gsettings/list-schemas", test_list_schemas
);
2885 g_test_add_func ("/gsettings/mapped", test_get_mapped
);
2886 g_test_add_func ("/gsettings/get-range", test_get_range
);
2887 g_test_add_func ("/gsettings/schema-source", test_schema_source
);
2888 g_test_add_func ("/gsettings/schema-list-keys", test_schema_list_keys
);
2889 g_test_add_func ("/gsettings/actions", test_actions
);
2890 g_test_add_func ("/gsettings/null-backend", test_null_backend
);
2891 g_test_add_func ("/gsettings/memory-backend", test_memory_backend
);
2892 g_test_add_func ("/gsettings/read-descriptions", test_read_descriptions
);
2893 g_test_add_func ("/gsettings/test-extended-schema", test_extended_schema
);
2894 g_test_add_func ("/gsettings/default-value", test_default_value
);
2895 g_test_add_func ("/gsettings/per-desktop", test_per_desktop
);
2896 g_test_add_func ("/gsettings/per-desktop/subprocess", test_per_desktop_subprocess
);
2898 result
= g_test_run ();