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.
17 /* Just to get warmed up: Read and set a string, and
18 * verify that can read the changed string back
26 settings = g_settings_new ("org.gtk.test");
28 g_object_get (settings, "schema", &str, NULL);
29 g_assert_cmpstr (str, ==, "org.gtk.test");
31 g_settings_get (settings, "greeting", "s", &str);
32 g_assert_cmpstr (str, ==, "Hello, earthlings");
35 g_settings_set (settings, "greeting", "s", "goodbye world");
36 g_settings_get (settings, "greeting", "s", &str);
37 g_assert_cmpstr (str, ==, "goodbye world");
43 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
45 settings = g_settings_new ("org.gtk.test");
46 g_settings_set (settings, "greeting", "i", 555);
49 g_test_trap_assert_failed ();
50 g_test_trap_assert_stderr ("*g_settings_type_check*");
53 g_settings_get (settings, "greeting", "s", &str);
54 g_assert_cmpstr (str, ==, "goodbye world");
58 g_settings_reset (settings, "greeting");
59 str = g_settings_get_string (settings, "greeting");
60 g_assert_cmpstr (str, ==, "Hello, earthlings");
63 g_settings_set (settings, "greeting", "s", "this is the end");
64 g_object_unref (settings);
67 /* Check that we get an error when getting a key
68 * that is not in the schema
71 test_unknown_key (void)
73 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
78 settings = g_settings_new ("org.gtk.test");
79 value = g_settings_get_value (settings, "no_such_key");
81 g_assert (value == NULL);
83 g_object_unref (settings);
85 g_test_trap_assert_failed ();
86 g_test_trap_assert_stderr ("*does not contain*");
89 /* Check that we get an error when the schema
90 * has not been installed
95 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
99 settings = g_settings_new ("no.such.schema");
101 g_assert (settings == NULL);
104 g_test_trap_assert_failed ();
105 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
108 /* Check that we get an error when passing a type string
109 * that does not match the schema
112 test_wrong_type (void)
114 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
119 settings = g_settings_new ("org.gtk.test");
121 g_settings_get (settings, "greeting", "o", &str);
123 g_assert (str == NULL);
125 g_test_trap_assert_failed ();
126 g_test_trap_assert_stderr ("*CRITICAL*");
128 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
132 settings = g_settings_new ("org.gtk.test");
134 g_settings_set (settings, "greeting", "o", "/a/path");
136 g_test_trap_assert_failed ();
137 g_test_trap_assert_stderr ("*CRITICAL*");
140 /* Check errors with explicit paths */
142 test_wrong_path (void)
144 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
148 settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
151 g_test_trap_assert_failed ();
152 g_test_trap_assert_stderr ("*but path * specified by schema*");
158 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
162 settings = g_settings_new ("org.gtk.test.no-path");
165 g_test_trap_assert_failed ();
166 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
170 /* Check that we can successfully read and set the full
171 * range of all basic types
174 test_basic_types (void)
188 settings = g_settings_new ("org.gtk.test.basic-types");
190 g_settings_get (settings, "test-boolean", "b", &b);
191 g_assert_cmpint (b, ==, 1);
193 g_settings_set (settings, "test-boolean", "b", 0);
194 g_settings_get (settings, "test-boolean", "b", &b);
195 g_assert_cmpint (b, ==, 0);
197 g_settings_get (settings, "test-byte", "y", &byte);
198 g_assert_cmpint (byte, ==, 25);
200 g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
201 g_settings_get (settings, "test-byte", "y", &byte);
202 g_assert_cmpint (byte, ==, G_MAXUINT8);
204 g_settings_get (settings, "test-int16", "n", &i16);
205 g_assert_cmpint (i16, ==, -1234);
207 g_settings_set (settings, "test-int16", "n", G_MININT16);
208 g_settings_get (settings, "test-int16", "n", &i16);
209 g_assert_cmpint (i16, ==, G_MININT16);
211 g_settings_set (settings, "test-int16", "n", G_MAXINT16);
212 g_settings_get (settings, "test-int16", "n", &i16);
213 g_assert_cmpint (i16, ==, G_MAXINT16);
215 g_settings_get (settings, "test-uint16", "q", &u16);
216 g_assert_cmpuint (u16, ==, 1234);
218 g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
219 g_settings_get (settings, "test-uint16", "q", &u16);
220 g_assert_cmpuint (u16, ==, G_MAXUINT16);
222 g_settings_get (settings, "test-int32", "i", &i32);
223 g_assert_cmpint (i32, ==, -123456);
225 g_settings_set (settings, "test-int32", "i", G_MININT32);
226 g_settings_get (settings, "test-int32", "i", &i32);
227 g_assert_cmpint (i32, ==, G_MININT32);
229 g_settings_set (settings, "test-int32", "i", G_MAXINT32);
230 g_settings_get (settings, "test-int32", "i", &i32);
231 g_assert_cmpint (i32, ==, G_MAXINT32);
233 g_settings_get (settings, "test-uint32", "u", &u32);
234 g_assert_cmpuint (u32, ==, 123456);
236 g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
237 g_settings_get (settings, "test-uint32", "u", &u32);
238 g_assert_cmpuint (u32, ==, G_MAXUINT32);
240 g_settings_get (settings, "test-int64", "x", &i64);
241 g_assert_cmpuint (i64, ==, -123456789);
243 g_settings_set (settings, "test-int64", "x", G_MININT64);
244 g_settings_get (settings, "test-int64", "x", &i64);
245 g_assert_cmpuint (i64, ==, G_MININT64);
247 g_settings_set (settings, "test-int64", "x", G_MAXINT64);
248 g_settings_get (settings, "test-int64", "x", &i64);
249 g_assert_cmpuint (i64, ==, G_MAXINT64);
251 g_settings_get (settings, "test-uint64", "t", &u64);
252 g_assert_cmpuint (u64, ==, 123456789);
254 g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
255 g_settings_get (settings, "test-uint64", "t", &u64);
256 g_assert_cmpuint (u64, ==, G_MAXUINT64);
258 g_settings_get (settings, "test-double", "d", &d);
259 g_assert_cmpfloat (d, ==, 123.456);
261 g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
262 g_settings_get (settings, "test-double", "d", &d);
263 g_assert_cmpfloat (d, ==, G_MINDOUBLE);
265 g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
266 g_settings_get (settings, "test-double", "d", &d);
267 g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
269 g_settings_get (settings, "test-string", "s", &str);
270 g_assert_cmpstr (str, ==, "a string, it seems");
274 g_settings_get (settings, "test-objectpath", "o", &str);
275 g_assert_cmpstr (str, ==, "/a/object/path");
276 g_object_unref (settings);
281 /* Check that we can read an set complex types like
282 * tuples, arrays and dictionaries
285 test_complex_types (void)
290 GVariantIter *iter = NULL;
292 settings = g_settings_new ("org.gtk.test.complex-types");
294 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
295 g_assert_cmpstr (s, ==, "one");
296 g_assert_cmpint (i1,==, 2);
297 g_assert_cmpint (i2,==, 3);
301 g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
302 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
303 g_assert_cmpstr (s, ==, "none");
304 g_assert_cmpint (i1,==, 0);
305 g_assert_cmpint (i2,==, 0);
309 g_settings_get (settings, "test-array", "ai", &iter);
310 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
311 g_assert (g_variant_iter_next (iter, "i", &i1));
312 g_assert_cmpint (i1, ==, 0);
313 g_assert (g_variant_iter_next (iter, "i", &i1));
314 g_assert_cmpint (i1, ==, 1);
315 g_assert (g_variant_iter_next (iter, "i", &i1));
316 g_assert_cmpint (i1, ==, 2);
317 g_assert (g_variant_iter_next (iter, "i", &i1));
318 g_assert_cmpint (i1, ==, 3);
319 g_assert (g_variant_iter_next (iter, "i", &i1));
320 g_assert_cmpint (i1, ==, 4);
321 g_assert (g_variant_iter_next (iter, "i", &i1));
322 g_assert_cmpint (i1, ==, 5);
323 g_assert (!g_variant_iter_next (iter, "i", &i1));
324 g_variant_iter_free (iter);
326 g_object_unref (settings);
329 static gboolean changed_cb_called;
332 changed_cb (GSettings *settings,
336 changed_cb_called = TRUE;
338 g_assert_cmpstr (key, ==, data);
341 /* Test that basic change notification with the changed signal works.
347 GSettings *settings2;
349 settings = g_settings_new ("org.gtk.test");
351 g_signal_connect (settings, "changed",
352 G_CALLBACK (changed_cb), "greeting");
354 changed_cb_called = FALSE;
356 g_settings_set (settings, "greeting", "s", "new greeting");
357 g_assert (changed_cb_called);
359 settings2 = g_settings_new ("org.gtk.test");
361 changed_cb_called = FALSE;
363 g_settings_set (settings2, "greeting", "s", "hi");
364 g_assert (changed_cb_called);
366 g_object_unref (settings2);
367 g_object_unref (settings);
370 static gboolean changed_cb_called2;
373 changed_cb2 (GSettings *settings,
382 /* Test that changes done to a delay-mode instance
383 * don't appear to the outside world until apply. Also
384 * check that we get change notification when they are
386 * Also test that the has-unapplied property is properly
390 test_delay_apply (void)
393 GSettings *settings2;
396 settings = g_settings_new ("org.gtk.test");
397 settings2 = g_settings_new ("org.gtk.test");
399 g_settings_set (settings2, "greeting", "s", "top o' the morning");
401 changed_cb_called = FALSE;
402 changed_cb_called2 = FALSE;
404 g_signal_connect (settings, "changed",
405 G_CALLBACK (changed_cb2), &changed_cb_called);
406 g_signal_connect (settings2, "changed",
407 G_CALLBACK (changed_cb2), &changed_cb_called2);
409 g_settings_delay (settings);
411 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
413 g_assert (changed_cb_called);
414 g_assert (!changed_cb_called2);
416 g_settings_get (settings, "greeting", "s", &str);
417 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
421 g_settings_get (settings2, "greeting", "s", &str);
422 g_assert_cmpstr (str, ==, "top o' the morning");
426 g_assert (g_settings_get_has_unapplied (settings));
427 g_assert (!g_settings_get_has_unapplied (settings2));
429 changed_cb_called = FALSE;
430 changed_cb_called2 = FALSE;
432 g_settings_apply (settings);
434 g_assert (!changed_cb_called);
435 g_assert (changed_cb_called2);
437 g_settings_get (settings, "greeting", "s", &str);
438 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
442 g_settings_get (settings2, "greeting", "s", &str);
443 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
447 g_assert (!g_settings_get_has_unapplied (settings));
448 g_assert (!g_settings_get_has_unapplied (settings2));
450 g_object_unref (settings2);
451 g_object_unref (settings);
454 /* Test that reverting unapplied changes in a delay-apply
455 * settings instance works.
458 test_delay_revert (void)
461 GSettings *settings2;
464 settings = g_settings_new ("org.gtk.test");
465 settings2 = g_settings_new ("org.gtk.test");
467 g_settings_set (settings2, "greeting", "s", "top o' the morning");
469 g_settings_delay (settings);
471 g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
473 g_settings_get (settings, "greeting", "s", &str);
474 g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
478 g_settings_get (settings2, "greeting", "s", &str);
479 g_assert_cmpstr (str, ==, "top o' the morning");
483 g_assert (g_settings_get_has_unapplied (settings));
485 g_settings_revert (settings);
487 g_assert (!g_settings_get_has_unapplied (settings));
489 g_settings_get (settings, "greeting", "s", &str);
490 g_assert_cmpstr (str, ==, "top o' the morning");
494 g_settings_get (settings2, "greeting", "s", &str);
495 g_assert_cmpstr (str, ==, "top o' the morning");
499 g_object_unref (settings2);
500 g_object_unref (settings);
504 keys_changed_cb (GSettings *settings,
510 g_assert_cmpint (n_keys, ==, 2);
512 g_assert ((keys[0] == g_quark_from_static_string ("greeting") &&
513 keys[1] == g_quark_from_static_string ("farewell")) ||
514 (keys[1] == g_quark_from_static_string ("greeting") &&
515 keys[0] == g_quark_from_static_string ("farewell")));
517 g_settings_get (settings, "greeting", "s", &str);
518 g_assert_cmpstr (str, ==, "greetings from test_atomic");
522 g_settings_get (settings, "farewell", "s", &str);
523 g_assert_cmpstr (str, ==, "atomic bye-bye");
528 /* Check that delay-applied changes appear atomically.
529 * More specifically, verify that all changed keys appear
530 * with their new value while handling the change-event signal.
536 GSettings *settings2;
539 settings = g_settings_new ("org.gtk.test");
540 settings2 = g_settings_new ("org.gtk.test");
542 g_settings_set (settings2, "greeting", "s", "top o' the morning");
544 changed_cb_called = FALSE;
545 changed_cb_called2 = FALSE;
547 g_signal_connect (settings2, "change-event",
548 G_CALLBACK (keys_changed_cb), NULL);
550 g_settings_delay (settings);
552 g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
553 g_settings_set (settings, "farewell", "s", "atomic bye-bye");
555 g_settings_apply (settings);
557 g_settings_get (settings, "greeting", "s", &str);
558 g_assert_cmpstr (str, ==, "greetings from test_atomic");
562 g_settings_get (settings, "farewell", "s", &str);
563 g_assert_cmpstr (str, ==, "atomic bye-bye");
567 g_settings_get (settings2, "greeting", "s", &str);
568 g_assert_cmpstr (str, ==, "greetings from test_atomic");
572 g_settings_get (settings2, "farewell", "s", &str);
573 g_assert_cmpstr (str, ==, "atomic bye-bye");
577 g_object_unref (settings2);
578 g_object_unref (settings);
581 /* On Windows the interaction between the C library locale and libintl
582 * (from GNU gettext) is not like on POSIX, so just skip these tests
585 * There are several issues:
587 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
588 * in libintl (defined in its <libintl.h>).
590 * 2) The locale names that setlocale() accepts and returns aren't in
591 * the "de_DE" style, but like "German_Germany".
593 * 3) libintl looks at the Win32 thread locale and not the C library
594 * locale. (And even if libintl would use the C library's locale, as
595 * there are several alternative C library DLLs, libintl might be
596 * linked to a different one than the application code, so they
597 * wouldn't have the same C library locale anyway.)
600 /* Test that translations work for schema defaults.
602 * This test relies on the de.po file in the same directory
603 * to be compiled into ./de/LC_MESSAGES/test.mo
612 bindtextdomain ("test", ".");
613 bind_textdomain_codeset ("test", "UTF-8");
615 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
617 settings = g_settings_new ("org.gtk.test.localized");
619 setlocale (LC_MESSAGES, "C");
620 str = g_settings_get_string (settings, "error-message");
621 setlocale (LC_MESSAGES, locale);
623 g_assert_cmpstr (str, ==, "Unnamed");
627 setlocale (LC_MESSAGES, "de_DE");
628 str = g_settings_get_string (settings, "error-message");
629 setlocale (LC_MESSAGES, locale);
631 g_assert_cmpstr (str, ==, "Unbenannt");
632 g_object_unref (settings);
639 /* Test that message context works as expected with translated
640 * schema defaults. Also, verify that non-ASCII UTF-8 content
643 * This test relies on the de.po file in the same directory
644 * to be compiled into ./de/LC_MESSAGES/test.mo
647 test_l10n_context (void)
653 bindtextdomain ("test", ".");
654 bind_textdomain_codeset ("test", "UTF-8");
656 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
658 settings = g_settings_new ("org.gtk.test.localized");
660 setlocale (LC_MESSAGES, "C");
661 g_settings_get (settings, "backspace", "s", &str);
662 setlocale (LC_MESSAGES, locale);
664 g_assert_cmpstr (str, ==, "BackSpace");
668 setlocale (LC_MESSAGES, "de_DE");
669 g_settings_get (settings, "backspace", "s", &str);
670 setlocale (LC_MESSAGES, locale);
672 g_assert_cmpstr (str, ==, "Löschen");
673 g_object_unref (settings);
702 GObject parent_instance;
705 gboolean anti_bool_prop;
716 gchar *no_write_prop;
723 GObjectClass parent_class;
726 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
729 test_object_init (TestObject *object)
734 test_object_finalize (GObject *object)
736 TestObject *testo = (TestObject*)object;
737 g_strfreev (testo->strv_prop);
738 g_free (testo->string_prop);
739 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
743 test_object_get_property (GObject *object,
748 TestObject *test_object = (TestObject *)object;
753 g_value_set_boolean (value, test_object->bool_prop);
756 g_value_set_boolean (value, test_object->anti_bool_prop);
759 g_value_set_char (value, test_object->byte_prop);
762 g_value_set_uint (value, test_object->uint16_prop);
765 g_value_set_int (value, test_object->int16_prop);
768 g_value_set_int (value, test_object->int_prop);
771 g_value_set_uint (value, test_object->uint_prop);
774 g_value_set_int64 (value, test_object->int64_prop);
777 g_value_set_uint64 (value, test_object->uint64_prop);
780 g_value_set_double (value, test_object->double_prop);
783 g_value_set_string (value, test_object->string_prop);
786 g_value_set_string (value, test_object->no_write_prop);
789 g_value_set_boxed (value, test_object->strv_prop);
792 g_value_set_enum (value, test_object->enum_prop);
795 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
801 test_object_set_property (GObject *object,
806 TestObject *test_object = (TestObject *)object;
811 test_object->bool_prop = g_value_get_boolean (value);
814 test_object->anti_bool_prop = g_value_get_boolean (value);
817 test_object->byte_prop = g_value_get_char (value);
820 test_object->int16_prop = g_value_get_int (value);
823 test_object->uint16_prop = g_value_get_uint (value);
826 test_object->int_prop = g_value_get_int (value);
829 test_object->uint_prop = g_value_get_uint (value);
832 test_object->int64_prop = g_value_get_int64 (value);
835 test_object->uint64_prop = g_value_get_uint64 (value);
838 test_object->double_prop = g_value_get_double (value);
841 g_free (test_object->string_prop);
842 test_object->string_prop = g_value_dup_string (value);
845 g_free (test_object->no_read_prop);
846 test_object->no_read_prop = g_value_dup_string (value);
849 g_strfreev (test_object->strv_prop);
850 test_object->strv_prop = g_value_dup_boxed (value);
853 test_object->enum_prop = g_value_get_enum (value);
856 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
862 test_enum_get_type (void)
864 static volatile gsize define_type_id = 0;
866 if (g_once_init_enter (&define_type_id))
868 static const GEnumValue values[] = {
869 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
870 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
871 { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
872 { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
876 GType type_id = g_enum_register_static ("TestEnum", values);
877 g_once_init_leave (&define_type_id, type_id);
880 return define_type_id;
884 test_object_class_init (TestObjectClass *class)
886 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
888 gobject_class->get_property = test_object_get_property;
889 gobject_class->set_property = test_object_set_property;
890 gobject_class->finalize = test_object_finalize;
892 g_object_class_install_property (gobject_class, PROP_BOOL,
893 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
894 g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
895 g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
896 g_object_class_install_property (gobject_class, PROP_BYTE,
897 g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
898 g_object_class_install_property (gobject_class, PROP_INT16,
899 g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
900 g_object_class_install_property (gobject_class, PROP_UINT16,
901 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
902 g_object_class_install_property (gobject_class, PROP_INT,
903 g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
904 g_object_class_install_property (gobject_class, PROP_UINT,
905 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
906 g_object_class_install_property (gobject_class, PROP_INT64,
907 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
908 g_object_class_install_property (gobject_class, PROP_UINT64,
909 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
910 g_object_class_install_property (gobject_class, PROP_DOUBLE,
911 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
912 g_object_class_install_property (gobject_class, PROP_STRING,
913 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
914 g_object_class_install_property (gobject_class, PROP_NO_WRITE,
915 g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
916 g_object_class_install_property (gobject_class, PROP_NO_READ,
917 g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
918 g_object_class_install_property (gobject_class, PROP_STRV,
919 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
920 g_object_class_install_property (gobject_class, PROP_ENUM,
921 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
925 test_object_new (void)
927 return (TestObject*)g_object_new (test_object_get_type (), NULL);
930 /* Test basic binding functionality for simple types.
931 * Verify that with bidirectional bindings, changes on either side
932 * are notified on the other end.
935 test_simple_binding (void)
951 settings = g_settings_new ("org.gtk.test.binding");
952 obj = test_object_new ();
954 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
955 g_object_set (obj, "bool", TRUE, NULL);
956 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
958 g_settings_set_boolean (settings, "bool", FALSE);
960 g_object_get (obj, "bool", &b, NULL);
961 g_assert_cmpint (b, ==, FALSE);
963 g_settings_bind (settings, "anti-bool", obj, "anti-bool",
964 G_SETTINGS_BIND_INVERT_BOOLEAN);
965 g_object_set (obj, "anti-bool", FALSE, NULL);
966 g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
968 g_settings_set_boolean (settings, "anti-bool", FALSE);
970 g_object_get (obj, "anti-bool", &b, NULL);
971 g_assert_cmpint (b, ==, TRUE);
973 g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
975 g_object_set (obj, "byte", 123, NULL);
977 g_settings_get (settings, "byte", "y", &y);
978 g_assert_cmpint (y, ==, 123);
980 g_settings_set (settings, "byte", "y", 54);
982 g_object_get (obj, "byte", &y, NULL);
983 g_assert_cmpint (y, ==, 54);
985 g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
987 g_object_set (obj, "int16", 1234, NULL);
989 g_settings_get (settings, "int16", "n", &n);
990 g_assert_cmpint (n, ==, 1234);
992 g_settings_set (settings, "int16", "n", 4321);
994 g_object_get (obj, "int16", &n, NULL);
995 g_assert_cmpint (n, ==, 4321);
997 g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
999 g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1001 g_settings_get (settings, "uint16", "q", &q);
1002 g_assert_cmpuint (q, ==, G_MAXUINT16);
1004 g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1006 g_object_get (obj, "uint16", &q, NULL);
1007 g_assert_cmpuint (q, ==, (guint16) G_MAXINT16);
1009 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1011 g_object_set (obj, "int", 12345, NULL);
1012 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1014 g_settings_set_int (settings, "int", 54321);
1016 g_object_get (obj, "int", &i, NULL);
1017 g_assert_cmpint (i, ==, 54321);
1019 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1021 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1023 g_settings_get (settings, "int64", "x", &i64);
1024 g_assert_cmpint (i64, ==, G_MAXINT64);
1026 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1028 g_object_get (obj, "int64", &i64, NULL);
1029 g_assert_cmpint (i64, ==, G_MININT64);
1031 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1033 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1035 g_settings_get (settings, "uint64", "t", &u64);
1036 g_assert_cmpuint (u64, ==, G_MAXUINT64);
1038 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1040 g_object_get (obj, "uint64", &u64, NULL);
1041 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1043 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1045 g_object_set (obj, "string", "bu ba", NULL);
1046 s = g_settings_get_string (settings, "string");
1047 g_assert_cmpstr (s, ==, "bu ba");
1050 g_settings_set_string (settings, "string", "bla bla");
1051 g_object_get (obj, "string", &s, NULL);
1052 g_assert_cmpstr (s, ==, "bla bla");
1055 g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1057 g_object_set (obj, "string", "non-unicode:\315", NULL);
1058 value = g_settings_get_value (settings, "chararray");
1059 g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1060 g_variant_unref (value);
1062 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1064 g_object_set (obj, "double", G_MAXFLOAT, NULL);
1065 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1067 g_settings_set_double (settings, "double", G_MINFLOAT);
1069 g_object_get (obj, "double", &d, NULL);
1070 g_assert_cmpfloat (d, ==, G_MINFLOAT);
1072 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1073 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1075 g_settings_set_double (settings, "double", -G_MINDOUBLE);
1077 g_object_get (obj, "double", &d, NULL);
1078 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1080 strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1081 g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1082 g_object_set (obj, "strv", strv, NULL);
1084 strv = g_settings_get_strv (settings, "strv");
1085 s = g_strjoinv (",", strv);
1086 g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1089 strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1090 g_settings_set_strv (settings, "strv", (const gchar **) strv);
1092 g_object_get (obj, "strv", &strv, NULL);
1093 s = g_strjoinv (",", strv);
1094 g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1098 g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1099 g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1100 s = g_settings_get_string (settings, "enum");
1101 g_assert_cmpstr (s, ==, "baz");
1103 g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1105 g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1107 g_object_get (obj, "enum", &i, NULL);
1108 g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1110 g_settings_set_string (settings, "enum", "baz");
1112 g_object_get (obj, "enum", &i, NULL);
1113 g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1115 g_object_unref (obj);
1116 g_object_unref (settings);
1123 GSettings *settings;
1125 settings = g_settings_new ("org.gtk.test.binding");
1126 obj = test_object_new ();
1128 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1130 g_object_set (obj, "int", 12345, NULL);
1131 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1133 g_settings_unbind (obj, "int");
1135 g_object_set (obj, "int", 54321, NULL);
1136 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1138 g_object_unref (obj);
1139 g_object_unref (settings);
1143 test_bind_writable (void)
1146 GSettings *settings;
1149 settings = g_settings_new ("org.gtk.test.binding");
1150 obj = test_object_new ();
1152 g_object_set (obj, "bool", FALSE, NULL);
1154 g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1156 g_object_get (obj, "bool", &b, NULL);
1159 g_settings_unbind (obj, "bool");
1161 g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1163 g_object_get (obj, "bool", &b, NULL);
1166 g_object_unref (obj);
1167 g_object_unref (settings);
1170 /* Test one-way bindings.
1171 * Verify that changes on one side show up on the other,
1172 * but not vice versa
1175 test_directional_binding (void)
1178 GSettings *settings;
1182 settings = g_settings_new ("org.gtk.test.binding");
1183 obj = test_object_new ();
1185 g_object_set (obj, "bool", FALSE, NULL);
1186 g_settings_set_boolean (settings, "bool", FALSE);
1188 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1190 g_settings_set_boolean (settings, "bool", TRUE);
1191 g_object_get (obj, "bool", &b, NULL);
1192 g_assert_cmpint (b, ==, TRUE);
1194 g_object_set (obj, "bool", FALSE, NULL);
1195 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1197 g_object_set (obj, "int", 20, NULL);
1198 g_settings_set_int (settings, "int", 20);
1200 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1202 g_object_set (obj, "int", 32, NULL);
1203 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1205 g_settings_set_int (settings, "int", 20);
1206 g_object_get (obj, "int", &i, NULL);
1207 g_assert_cmpint (i, ==, 32);
1209 g_object_unref (obj);
1210 g_object_unref (settings);
1213 /* Test that type mismatch is caught when creating a binding
1216 test_typesafe_binding (void)
1218 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1221 GSettings *settings;
1223 settings = g_settings_new ("org.gtk.test.binding");
1224 obj = test_object_new ();
1226 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1228 g_object_unref (obj);
1229 g_object_unref (settings);
1231 g_test_trap_assert_failed ();
1232 g_test_trap_assert_stderr ("*not compatible*");
1236 string_to_bool (GValue *value,
1242 s = g_variant_get_string (variant, NULL);
1243 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1249 bool_to_string (const GValue *value,
1250 const GVariantType *expected_type,
1253 if (g_value_get_boolean (value))
1254 return g_variant_new_string ("true");
1256 return g_variant_new_string ("false");
1259 /* Test custom bindings.
1260 * Translate strings to booleans and back
1263 test_custom_binding (void)
1266 GSettings *settings;
1270 settings = g_settings_new ("org.gtk.test.binding");
1271 obj = test_object_new ();
1273 g_settings_set_string (settings, "string", "true");
1275 g_settings_bind_with_mapping (settings, "string",
1277 G_SETTINGS_BIND_DEFAULT,
1282 g_settings_set_string (settings, "string", "false");
1283 g_object_get (obj, "bool", &b, NULL);
1284 g_assert_cmpint (b, ==, FALSE);
1286 g_settings_set_string (settings, "string", "not true");
1287 g_object_get (obj, "bool", &b, NULL);
1288 g_assert_cmpint (b, ==, FALSE);
1290 g_object_set (obj, "bool", TRUE, NULL);
1291 s = g_settings_get_string (settings, "string");
1292 g_assert_cmpstr (s, ==, "true");
1295 g_object_unref (obj);
1296 g_object_unref (settings);
1299 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1300 * initial settings value is transported to the object
1301 * side, but later settings changes do not affect the
1305 test_no_change_binding (void)
1308 GSettings *settings;
1311 settings = g_settings_new ("org.gtk.test.binding");
1312 obj = test_object_new ();
1314 g_object_set (obj, "bool", TRUE, NULL);
1315 g_settings_set_boolean (settings, "bool", FALSE);
1317 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1319 g_object_get (obj, "bool", &b, NULL);
1320 g_assert_cmpint (b, ==, FALSE);
1322 g_settings_set_boolean (settings, "bool", TRUE);
1323 g_object_get (obj, "bool", &b, NULL);
1324 g_assert_cmpint (b, ==, FALSE);
1326 g_settings_set_boolean (settings, "bool", FALSE);
1327 g_object_set (obj, "bool", TRUE, NULL);
1328 b = g_settings_get_boolean (settings, "bool");
1329 g_assert_cmpint (b, ==, TRUE);
1331 g_object_unref (obj);
1332 g_object_unref (settings);
1335 /* Test that binding a non-readable property only
1336 * works in 'GET' mode.
1339 test_no_read_binding (void)
1341 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1344 GSettings *settings;
1346 settings = g_settings_new ("org.gtk.test.binding");
1347 obj = test_object_new ();
1349 g_settings_bind (settings, "string", obj, "no-read", 0);
1351 g_test_trap_assert_failed ();
1352 g_test_trap_assert_stderr ("*property*is not readable*");
1354 if (g_test_trap_fork (0, 0))
1357 GSettings *settings;
1359 settings = g_settings_new ("org.gtk.test.binding");
1360 obj = test_object_new ();
1362 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1366 g_test_trap_assert_passed ();
1369 /* Test that binding a non-writable property only
1370 * works in 'SET' mode.
1373 test_no_write_binding (void)
1375 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1378 GSettings *settings;
1380 settings = g_settings_new ("org.gtk.test.binding");
1381 obj = test_object_new ();
1383 g_settings_bind (settings, "string", obj, "no-write", 0);
1385 g_test_trap_assert_failed ();
1386 g_test_trap_assert_stderr ("*property*is not writable*");
1388 if (g_test_trap_fork (0, 0))
1391 GSettings *settings;
1393 settings = g_settings_new ("org.gtk.test.binding");
1394 obj = test_object_new ();
1396 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1400 g_test_trap_assert_passed ();
1404 * Test that using a keyfile works
1409 GSettingsBackend *kf_backend;
1410 GSettings *settings;
1414 g_remove ("gsettings.store");
1416 kf_backend = g_keyfile_settings_backend_new ("gsettings.store", "/", "root");
1417 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1418 g_object_unref (kf_backend);
1420 g_settings_set (settings, "greeting", "s", "see if this works");
1422 keyfile = g_key_file_new ();
1423 g_assert (g_key_file_load_from_file (keyfile, "gsettings.store", 0, NULL));
1425 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1426 g_assert_cmpstr (str, ==, "'see if this works'");
1429 g_key_file_free (keyfile);
1430 g_object_unref (settings);
1433 /* Test that getting child schemas works
1436 test_child_schema (void)
1438 GSettings *settings;
1442 /* first establish some known conditions */
1443 settings = g_settings_new ("org.gtk.test.basic-types");
1444 g_settings_set (settings, "test-byte", "y", 36);
1446 g_settings_get (settings, "test-byte", "y", &byte);
1447 g_assert_cmpint (byte, ==, 36);
1449 g_object_unref (settings);
1451 settings = g_settings_new ("org.gtk.test");
1452 child = g_settings_get_child (settings, "basic-types");
1453 g_assert (child != NULL);
1455 g_settings_get (child, "test-byte", "y", &byte);
1456 g_assert_cmpint (byte, ==, 36);
1458 g_object_unref (child);
1459 g_object_unref (settings);
1463 glib_translations_work (void)
1466 gchar *orig = "Unnamed";
1469 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
1470 setlocale (LC_MESSAGES, "de");
1471 str = dgettext ("glib20", orig);
1472 setlocale (LC_MESSAGES, locale);
1478 #include "../strinfo.c"
1483 /* "foo" has a value of 1
1484 * "bar" has a value of 2
1485 * "baz" is an alias for "bar"
1488 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1489 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1491 const guint32 *strinfo = (guint32 *) array;
1492 guint length = sizeof array / 4;
1496 /* build it and compare */
1499 builder = g_string_new (NULL);
1500 strinfo_builder_append_item (builder, "foo", 1);
1501 strinfo_builder_append_item (builder, "bar", 2);
1502 g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
1503 g_assert_cmpint (builder->len % 4, ==, 0);
1504 g_assert_cmpint (builder->len / 4, ==, length);
1505 g_assert (memcmp (builder->str, strinfo, length * 4) == 0);
1506 g_string_free (builder, TRUE);
1509 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
1511 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
1513 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
1515 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
1518 g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
1519 g_assert_cmpint (result, ==, 1);
1520 g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
1521 g_assert_cmpint (result, ==, 2);
1522 g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
1523 g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
1525 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
1526 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
1527 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
1528 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
1530 g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
1531 g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
1532 g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
1533 g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
1539 GSettings *settings, *direct;
1542 settings = g_settings_new ("org.gtk.test.enums");
1543 direct = g_settings_new ("org.gtk.test.enums.direct");
1547 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1548 g_settings_get_enum (direct, "test");
1549 g_test_trap_assert_failed ();
1550 g_test_trap_assert_stderr ("*not associated with an enum*");
1552 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1553 g_settings_set_enum (settings, "test", 42);
1554 g_test_trap_assert_failed ();
1555 g_test_trap_assert_stderr ("*invalid enum value 42*");
1557 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1558 g_settings_set_string (settings, "test", "qux");
1559 g_test_trap_assert_failed ();
1560 g_test_trap_assert_stderr ("*g_settings_range_check*");
1562 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1563 g_settings_get_flags (settings, "test");
1564 g_test_trap_assert_failed ();
1565 g_test_trap_assert_stderr ("*not associated with a flags*");
1568 str = g_settings_get_string (settings, "test");
1569 g_assert_cmpstr (str, ==, "bar");
1572 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
1574 str = g_settings_get_string (settings, "test");
1575 g_assert_cmpstr (str, ==, "foo");
1578 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
1580 g_settings_set_string (direct, "test", "qux");
1582 str = g_settings_get_string (direct, "test");
1583 g_assert_cmpstr (str, ==, "qux");
1586 str = g_settings_get_string (settings, "test");
1587 g_assert_cmpstr (str, ==, "quux");
1590 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
1596 GSettings *settings, *direct;
1600 settings = g_settings_new ("org.gtk.test.enums");
1601 direct = g_settings_new ("org.gtk.test.enums.direct");
1605 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1606 g_settings_get_flags (direct, "test");
1607 g_test_trap_assert_failed ();
1608 g_test_trap_assert_stderr ("*not associated with a flags*");
1610 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1611 g_settings_set_flags (settings, "f-test", 0x42);
1612 g_test_trap_assert_failed ();
1613 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
1615 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1616 g_settings_set_strv (settings, "f-test",
1617 (const gchar **) g_strsplit ("rock", ",", 0));
1618 g_test_trap_assert_failed ();
1619 g_test_trap_assert_stderr ("*g_settings_range_check*");
1621 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1622 g_settings_get_enum (settings, "f-test");
1623 g_test_trap_assert_failed ();
1624 g_test_trap_assert_stderr ("*not associated with an enum*");
1627 strv = g_settings_get_strv (settings, "f-test");
1628 str = g_strjoinv (",", strv);
1629 g_assert_cmpstr (str, ==, "");
1633 g_settings_set_flags (settings, "f-test",
1634 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1636 strv = g_settings_get_strv (settings, "f-test");
1637 str = g_strjoinv (",", strv);
1638 g_assert_cmpstr (str, ==, "talking,walking");
1642 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1643 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1645 strv = g_strsplit ("speaking,laughing", ",", 0);
1646 g_settings_set_strv (direct, "f-test", (const gchar **) strv);
1649 strv = g_settings_get_strv (direct, "f-test");
1650 str = g_strjoinv (",", strv);
1651 g_assert_cmpstr (str, ==, "speaking,laughing");
1655 strv = g_settings_get_strv (settings, "f-test");
1656 str = g_strjoinv (",", strv);
1657 g_assert_cmpstr (str, ==, "talking,laughing");
1661 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1662 TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
1668 GSettings *settings, *direct;
1670 settings = g_settings_new ("org.gtk.test.range");
1671 direct = g_settings_new ("org.gtk.test.range.direct");
1675 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1676 g_settings_set_int (settings, "val", 45);
1677 g_test_trap_assert_failed ();
1678 g_test_trap_assert_stderr ("*g_settings_range_check*");
1680 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1681 g_settings_set_int (settings, "val", 1);
1682 g_test_trap_assert_failed ();
1683 g_test_trap_assert_stderr ("*g_settings_range_check*");
1686 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1687 g_settings_set_int (direct, "val", 22);
1688 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
1689 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
1690 g_settings_set_int (direct, "val", 45);
1691 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
1692 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1693 g_settings_set_int (direct, "val", 1);
1694 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
1695 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1699 strv_has_string (const gchar **haystack,
1700 const gchar *needle)
1704 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
1706 if (g_strcmp0 (haystack[n], needle) == 0)
1713 strv_set_equal (const gchar **strv, ...)
1722 va_start (list, strv);
1725 str = va_arg (list, const gchar *);
1728 if (!strv_has_string (strv, str))
1738 res = g_strv_length ((gchar**)strv) == count;
1744 test_list_items (void)
1746 GSettings *settings;
1747 const gchar **items;
1749 settings = g_settings_new ("org.gtk.test");
1750 items = g_settings_list_items (settings);
1752 g_assert (strv_set_equal (items, "greeting", "farewell", "basic-types/", "complex-types/", "localized/", NULL));
1756 g_object_unref (settings);
1760 test_list_schemas (void)
1762 const gchar * const *schemas;
1764 schemas = g_settings_list_schemas ();
1766 g_assert (strv_set_equal ((const gchar **)schemas,
1768 "org.gtk.test.no-path",
1769 "org.gtk.test.basic-types",
1770 "org.gtk.test.complex-types",
1771 "org.gtk.test.localized",
1772 "org.gtk.test.binding",
1773 "org.gtk.test.enums",
1774 "org.gtk.test.enums.direct",
1775 "org.gtk.test.range",
1776 "org.gtk.test.range.direct",
1777 "org.gtk.test.mapped",
1782 map_func (GVariant *value,
1786 gint *state = user_data;
1790 v = g_variant_get_int32 (value);
1796 g_assert_cmpint (v, ==, 1);
1800 else if (*state == 1)
1802 g_assert_cmpint (v, ==, 0);
1808 g_assert (value == NULL);
1809 *result = g_variant_new_int32 (5);
1815 test_get_mapped (void)
1817 GSettings *settings;
1822 settings = g_settings_new ("org.gtk.test.mapped");
1823 g_settings_set_int (settings, "val", 1);
1826 p = g_settings_get_mapped (settings, "val", map_func, &state);
1827 val = g_variant_get_int32 ((GVariant*)p);
1828 g_assert_cmpint (val, ==, 5);
1830 g_variant_unref (p);
1831 g_object_unref (settings);
1835 main (int argc, char *argv[])
1840 setlocale (LC_ALL, "");
1842 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
1844 g_setenv ("XDG_DATA_DIRS", ".", TRUE);
1845 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
1848 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
1851 g_test_init (&argc, &argv, NULL);
1853 g_remove ("org.gtk.test.enums.xml");
1854 g_assert (g_spawn_command_line_sync ("../../gobject/glib-mkenums "
1855 "--template " SRCDIR "/enums.xml.template "
1856 SRCDIR "/testenum.h",
1857 &enums, NULL, &result, NULL));
1858 g_assert (result == 0);
1859 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
1862 g_remove ("gschemas.compiled");
1863 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=. "
1864 "--schema-file=org.gtk.test.enums.xml "
1865 "--schema-file=" SRCDIR "/org.gtk.test.gschema.xml",
1866 NULL, NULL, &result, NULL));
1867 g_assert (result == 0);
1869 g_test_add_func ("/gsettings/basic", test_basic);
1873 g_test_add_func ("/gsettings/no-schema", test_no_schema);
1874 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
1875 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
1876 g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
1877 g_test_add_func ("/gsettings/no-path", test_no_path);
1880 g_test_add_func ("/gsettings/basic-types", test_basic_types);
1881 g_test_add_func ("/gsettings/complex-types", test_complex_types);
1882 g_test_add_func ("/gsettings/changes", test_changes);
1884 if (glib_translations_work ())
1886 g_test_add_func ("/gsettings/l10n", test_l10n);
1887 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
1890 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
1891 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
1892 g_test_add_func ("/gsettings/atomic", test_atomic);
1894 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
1895 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
1896 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
1897 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
1898 g_test_add_func ("/gsettings/unbinding", test_unbind);
1899 g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
1903 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
1904 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
1905 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
1908 g_test_add_func ("/gsettings/keyfile", test_keyfile);
1909 g_test_add_func ("/gsettings/child-schema", test_child_schema);
1910 g_test_add_func ("/gsettings/strinfo", test_strinfo);
1911 g_test_add_func ("/gsettings/enums", test_enums);
1912 g_test_add_func ("/gsettings/flags", test_flags);
1913 g_test_add_func ("/gsettings/range", test_range);
1914 g_test_add_func ("/gsettings/list-items", test_list_items);
1915 g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
1916 g_test_add_func ("/gsettings/mapped", test_get_mapped);
1918 result = g_test_run ();