6 #define G_SETTINGS_ENABLE_BACKEND
7 #include <gio/gsettingsbackend.h>
11 static gboolean backend_set;
13 /* These tests rely on the schemas in org.gtk.test.gschema.xml
14 * to be compiled and installed in the same directory.
18 check_and_free (GVariant *value,
19 const gchar *expected)
23 printed = g_variant_print (value, TRUE);
24 g_assert_cmpstr (printed, ==, expected);
27 g_variant_unref (value);
31 /* Just to get warmed up: Read and set a string, and
32 * verify that can read the changed string back
40 settings = g_settings_new ("org.gtk.test");
42 g_object_get (settings, "schema", &str, NULL);
43 g_assert_cmpstr (str, ==, "org.gtk.test");
46 g_settings_get (settings, "greeting", "s", &str);
47 g_assert_cmpstr (str, ==, "Hello, earthlings");
50 g_settings_set (settings, "greeting", "s", "goodbye world");
51 g_settings_get (settings, "greeting", "s", &str);
52 g_assert_cmpstr (str, ==, "goodbye world");
56 if (!backend_set && g_test_undefined ())
58 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
60 settings = g_settings_new ("org.gtk.test");
61 g_settings_set (settings, "greeting", "i", 555);
64 g_test_trap_assert_failed ();
65 g_test_trap_assert_stderr ("*g_settings_set_value*expects type*");
68 g_settings_get (settings, "greeting", "s", &str);
69 g_assert_cmpstr (str, ==, "goodbye world");
73 g_settings_reset (settings, "greeting");
74 str = g_settings_get_string (settings, "greeting");
75 g_assert_cmpstr (str, ==, "Hello, earthlings");
78 g_settings_set (settings, "greeting", "s", "this is the end");
79 g_object_unref (settings);
82 /* Check that we get an error when getting a key
83 * that is not in the schema
86 test_unknown_key (void)
88 if (!g_test_undefined ())
91 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
96 settings = g_settings_new ("org.gtk.test");
97 value = g_settings_get_value (settings, "no_such_key");
99 g_assert (value == NULL);
101 g_object_unref (settings);
103 g_test_trap_assert_failed ();
104 g_test_trap_assert_stderr ("*does not contain*");
107 /* Check that we get an error when the schema
108 * has not been installed
111 test_no_schema (void)
113 if (!g_test_undefined ())
116 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
120 settings = g_settings_new ("no.such.schema");
122 g_assert (settings == NULL);
125 g_test_trap_assert_failed ();
126 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
129 /* Check that we get an error when passing a type string
130 * that does not match the schema
133 test_wrong_type (void)
135 if (!g_test_undefined ())
138 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
143 settings = g_settings_new ("org.gtk.test");
145 g_settings_get (settings, "greeting", "o", &str);
147 g_assert (str == NULL);
149 g_test_trap_assert_failed ();
150 g_test_trap_assert_stderr ("*CRITICAL*");
152 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
156 settings = g_settings_new ("org.gtk.test");
158 g_settings_set (settings, "greeting", "o", "/a/path");
160 g_test_trap_assert_failed ();
161 g_test_trap_assert_stderr ("*CRITICAL*");
164 /* Check errors with explicit paths */
166 test_wrong_path (void)
168 if (!g_test_undefined ())
171 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
173 GSettings *settings G_GNUC_UNUSED;
175 settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
178 g_test_trap_assert_failed ();
179 g_test_trap_assert_stderr ("*but path * specified by schema*");
185 if (!g_test_undefined ())
188 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
190 GSettings *settings G_GNUC_UNUSED;
192 settings = g_settings_new ("org.gtk.test.no-path");
195 g_test_trap_assert_failed ();
196 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
200 /* Check that we can successfully read and set the full
201 * range of all basic types
204 test_basic_types (void)
218 settings = g_settings_new ("org.gtk.test.basic-types");
220 g_settings_get (settings, "test-boolean", "b", &b);
221 g_assert_cmpint (b, ==, 1);
223 g_settings_set (settings, "test-boolean", "b", 0);
224 g_settings_get (settings, "test-boolean", "b", &b);
225 g_assert_cmpint (b, ==, 0);
227 g_settings_get (settings, "test-byte", "y", &byte);
228 g_assert_cmpint (byte, ==, 25);
230 g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
231 g_settings_get (settings, "test-byte", "y", &byte);
232 g_assert_cmpint (byte, ==, G_MAXUINT8);
234 g_settings_get (settings, "test-int16", "n", &i16);
235 g_assert_cmpint (i16, ==, -1234);
237 g_settings_set (settings, "test-int16", "n", G_MININT16);
238 g_settings_get (settings, "test-int16", "n", &i16);
239 g_assert_cmpint (i16, ==, G_MININT16);
241 g_settings_set (settings, "test-int16", "n", G_MAXINT16);
242 g_settings_get (settings, "test-int16", "n", &i16);
243 g_assert_cmpint (i16, ==, G_MAXINT16);
245 g_settings_get (settings, "test-uint16", "q", &u16);
246 g_assert_cmpuint (u16, ==, 1234);
248 g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
249 g_settings_get (settings, "test-uint16", "q", &u16);
250 g_assert_cmpuint (u16, ==, G_MAXUINT16);
252 g_settings_get (settings, "test-int32", "i", &i32);
253 g_assert_cmpint (i32, ==, -123456);
255 g_settings_set (settings, "test-int32", "i", G_MININT32);
256 g_settings_get (settings, "test-int32", "i", &i32);
257 g_assert_cmpint (i32, ==, G_MININT32);
259 g_settings_set (settings, "test-int32", "i", G_MAXINT32);
260 g_settings_get (settings, "test-int32", "i", &i32);
261 g_assert_cmpint (i32, ==, G_MAXINT32);
263 g_settings_get (settings, "test-uint32", "u", &u32);
264 g_assert_cmpuint (u32, ==, 123456);
266 g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
267 g_settings_get (settings, "test-uint32", "u", &u32);
268 g_assert_cmpuint (u32, ==, G_MAXUINT32);
270 g_settings_get (settings, "test-int64", "x", &i64);
271 g_assert_cmpuint (i64, ==, -123456789);
273 g_settings_set (settings, "test-int64", "x", G_MININT64);
274 g_settings_get (settings, "test-int64", "x", &i64);
275 g_assert_cmpuint (i64, ==, G_MININT64);
277 g_settings_set (settings, "test-int64", "x", G_MAXINT64);
278 g_settings_get (settings, "test-int64", "x", &i64);
279 g_assert_cmpuint (i64, ==, G_MAXINT64);
281 g_settings_get (settings, "test-uint64", "t", &u64);
282 g_assert_cmpuint (u64, ==, 123456789);
284 g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
285 g_settings_get (settings, "test-uint64", "t", &u64);
286 g_assert_cmpuint (u64, ==, G_MAXUINT64);
288 g_settings_get (settings, "test-double", "d", &d);
289 g_assert_cmpfloat (d, ==, 123.456);
291 g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
292 g_settings_get (settings, "test-double", "d", &d);
293 g_assert_cmpfloat (d, ==, G_MINDOUBLE);
295 g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
296 g_settings_get (settings, "test-double", "d", &d);
297 g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
299 g_settings_get (settings, "test-string", "s", &str);
300 g_assert_cmpstr (str, ==, "a string, it seems");
304 g_settings_get (settings, "test-objectpath", "o", &str);
305 g_assert_cmpstr (str, ==, "/a/object/path");
306 g_object_unref (settings);
311 /* Check that we can read an set complex types like
312 * tuples, arrays and dictionaries
315 test_complex_types (void)
320 GVariantIter *iter = NULL;
322 settings = g_settings_new ("org.gtk.test.complex-types");
324 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
325 g_assert_cmpstr (s, ==, "one");
326 g_assert_cmpint (i1,==, 2);
327 g_assert_cmpint (i2,==, 3);
331 g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
332 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
333 g_assert_cmpstr (s, ==, "none");
334 g_assert_cmpint (i1,==, 0);
335 g_assert_cmpint (i2,==, 0);
339 g_settings_get (settings, "test-array", "ai", &iter);
340 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
341 g_assert (g_variant_iter_next (iter, "i", &i1));
342 g_assert_cmpint (i1, ==, 0);
343 g_assert (g_variant_iter_next (iter, "i", &i1));
344 g_assert_cmpint (i1, ==, 1);
345 g_assert (g_variant_iter_next (iter, "i", &i1));
346 g_assert_cmpint (i1, ==, 2);
347 g_assert (g_variant_iter_next (iter, "i", &i1));
348 g_assert_cmpint (i1, ==, 3);
349 g_assert (g_variant_iter_next (iter, "i", &i1));
350 g_assert_cmpint (i1, ==, 4);
351 g_assert (g_variant_iter_next (iter, "i", &i1));
352 g_assert_cmpint (i1, ==, 5);
353 g_assert (!g_variant_iter_next (iter, "i", &i1));
354 g_variant_iter_free (iter);
356 g_object_unref (settings);
359 static gboolean changed_cb_called;
362 changed_cb (GSettings *settings,
366 changed_cb_called = TRUE;
368 g_assert_cmpstr (key, ==, data);
371 /* Test that basic change notification with the changed signal works.
377 GSettings *settings2;
379 settings = g_settings_new ("org.gtk.test");
381 g_signal_connect (settings, "changed",
382 G_CALLBACK (changed_cb), "greeting");
384 changed_cb_called = FALSE;
386 g_settings_set (settings, "greeting", "s", "new greeting");
387 g_assert (changed_cb_called);
389 settings2 = g_settings_new ("org.gtk.test");
391 changed_cb_called = FALSE;
393 g_settings_set (settings2, "greeting", "s", "hi");
394 g_assert (changed_cb_called);
396 g_object_unref (settings2);
397 g_object_unref (settings);
400 static gboolean changed_cb_called2;
403 changed_cb2 (GSettings *settings,
412 /* Test that changes done to a delay-mode instance
413 * don't appear to the outside world until apply. Also
414 * check that we get change notification when they are
416 * Also test that the has-unapplied property is properly
420 test_delay_apply (void)
423 GSettings *settings2;
426 settings = g_settings_new ("org.gtk.test");
427 settings2 = g_settings_new ("org.gtk.test");
429 g_settings_set (settings2, "greeting", "s", "top o' the morning");
431 changed_cb_called = FALSE;
432 changed_cb_called2 = FALSE;
434 g_signal_connect (settings, "changed",
435 G_CALLBACK (changed_cb2), &changed_cb_called);
436 g_signal_connect (settings2, "changed",
437 G_CALLBACK (changed_cb2), &changed_cb_called2);
439 g_settings_delay (settings);
441 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
443 g_assert (changed_cb_called);
444 g_assert (!changed_cb_called2);
446 g_settings_get (settings, "greeting", "s", &str);
447 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
451 g_settings_get (settings2, "greeting", "s", &str);
452 g_assert_cmpstr (str, ==, "top o' the morning");
456 g_assert (g_settings_get_has_unapplied (settings));
457 g_assert (!g_settings_get_has_unapplied (settings2));
459 changed_cb_called = FALSE;
460 changed_cb_called2 = FALSE;
462 g_settings_apply (settings);
464 g_assert (!changed_cb_called);
465 g_assert (changed_cb_called2);
467 g_settings_get (settings, "greeting", "s", &str);
468 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
472 g_settings_get (settings2, "greeting", "s", &str);
473 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
477 g_assert (!g_settings_get_has_unapplied (settings));
478 g_assert (!g_settings_get_has_unapplied (settings2));
480 g_object_unref (settings2);
481 g_object_unref (settings);
484 /* Test that reverting unapplied changes in a delay-apply
485 * settings instance works.
488 test_delay_revert (void)
491 GSettings *settings2;
494 settings = g_settings_new ("org.gtk.test");
495 settings2 = g_settings_new ("org.gtk.test");
497 g_settings_set (settings2, "greeting", "s", "top o' the morning");
499 g_settings_delay (settings);
501 g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
503 g_settings_get (settings, "greeting", "s", &str);
504 g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
508 g_settings_get (settings2, "greeting", "s", &str);
509 g_assert_cmpstr (str, ==, "top o' the morning");
513 g_assert (g_settings_get_has_unapplied (settings));
515 g_settings_revert (settings);
517 g_assert (!g_settings_get_has_unapplied (settings));
519 g_settings_get (settings, "greeting", "s", &str);
520 g_assert_cmpstr (str, ==, "top o' the morning");
524 g_settings_get (settings2, "greeting", "s", &str);
525 g_assert_cmpstr (str, ==, "top o' the morning");
529 g_object_unref (settings2);
530 g_object_unref (settings);
534 keys_changed_cb (GSettings *settings,
540 g_assert_cmpint (n_keys, ==, 2);
542 g_assert ((keys[0] == g_quark_from_static_string ("greeting") &&
543 keys[1] == g_quark_from_static_string ("farewell")) ||
544 (keys[1] == g_quark_from_static_string ("greeting") &&
545 keys[0] == g_quark_from_static_string ("farewell")));
547 g_settings_get (settings, "greeting", "s", &str);
548 g_assert_cmpstr (str, ==, "greetings from test_atomic");
552 g_settings_get (settings, "farewell", "s", &str);
553 g_assert_cmpstr (str, ==, "atomic bye-bye");
558 /* Check that delay-applied changes appear atomically.
559 * More specifically, verify that all changed keys appear
560 * with their new value while handling the change-event signal.
566 GSettings *settings2;
569 settings = g_settings_new ("org.gtk.test");
570 settings2 = g_settings_new ("org.gtk.test");
572 g_settings_set (settings2, "greeting", "s", "top o' the morning");
574 changed_cb_called = FALSE;
575 changed_cb_called2 = FALSE;
577 g_signal_connect (settings2, "change-event",
578 G_CALLBACK (keys_changed_cb), NULL);
580 g_settings_delay (settings);
582 g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
583 g_settings_set (settings, "farewell", "s", "atomic bye-bye");
585 g_settings_apply (settings);
587 g_settings_get (settings, "greeting", "s", &str);
588 g_assert_cmpstr (str, ==, "greetings from test_atomic");
592 g_settings_get (settings, "farewell", "s", &str);
593 g_assert_cmpstr (str, ==, "atomic bye-bye");
597 g_settings_get (settings2, "greeting", "s", &str);
598 g_assert_cmpstr (str, ==, "greetings from test_atomic");
602 g_settings_get (settings2, "farewell", "s", &str);
603 g_assert_cmpstr (str, ==, "atomic bye-bye");
607 g_object_unref (settings2);
608 g_object_unref (settings);
611 /* On Windows the interaction between the C library locale and libintl
612 * (from GNU gettext) is not like on POSIX, so just skip these tests
615 * There are several issues:
617 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
618 * in libintl (defined in its <libintl.h>).
620 * 2) The locale names that setlocale() accepts and returns aren't in
621 * the "de_DE" style, but like "German_Germany".
623 * 3) libintl looks at the Win32 thread locale and not the C library
624 * locale. (And even if libintl would use the C library's locale, as
625 * there are several alternative C library DLLs, libintl might be
626 * linked to a different one than the application code, so they
627 * wouldn't have the same C library locale anyway.)
630 /* Test that translations work for schema defaults.
632 * This test relies on the de.po file in the same directory
633 * to be compiled into ./de/LC_MESSAGES/test.mo
642 bindtextdomain ("test", ".");
643 bind_textdomain_codeset ("test", "UTF-8");
645 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
647 settings = g_settings_new ("org.gtk.test.localized");
649 setlocale (LC_MESSAGES, "C");
650 str = g_settings_get_string (settings, "error-message");
651 setlocale (LC_MESSAGES, locale);
653 g_assert_cmpstr (str, ==, "Unnamed");
657 setlocale (LC_MESSAGES, "de_DE");
658 str = g_settings_get_string (settings, "error-message");
659 setlocale (LC_MESSAGES, locale);
661 g_assert_cmpstr (str, ==, "Unbenannt");
662 g_object_unref (settings);
669 /* Test that message context works as expected with translated
670 * schema defaults. Also, verify that non-ASCII UTF-8 content
673 * This test relies on the de.po file in the same directory
674 * to be compiled into ./de/LC_MESSAGES/test.mo
677 test_l10n_context (void)
683 bindtextdomain ("test", ".");
684 bind_textdomain_codeset ("test", "UTF-8");
686 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
688 settings = g_settings_new ("org.gtk.test.localized");
690 setlocale (LC_MESSAGES, "C");
691 g_settings_get (settings, "backspace", "s", &str);
692 setlocale (LC_MESSAGES, locale);
694 g_assert_cmpstr (str, ==, "BackSpace");
698 setlocale (LC_MESSAGES, "de_DE");
699 g_settings_get (settings, "backspace", "s", &str);
700 setlocale (LC_MESSAGES, locale);
702 g_assert_cmpstr (str, ==, "Löschen");
703 g_object_unref (settings);
733 GObject parent_instance;
736 gboolean anti_bool_prop;
747 gchar *no_write_prop;
755 GObjectClass parent_class;
758 static GType test_object_get_type (void);
759 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
762 test_object_init (TestObject *object)
767 test_object_finalize (GObject *object)
769 TestObject *testo = (TestObject*)object;
770 g_strfreev (testo->strv_prop);
771 g_free (testo->string_prop);
772 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
776 test_object_get_property (GObject *object,
781 TestObject *test_object = (TestObject *)object;
786 g_value_set_boolean (value, test_object->bool_prop);
789 g_value_set_boolean (value, test_object->anti_bool_prop);
792 g_value_set_schar (value, test_object->byte_prop);
795 g_value_set_uint (value, test_object->uint16_prop);
798 g_value_set_int (value, test_object->int16_prop);
801 g_value_set_int (value, test_object->int_prop);
804 g_value_set_uint (value, test_object->uint_prop);
807 g_value_set_int64 (value, test_object->int64_prop);
810 g_value_set_uint64 (value, test_object->uint64_prop);
813 g_value_set_double (value, test_object->double_prop);
816 g_value_set_string (value, test_object->string_prop);
819 g_value_set_string (value, test_object->no_write_prop);
822 g_value_set_boxed (value, test_object->strv_prop);
825 g_value_set_enum (value, test_object->enum_prop);
828 g_value_set_flags (value, test_object->flags_prop);
831 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
837 test_object_set_property (GObject *object,
842 TestObject *test_object = (TestObject *)object;
847 test_object->bool_prop = g_value_get_boolean (value);
850 test_object->anti_bool_prop = g_value_get_boolean (value);
853 test_object->byte_prop = g_value_get_schar (value);
856 test_object->int16_prop = g_value_get_int (value);
859 test_object->uint16_prop = g_value_get_uint (value);
862 test_object->int_prop = g_value_get_int (value);
865 test_object->uint_prop = g_value_get_uint (value);
868 test_object->int64_prop = g_value_get_int64 (value);
871 test_object->uint64_prop = g_value_get_uint64 (value);
874 test_object->double_prop = g_value_get_double (value);
877 g_free (test_object->string_prop);
878 test_object->string_prop = g_value_dup_string (value);
881 g_free (test_object->no_read_prop);
882 test_object->no_read_prop = g_value_dup_string (value);
885 g_strfreev (test_object->strv_prop);
886 test_object->strv_prop = g_value_dup_boxed (value);
889 test_object->enum_prop = g_value_get_enum (value);
892 test_object->flags_prop = g_value_get_flags (value);
895 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
901 test_enum_get_type (void)
903 static volatile gsize define_type_id = 0;
905 if (g_once_init_enter (&define_type_id))
907 static const GEnumValue values[] = {
908 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
909 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
910 { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
911 { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
915 GType type_id = g_enum_register_static ("TestEnum", values);
916 g_once_init_leave (&define_type_id, type_id);
919 return define_type_id;
923 test_flags_get_type (void)
925 static volatile gsize define_type_id = 0;
927 if (g_once_init_enter (&define_type_id))
929 static const GFlagsValue values[] = {
930 { TEST_FLAGS_NONE, "TEST_FLAGS_NONE", "none" },
931 { TEST_FLAGS_MOURNING, "TEST_FLAGS_MOURNING", "mourning" },
932 { TEST_FLAGS_LAUGHING, "TEST_FLAGS_LAUGHING", "laughing" },
933 { TEST_FLAGS_WALKING, "TEST_FLAGS_WALKING", "walking" },
937 GType type_id = g_flags_register_static ("TestFlags", values);
938 g_once_init_leave (&define_type_id, type_id);
941 return define_type_id;
945 test_object_class_init (TestObjectClass *class)
947 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
949 gobject_class->get_property = test_object_get_property;
950 gobject_class->set_property = test_object_set_property;
951 gobject_class->finalize = test_object_finalize;
953 g_object_class_install_property (gobject_class, PROP_BOOL,
954 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
955 g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
956 g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
957 g_object_class_install_property (gobject_class, PROP_BYTE,
958 g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
959 g_object_class_install_property (gobject_class, PROP_INT16,
960 g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
961 g_object_class_install_property (gobject_class, PROP_UINT16,
962 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
963 g_object_class_install_property (gobject_class, PROP_INT,
964 g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
965 g_object_class_install_property (gobject_class, PROP_UINT,
966 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
967 g_object_class_install_property (gobject_class, PROP_INT64,
968 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
969 g_object_class_install_property (gobject_class, PROP_UINT64,
970 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
971 g_object_class_install_property (gobject_class, PROP_DOUBLE,
972 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
973 g_object_class_install_property (gobject_class, PROP_STRING,
974 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
975 g_object_class_install_property (gobject_class, PROP_NO_WRITE,
976 g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
977 g_object_class_install_property (gobject_class, PROP_NO_READ,
978 g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
979 g_object_class_install_property (gobject_class, PROP_STRV,
980 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
981 g_object_class_install_property (gobject_class, PROP_ENUM,
982 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
983 g_object_class_install_property (gobject_class, PROP_FLAGS,
984 g_param_spec_flags ("flags", "", "", test_flags_get_type (), TEST_FLAGS_NONE, G_PARAM_READWRITE));
988 test_object_new (void)
990 return (TestObject*)g_object_new (test_object_get_type (), NULL);
993 /* Test basic binding functionality for simple types.
994 * Verify that with bidirectional bindings, changes on either side
995 * are notified on the other end.
998 test_simple_binding (void)
1001 GSettings *settings;
1017 settings = g_settings_new ("org.gtk.test.binding");
1018 obj = test_object_new ();
1020 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
1021 g_object_set (obj, "bool", TRUE, NULL);
1022 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1024 g_settings_set_boolean (settings, "bool", FALSE);
1026 g_object_get (obj, "bool", &b, NULL);
1027 g_assert_cmpint (b, ==, FALSE);
1029 g_settings_bind (settings, "anti-bool", obj, "anti-bool",
1030 G_SETTINGS_BIND_INVERT_BOOLEAN);
1031 g_object_set (obj, "anti-bool", FALSE, NULL);
1032 g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
1034 g_settings_set_boolean (settings, "anti-bool", FALSE);
1036 g_object_get (obj, "anti-bool", &b, NULL);
1037 g_assert_cmpint (b, ==, TRUE);
1039 g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
1041 g_object_set (obj, "byte", 123, NULL);
1043 g_settings_get (settings, "byte", "y", &y);
1044 g_assert_cmpint (y, ==, 123);
1046 g_settings_set (settings, "byte", "y", 54);
1048 g_object_get (obj, "byte", &y, NULL);
1049 g_assert_cmpint (y, ==, 54);
1051 g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
1053 g_object_set (obj, "int16", 1234, NULL);
1055 g_settings_get (settings, "int16", "n", &n);
1056 g_assert_cmpint (n, ==, 1234);
1058 g_settings_set (settings, "int16", "n", 4321);
1060 g_object_get (obj, "int16", &n2, NULL);
1061 g_assert_cmpint (n2, ==, 4321);
1063 g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
1065 g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1067 g_settings_get (settings, "uint16", "q", &q);
1068 g_assert_cmpuint (q, ==, G_MAXUINT16);
1070 g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1072 g_object_get (obj, "uint16", &q2, NULL);
1073 g_assert_cmpuint (q2, ==, (guint16) G_MAXINT16);
1075 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1077 g_object_set (obj, "int", 12345, NULL);
1078 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1080 g_settings_set_int (settings, "int", 54321);
1082 g_object_get (obj, "int", &i, NULL);
1083 g_assert_cmpint (i, ==, 54321);
1085 g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1087 g_object_set (obj, "uint", 12345, NULL);
1088 g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);
1090 g_settings_set_uint (settings, "uint", 54321);
1092 g_object_get (obj, "uint", &u, NULL);
1093 g_assert_cmpuint (u, ==, 54321);
1095 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1097 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1099 g_settings_get (settings, "int64", "x", &i64);
1100 g_assert_cmpint (i64, ==, G_MAXINT64);
1102 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1104 g_object_get (obj, "int64", &i64, NULL);
1105 g_assert_cmpint (i64, ==, G_MININT64);
1107 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1109 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1111 g_settings_get (settings, "uint64", "t", &u64);
1112 g_assert_cmpuint (u64, ==, G_MAXUINT64);
1114 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1116 g_object_get (obj, "uint64", &u64, NULL);
1117 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1119 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1121 g_object_set (obj, "string", "bu ba", NULL);
1122 s = g_settings_get_string (settings, "string");
1123 g_assert_cmpstr (s, ==, "bu ba");
1126 g_settings_set_string (settings, "string", "bla bla");
1127 g_object_get (obj, "string", &s, NULL);
1128 g_assert_cmpstr (s, ==, "bla bla");
1131 g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1133 g_object_set (obj, "string", "non-unicode:\315", NULL);
1134 value = g_settings_get_value (settings, "chararray");
1135 g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1136 g_variant_unref (value);
1138 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1140 g_object_set (obj, "double", G_MAXFLOAT, NULL);
1141 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1143 g_settings_set_double (settings, "double", G_MINFLOAT);
1145 g_object_get (obj, "double", &d, NULL);
1146 g_assert_cmpfloat (d, ==, G_MINFLOAT);
1148 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1149 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1151 g_settings_set_double (settings, "double", -G_MINDOUBLE);
1153 g_object_get (obj, "double", &d, NULL);
1154 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1156 strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1157 g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1158 g_object_set (obj, "strv", strv, NULL);
1160 strv = g_settings_get_strv (settings, "strv");
1161 s = g_strjoinv (",", strv);
1162 g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1165 strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1166 g_settings_set_strv (settings, "strv", (const gchar **) strv);
1168 g_object_get (obj, "strv", &strv, NULL);
1169 s = g_strjoinv (",", strv);
1170 g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1174 g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1175 g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1176 s = g_settings_get_string (settings, "enum");
1177 g_assert_cmpstr (s, ==, "baz");
1179 g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1181 g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1183 g_object_get (obj, "enum", &i, NULL);
1184 g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1186 g_settings_set_string (settings, "enum", "baz");
1188 g_object_get (obj, "enum", &i, NULL);
1189 g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1191 g_settings_bind (settings, "flags", obj, "flags", G_SETTINGS_BIND_DEFAULT);
1192 g_object_set (obj, "flags", TEST_FLAGS_MOURNING, NULL);
1193 strv = g_settings_get_strv (settings, "flags");
1194 g_assert_cmpint (g_strv_length (strv), ==, 1);
1195 g_assert_cmpstr (strv[0], ==, "mourning");
1198 g_assert_cmpint (g_settings_get_flags (settings, "flags"), ==, TEST_FLAGS_MOURNING);
1200 g_settings_set_flags (settings, "flags", TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1202 g_object_get (obj, "flags", &i, NULL);
1203 g_assert_cmpint (i, ==, TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1205 g_object_unref (obj);
1206 g_object_unref (settings);
1213 GSettings *settings;
1215 settings = g_settings_new ("org.gtk.test.binding");
1216 obj = test_object_new ();
1218 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1220 g_object_set (obj, "int", 12345, NULL);
1221 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1223 g_settings_unbind (obj, "int");
1225 g_object_set (obj, "int", 54321, NULL);
1226 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1228 g_object_unref (obj);
1229 g_object_unref (settings);
1233 test_bind_writable (void)
1236 GSettings *settings;
1239 settings = g_settings_new ("org.gtk.test.binding");
1240 obj = test_object_new ();
1242 g_object_set (obj, "bool", FALSE, NULL);
1244 g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1246 g_object_get (obj, "bool", &b, NULL);
1249 g_settings_unbind (obj, "bool");
1251 g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1253 g_object_get (obj, "bool", &b, NULL);
1256 g_object_unref (obj);
1257 g_object_unref (settings);
1260 /* Test one-way bindings.
1261 * Verify that changes on one side show up on the other,
1262 * but not vice versa
1265 test_directional_binding (void)
1268 GSettings *settings;
1272 settings = g_settings_new ("org.gtk.test.binding");
1273 obj = test_object_new ();
1275 g_object_set (obj, "bool", FALSE, NULL);
1276 g_settings_set_boolean (settings, "bool", FALSE);
1278 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1280 g_settings_set_boolean (settings, "bool", TRUE);
1281 g_object_get (obj, "bool", &b, NULL);
1282 g_assert_cmpint (b, ==, TRUE);
1284 g_object_set (obj, "bool", FALSE, NULL);
1285 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1287 g_object_set (obj, "int", 20, NULL);
1288 g_settings_set_int (settings, "int", 20);
1290 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1292 g_object_set (obj, "int", 32, NULL);
1293 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1295 g_settings_set_int (settings, "int", 20);
1296 g_object_get (obj, "int", &i, NULL);
1297 g_assert_cmpint (i, ==, 32);
1299 g_object_unref (obj);
1300 g_object_unref (settings);
1303 /* Test that type mismatch is caught when creating a binding
1306 test_typesafe_binding (void)
1308 if (!g_test_undefined ())
1311 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1314 GSettings *settings;
1316 settings = g_settings_new ("org.gtk.test.binding");
1317 obj = test_object_new ();
1319 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1321 g_object_unref (obj);
1322 g_object_unref (settings);
1324 g_test_trap_assert_failed ();
1325 g_test_trap_assert_stderr ("*not compatible*");
1329 string_to_bool (GValue *value,
1335 s = g_variant_get_string (variant, NULL);
1336 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1342 bool_to_string (const GValue *value,
1343 const GVariantType *expected_type,
1346 if (g_value_get_boolean (value))
1347 return g_variant_new_string ("true");
1349 return g_variant_new_string ("false");
1352 /* Test custom bindings.
1353 * Translate strings to booleans and back
1356 test_custom_binding (void)
1359 GSettings *settings;
1363 settings = g_settings_new ("org.gtk.test.binding");
1364 obj = test_object_new ();
1366 g_settings_set_string (settings, "string", "true");
1368 g_settings_bind_with_mapping (settings, "string",
1370 G_SETTINGS_BIND_DEFAULT,
1375 g_settings_set_string (settings, "string", "false");
1376 g_object_get (obj, "bool", &b, NULL);
1377 g_assert_cmpint (b, ==, FALSE);
1379 g_settings_set_string (settings, "string", "not true");
1380 g_object_get (obj, "bool", &b, NULL);
1381 g_assert_cmpint (b, ==, FALSE);
1383 g_object_set (obj, "bool", TRUE, NULL);
1384 s = g_settings_get_string (settings, "string");
1385 g_assert_cmpstr (s, ==, "true");
1388 g_object_unref (obj);
1389 g_object_unref (settings);
1392 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1393 * initial settings value is transported to the object
1394 * side, but later settings changes do not affect the
1398 test_no_change_binding (void)
1401 GSettings *settings;
1404 settings = g_settings_new ("org.gtk.test.binding");
1405 obj = test_object_new ();
1407 g_object_set (obj, "bool", TRUE, NULL);
1408 g_settings_set_boolean (settings, "bool", FALSE);
1410 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1412 g_object_get (obj, "bool", &b, NULL);
1413 g_assert_cmpint (b, ==, FALSE);
1415 g_settings_set_boolean (settings, "bool", TRUE);
1416 g_object_get (obj, "bool", &b, NULL);
1417 g_assert_cmpint (b, ==, FALSE);
1419 g_settings_set_boolean (settings, "bool", FALSE);
1420 g_object_set (obj, "bool", TRUE, NULL);
1421 b = g_settings_get_boolean (settings, "bool");
1422 g_assert_cmpint (b, ==, TRUE);
1424 g_object_unref (obj);
1425 g_object_unref (settings);
1428 /* Test that binding a non-readable property only
1429 * works in 'GET' mode.
1432 test_no_read_binding (void)
1434 if (g_test_undefined ())
1436 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1439 GSettings *settings;
1441 settings = g_settings_new ("org.gtk.test.binding");
1442 obj = test_object_new ();
1444 g_settings_bind (settings, "string", obj, "no-read", 0);
1446 g_test_trap_assert_failed ();
1447 g_test_trap_assert_stderr ("*property*is not readable*");
1450 if (g_test_trap_fork (0, 0))
1453 GSettings *settings;
1455 settings = g_settings_new ("org.gtk.test.binding");
1456 obj = test_object_new ();
1458 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1462 g_test_trap_assert_passed ();
1465 /* Test that binding a non-writable property only
1466 * works in 'SET' mode.
1469 test_no_write_binding (void)
1471 if (g_test_undefined ())
1473 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1476 GSettings *settings;
1478 settings = g_settings_new ("org.gtk.test.binding");
1479 obj = test_object_new ();
1481 g_settings_bind (settings, "string", obj, "no-write", 0);
1483 g_test_trap_assert_failed ();
1484 g_test_trap_assert_stderr ("*property*is not writable*");
1487 if (g_test_trap_fork (0, 0))
1490 GSettings *settings;
1492 settings = g_settings_new ("org.gtk.test.binding");
1493 obj = test_object_new ();
1495 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1499 g_test_trap_assert_passed ();
1503 * Test that using a keyfile works
1508 GSettingsBackend *kf_backend;
1509 GSettings *settings;
1513 g_remove ("gsettings.store");
1515 kf_backend = g_keyfile_settings_backend_new ("gsettings.store", "/", "root");
1516 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1517 g_object_unref (kf_backend);
1519 g_settings_set (settings, "greeting", "s", "see if this works");
1521 keyfile = g_key_file_new ();
1522 g_assert (g_key_file_load_from_file (keyfile, "gsettings.store", 0, NULL));
1524 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1525 g_assert_cmpstr (str, ==, "'see if this works'");
1528 g_key_file_free (keyfile);
1529 g_object_unref (settings);
1532 /* Test that getting child schemas works
1535 test_child_schema (void)
1537 GSettings *settings;
1541 /* first establish some known conditions */
1542 settings = g_settings_new ("org.gtk.test.basic-types");
1543 g_settings_set (settings, "test-byte", "y", 36);
1545 g_settings_get (settings, "test-byte", "y", &byte);
1546 g_assert_cmpint (byte, ==, 36);
1548 g_object_unref (settings);
1550 settings = g_settings_new ("org.gtk.test");
1551 child = g_settings_get_child (settings, "basic-types");
1552 g_assert (child != NULL);
1554 g_settings_get (child, "test-byte", "y", &byte);
1555 g_assert_cmpint (byte, ==, 36);
1557 g_object_unref (child);
1558 g_object_unref (settings);
1562 glib_translations_work (void)
1566 gchar *orig = "Unnamed";
1568 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
1569 if (!setlocale (LC_MESSAGES, "de"))
1572 works = dgettext ("glib20", orig) != orig;
1573 setlocale (LC_MESSAGES, locale);
1579 #include "../strinfo.c"
1584 /* "foo" has a value of 1
1585 * "bar" has a value of 2
1586 * "baz" is an alias for "bar"
1589 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1590 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1592 const guint32 *strinfo = (guint32 *) array;
1593 guint length = sizeof array / 4;
1597 /* build it and compare */
1600 builder = g_string_new (NULL);
1601 strinfo_builder_append_item (builder, "foo", 1);
1602 strinfo_builder_append_item (builder, "bar", 2);
1603 g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
1604 g_assert_cmpint (builder->len % 4, ==, 0);
1605 g_assert_cmpint (builder->len / 4, ==, length);
1606 g_assert (memcmp (builder->str, strinfo, length * 4) == 0);
1607 g_string_free (builder, TRUE);
1610 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
1612 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
1614 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
1616 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
1619 g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
1620 g_assert_cmpint (result, ==, 1);
1621 g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
1622 g_assert_cmpint (result, ==, 2);
1623 g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
1624 g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
1626 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
1627 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
1628 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
1629 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
1631 g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
1632 g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
1633 g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
1634 g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
1640 GSettings *settings, *direct;
1643 settings = g_settings_new ("org.gtk.test.enums");
1644 direct = g_settings_new ("org.gtk.test.enums.direct");
1646 if (g_test_undefined () && !backend_set)
1648 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1649 g_settings_get_enum (direct, "test");
1650 g_test_trap_assert_failed ();
1651 g_test_trap_assert_stderr ("*not associated with an enum*");
1653 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1654 g_settings_set_enum (settings, "test", 42);
1655 g_test_trap_assert_failed ();
1656 g_test_trap_assert_stderr ("*invalid enum value 42*");
1658 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1659 g_settings_set_string (settings, "test", "qux");
1660 g_test_trap_assert_failed ();
1661 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1663 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1664 g_settings_get_flags (settings, "test");
1665 g_test_trap_assert_failed ();
1666 g_test_trap_assert_stderr ("*not associated with a flags*");
1669 str = g_settings_get_string (settings, "test");
1670 g_assert_cmpstr (str, ==, "bar");
1673 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
1675 str = g_settings_get_string (settings, "test");
1676 g_assert_cmpstr (str, ==, "foo");
1679 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
1681 g_settings_set_string (direct, "test", "qux");
1683 str = g_settings_get_string (direct, "test");
1684 g_assert_cmpstr (str, ==, "qux");
1687 str = g_settings_get_string (settings, "test");
1688 g_assert_cmpstr (str, ==, "quux");
1691 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
1697 GSettings *settings, *direct;
1701 settings = g_settings_new ("org.gtk.test.enums");
1702 direct = g_settings_new ("org.gtk.test.enums.direct");
1704 if (g_test_undefined () && !backend_set)
1706 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1707 g_settings_get_flags (direct, "test");
1708 g_test_trap_assert_failed ();
1709 g_test_trap_assert_stderr ("*not associated with a flags*");
1711 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1712 g_settings_set_flags (settings, "f-test", 0x42);
1713 g_test_trap_assert_failed ();
1714 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
1716 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1717 g_settings_set_strv (settings, "f-test",
1718 (const gchar **) g_strsplit ("rock", ",", 0));
1719 g_test_trap_assert_failed ();
1720 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1722 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1723 g_settings_get_enum (settings, "f-test");
1724 g_test_trap_assert_failed ();
1725 g_test_trap_assert_stderr ("*not associated with an enum*");
1728 strv = g_settings_get_strv (settings, "f-test");
1729 str = g_strjoinv (",", strv);
1730 g_assert_cmpstr (str, ==, "");
1734 g_settings_set_flags (settings, "f-test",
1735 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1737 strv = g_settings_get_strv (settings, "f-test");
1738 str = g_strjoinv (",", strv);
1739 g_assert_cmpstr (str, ==, "talking,walking");
1743 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1744 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1746 strv = g_strsplit ("speaking,laughing", ",", 0);
1747 g_settings_set_strv (direct, "f-test", (const gchar **) strv);
1750 strv = g_settings_get_strv (direct, "f-test");
1751 str = g_strjoinv (",", strv);
1752 g_assert_cmpstr (str, ==, "speaking,laughing");
1756 strv = g_settings_get_strv (settings, "f-test");
1757 str = g_strjoinv (",", strv);
1758 g_assert_cmpstr (str, ==, "talking,laughing");
1762 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1763 TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
1769 GSettings *settings, *direct;
1772 settings = g_settings_new ("org.gtk.test.range");
1773 direct = g_settings_new ("org.gtk.test.range.direct");
1775 if (g_test_undefined () && !backend_set)
1777 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1778 g_settings_set_int (settings, "val", 45);
1779 g_test_trap_assert_failed ();
1780 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1782 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1783 g_settings_set_int (settings, "val", 1);
1784 g_test_trap_assert_failed ();
1785 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1788 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1789 g_settings_set_int (direct, "val", 22);
1790 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
1791 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
1792 g_settings_set_int (direct, "val", 45);
1793 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
1794 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1795 g_settings_set_int (direct, "val", 1);
1796 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
1797 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1799 value = g_variant_new_int32 (1);
1800 g_assert (!g_settings_range_check (settings, "val", value));
1801 g_variant_unref (value);
1802 value = g_variant_new_int32 (33);
1803 g_assert (g_settings_range_check (settings, "val", value));
1804 g_variant_unref (value);
1805 value = g_variant_new_int32 (45);
1806 g_assert (!g_settings_range_check (settings, "val", value));
1807 g_variant_unref (value);
1811 strv_has_string (gchar **haystack,
1812 const gchar *needle)
1816 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
1818 if (g_strcmp0 (haystack[n], needle) == 0)
1825 strv_set_equal (gchar **strv, ...)
1834 va_start (list, strv);
1837 str = va_arg (list, const gchar *);
1840 if (!strv_has_string (strv, str))
1850 res = g_strv_length ((gchar**)strv) == count;
1856 test_list_items (void)
1858 GSettings *settings;
1862 settings = g_settings_new ("org.gtk.test");
1863 children = g_settings_list_children (settings);
1864 keys = g_settings_list_keys (settings);
1866 g_assert (strv_set_equal (children, "basic-types", "complex-types", "localized", NULL));
1867 g_assert (strv_set_equal (keys, "greeting", "farewell", NULL));
1869 g_strfreev (children);
1872 g_object_unref (settings);
1876 test_list_schemas (void)
1878 const gchar * const *schemas;
1879 const gchar * const *relocs;
1881 relocs = g_settings_list_relocatable_schemas ();
1882 schemas = g_settings_list_schemas ();
1884 g_assert (strv_set_equal ((gchar **)relocs,
1885 "org.gtk.test.no-path",
1888 g_assert (strv_set_equal ((gchar **)schemas,
1890 "org.gtk.test.basic-types",
1891 "org.gtk.test.complex-types",
1892 "org.gtk.test.localized",
1893 "org.gtk.test.binding",
1894 "org.gtk.test.enums",
1895 "org.gtk.test.enums.direct",
1896 "org.gtk.test.range",
1897 "org.gtk.test.range.direct",
1898 "org.gtk.test.mapped",
1903 map_func (GVariant *value,
1907 gint *state = user_data;
1911 v = g_variant_get_int32 (value);
1917 g_assert_cmpint (v, ==, 1);
1921 else if (*state == 1)
1923 g_assert_cmpint (v, ==, 0);
1929 g_assert (value == NULL);
1930 *result = g_variant_new_int32 (5);
1936 test_get_mapped (void)
1938 GSettings *settings;
1943 settings = g_settings_new ("org.gtk.test.mapped");
1944 g_settings_set_int (settings, "val", 1);
1947 p = g_settings_get_mapped (settings, "val", map_func, &state);
1948 val = g_variant_get_int32 ((GVariant*)p);
1949 g_assert_cmpint (val, ==, 5);
1951 g_variant_unref (p);
1952 g_object_unref (settings);
1956 test_get_range (void)
1958 GSettings *settings;
1961 settings = g_settings_new ("org.gtk.test.range");
1962 range = g_settings_get_range (settings, "val");
1963 check_and_free (range, "('range', <(2, 44)>)");
1964 g_object_unref (settings);
1966 settings = g_settings_new ("org.gtk.test.enums");
1967 range = g_settings_get_range (settings, "test");
1968 check_and_free (range, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
1969 g_object_unref (settings);
1971 settings = g_settings_new ("org.gtk.test.enums");
1972 range = g_settings_get_range (settings, "f-test");
1973 check_and_free (range, "('flags', "
1974 "<['mourning', 'laughing', 'talking', 'walking']>)");
1975 g_object_unref (settings);
1977 settings = g_settings_new ("org.gtk.test");
1978 range = g_settings_get_range (settings, "greeting");
1979 check_and_free (range, "('type', <@as []>)");
1980 g_object_unref (settings);
1984 test_schema_source (void)
1986 GSettingsSchemaSource *parent;
1987 GSettingsSchemaSource *source;
1988 GSettingsBackend *backend;
1989 GSettingsSchema *schema;
1990 GError *error = NULL;
1991 GSettings *settings;
1994 backend = g_settings_backend_get_default ();
1996 /* make sure it fails properly */
1997 parent = g_settings_schema_source_get_default ();
1998 source = g_settings_schema_source_new_from_directory ("/path/that/does/not/exist", parent, TRUE, &error);
1999 g_assert (source == NULL);
2000 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
2001 g_clear_error (&error);
2003 /* create a source with the parent */
2004 source = g_settings_schema_source_new_from_directory ("schema-source", parent, TRUE, &error);
2005 g_assert_no_error (error);
2006 g_assert (source != NULL);
2008 /* check recursive lookups are working */
2009 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2010 g_assert (schema != NULL);
2011 g_settings_schema_unref (schema);
2013 /* check recursive lookups for non-existent schemas */
2014 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", TRUE);
2015 g_assert (schema == NULL);
2017 /* check non-recursive for schema that only exists in lower layers */
2018 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2019 g_assert (schema == NULL);
2021 /* check non-recursive lookup for non-existent */
2022 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", FALSE);
2023 g_assert (schema == NULL);
2025 /* check non-recursive for schema that exists in toplevel */
2026 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2027 g_assert (schema != NULL);
2028 g_settings_schema_unref (schema);
2030 /* check recursive for schema that exists in toplevel */
2031 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2032 g_assert (schema != NULL);
2034 /* try to use it for something */
2035 settings = g_settings_new_full (schema, backend, g_settings_schema_get_path (schema));
2036 g_settings_schema_unref (schema);
2038 g_settings_get (settings, "enabled", "b", &enabled);
2040 g_object_unref (settings);
2042 g_settings_schema_source_unref (source);
2044 /* try again, but with no parent */
2045 source = g_settings_schema_source_new_from_directory ("schema-source", NULL, FALSE, NULL);
2046 g_assert (source != NULL);
2048 /* should not find it this time, even if recursive... */
2049 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2050 g_assert (schema == NULL);
2051 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2052 g_assert (schema == NULL);
2054 /* should still find our own... */
2055 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2056 g_assert (schema != NULL);
2057 g_settings_schema_unref (schema);
2058 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2059 g_assert (schema != NULL);
2060 g_settings_schema_unref (schema);
2062 g_settings_schema_source_unref (source);
2068 GAction *string, *toggle;
2069 gboolean c1, c2, c3;
2070 GSettings *settings;
2072 GVariantType *param_type;
2074 GVariantType *state_type;
2077 settings = g_settings_new ("org.gtk.test.basic-types");
2078 string = g_settings_create_action (settings, "test-string");
2079 toggle = g_settings_create_action (settings, "test-boolean");
2080 g_object_unref (settings); /* should be held by the actions */
2082 g_signal_connect (settings, "changed", G_CALLBACK (changed_cb2), &c1);
2083 g_signal_connect (string, "notify::state", G_CALLBACK (changed_cb2), &c2);
2084 g_signal_connect (toggle, "notify::state", G_CALLBACK (changed_cb2), &c3);
2086 c1 = c2 = c3 = FALSE;
2087 g_settings_set_string (settings, "test-string", "hello world");
2088 check_and_free (g_action_get_state (string), "'hello world'");
2089 g_assert (c1 && c2 && !c3);
2090 c1 = c2 = c3 = FALSE;
2092 g_action_activate (string, g_variant_new_string ("hihi"));
2093 check_and_free (g_settings_get_value (settings, "test-string"), "'hihi'");
2094 g_assert (c1 && c2 && !c3);
2095 c1 = c2 = c3 = FALSE;
2097 g_action_change_state (string, g_variant_new_string ("kthxbye"));
2098 check_and_free (g_settings_get_value (settings, "test-string"), "'kthxbye'");
2099 g_assert (c1 && c2 && !c3);
2100 c1 = c2 = c3 = FALSE;
2102 g_action_change_state (toggle, g_variant_new_boolean (TRUE));
2103 g_assert (g_settings_get_boolean (settings, "test-boolean"));
2104 g_assert (c1 && !c2 && c3);
2105 c1 = c2 = c3 = FALSE;
2107 g_action_activate (toggle, NULL);
2108 g_assert (!g_settings_get_boolean (settings, "test-boolean"));
2109 g_assert (c1 && !c2 && c3);
2111 g_object_get (string,
2113 "parameter-type", ¶m_type,
2114 "enabled", &enabled,
2115 "state-type", &state_type,
2119 g_assert_cmpstr (name, ==, "test-string");
2120 g_assert (g_variant_type_equal (param_type, G_VARIANT_TYPE_STRING));
2122 g_assert (g_variant_type_equal (state_type, G_VARIANT_TYPE_STRING));
2123 g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "kthxbye");
2126 g_variant_unref (state);
2128 g_object_unref (string);
2129 g_object_unref (toggle);
2133 main (int argc, char *argv[])
2138 setlocale (LC_ALL, "");
2140 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
2142 g_setenv ("XDG_DATA_DIRS", ".", TRUE);
2143 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
2146 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
2149 g_test_init (&argc, &argv, NULL);
2151 g_remove ("org.gtk.test.enums.xml");
2152 g_assert (g_spawn_command_line_sync ("../../gobject/glib-mkenums "
2153 "--template " SRCDIR "/enums.xml.template "
2154 SRCDIR "/testenum.h",
2155 &enums, NULL, &result, NULL));
2156 g_assert (result == 0);
2157 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
2160 g_remove ("gschemas.compiled");
2161 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=. "
2162 "--schema-file=org.gtk.test.enums.xml "
2163 "--schema-file=" SRCDIR "/org.gtk.test.gschema.xml",
2164 NULL, NULL, &result, NULL));
2165 g_assert (result == 0);
2167 g_remove ("schema-source/gschemas.compiled");
2168 g_mkdir ("schema-source", 0777);
2169 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=schema-source "
2170 "--schema-file=" SRCDIR "/org.gtk.schemasourcecheck.gschema.xml",
2171 NULL, NULL, &result, NULL));
2172 g_assert (result == 0);
2174 g_test_add_func ("/gsettings/basic", test_basic);
2178 g_test_add_func ("/gsettings/no-schema", test_no_schema);
2179 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
2180 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
2181 g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
2182 g_test_add_func ("/gsettings/no-path", test_no_path);
2185 g_test_add_func ("/gsettings/basic-types", test_basic_types);
2186 g_test_add_func ("/gsettings/complex-types", test_complex_types);
2187 g_test_add_func ("/gsettings/changes", test_changes);
2189 if (glib_translations_work ())
2191 g_test_add_func ("/gsettings/l10n", test_l10n);
2192 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
2195 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
2196 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
2197 g_test_add_func ("/gsettings/atomic", test_atomic);
2199 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
2200 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
2201 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
2202 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
2203 g_test_add_func ("/gsettings/unbinding", test_unbind);
2204 g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
2208 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
2209 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
2210 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
2213 g_test_add_func ("/gsettings/keyfile", test_keyfile);
2214 g_test_add_func ("/gsettings/child-schema", test_child_schema);
2215 g_test_add_func ("/gsettings/strinfo", test_strinfo);
2216 g_test_add_func ("/gsettings/enums", test_enums);
2217 g_test_add_func ("/gsettings/flags", test_flags);
2218 g_test_add_func ("/gsettings/range", test_range);
2219 g_test_add_func ("/gsettings/list-items", test_list_items);
2220 g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
2221 g_test_add_func ("/gsettings/mapped", test_get_mapped);
2222 g_test_add_func ("/gsettings/get-range", test_get_range);
2223 g_test_add_func ("/gsettings/schema-source", test_schema_source);
2224 g_test_add_func ("/gsettings/actions", test_actions);
2226 result = g_test_run ();