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 GSettings *tmp_settings = g_settings_new ("org.gtk.test");
60 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
61 "*g_settings_set_value*expects type*");
62 g_settings_set (tmp_settings, "greeting", "i", 555);
63 g_test_assert_expected_messages ();
65 g_object_unref (tmp_settings);
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_subprocess (void)
91 settings = g_settings_new ("org.gtk.test");
92 value = g_settings_get_value (settings, "no_such_key");
94 g_assert (value == NULL);
96 g_object_unref (settings);
100 test_unknown_key (void)
102 if (!g_test_undefined ())
105 g_test_trap_subprocess ("/gsettings/unknown-key/subprocess", 0, 0);
106 g_test_trap_assert_failed ();
107 g_test_trap_assert_stderr ("*does not contain*");
110 /* Check that we get an error when the schema
111 * has not been installed
114 test_no_schema_subprocess (void)
118 settings = g_settings_new ("no.such.schema");
120 g_assert (settings == NULL);
124 test_no_schema (void)
126 if (!g_test_undefined ())
129 g_test_trap_subprocess ("/gsettings/no-schema/subprocess", 0, 0);
130 g_test_trap_assert_failed ();
131 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
134 /* Check that we get an error when passing a type string
135 * that does not match the schema
138 test_wrong_type (void)
143 if (!g_test_undefined ())
146 settings = g_settings_new ("org.gtk.test");
148 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
149 "*given value has a type of*");
150 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
151 "*valid_format_string*");
152 g_settings_get (settings, "greeting", "o", &str);
153 g_test_assert_expected_messages ();
155 g_assert (str == NULL);
157 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
158 "*expects type 's'*");
159 g_settings_set (settings, "greeting", "o", "/a/path");
160 g_test_assert_expected_messages ();
162 g_object_unref (settings);
165 /* Check errors with explicit paths */
167 test_wrong_path_subprocess (void)
169 GSettings *settings G_GNUC_UNUSED;
171 settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
175 test_wrong_path (void)
177 if (!g_test_undefined ())
180 g_test_trap_subprocess ("/gsettings/wrong-path/subprocess", 0, 0);
181 g_test_trap_assert_failed ();
182 g_test_trap_assert_stderr ("*but path * specified by schema*");
186 test_no_path_subprocess (void)
188 GSettings *settings G_GNUC_UNUSED;
190 settings = g_settings_new ("org.gtk.test.no-path");
196 if (!g_test_undefined ())
199 g_test_trap_subprocess ("/gsettings/no-path/subprocess", 0, 0);
200 g_test_trap_assert_failed ();
201 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
205 /* Check that we can successfully read and set the full
206 * range of all basic types
209 test_basic_types (void)
223 settings = g_settings_new ("org.gtk.test.basic-types");
225 g_settings_get (settings, "test-boolean", "b", &b);
226 g_assert_cmpint (b, ==, 1);
228 g_settings_set (settings, "test-boolean", "b", 0);
229 g_settings_get (settings, "test-boolean", "b", &b);
230 g_assert_cmpint (b, ==, 0);
232 g_settings_get (settings, "test-byte", "y", &byte);
233 g_assert_cmpint (byte, ==, 25);
235 g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
236 g_settings_get (settings, "test-byte", "y", &byte);
237 g_assert_cmpint (byte, ==, G_MAXUINT8);
239 g_settings_get (settings, "test-int16", "n", &i16);
240 g_assert_cmpint (i16, ==, -1234);
242 g_settings_set (settings, "test-int16", "n", G_MININT16);
243 g_settings_get (settings, "test-int16", "n", &i16);
244 g_assert_cmpint (i16, ==, G_MININT16);
246 g_settings_set (settings, "test-int16", "n", G_MAXINT16);
247 g_settings_get (settings, "test-int16", "n", &i16);
248 g_assert_cmpint (i16, ==, G_MAXINT16);
250 g_settings_get (settings, "test-uint16", "q", &u16);
251 g_assert_cmpuint (u16, ==, 1234);
253 g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
254 g_settings_get (settings, "test-uint16", "q", &u16);
255 g_assert_cmpuint (u16, ==, G_MAXUINT16);
257 g_settings_get (settings, "test-int32", "i", &i32);
258 g_assert_cmpint (i32, ==, -123456);
260 g_settings_set (settings, "test-int32", "i", G_MININT32);
261 g_settings_get (settings, "test-int32", "i", &i32);
262 g_assert_cmpint (i32, ==, G_MININT32);
264 g_settings_set (settings, "test-int32", "i", G_MAXINT32);
265 g_settings_get (settings, "test-int32", "i", &i32);
266 g_assert_cmpint (i32, ==, G_MAXINT32);
268 g_settings_get (settings, "test-uint32", "u", &u32);
269 g_assert_cmpuint (u32, ==, 123456);
271 g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
272 g_settings_get (settings, "test-uint32", "u", &u32);
273 g_assert_cmpuint (u32, ==, G_MAXUINT32);
275 g_settings_get (settings, "test-int64", "x", &i64);
276 g_assert_cmpuint (i64, ==, -123456789);
278 g_settings_set (settings, "test-int64", "x", G_MININT64);
279 g_settings_get (settings, "test-int64", "x", &i64);
280 g_assert_cmpuint (i64, ==, G_MININT64);
282 g_settings_set (settings, "test-int64", "x", G_MAXINT64);
283 g_settings_get (settings, "test-int64", "x", &i64);
284 g_assert_cmpuint (i64, ==, G_MAXINT64);
286 g_settings_get (settings, "test-uint64", "t", &u64);
287 g_assert_cmpuint (u64, ==, 123456789);
289 g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
290 g_settings_get (settings, "test-uint64", "t", &u64);
291 g_assert_cmpuint (u64, ==, G_MAXUINT64);
293 g_settings_get (settings, "test-double", "d", &d);
294 g_assert_cmpfloat (d, ==, 123.456);
296 g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
297 g_settings_get (settings, "test-double", "d", &d);
298 g_assert_cmpfloat (d, ==, G_MINDOUBLE);
300 g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
301 g_settings_get (settings, "test-double", "d", &d);
302 g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
304 g_settings_get (settings, "test-string", "s", &str);
305 g_assert_cmpstr (str, ==, "a string, it seems");
309 g_settings_get (settings, "test-objectpath", "o", &str);
310 g_assert_cmpstr (str, ==, "/a/object/path");
311 g_object_unref (settings);
316 /* Check that we can read an set complex types like
317 * tuples, arrays and dictionaries
320 test_complex_types (void)
325 GVariantIter *iter = NULL;
327 settings = g_settings_new ("org.gtk.test.complex-types");
329 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
330 g_assert_cmpstr (s, ==, "one");
331 g_assert_cmpint (i1,==, 2);
332 g_assert_cmpint (i2,==, 3);
336 g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
337 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
338 g_assert_cmpstr (s, ==, "none");
339 g_assert_cmpint (i1,==, 0);
340 g_assert_cmpint (i2,==, 0);
344 g_settings_get (settings, "test-array", "ai", &iter);
345 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
346 g_assert (g_variant_iter_next (iter, "i", &i1));
347 g_assert_cmpint (i1, ==, 0);
348 g_assert (g_variant_iter_next (iter, "i", &i1));
349 g_assert_cmpint (i1, ==, 1);
350 g_assert (g_variant_iter_next (iter, "i", &i1));
351 g_assert_cmpint (i1, ==, 2);
352 g_assert (g_variant_iter_next (iter, "i", &i1));
353 g_assert_cmpint (i1, ==, 3);
354 g_assert (g_variant_iter_next (iter, "i", &i1));
355 g_assert_cmpint (i1, ==, 4);
356 g_assert (g_variant_iter_next (iter, "i", &i1));
357 g_assert_cmpint (i1, ==, 5);
358 g_assert (!g_variant_iter_next (iter, "i", &i1));
359 g_variant_iter_free (iter);
361 g_object_unref (settings);
364 static gboolean changed_cb_called;
367 changed_cb (GSettings *settings,
371 changed_cb_called = TRUE;
373 g_assert_cmpstr (key, ==, data);
376 /* Test that basic change notification with the changed signal works.
382 GSettings *settings2;
384 settings = g_settings_new ("org.gtk.test");
386 g_signal_connect (settings, "changed",
387 G_CALLBACK (changed_cb), "greeting");
389 changed_cb_called = FALSE;
391 g_settings_set (settings, "greeting", "s", "new greeting");
392 g_assert (changed_cb_called);
394 settings2 = g_settings_new ("org.gtk.test");
396 changed_cb_called = FALSE;
398 g_settings_set (settings2, "greeting", "s", "hi");
399 g_assert (changed_cb_called);
401 g_object_unref (settings2);
402 g_object_unref (settings);
405 static gboolean changed_cb_called2;
408 changed_cb2 (GSettings *settings,
417 /* Test that changes done to a delay-mode instance
418 * don't appear to the outside world until apply. Also
419 * check that we get change notification when they are
421 * Also test that the has-unapplied property is properly
425 test_delay_apply (void)
428 GSettings *settings2;
431 settings = g_settings_new ("org.gtk.test");
432 settings2 = g_settings_new ("org.gtk.test");
434 g_settings_set (settings2, "greeting", "s", "top o' the morning");
436 changed_cb_called = FALSE;
437 changed_cb_called2 = FALSE;
439 g_signal_connect (settings, "changed",
440 G_CALLBACK (changed_cb2), &changed_cb_called);
441 g_signal_connect (settings2, "changed",
442 G_CALLBACK (changed_cb2), &changed_cb_called2);
444 g_settings_delay (settings);
446 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
448 g_assert (changed_cb_called);
449 g_assert (!changed_cb_called2);
451 g_settings_get (settings, "greeting", "s", &str);
452 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
456 g_settings_get (settings2, "greeting", "s", &str);
457 g_assert_cmpstr (str, ==, "top o' the morning");
461 g_assert (g_settings_get_has_unapplied (settings));
462 g_assert (!g_settings_get_has_unapplied (settings2));
464 changed_cb_called = FALSE;
465 changed_cb_called2 = FALSE;
467 g_settings_apply (settings);
469 g_assert (!changed_cb_called);
470 g_assert (changed_cb_called2);
472 g_settings_get (settings, "greeting", "s", &str);
473 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
477 g_settings_get (settings2, "greeting", "s", &str);
478 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
482 g_assert (!g_settings_get_has_unapplied (settings));
483 g_assert (!g_settings_get_has_unapplied (settings2));
485 g_object_unref (settings2);
486 g_object_unref (settings);
489 /* Test that reverting unapplied changes in a delay-apply
490 * settings instance works.
493 test_delay_revert (void)
496 GSettings *settings2;
499 settings = g_settings_new ("org.gtk.test");
500 settings2 = g_settings_new ("org.gtk.test");
502 g_settings_set (settings2, "greeting", "s", "top o' the morning");
504 g_settings_delay (settings);
506 g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
508 g_settings_get (settings, "greeting", "s", &str);
509 g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
513 g_settings_get (settings2, "greeting", "s", &str);
514 g_assert_cmpstr (str, ==, "top o' the morning");
518 g_assert (g_settings_get_has_unapplied (settings));
520 g_settings_revert (settings);
522 g_assert (!g_settings_get_has_unapplied (settings));
524 g_settings_get (settings, "greeting", "s", &str);
525 g_assert_cmpstr (str, ==, "top o' the morning");
529 g_settings_get (settings2, "greeting", "s", &str);
530 g_assert_cmpstr (str, ==, "top o' the morning");
534 g_object_unref (settings2);
535 g_object_unref (settings);
539 keys_changed_cb (GSettings *settings,
545 g_assert_cmpint (n_keys, ==, 2);
547 g_assert ((keys[0] == g_quark_from_static_string ("greeting") &&
548 keys[1] == g_quark_from_static_string ("farewell")) ||
549 (keys[1] == g_quark_from_static_string ("greeting") &&
550 keys[0] == g_quark_from_static_string ("farewell")));
552 g_settings_get (settings, "greeting", "s", &str);
553 g_assert_cmpstr (str, ==, "greetings from test_atomic");
557 g_settings_get (settings, "farewell", "s", &str);
558 g_assert_cmpstr (str, ==, "atomic bye-bye");
563 /* Check that delay-applied changes appear atomically.
564 * More specifically, verify that all changed keys appear
565 * with their new value while handling the change-event signal.
571 GSettings *settings2;
574 settings = g_settings_new ("org.gtk.test");
575 settings2 = g_settings_new ("org.gtk.test");
577 g_settings_set (settings2, "greeting", "s", "top o' the morning");
579 changed_cb_called = FALSE;
580 changed_cb_called2 = FALSE;
582 g_signal_connect (settings2, "change-event",
583 G_CALLBACK (keys_changed_cb), NULL);
585 g_settings_delay (settings);
587 g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
588 g_settings_set (settings, "farewell", "s", "atomic bye-bye");
590 g_settings_apply (settings);
592 g_settings_get (settings, "greeting", "s", &str);
593 g_assert_cmpstr (str, ==, "greetings from test_atomic");
597 g_settings_get (settings, "farewell", "s", &str);
598 g_assert_cmpstr (str, ==, "atomic bye-bye");
602 g_settings_get (settings2, "greeting", "s", &str);
603 g_assert_cmpstr (str, ==, "greetings from test_atomic");
607 g_settings_get (settings2, "farewell", "s", &str);
608 g_assert_cmpstr (str, ==, "atomic bye-bye");
612 g_object_unref (settings2);
613 g_object_unref (settings);
616 /* On Windows the interaction between the C library locale and libintl
617 * (from GNU gettext) is not like on POSIX, so just skip these tests
620 * There are several issues:
622 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
623 * in libintl (defined in its <libintl.h>).
625 * 2) The locale names that setlocale() accepts and returns aren't in
626 * the "de_DE" style, but like "German_Germany".
628 * 3) libintl looks at the Win32 thread locale and not the C library
629 * locale. (And even if libintl would use the C library's locale, as
630 * there are several alternative C library DLLs, libintl might be
631 * linked to a different one than the application code, so they
632 * wouldn't have the same C library locale anyway.)
635 /* Test that translations work for schema defaults.
637 * This test relies on the de.po file in the same directory
638 * to be compiled into ./de/LC_MESSAGES/test.mo
647 bindtextdomain ("test", ".");
648 bind_textdomain_codeset ("test", "UTF-8");
650 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
652 settings = g_settings_new ("org.gtk.test.localized");
654 setlocale (LC_MESSAGES, "C");
655 str = g_settings_get_string (settings, "error-message");
656 setlocale (LC_MESSAGES, locale);
658 g_assert_cmpstr (str, ==, "Unnamed");
662 setlocale (LC_MESSAGES, "de_DE");
663 /* Only do the test if translation is actually working... */
664 if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
666 str = g_settings_get_string (settings, "error-message");
668 g_assert_cmpstr (str, ==, "Unbenannt");
669 g_object_unref (settings);
674 g_printerr ("warning: translation is not working... skipping test. ");
676 setlocale (LC_MESSAGES, locale);
680 /* Test that message context works as expected with translated
681 * schema defaults. Also, verify that non-ASCII UTF-8 content
684 * This test relies on the de.po file in the same directory
685 * to be compiled into ./de/LC_MESSAGES/test.mo
688 test_l10n_context (void)
694 bindtextdomain ("test", ".");
695 bind_textdomain_codeset ("test", "UTF-8");
697 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
699 settings = g_settings_new ("org.gtk.test.localized");
701 setlocale (LC_MESSAGES, "C");
702 g_settings_get (settings, "backspace", "s", &str);
703 setlocale (LC_MESSAGES, locale);
705 g_assert_cmpstr (str, ==, "BackSpace");
709 setlocale (LC_MESSAGES, "de_DE");
710 /* Only do the test if translation is actually working... */
711 if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
713 g_settings_get (settings, "backspace", "s", &str);
715 g_assert_cmpstr (str, ==, "Löschen");
716 g_object_unref (settings);
721 g_printerr ("warning: translation is not working... skipping test. ");
723 setlocale (LC_MESSAGES, locale);
750 GObject parent_instance;
753 gboolean anti_bool_prop;
764 gchar *no_write_prop;
772 GObjectClass parent_class;
775 static GType test_object_get_type (void);
776 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
779 test_object_init (TestObject *object)
784 test_object_finalize (GObject *object)
786 TestObject *testo = (TestObject*)object;
787 g_strfreev (testo->strv_prop);
788 g_free (testo->string_prop);
789 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
793 test_object_get_property (GObject *object,
798 TestObject *test_object = (TestObject *)object;
803 g_value_set_boolean (value, test_object->bool_prop);
806 g_value_set_boolean (value, test_object->anti_bool_prop);
809 g_value_set_schar (value, test_object->byte_prop);
812 g_value_set_uint (value, test_object->uint16_prop);
815 g_value_set_int (value, test_object->int16_prop);
818 g_value_set_int (value, test_object->int_prop);
821 g_value_set_uint (value, test_object->uint_prop);
824 g_value_set_int64 (value, test_object->int64_prop);
827 g_value_set_uint64 (value, test_object->uint64_prop);
830 g_value_set_double (value, test_object->double_prop);
833 g_value_set_string (value, test_object->string_prop);
836 g_value_set_string (value, test_object->no_write_prop);
839 g_value_set_boxed (value, test_object->strv_prop);
842 g_value_set_enum (value, test_object->enum_prop);
845 g_value_set_flags (value, test_object->flags_prop);
848 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
854 test_object_set_property (GObject *object,
859 TestObject *test_object = (TestObject *)object;
864 test_object->bool_prop = g_value_get_boolean (value);
867 test_object->anti_bool_prop = g_value_get_boolean (value);
870 test_object->byte_prop = g_value_get_schar (value);
873 test_object->int16_prop = g_value_get_int (value);
876 test_object->uint16_prop = g_value_get_uint (value);
879 test_object->int_prop = g_value_get_int (value);
882 test_object->uint_prop = g_value_get_uint (value);
885 test_object->int64_prop = g_value_get_int64 (value);
888 test_object->uint64_prop = g_value_get_uint64 (value);
891 test_object->double_prop = g_value_get_double (value);
894 g_free (test_object->string_prop);
895 test_object->string_prop = g_value_dup_string (value);
898 g_free (test_object->no_read_prop);
899 test_object->no_read_prop = g_value_dup_string (value);
902 g_strfreev (test_object->strv_prop);
903 test_object->strv_prop = g_value_dup_boxed (value);
906 test_object->enum_prop = g_value_get_enum (value);
909 test_object->flags_prop = g_value_get_flags (value);
912 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
918 test_enum_get_type (void)
920 static volatile gsize define_type_id = 0;
922 if (g_once_init_enter (&define_type_id))
924 static const GEnumValue values[] = {
925 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
926 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
927 { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
928 { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
932 GType type_id = g_enum_register_static ("TestEnum", values);
933 g_once_init_leave (&define_type_id, type_id);
936 return define_type_id;
940 test_flags_get_type (void)
942 static volatile gsize define_type_id = 0;
944 if (g_once_init_enter (&define_type_id))
946 static const GFlagsValue values[] = {
947 { TEST_FLAGS_NONE, "TEST_FLAGS_NONE", "none" },
948 { TEST_FLAGS_MOURNING, "TEST_FLAGS_MOURNING", "mourning" },
949 { TEST_FLAGS_LAUGHING, "TEST_FLAGS_LAUGHING", "laughing" },
950 { TEST_FLAGS_WALKING, "TEST_FLAGS_WALKING", "walking" },
954 GType type_id = g_flags_register_static ("TestFlags", values);
955 g_once_init_leave (&define_type_id, type_id);
958 return define_type_id;
962 test_object_class_init (TestObjectClass *class)
964 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
966 gobject_class->get_property = test_object_get_property;
967 gobject_class->set_property = test_object_set_property;
968 gobject_class->finalize = test_object_finalize;
970 g_object_class_install_property (gobject_class, PROP_BOOL,
971 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
972 g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
973 g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
974 g_object_class_install_property (gobject_class, PROP_BYTE,
975 g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
976 g_object_class_install_property (gobject_class, PROP_INT16,
977 g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
978 g_object_class_install_property (gobject_class, PROP_UINT16,
979 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
980 g_object_class_install_property (gobject_class, PROP_INT,
981 g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
982 g_object_class_install_property (gobject_class, PROP_UINT,
983 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
984 g_object_class_install_property (gobject_class, PROP_INT64,
985 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
986 g_object_class_install_property (gobject_class, PROP_UINT64,
987 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
988 g_object_class_install_property (gobject_class, PROP_DOUBLE,
989 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
990 g_object_class_install_property (gobject_class, PROP_STRING,
991 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
992 g_object_class_install_property (gobject_class, PROP_NO_WRITE,
993 g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
994 g_object_class_install_property (gobject_class, PROP_NO_READ,
995 g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
996 g_object_class_install_property (gobject_class, PROP_STRV,
997 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
998 g_object_class_install_property (gobject_class, PROP_ENUM,
999 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
1000 g_object_class_install_property (gobject_class, PROP_FLAGS,
1001 g_param_spec_flags ("flags", "", "", test_flags_get_type (), TEST_FLAGS_NONE, G_PARAM_READWRITE));
1005 test_object_new (void)
1007 return (TestObject*)g_object_new (test_object_get_type (), NULL);
1010 /* Test basic binding functionality for simple types.
1011 * Verify that with bidirectional bindings, changes on either side
1012 * are notified on the other end.
1015 test_simple_binding (void)
1018 GSettings *settings;
1034 settings = g_settings_new ("org.gtk.test.binding");
1035 obj = test_object_new ();
1037 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
1038 g_object_set (obj, "bool", TRUE, NULL);
1039 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1041 g_settings_set_boolean (settings, "bool", FALSE);
1043 g_object_get (obj, "bool", &b, NULL);
1044 g_assert_cmpint (b, ==, FALSE);
1046 g_settings_bind (settings, "anti-bool", obj, "anti-bool",
1047 G_SETTINGS_BIND_INVERT_BOOLEAN);
1048 g_object_set (obj, "anti-bool", FALSE, NULL);
1049 g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
1051 g_settings_set_boolean (settings, "anti-bool", FALSE);
1053 g_object_get (obj, "anti-bool", &b, NULL);
1054 g_assert_cmpint (b, ==, TRUE);
1056 g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
1058 g_object_set (obj, "byte", 123, NULL);
1060 g_settings_get (settings, "byte", "y", &y);
1061 g_assert_cmpint (y, ==, 123);
1063 g_settings_set (settings, "byte", "y", 54);
1065 g_object_get (obj, "byte", &y, NULL);
1066 g_assert_cmpint (y, ==, 54);
1068 g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
1070 g_object_set (obj, "int16", 1234, NULL);
1072 g_settings_get (settings, "int16", "n", &n);
1073 g_assert_cmpint (n, ==, 1234);
1075 g_settings_set (settings, "int16", "n", 4321);
1077 g_object_get (obj, "int16", &n2, NULL);
1078 g_assert_cmpint (n2, ==, 4321);
1080 g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
1082 g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1084 g_settings_get (settings, "uint16", "q", &q);
1085 g_assert_cmpuint (q, ==, G_MAXUINT16);
1087 g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1089 g_object_get (obj, "uint16", &q2, NULL);
1090 g_assert_cmpuint (q2, ==, (guint16) G_MAXINT16);
1092 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1094 g_object_set (obj, "int", 12345, NULL);
1095 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1097 g_settings_set_int (settings, "int", 54321);
1099 g_object_get (obj, "int", &i, NULL);
1100 g_assert_cmpint (i, ==, 54321);
1102 g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1104 g_object_set (obj, "uint", 12345, NULL);
1105 g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);
1107 g_settings_set_uint (settings, "uint", 54321);
1109 g_object_get (obj, "uint", &u, NULL);
1110 g_assert_cmpuint (u, ==, 54321);
1112 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1114 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1116 g_settings_get (settings, "int64", "x", &i64);
1117 g_assert_cmpint (i64, ==, G_MAXINT64);
1119 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1121 g_object_get (obj, "int64", &i64, NULL);
1122 g_assert_cmpint (i64, ==, G_MININT64);
1124 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1126 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1128 g_settings_get (settings, "uint64", "t", &u64);
1129 g_assert_cmpuint (u64, ==, G_MAXUINT64);
1131 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1133 g_object_get (obj, "uint64", &u64, NULL);
1134 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1136 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1138 g_object_set (obj, "string", "bu ba", NULL);
1139 s = g_settings_get_string (settings, "string");
1140 g_assert_cmpstr (s, ==, "bu ba");
1143 g_settings_set_string (settings, "string", "bla bla");
1144 g_object_get (obj, "string", &s, NULL);
1145 g_assert_cmpstr (s, ==, "bla bla");
1148 g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1150 g_object_set (obj, "string", "non-unicode:\315", NULL);
1151 value = g_settings_get_value (settings, "chararray");
1152 g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1153 g_variant_unref (value);
1155 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1157 g_object_set (obj, "double", G_MAXFLOAT, NULL);
1158 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1160 g_settings_set_double (settings, "double", G_MINFLOAT);
1162 g_object_get (obj, "double", &d, NULL);
1163 g_assert_cmpfloat (d, ==, G_MINFLOAT);
1165 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1166 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1168 g_settings_set_double (settings, "double", -G_MINDOUBLE);
1170 g_object_get (obj, "double", &d, NULL);
1171 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1173 strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1174 g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1175 g_object_set (obj, "strv", strv, NULL);
1177 strv = g_settings_get_strv (settings, "strv");
1178 s = g_strjoinv (",", strv);
1179 g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1182 strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1183 g_settings_set_strv (settings, "strv", (const gchar **) strv);
1185 g_object_get (obj, "strv", &strv, NULL);
1186 s = g_strjoinv (",", strv);
1187 g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1191 g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1192 g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1193 s = g_settings_get_string (settings, "enum");
1194 g_assert_cmpstr (s, ==, "baz");
1196 g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1198 g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1200 g_object_get (obj, "enum", &i, NULL);
1201 g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1203 g_settings_set_string (settings, "enum", "baz");
1205 g_object_get (obj, "enum", &i, NULL);
1206 g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1208 g_settings_bind (settings, "flags", obj, "flags", G_SETTINGS_BIND_DEFAULT);
1209 g_object_set (obj, "flags", TEST_FLAGS_MOURNING, NULL);
1210 strv = g_settings_get_strv (settings, "flags");
1211 g_assert_cmpint (g_strv_length (strv), ==, 1);
1212 g_assert_cmpstr (strv[0], ==, "mourning");
1215 g_assert_cmpint (g_settings_get_flags (settings, "flags"), ==, TEST_FLAGS_MOURNING);
1217 g_settings_set_flags (settings, "flags", TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1219 g_object_get (obj, "flags", &i, NULL);
1220 g_assert_cmpint (i, ==, TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1222 g_object_unref (obj);
1223 g_object_unref (settings);
1230 GSettings *settings;
1232 settings = g_settings_new ("org.gtk.test.binding");
1233 obj = test_object_new ();
1235 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1237 g_object_set (obj, "int", 12345, NULL);
1238 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1240 g_settings_unbind (obj, "int");
1242 g_object_set (obj, "int", 54321, NULL);
1243 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1245 g_object_unref (obj);
1246 g_object_unref (settings);
1250 test_bind_writable (void)
1253 GSettings *settings;
1256 settings = g_settings_new ("org.gtk.test.binding");
1257 obj = test_object_new ();
1259 g_object_set (obj, "bool", FALSE, NULL);
1261 g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1263 g_object_get (obj, "bool", &b, NULL);
1266 g_settings_unbind (obj, "bool");
1268 g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1270 g_object_get (obj, "bool", &b, NULL);
1273 g_object_unref (obj);
1274 g_object_unref (settings);
1277 /* Test one-way bindings.
1278 * Verify that changes on one side show up on the other,
1279 * but not vice versa
1282 test_directional_binding (void)
1285 GSettings *settings;
1289 settings = g_settings_new ("org.gtk.test.binding");
1290 obj = test_object_new ();
1292 g_object_set (obj, "bool", FALSE, NULL);
1293 g_settings_set_boolean (settings, "bool", FALSE);
1295 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1297 g_settings_set_boolean (settings, "bool", TRUE);
1298 g_object_get (obj, "bool", &b, NULL);
1299 g_assert_cmpint (b, ==, TRUE);
1301 g_object_set (obj, "bool", FALSE, NULL);
1302 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1304 g_object_set (obj, "int", 20, NULL);
1305 g_settings_set_int (settings, "int", 20);
1307 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1309 g_object_set (obj, "int", 32, NULL);
1310 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1312 g_settings_set_int (settings, "int", 20);
1313 g_object_get (obj, "int", &i, NULL);
1314 g_assert_cmpint (i, ==, 32);
1316 g_object_unref (obj);
1317 g_object_unref (settings);
1320 /* Test that type mismatch is caught when creating a binding
1323 test_typesafe_binding_subprocess (void)
1326 GSettings *settings;
1328 settings = g_settings_new ("org.gtk.test.binding");
1329 obj = test_object_new ();
1331 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1333 g_object_unref (obj);
1334 g_object_unref (settings);
1338 test_typesafe_binding (void)
1340 if (!g_test_undefined ())
1343 g_test_trap_subprocess ("/gsettings/typesafe-binding/subprocess", 0, 0);
1344 g_test_trap_assert_failed ();
1345 g_test_trap_assert_stderr ("*not compatible*");
1349 string_to_bool (GValue *value,
1355 s = g_variant_get_string (variant, NULL);
1356 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1362 bool_to_string (const GValue *value,
1363 const GVariantType *expected_type,
1366 if (g_value_get_boolean (value))
1367 return g_variant_new_string ("true");
1369 return g_variant_new_string ("false");
1372 /* Test custom bindings.
1373 * Translate strings to booleans and back
1376 test_custom_binding (void)
1379 GSettings *settings;
1383 settings = g_settings_new ("org.gtk.test.binding");
1384 obj = test_object_new ();
1386 g_settings_set_string (settings, "string", "true");
1388 g_settings_bind_with_mapping (settings, "string",
1390 G_SETTINGS_BIND_DEFAULT,
1395 g_settings_set_string (settings, "string", "false");
1396 g_object_get (obj, "bool", &b, NULL);
1397 g_assert_cmpint (b, ==, FALSE);
1399 g_settings_set_string (settings, "string", "not true");
1400 g_object_get (obj, "bool", &b, NULL);
1401 g_assert_cmpint (b, ==, FALSE);
1403 g_object_set (obj, "bool", TRUE, NULL);
1404 s = g_settings_get_string (settings, "string");
1405 g_assert_cmpstr (s, ==, "true");
1408 g_object_unref (obj);
1409 g_object_unref (settings);
1412 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1413 * initial settings value is transported to the object
1414 * side, but later settings changes do not affect the
1418 test_no_change_binding (void)
1421 GSettings *settings;
1424 settings = g_settings_new ("org.gtk.test.binding");
1425 obj = test_object_new ();
1427 g_object_set (obj, "bool", TRUE, NULL);
1428 g_settings_set_boolean (settings, "bool", FALSE);
1430 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1432 g_object_get (obj, "bool", &b, NULL);
1433 g_assert_cmpint (b, ==, FALSE);
1435 g_settings_set_boolean (settings, "bool", TRUE);
1436 g_object_get (obj, "bool", &b, NULL);
1437 g_assert_cmpint (b, ==, FALSE);
1439 g_settings_set_boolean (settings, "bool", FALSE);
1440 g_object_set (obj, "bool", TRUE, NULL);
1441 b = g_settings_get_boolean (settings, "bool");
1442 g_assert_cmpint (b, ==, TRUE);
1444 g_object_unref (obj);
1445 g_object_unref (settings);
1448 /* Test that binding a non-readable property only
1449 * works in 'GET' mode.
1452 test_no_read_binding_fail (void)
1455 GSettings *settings;
1457 settings = g_settings_new ("org.gtk.test.binding");
1458 obj = test_object_new ();
1460 g_settings_bind (settings, "string", obj, "no-read", 0);
1464 test_no_read_binding_pass (void)
1467 GSettings *settings;
1469 settings = g_settings_new ("org.gtk.test.binding");
1470 obj = test_object_new ();
1472 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1478 test_no_read_binding (void)
1480 if (g_test_undefined ())
1482 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/fail", 0, 0);
1483 g_test_trap_assert_failed ();
1484 g_test_trap_assert_stderr ("*property*is not readable*");
1487 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/pass", 0, 0);
1488 g_test_trap_assert_passed ();
1491 /* Test that binding a non-writable property only
1492 * works in 'SET' mode.
1495 test_no_write_binding_fail (void)
1498 GSettings *settings;
1500 settings = g_settings_new ("org.gtk.test.binding");
1501 obj = test_object_new ();
1503 g_settings_bind (settings, "string", obj, "no-write", 0);
1507 test_no_write_binding_pass (void)
1510 GSettings *settings;
1512 settings = g_settings_new ("org.gtk.test.binding");
1513 obj = test_object_new ();
1515 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1521 test_no_write_binding (void)
1523 if (g_test_undefined ())
1525 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/fail", 0, 0);
1526 g_test_trap_assert_failed ();
1527 g_test_trap_assert_stderr ("*property*is not writable*");
1530 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/pass", 0, 0);
1531 g_test_trap_assert_passed ();
1535 * Test that using a keyfile works
1540 GSettingsBackend *kf_backend;
1541 GSettings *settings;
1545 g_remove ("gsettings.store");
1547 kf_backend = g_keyfile_settings_backend_new ("gsettings.store", "/", "root");
1548 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1549 g_object_unref (kf_backend);
1551 g_settings_set (settings, "greeting", "s", "see if this works");
1553 keyfile = g_key_file_new ();
1554 g_assert (g_key_file_load_from_file (keyfile, "gsettings.store", 0, NULL));
1556 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1557 g_assert_cmpstr (str, ==, "'see if this works'");
1560 g_key_file_free (keyfile);
1561 g_object_unref (settings);
1564 /* Test that getting child schemas works
1567 test_child_schema (void)
1569 GSettings *settings;
1573 /* first establish some known conditions */
1574 settings = g_settings_new ("org.gtk.test.basic-types");
1575 g_settings_set (settings, "test-byte", "y", 36);
1577 g_settings_get (settings, "test-byte", "y", &byte);
1578 g_assert_cmpint (byte, ==, 36);
1580 g_object_unref (settings);
1582 settings = g_settings_new ("org.gtk.test");
1583 child = g_settings_get_child (settings, "basic-types");
1584 g_assert (child != NULL);
1586 g_settings_get (child, "test-byte", "y", &byte);
1587 g_assert_cmpint (byte, ==, 36);
1589 g_object_unref (child);
1590 g_object_unref (settings);
1593 #include "../strinfo.c"
1598 /* "foo" has a value of 1
1599 * "bar" has a value of 2
1600 * "baz" is an alias for "bar"
1603 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1604 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1606 const guint32 *strinfo = (guint32 *) array;
1607 guint length = sizeof array / 4;
1611 /* build it and compare */
1614 builder = g_string_new (NULL);
1615 strinfo_builder_append_item (builder, "foo", 1);
1616 strinfo_builder_append_item (builder, "bar", 2);
1617 g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
1618 g_assert_cmpint (builder->len % 4, ==, 0);
1619 g_assert_cmpint (builder->len / 4, ==, length);
1620 g_assert (memcmp (builder->str, strinfo, length * 4) == 0);
1621 g_string_free (builder, TRUE);
1624 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
1626 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
1628 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
1630 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
1633 g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
1634 g_assert_cmpint (result, ==, 1);
1635 g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
1636 g_assert_cmpint (result, ==, 2);
1637 g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
1638 g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
1640 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
1641 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
1642 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
1643 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
1645 g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
1646 g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
1647 g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
1648 g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
1652 test_enums_non_enum_key (void)
1656 direct = g_settings_new ("org.gtk.test.enums.direct");
1657 g_settings_get_enum (direct, "test");
1658 g_assert_not_reached ();
1662 test_enums_non_enum_value (void)
1664 GSettings *settings;
1666 settings = g_settings_new ("org.gtk.test.enums");
1667 g_settings_set_enum (settings, "test", 42);
1668 g_assert_not_reached ();
1672 test_enums_range (void)
1674 GSettings *settings;
1676 settings = g_settings_new ("org.gtk.test.enums");
1677 g_settings_set_string (settings, "test", "qux");
1678 g_assert_not_reached ();
1682 test_enums_non_flags (void)
1684 GSettings *settings;
1686 settings = g_settings_new ("org.gtk.test.enums");
1687 g_settings_get_flags (settings, "test");
1688 g_assert_not_reached ();
1694 GSettings *settings, *direct;
1697 settings = g_settings_new ("org.gtk.test.enums");
1698 direct = g_settings_new ("org.gtk.test.enums.direct");
1700 if (g_test_undefined () && !backend_set)
1702 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-key", 0, 0);
1703 g_test_trap_assert_failed ();
1704 g_test_trap_assert_stderr ("*not associated with an enum*");
1706 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-value", 0, 0);
1707 g_test_trap_assert_failed ();
1708 g_test_trap_assert_stderr ("*invalid enum value 42*");
1710 g_test_trap_subprocess ("/gsettings/enums/subprocess/range", 0, 0);
1711 g_test_trap_assert_failed ();
1712 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1714 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-flags", 0, 0);
1715 g_test_trap_assert_failed ();
1716 g_test_trap_assert_stderr ("*not associated with a flags*");
1719 str = g_settings_get_string (settings, "test");
1720 g_assert_cmpstr (str, ==, "bar");
1723 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
1725 str = g_settings_get_string (settings, "test");
1726 g_assert_cmpstr (str, ==, "foo");
1729 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
1731 g_settings_set_string (direct, "test", "qux");
1733 str = g_settings_get_string (direct, "test");
1734 g_assert_cmpstr (str, ==, "qux");
1737 str = g_settings_get_string (settings, "test");
1738 g_assert_cmpstr (str, ==, "quux");
1741 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
1745 test_flags_non_flags_key (void)
1749 direct = g_settings_new ("org.gtk.test.enums.direct");
1750 g_settings_get_flags (direct, "test");
1751 g_assert_not_reached ();
1755 test_flags_non_flags_value (void)
1757 GSettings *settings;
1759 settings = g_settings_new ("org.gtk.test.enums");
1760 g_settings_set_flags (settings, "f-test", 0x42);
1761 g_assert_not_reached ();
1765 test_flags_range (void)
1767 GSettings *settings;
1769 settings = g_settings_new ("org.gtk.test.enums");
1770 g_settings_set_strv (settings, "f-test",
1771 (const gchar **) g_strsplit ("rock", ",", 0));
1772 g_assert_not_reached ();
1776 test_flags_non_enum (void)
1778 GSettings *settings;
1780 settings = g_settings_new ("org.gtk.test.enums");
1781 g_settings_get_enum (settings, "f-test");
1782 g_assert_not_reached ();
1788 GSettings *settings, *direct;
1792 settings = g_settings_new ("org.gtk.test.enums");
1793 direct = g_settings_new ("org.gtk.test.enums.direct");
1795 if (g_test_undefined () && !backend_set)
1797 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-key", 0, 0);
1798 g_test_trap_assert_failed ();
1799 g_test_trap_assert_stderr ("*not associated with a flags*");
1801 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-value", 0, 0);
1802 g_test_trap_assert_failed ();
1803 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
1805 g_test_trap_subprocess ("/gsettings/flags/subprocess/range", 0, 0);
1806 g_test_trap_assert_failed ();
1807 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1809 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-enum", 0, 0);
1810 g_test_trap_assert_failed ();
1811 g_test_trap_assert_stderr ("*not associated with an enum*");
1814 strv = g_settings_get_strv (settings, "f-test");
1815 str = g_strjoinv (",", strv);
1816 g_assert_cmpstr (str, ==, "");
1820 g_settings_set_flags (settings, "f-test",
1821 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1823 strv = g_settings_get_strv (settings, "f-test");
1824 str = g_strjoinv (",", strv);
1825 g_assert_cmpstr (str, ==, "talking,walking");
1829 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1830 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1832 strv = g_strsplit ("speaking,laughing", ",", 0);
1833 g_settings_set_strv (direct, "f-test", (const gchar **) strv);
1836 strv = g_settings_get_strv (direct, "f-test");
1837 str = g_strjoinv (",", strv);
1838 g_assert_cmpstr (str, ==, "speaking,laughing");
1842 strv = g_settings_get_strv (settings, "f-test");
1843 str = g_strjoinv (",", strv);
1844 g_assert_cmpstr (str, ==, "talking,laughing");
1848 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1849 TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
1853 test_range_high (void)
1855 GSettings *settings;
1857 settings = g_settings_new ("org.gtk.test.range");
1858 g_settings_set_int (settings, "val", 45);
1859 g_assert_not_reached ();
1863 test_range_low (void)
1865 GSettings *settings;
1867 settings = g_settings_new ("org.gtk.test.range");
1868 g_settings_set_int (settings, "val", 1);
1869 g_assert_not_reached ();
1875 GSettings *settings, *direct;
1878 settings = g_settings_new ("org.gtk.test.range");
1879 direct = g_settings_new ("org.gtk.test.range.direct");
1881 if (g_test_undefined () && !backend_set)
1883 g_test_trap_subprocess ("/gsettings/range/subprocess/high", 0, 0);
1884 g_test_trap_assert_failed ();
1885 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1887 g_test_trap_subprocess ("/gsettings/range/subprocess/low", 0, 0);
1888 g_test_trap_assert_failed ();
1889 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1892 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1893 g_settings_set_int (direct, "val", 22);
1894 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
1895 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
1896 g_settings_set_int (direct, "val", 45);
1897 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
1898 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1899 g_settings_set_int (direct, "val", 1);
1900 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
1901 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1903 value = g_variant_new_int32 (1);
1904 g_assert (!g_settings_range_check (settings, "val", value));
1905 g_variant_unref (value);
1906 value = g_variant_new_int32 (33);
1907 g_assert (g_settings_range_check (settings, "val", value));
1908 g_variant_unref (value);
1909 value = g_variant_new_int32 (45);
1910 g_assert (!g_settings_range_check (settings, "val", value));
1911 g_variant_unref (value);
1915 strv_has_string (gchar **haystack,
1916 const gchar *needle)
1920 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
1922 if (g_strcmp0 (haystack[n], needle) == 0)
1929 strv_set_equal (gchar **strv, ...)
1938 va_start (list, strv);
1941 str = va_arg (list, const gchar *);
1944 if (!strv_has_string (strv, str))
1954 res = g_strv_length ((gchar**)strv) == count;
1960 test_list_items (void)
1962 GSettings *settings;
1966 settings = g_settings_new ("org.gtk.test");
1967 children = g_settings_list_children (settings);
1968 keys = g_settings_list_keys (settings);
1970 g_assert (strv_set_equal (children, "basic-types", "complex-types", "localized", NULL));
1971 g_assert (strv_set_equal (keys, "greeting", "farewell", NULL));
1973 g_strfreev (children);
1976 g_object_unref (settings);
1980 test_list_schemas (void)
1982 const gchar * const *schemas;
1983 const gchar * const *relocs;
1985 relocs = g_settings_list_relocatable_schemas ();
1986 schemas = g_settings_list_schemas ();
1988 g_assert (strv_set_equal ((gchar **)relocs,
1989 "org.gtk.test.no-path",
1992 g_assert (strv_set_equal ((gchar **)schemas,
1994 "org.gtk.test.basic-types",
1995 "org.gtk.test.complex-types",
1996 "org.gtk.test.localized",
1997 "org.gtk.test.binding",
1998 "org.gtk.test.enums",
1999 "org.gtk.test.enums.direct",
2000 "org.gtk.test.range",
2001 "org.gtk.test.range.direct",
2002 "org.gtk.test.mapped",
2007 map_func (GVariant *value,
2011 gint *state = user_data;
2015 v = g_variant_get_int32 (value);
2021 g_assert_cmpint (v, ==, 1);
2025 else if (*state == 1)
2027 g_assert_cmpint (v, ==, 0);
2033 g_assert (value == NULL);
2034 *result = g_variant_new_int32 (5);
2040 test_get_mapped (void)
2042 GSettings *settings;
2047 settings = g_settings_new ("org.gtk.test.mapped");
2048 g_settings_set_int (settings, "val", 1);
2051 p = g_settings_get_mapped (settings, "val", map_func, &state);
2052 val = g_variant_get_int32 ((GVariant*)p);
2053 g_assert_cmpint (val, ==, 5);
2055 g_variant_unref (p);
2056 g_object_unref (settings);
2060 test_get_range (void)
2062 GSettings *settings;
2065 settings = g_settings_new ("org.gtk.test.range");
2066 range = g_settings_get_range (settings, "val");
2067 check_and_free (range, "('range', <(2, 44)>)");
2068 g_object_unref (settings);
2070 settings = g_settings_new ("org.gtk.test.enums");
2071 range = g_settings_get_range (settings, "test");
2072 check_and_free (range, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
2073 g_object_unref (settings);
2075 settings = g_settings_new ("org.gtk.test.enums");
2076 range = g_settings_get_range (settings, "f-test");
2077 check_and_free (range, "('flags', "
2078 "<['mourning', 'laughing', 'talking', 'walking']>)");
2079 g_object_unref (settings);
2081 settings = g_settings_new ("org.gtk.test");
2082 range = g_settings_get_range (settings, "greeting");
2083 check_and_free (range, "('type', <@as []>)");
2084 g_object_unref (settings);
2088 test_schema_source (void)
2090 GSettingsSchemaSource *parent;
2091 GSettingsSchemaSource *source;
2092 GSettingsBackend *backend;
2093 GSettingsSchema *schema;
2094 GError *error = NULL;
2095 GSettings *settings;
2098 backend = g_settings_backend_get_default ();
2100 /* make sure it fails properly */
2101 parent = g_settings_schema_source_get_default ();
2102 source = g_settings_schema_source_new_from_directory ("/path/that/does/not/exist", parent, TRUE, &error);
2103 g_assert (source == NULL);
2104 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
2105 g_clear_error (&error);
2107 /* create a source with the parent */
2108 source = g_settings_schema_source_new_from_directory ("schema-source", parent, TRUE, &error);
2109 g_assert_no_error (error);
2110 g_assert (source != NULL);
2112 /* check recursive lookups are working */
2113 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2114 g_assert (schema != NULL);
2115 g_settings_schema_unref (schema);
2117 /* check recursive lookups for non-existent schemas */
2118 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", TRUE);
2119 g_assert (schema == NULL);
2121 /* check non-recursive for schema that only exists in lower layers */
2122 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2123 g_assert (schema == NULL);
2125 /* check non-recursive lookup for non-existent */
2126 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", FALSE);
2127 g_assert (schema == NULL);
2129 /* check non-recursive for schema that exists in toplevel */
2130 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2131 g_assert (schema != NULL);
2132 g_settings_schema_unref (schema);
2134 /* check recursive for schema that exists in toplevel */
2135 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2136 g_assert (schema != NULL);
2138 /* try to use it for something */
2139 settings = g_settings_new_full (schema, backend, g_settings_schema_get_path (schema));
2140 g_settings_schema_unref (schema);
2142 g_settings_get (settings, "enabled", "b", &enabled);
2144 g_object_unref (settings);
2146 g_settings_schema_source_unref (source);
2148 /* try again, but with no parent */
2149 source = g_settings_schema_source_new_from_directory ("schema-source", NULL, FALSE, NULL);
2150 g_assert (source != NULL);
2152 /* should not find it this time, even if recursive... */
2153 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2154 g_assert (schema == NULL);
2155 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2156 g_assert (schema == NULL);
2158 /* should still find our own... */
2159 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2160 g_assert (schema != NULL);
2161 g_settings_schema_unref (schema);
2162 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2163 g_assert (schema != NULL);
2164 g_settings_schema_unref (schema);
2166 g_settings_schema_source_unref (source);
2172 GAction *string, *toggle;
2173 gboolean c1, c2, c3;
2174 GSettings *settings;
2176 GVariantType *param_type;
2178 GVariantType *state_type;
2181 settings = g_settings_new ("org.gtk.test.basic-types");
2182 string = g_settings_create_action (settings, "test-string");
2183 toggle = g_settings_create_action (settings, "test-boolean");
2184 g_object_unref (settings); /* should be held by the actions */
2186 g_signal_connect (settings, "changed", G_CALLBACK (changed_cb2), &c1);
2187 g_signal_connect (string, "notify::state", G_CALLBACK (changed_cb2), &c2);
2188 g_signal_connect (toggle, "notify::state", G_CALLBACK (changed_cb2), &c3);
2190 c1 = c2 = c3 = FALSE;
2191 g_settings_set_string (settings, "test-string", "hello world");
2192 check_and_free (g_action_get_state (string), "'hello world'");
2193 g_assert (c1 && c2 && !c3);
2194 c1 = c2 = c3 = FALSE;
2196 g_action_activate (string, g_variant_new_string ("hihi"));
2197 check_and_free (g_settings_get_value (settings, "test-string"), "'hihi'");
2198 g_assert (c1 && c2 && !c3);
2199 c1 = c2 = c3 = FALSE;
2201 g_action_change_state (string, g_variant_new_string ("kthxbye"));
2202 check_and_free (g_settings_get_value (settings, "test-string"), "'kthxbye'");
2203 g_assert (c1 && c2 && !c3);
2204 c1 = c2 = c3 = FALSE;
2206 g_action_change_state (toggle, g_variant_new_boolean (TRUE));
2207 g_assert (g_settings_get_boolean (settings, "test-boolean"));
2208 g_assert (c1 && !c2 && c3);
2209 c1 = c2 = c3 = FALSE;
2211 g_action_activate (toggle, NULL);
2212 g_assert (!g_settings_get_boolean (settings, "test-boolean"));
2213 g_assert (c1 && !c2 && c3);
2215 g_object_get (string,
2217 "parameter-type", ¶m_type,
2218 "enabled", &enabled,
2219 "state-type", &state_type,
2223 g_assert_cmpstr (name, ==, "test-string");
2224 g_assert (g_variant_type_equal (param_type, G_VARIANT_TYPE_STRING));
2226 g_assert (g_variant_type_equal (state_type, G_VARIANT_TYPE_STRING));
2227 g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "kthxbye");
2230 g_variant_unref (state);
2232 g_object_unref (string);
2233 g_object_unref (toggle);
2237 main (int argc, char *argv[])
2242 setlocale (LC_ALL, "");
2244 g_test_init (&argc, &argv, NULL);
2246 if (!g_test_subprocess ())
2248 const gchar *glib_mkenums;
2249 const gchar *glib_compile_schemas;
2252 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
2254 g_setenv ("XDG_DATA_DIRS", g_test_get_dir (G_TEST_DIST), TRUE);
2255 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
2258 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
2260 if (g_getenv ("GLIB_MKENUMS"))
2261 glib_mkenums = g_getenv ("GLIB_MKENUMS");
2263 glib_mkenums = "glib-mkenums";
2265 cmdline = g_strdup_printf ("%s --template %s/enums.xml.template %s/testenum.h", glib_mkenums,
2266 g_test_get_dir (G_TEST_DIST), g_test_get_dir (G_TEST_DIST));
2268 g_assert (g_spawn_command_line_sync (cmdline, &enums, NULL, &result, NULL));
2269 g_assert (result == 0);
2270 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
2275 if (g_getenv ("GLIB_COMPILE_SCHEMAS"))
2276 glib_compile_schemas = g_getenv ("GLIB_COMPILE_SCHEMAS");
2278 glib_compile_schemas = "glib-compile-schemas";
2280 cmdline = g_strdup_printf ("%s --targetdir=. --schema-file=org.gtk.test.enums.xml --schema-file=%s/org.gtk.test.gschema.xml", glib_compile_schemas, g_test_get_dir (G_TEST_DIST));
2281 g_assert (g_spawn_command_line_sync (cmdline, NULL, NULL, &result, NULL));
2282 g_assert (result == 0);
2285 g_mkdir ("schema-source", 0777);
2286 g_remove ("schema-source/gschemas.compiled");
2288 cmdline = g_strdup_printf ("%s --targetdir=schema-source --schema-file=%s/org.gtk.schemasourcecheck.gschema.xml", glib_compile_schemas, g_test_get_dir (G_TEST_DIST));
2289 g_assert (g_spawn_command_line_sync (cmdline, NULL, NULL, &result, NULL));
2290 g_assert (result == 0);
2294 g_test_add_func ("/gsettings/basic", test_basic);
2298 g_test_add_func ("/gsettings/no-schema", test_no_schema);
2299 g_test_add_func ("/gsettings/no-schema/subprocess", test_no_schema_subprocess);
2300 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
2301 g_test_add_func ("/gsettings/unknown-key/subprocess", test_unknown_key_subprocess);
2302 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
2303 g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
2304 g_test_add_func ("/gsettings/wrong-path/subprocess", test_wrong_path_subprocess);
2305 g_test_add_func ("/gsettings/no-path", test_no_path);
2306 g_test_add_func ("/gsettings/no-path/subprocess", test_no_path_subprocess);
2309 g_test_add_func ("/gsettings/basic-types", test_basic_types);
2310 g_test_add_func ("/gsettings/complex-types", test_complex_types);
2311 g_test_add_func ("/gsettings/changes", test_changes);
2313 g_test_add_func ("/gsettings/l10n", test_l10n);
2314 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
2316 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
2317 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
2318 g_test_add_func ("/gsettings/atomic", test_atomic);
2320 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
2321 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
2322 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
2323 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
2324 g_test_add_func ("/gsettings/unbinding", test_unbind);
2325 g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
2329 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
2330 g_test_add_func ("/gsettings/typesafe-binding/subprocess", test_typesafe_binding_subprocess);
2331 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
2332 g_test_add_func ("/gsettings/no-read-binding/subprocess/fail", test_no_read_binding_fail);
2333 g_test_add_func ("/gsettings/no-read-binding/subprocess/pass", test_no_read_binding_pass);
2334 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
2335 g_test_add_func ("/gsettings/no-write-binding/subprocess/fail", test_no_write_binding_fail);
2336 g_test_add_func ("/gsettings/no-write-binding/subprocess/pass", test_no_write_binding_pass);
2339 g_test_add_func ("/gsettings/keyfile", test_keyfile);
2340 g_test_add_func ("/gsettings/child-schema", test_child_schema);
2341 g_test_add_func ("/gsettings/strinfo", test_strinfo);
2342 g_test_add_func ("/gsettings/enums", test_enums);
2343 g_test_add_func ("/gsettings/enums/subprocess/non-enum-key", test_enums_non_enum_key);
2344 g_test_add_func ("/gsettings/enums/subprocess/non-enum-value", test_enums_non_enum_value);
2345 g_test_add_func ("/gsettings/enums/subprocess/range", test_enums_range);
2346 g_test_add_func ("/gsettings/enums/subprocess/non-flags", test_enums_non_flags);
2347 g_test_add_func ("/gsettings/flags", test_flags);
2348 g_test_add_func ("/gsettings/flags/subprocess/non-flags-key", test_flags_non_flags_key);
2349 g_test_add_func ("/gsettings/flags/subprocess/non-flags-value", test_flags_non_flags_value);
2350 g_test_add_func ("/gsettings/flags/subprocess/range", test_flags_range);
2351 g_test_add_func ("/gsettings/flags/subprocess/non-enum", test_flags_non_enum);
2352 g_test_add_func ("/gsettings/range", test_range);
2353 g_test_add_func ("/gsettings/range/subprocess/high", test_range_high);
2354 g_test_add_func ("/gsettings/range/subprocess/low", test_range_low);
2355 g_test_add_func ("/gsettings/list-items", test_list_items);
2356 g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
2357 g_test_add_func ("/gsettings/mapped", test_get_mapped);
2358 g_test_add_func ("/gsettings/get-range", test_get_range);
2359 g_test_add_func ("/gsettings/schema-source", test_schema_source);
2360 g_test_add_func ("/gsettings/actions", test_actions);
2362 result = g_test_run ();