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_set (settings, "greeting", "s", "this is the end");
59 g_object_unref (settings);
62 /* Check that we get an error when getting a key
63 * that is not in the schema
66 test_unknown_key (void)
68 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
73 settings = g_settings_new ("org.gtk.test");
74 value = g_settings_get_value (settings, "no_such_key");
76 g_assert (value == NULL);
78 g_object_unref (settings);
80 g_test_trap_assert_failed ();
81 g_test_trap_assert_stderr ("*does not contain*");
84 /* Check that we get an error when the schema
85 * has not been installed
90 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
94 settings = g_settings_new ("no.such.schema");
96 g_assert (settings == NULL);
99 g_test_trap_assert_failed ();
100 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
103 /* Check that we get an error when passing a type string
104 * that does not match the schema
107 test_wrong_type (void)
109 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
114 settings = g_settings_new ("org.gtk.test");
116 g_settings_get (settings, "greeting", "o", &str);
118 g_assert (str == NULL);
120 g_test_trap_assert_failed ();
121 g_test_trap_assert_stderr ("*CRITICAL*");
123 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
127 settings = g_settings_new ("org.gtk.test");
129 g_settings_set (settings, "greeting", "o", "/a/path");
131 g_test_trap_assert_failed ();
132 g_test_trap_assert_stderr ("*CRITICAL*");
135 /* Check errors with explicit paths */
137 test_wrong_path (void)
139 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
143 settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
146 g_test_trap_assert_failed ();
147 g_test_trap_assert_stderr ("*but path * specified by schema*");
153 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
157 settings = g_settings_new ("org.gtk.test.no-path");
160 g_test_trap_assert_failed ();
161 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
165 /* Check that we can successfully read and set the full
166 * range of all basic types
169 test_basic_types (void)
183 settings = g_settings_new ("org.gtk.test.basic-types");
185 g_settings_get (settings, "test-boolean", "b", &b);
186 g_assert_cmpint (b, ==, 1);
188 g_settings_set (settings, "test-boolean", "b", 0);
189 g_settings_get (settings, "test-boolean", "b", &b);
190 g_assert_cmpint (b, ==, 0);
192 g_settings_get (settings, "test-byte", "y", &byte);
193 g_assert_cmpint (byte, ==, 25);
195 g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
196 g_settings_get (settings, "test-byte", "y", &byte);
197 g_assert_cmpint (byte, ==, G_MAXUINT8);
199 g_settings_get (settings, "test-int16", "n", &i16);
200 g_assert_cmpint (i16, ==, -1234);
202 g_settings_set (settings, "test-int16", "n", G_MININT16);
203 g_settings_get (settings, "test-int16", "n", &i16);
204 g_assert_cmpint (i16, ==, G_MININT16);
206 g_settings_set (settings, "test-int16", "n", G_MAXINT16);
207 g_settings_get (settings, "test-int16", "n", &i16);
208 g_assert_cmpint (i16, ==, G_MAXINT16);
210 g_settings_get (settings, "test-uint16", "q", &u16);
211 g_assert_cmpuint (u16, ==, 1234);
213 g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
214 g_settings_get (settings, "test-uint16", "q", &u16);
215 g_assert_cmpuint (u16, ==, G_MAXUINT16);
217 g_settings_get (settings, "test-int32", "i", &i32);
218 g_assert_cmpint (i32, ==, -123456);
220 g_settings_set (settings, "test-int32", "i", G_MININT32);
221 g_settings_get (settings, "test-int32", "i", &i32);
222 g_assert_cmpint (i32, ==, G_MININT32);
224 g_settings_set (settings, "test-int32", "i", G_MAXINT32);
225 g_settings_get (settings, "test-int32", "i", &i32);
226 g_assert_cmpint (i32, ==, G_MAXINT32);
228 g_settings_get (settings, "test-uint32", "u", &u32);
229 g_assert_cmpuint (u32, ==, 123456);
231 g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
232 g_settings_get (settings, "test-uint32", "u", &u32);
233 g_assert_cmpuint (u32, ==, G_MAXUINT32);
235 g_settings_get (settings, "test-int64", "x", &i64);
236 g_assert_cmpuint (i64, ==, -123456789);
238 g_settings_set (settings, "test-int64", "x", G_MININT64);
239 g_settings_get (settings, "test-int64", "x", &i64);
240 g_assert_cmpuint (i64, ==, G_MININT64);
242 g_settings_set (settings, "test-int64", "x", G_MAXINT64);
243 g_settings_get (settings, "test-int64", "x", &i64);
244 g_assert_cmpuint (i64, ==, G_MAXINT64);
246 g_settings_get (settings, "test-uint64", "t", &u64);
247 g_assert_cmpuint (u64, ==, 123456789);
249 g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
250 g_settings_get (settings, "test-uint64", "t", &u64);
251 g_assert_cmpuint (u64, ==, G_MAXUINT64);
253 g_settings_get (settings, "test-double", "d", &d);
254 g_assert_cmpfloat (d, ==, 123.456);
256 g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
257 g_settings_get (settings, "test-double", "d", &d);
258 g_assert_cmpfloat (d, ==, G_MINDOUBLE);
260 g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
261 g_settings_get (settings, "test-double", "d", &d);
262 g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
264 g_settings_get (settings, "test-string", "s", &str);
265 g_assert_cmpstr (str, ==, "a string, it seems");
269 g_settings_get (settings, "test-objectpath", "o", &str);
270 g_assert_cmpstr (str, ==, "/a/object/path");
271 g_object_unref (settings);
276 /* Check that we can read an set complex types like
277 * tuples, arrays and dictionaries
280 test_complex_types (void)
285 GVariantIter *iter = NULL;
287 settings = g_settings_new ("org.gtk.test.complex-types");
289 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
290 g_assert_cmpstr (s, ==, "one");
291 g_assert_cmpint (i1,==, 2);
292 g_assert_cmpint (i2,==, 3);
296 g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
297 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
298 g_assert_cmpstr (s, ==, "none");
299 g_assert_cmpint (i1,==, 0);
300 g_assert_cmpint (i2,==, 0);
304 g_settings_get (settings, "test-array", "ai", &iter);
305 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
306 g_assert (g_variant_iter_next (iter, "i", &i1));
307 g_assert_cmpint (i1, ==, 0);
308 g_assert (g_variant_iter_next (iter, "i", &i1));
309 g_assert_cmpint (i1, ==, 1);
310 g_assert (g_variant_iter_next (iter, "i", &i1));
311 g_assert_cmpint (i1, ==, 2);
312 g_assert (g_variant_iter_next (iter, "i", &i1));
313 g_assert_cmpint (i1, ==, 3);
314 g_assert (g_variant_iter_next (iter, "i", &i1));
315 g_assert_cmpint (i1, ==, 4);
316 g_assert (g_variant_iter_next (iter, "i", &i1));
317 g_assert_cmpint (i1, ==, 5);
318 g_assert (!g_variant_iter_next (iter, "i", &i1));
319 g_variant_iter_free (iter);
321 g_object_unref (settings);
324 static gboolean changed_cb_called;
327 changed_cb (GSettings *settings,
331 changed_cb_called = TRUE;
333 g_assert_cmpstr (key, ==, data);
336 /* Test that basic change notification with the changed signal works.
342 GSettings *settings2;
344 settings = g_settings_new ("org.gtk.test");
346 g_signal_connect (settings, "changed",
347 G_CALLBACK (changed_cb), "greeting");
349 changed_cb_called = FALSE;
351 g_settings_set (settings, "greeting", "s", "new greeting");
352 g_assert (changed_cb_called);
354 settings2 = g_settings_new ("org.gtk.test");
356 changed_cb_called = FALSE;
358 g_settings_set (settings2, "greeting", "s", "hi");
359 g_assert (changed_cb_called);
361 g_object_unref (settings2);
362 g_object_unref (settings);
365 static gboolean changed_cb_called2;
368 changed_cb2 (GSettings *settings,
377 /* Test that changes done to a delay-mode instance
378 * don't appear to the outside world until apply. Also
379 * check that we get change notification when they are
381 * Also test that the has-unapplied property is properly
385 test_delay_apply (void)
388 GSettings *settings2;
391 settings = g_settings_new ("org.gtk.test");
392 settings2 = g_settings_new ("org.gtk.test");
394 g_settings_set (settings2, "greeting", "s", "top o' the morning");
396 changed_cb_called = FALSE;
397 changed_cb_called2 = FALSE;
399 g_signal_connect (settings, "changed",
400 G_CALLBACK (changed_cb2), &changed_cb_called);
401 g_signal_connect (settings2, "changed",
402 G_CALLBACK (changed_cb2), &changed_cb_called2);
404 g_settings_delay (settings);
406 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
408 g_assert (changed_cb_called);
409 g_assert (!changed_cb_called2);
411 g_settings_get (settings, "greeting", "s", &str);
412 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
416 g_settings_get (settings2, "greeting", "s", &str);
417 g_assert_cmpstr (str, ==, "top o' the morning");
421 g_assert (g_settings_get_has_unapplied (settings));
422 g_assert (!g_settings_get_has_unapplied (settings2));
424 changed_cb_called = FALSE;
425 changed_cb_called2 = FALSE;
427 g_settings_apply (settings);
429 g_assert (!changed_cb_called);
430 g_assert (changed_cb_called2);
432 g_settings_get (settings, "greeting", "s", &str);
433 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
437 g_settings_get (settings2, "greeting", "s", &str);
438 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
442 g_assert (!g_settings_get_has_unapplied (settings));
443 g_assert (!g_settings_get_has_unapplied (settings2));
445 g_object_unref (settings2);
446 g_object_unref (settings);
449 /* Test that reverting unapplied changes in a delay-apply
450 * settings instance works.
453 test_delay_revert (void)
456 GSettings *settings2;
459 settings = g_settings_new ("org.gtk.test");
460 settings2 = g_settings_new ("org.gtk.test");
462 g_settings_set (settings2, "greeting", "s", "top o' the morning");
464 g_settings_delay (settings);
466 g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
468 g_settings_get (settings, "greeting", "s", &str);
469 g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
473 g_settings_get (settings2, "greeting", "s", &str);
474 g_assert_cmpstr (str, ==, "top o' the morning");
478 g_assert (g_settings_get_has_unapplied (settings));
480 g_settings_revert (settings);
482 g_assert (!g_settings_get_has_unapplied (settings));
484 g_settings_get (settings, "greeting", "s", &str);
485 g_assert_cmpstr (str, ==, "top o' the morning");
489 g_settings_get (settings2, "greeting", "s", &str);
490 g_assert_cmpstr (str, ==, "top o' the morning");
494 g_object_unref (settings2);
495 g_object_unref (settings);
499 keys_changed_cb (GSettings *settings,
505 g_assert_cmpint (n_keys, ==, 2);
507 g_assert ((keys[0] == g_quark_from_static_string ("greeting") &&
508 keys[1] == g_quark_from_static_string ("farewell")) ||
509 (keys[1] == g_quark_from_static_string ("greeting") &&
510 keys[0] == g_quark_from_static_string ("farewell")));
512 g_settings_get (settings, "greeting", "s", &str);
513 g_assert_cmpstr (str, ==, "greetings from test_atomic");
517 g_settings_get (settings, "farewell", "s", &str);
518 g_assert_cmpstr (str, ==, "atomic bye-bye");
523 /* Check that delay-applied changes appear atomically.
524 * More specifically, verify that all changed keys appear
525 * with their new value while handling the change-event signal.
531 GSettings *settings2;
534 settings = g_settings_new ("org.gtk.test");
535 settings2 = g_settings_new ("org.gtk.test");
537 g_settings_set (settings2, "greeting", "s", "top o' the morning");
539 changed_cb_called = FALSE;
540 changed_cb_called2 = FALSE;
542 g_signal_connect (settings2, "change-event",
543 G_CALLBACK (keys_changed_cb), NULL);
545 g_settings_delay (settings);
547 g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
548 g_settings_set (settings, "farewell", "s", "atomic bye-bye");
550 g_settings_apply (settings);
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");
562 g_settings_get (settings2, "greeting", "s", &str);
563 g_assert_cmpstr (str, ==, "greetings from test_atomic");
567 g_settings_get (settings2, "farewell", "s", &str);
568 g_assert_cmpstr (str, ==, "atomic bye-bye");
572 g_object_unref (settings2);
573 g_object_unref (settings);
576 /* On Windows the interaction between the C library locale and libintl
577 * (from GNU gettext) is not like on POSIX, so just skip these tests
580 * There are several issues:
582 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
583 * in libintl (defined in its <libintl.h>).
585 * 2) The locale names that setlocale() accepts and returns aren't in
586 * the "de_DE" style, but like "German_Germany".
588 * 3) libintl looks at the Win32 thread locale and not the C library
589 * locale. (And even if libintl would use the C library's locale, as
590 * there are several alternative C library DLLs, libintl might be
591 * linked to a different one than the application code, so they
592 * wouldn't have the same C library locale anyway.)
595 /* Test that translations work for schema defaults.
597 * This test relies on the de.po file in the same directory
598 * to be compiled into ./de/LC_MESSAGES/test.mo
607 bindtextdomain ("test", ".");
608 bind_textdomain_codeset ("test", "UTF-8");
610 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
612 settings = g_settings_new ("org.gtk.test.localized");
614 setlocale (LC_MESSAGES, "C");
615 str = g_settings_get_string (settings, "error-message");
616 setlocale (LC_MESSAGES, locale);
618 g_assert_cmpstr (str, ==, "Unnamed");
622 setlocale (LC_MESSAGES, "de_DE");
623 str = g_settings_get_string (settings, "error-message");
624 setlocale (LC_MESSAGES, locale);
626 g_assert_cmpstr (str, ==, "Unbenannt");
627 g_object_unref (settings);
634 /* Test that message context works as expected with translated
635 * schema defaults. Also, verify that non-ASCII UTF-8 content
638 * This test relies on the de.po file in the same directory
639 * to be compiled into ./de/LC_MESSAGES/test.mo
642 test_l10n_context (void)
648 bindtextdomain ("test", ".");
649 bind_textdomain_codeset ("test", "UTF-8");
651 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
653 settings = g_settings_new ("org.gtk.test.localized");
655 setlocale (LC_MESSAGES, "C");
656 g_settings_get (settings, "backspace", "s", &str);
657 setlocale (LC_MESSAGES, locale);
659 g_assert_cmpstr (str, ==, "BackSpace");
663 setlocale (LC_MESSAGES, "de_DE");
664 g_settings_get (settings, "backspace", "s", &str);
665 setlocale (LC_MESSAGES, locale);
667 g_assert_cmpstr (str, ==, "Löschen");
668 g_object_unref (settings);
696 GObject parent_instance;
709 gchar *no_write_prop;
716 GObjectClass parent_class;
719 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
722 test_object_init (TestObject *object)
727 test_object_finalize (GObject *object)
729 TestObject *testo = (TestObject*)object;
730 g_strfreev (testo->strv_prop);
731 g_free (testo->string_prop);
732 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
736 test_object_get_property (GObject *object,
741 TestObject *test_object = (TestObject *)object;
746 g_value_set_boolean (value, test_object->bool_prop);
749 g_value_set_char (value, test_object->byte_prop);
752 g_value_set_uint (value, test_object->uint16_prop);
755 g_value_set_int (value, test_object->int16_prop);
758 g_value_set_int (value, test_object->int_prop);
761 g_value_set_uint (value, test_object->uint_prop);
764 g_value_set_int64 (value, test_object->int64_prop);
767 g_value_set_uint64 (value, test_object->uint64_prop);
770 g_value_set_double (value, test_object->double_prop);
773 g_value_set_string (value, test_object->string_prop);
776 g_value_set_string (value, test_object->no_write_prop);
779 g_value_set_boxed (value, test_object->strv_prop);
782 g_value_set_enum (value, test_object->enum_prop);
785 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
791 test_object_set_property (GObject *object,
796 TestObject *test_object = (TestObject *)object;
801 test_object->bool_prop = g_value_get_boolean (value);
804 test_object->byte_prop = g_value_get_char (value);
807 test_object->int16_prop = g_value_get_int (value);
810 test_object->uint16_prop = g_value_get_uint (value);
813 test_object->int_prop = g_value_get_int (value);
816 test_object->uint_prop = g_value_get_uint (value);
819 test_object->int64_prop = g_value_get_int64 (value);
822 test_object->uint64_prop = g_value_get_uint64 (value);
825 test_object->double_prop = g_value_get_double (value);
828 g_free (test_object->string_prop);
829 test_object->string_prop = g_value_dup_string (value);
832 g_free (test_object->no_read_prop);
833 test_object->no_read_prop = g_value_dup_string (value);
836 g_strfreev (test_object->strv_prop);
837 test_object->strv_prop = g_value_dup_boxed (value);
840 test_object->enum_prop = g_value_get_enum (value);
843 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
849 test_enum_get_type (void)
851 static volatile gsize define_type_id = 0;
853 if (g_once_init_enter (&define_type_id))
855 static const GEnumValue values[] = {
856 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
857 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
858 { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
859 { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
863 GType type_id = g_enum_register_static ("TestEnum", values);
864 g_once_init_leave (&define_type_id, type_id);
867 return define_type_id;
871 test_object_class_init (TestObjectClass *class)
873 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
875 gobject_class->get_property = test_object_get_property;
876 gobject_class->set_property = test_object_set_property;
877 gobject_class->finalize = test_object_finalize;
879 g_object_class_install_property (gobject_class, PROP_BOOL,
880 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
881 g_object_class_install_property (gobject_class, PROP_BYTE,
882 g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
883 g_object_class_install_property (gobject_class, PROP_INT16,
884 g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
885 g_object_class_install_property (gobject_class, PROP_UINT16,
886 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
887 g_object_class_install_property (gobject_class, PROP_INT,
888 g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
889 g_object_class_install_property (gobject_class, PROP_UINT,
890 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
891 g_object_class_install_property (gobject_class, PROP_INT64,
892 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
893 g_object_class_install_property (gobject_class, PROP_UINT64,
894 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
895 g_object_class_install_property (gobject_class, PROP_DOUBLE,
896 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
897 g_object_class_install_property (gobject_class, PROP_STRING,
898 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
899 g_object_class_install_property (gobject_class, PROP_NO_WRITE,
900 g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
901 g_object_class_install_property (gobject_class, PROP_NO_READ,
902 g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
903 g_object_class_install_property (gobject_class, PROP_STRV,
904 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
905 g_object_class_install_property (gobject_class, PROP_ENUM,
906 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
910 test_object_new (void)
912 return (TestObject*)g_object_new (test_object_get_type (), NULL);
915 /* Test basic binding functionality for simple types.
916 * Verify that with bidirectional bindings, changes on either side
917 * are notified on the other end.
920 test_simple_binding (void)
936 settings = g_settings_new ("org.gtk.test.binding");
937 obj = test_object_new ();
939 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
941 g_object_set (obj, "bool", TRUE, NULL);
942 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
944 g_settings_set_boolean (settings, "bool", FALSE);
946 g_object_get (obj, "bool", &b, NULL);
947 g_assert_cmpint (b, ==, FALSE);
949 g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
951 g_object_set (obj, "byte", 123, NULL);
953 g_settings_get (settings, "byte", "y", &y);
954 g_assert_cmpint (y, ==, 123);
956 g_settings_set (settings, "byte", "y", 54);
958 g_object_get (obj, "byte", &y, NULL);
959 g_assert_cmpint (y, ==, 54);
961 g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
963 g_object_set (obj, "int16", 1234, NULL);
965 g_settings_get (settings, "int16", "n", &n);
966 g_assert_cmpint (n, ==, 1234);
968 g_settings_set (settings, "int16", "n", 4321);
970 g_object_get (obj, "int16", &n, NULL);
971 g_assert_cmpint (n, ==, 4321);
973 g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
975 g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
977 g_settings_get (settings, "uint16", "q", &q);
978 g_assert_cmpuint (q, ==, G_MAXUINT16);
980 g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
982 g_object_get (obj, "uint16", &q, NULL);
983 g_assert_cmpuint (q, ==, (guint16) G_MAXINT16);
985 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
987 g_object_set (obj, "int", 12345, NULL);
988 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
990 g_settings_set_int (settings, "int", 54321);
992 g_object_get (obj, "int", &i, NULL);
993 g_assert_cmpint (i, ==, 54321);
995 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
997 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
999 g_settings_get (settings, "int64", "x", &i64);
1000 g_assert_cmpint (i64, ==, G_MAXINT64);
1002 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1004 g_object_get (obj, "int64", &i64, NULL);
1005 g_assert_cmpint (i64, ==, G_MININT64);
1007 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1009 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1011 g_settings_get (settings, "uint64", "t", &u64);
1012 g_assert_cmpuint (u64, ==, G_MAXUINT64);
1014 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1016 g_object_get (obj, "uint64", &u64, NULL);
1017 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1019 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1021 g_object_set (obj, "string", "bu ba", NULL);
1022 s = g_settings_get_string (settings, "string");
1023 g_assert_cmpstr (s, ==, "bu ba");
1026 g_settings_set_string (settings, "string", "bla bla");
1027 g_object_get (obj, "string", &s, NULL);
1028 g_assert_cmpstr (s, ==, "bla bla");
1031 g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1033 g_object_set (obj, "string", "non-unicode:\315", NULL);
1034 value = g_settings_get_value (settings, "chararray");
1035 g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1036 g_variant_unref (value);
1038 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1040 g_object_set (obj, "double", G_MAXFLOAT, NULL);
1041 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1043 g_settings_set_double (settings, "double", G_MINFLOAT);
1045 g_object_get (obj, "double", &d, NULL);
1046 g_assert_cmpfloat (d, ==, G_MINFLOAT);
1048 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1049 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1051 g_settings_set_double (settings, "double", -G_MINDOUBLE);
1053 g_object_get (obj, "double", &d, NULL);
1054 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1056 strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1057 g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1058 g_object_set (obj, "strv", strv, NULL);
1060 strv = g_settings_get_strv (settings, "strv");
1061 s = g_strjoinv (",", strv);
1062 g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1065 strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1066 g_settings_set_strv (settings, "strv", (const gchar **) strv);
1068 g_object_get (obj, "strv", &strv, NULL);
1069 s = g_strjoinv (",", strv);
1070 g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1074 g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1075 g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1076 g_assert_cmpstr (g_settings_get_string (settings, "enum"), ==, "baz");
1077 g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1079 g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1081 g_object_get (obj, "enum", &i, NULL);
1082 g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1084 g_settings_set_string (settings, "enum", "baz");
1086 g_object_get (obj, "enum", &i, NULL);
1087 g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1089 g_object_unref (obj);
1090 g_object_unref (settings);
1097 GSettings *settings;
1099 settings = g_settings_new ("org.gtk.test.binding");
1100 obj = test_object_new ();
1102 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1104 g_object_set (obj, "int", 12345, NULL);
1105 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1107 g_settings_unbind (obj, "int");
1109 g_object_set (obj, "int", 54321, NULL);
1110 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1112 g_object_unref (obj);
1113 g_object_unref (settings);
1117 test_bind_writable (void)
1120 GSettings *settings;
1123 settings = g_settings_new ("org.gtk.test.binding");
1124 obj = test_object_new ();
1126 g_object_set (obj, "bool", FALSE, NULL);
1128 g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1130 g_object_get (obj, "bool", &b, NULL);
1133 g_settings_unbind (obj, "bool");
1135 g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1137 g_object_get (obj, "bool", &b, NULL);
1140 g_object_unref (obj);
1141 g_object_unref (settings);
1144 /* Test one-way bindings.
1145 * Verify that changes on one side show up on the other,
1146 * but not vice versa
1149 test_directional_binding (void)
1152 GSettings *settings;
1156 settings = g_settings_new ("org.gtk.test.binding");
1157 obj = test_object_new ();
1159 g_object_set (obj, "bool", FALSE, NULL);
1160 g_settings_set_boolean (settings, "bool", FALSE);
1162 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1164 g_settings_set_boolean (settings, "bool", TRUE);
1165 g_object_get (obj, "bool", &b, NULL);
1166 g_assert_cmpint (b, ==, TRUE);
1168 g_object_set (obj, "bool", FALSE, NULL);
1169 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1171 g_object_set (obj, "int", 20, NULL);
1172 g_settings_set_int (settings, "int", 20);
1174 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1176 g_object_set (obj, "int", 32, NULL);
1177 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1179 g_settings_set_int (settings, "int", 20);
1180 g_object_get (obj, "int", &i, NULL);
1181 g_assert_cmpint (i, ==, 32);
1183 g_object_unref (obj);
1184 g_object_unref (settings);
1187 /* Test that type mismatch is caught when creating a binding
1190 test_typesafe_binding (void)
1192 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1195 GSettings *settings;
1197 settings = g_settings_new ("org.gtk.test.binding");
1198 obj = test_object_new ();
1200 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1202 g_object_unref (obj);
1203 g_object_unref (settings);
1205 g_test_trap_assert_failed ();
1206 g_test_trap_assert_stderr ("*not compatible*");
1210 string_to_bool (GValue *value,
1216 s = g_variant_get_string (variant, NULL);
1217 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1223 bool_to_string (const GValue *value,
1224 const GVariantType *expected_type,
1227 if (g_value_get_boolean (value))
1228 return g_variant_new_string ("true");
1230 return g_variant_new_string ("false");
1233 /* Test custom bindings.
1234 * Translate strings to booleans and back
1237 test_custom_binding (void)
1240 GSettings *settings;
1244 settings = g_settings_new ("org.gtk.test.binding");
1245 obj = test_object_new ();
1247 g_settings_set_string (settings, "string", "true");
1249 g_settings_bind_with_mapping (settings, "string",
1251 G_SETTINGS_BIND_DEFAULT,
1256 g_settings_set_string (settings, "string", "false");
1257 g_object_get (obj, "bool", &b, NULL);
1258 g_assert_cmpint (b, ==, FALSE);
1260 g_settings_set_string (settings, "string", "not true");
1261 g_object_get (obj, "bool", &b, NULL);
1262 g_assert_cmpint (b, ==, FALSE);
1264 g_object_set (obj, "bool", TRUE, NULL);
1265 s = g_settings_get_string (settings, "string");
1266 g_assert_cmpstr (s, ==, "true");
1269 g_object_unref (obj);
1270 g_object_unref (settings);
1273 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1274 * initial settings value is transported to the object
1275 * side, but later settings changes do not affect the
1279 test_no_change_binding (void)
1282 GSettings *settings;
1285 settings = g_settings_new ("org.gtk.test.binding");
1286 obj = test_object_new ();
1288 g_object_set (obj, "bool", TRUE, NULL);
1289 g_settings_set_boolean (settings, "bool", FALSE);
1291 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1293 g_object_get (obj, "bool", &b, NULL);
1294 g_assert_cmpint (b, ==, FALSE);
1296 g_settings_set_boolean (settings, "bool", TRUE);
1297 g_object_get (obj, "bool", &b, NULL);
1298 g_assert_cmpint (b, ==, FALSE);
1300 g_settings_set_boolean (settings, "bool", FALSE);
1301 g_object_set (obj, "bool", TRUE, NULL);
1302 b = g_settings_get_boolean (settings, "bool");
1303 g_assert_cmpint (b, ==, TRUE);
1305 g_object_unref (obj);
1306 g_object_unref (settings);
1309 /* Test that binding a non-readable property only
1310 * works in 'GET' mode.
1313 test_no_read_binding (void)
1315 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1318 GSettings *settings;
1320 settings = g_settings_new ("org.gtk.test.binding");
1321 obj = test_object_new ();
1323 g_settings_bind (settings, "string", obj, "no-read", 0);
1325 g_test_trap_assert_failed ();
1326 g_test_trap_assert_stderr ("*property*is not readable*");
1328 if (g_test_trap_fork (0, 0))
1331 GSettings *settings;
1333 settings = g_settings_new ("org.gtk.test.binding");
1334 obj = test_object_new ();
1336 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1340 g_test_trap_assert_passed ();
1343 /* Test that binding a non-writable property only
1344 * works in 'SET' mode.
1347 test_no_write_binding (void)
1349 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1352 GSettings *settings;
1354 settings = g_settings_new ("org.gtk.test.binding");
1355 obj = test_object_new ();
1357 g_settings_bind (settings, "string", obj, "no-write", 0);
1359 g_test_trap_assert_failed ();
1360 g_test_trap_assert_stderr ("*property*is not writable*");
1362 if (g_test_trap_fork (0, 0))
1365 GSettings *settings;
1367 settings = g_settings_new ("org.gtk.test.binding");
1368 obj = test_object_new ();
1370 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1374 g_test_trap_assert_passed ();
1378 * Test that using a keyfile works
1383 GSettingsBackend *kf_backend;
1384 GSettings *settings;
1388 g_remove ("gsettings.store");
1390 kf_backend = g_keyfile_settings_backend_new ("gsettings.store", "/", "root");
1391 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1392 g_object_unref (kf_backend);
1394 g_settings_set (settings, "greeting", "s", "see if this works");
1396 keyfile = g_key_file_new ();
1397 g_assert (g_key_file_load_from_file (keyfile, "gsettings.store", 0, NULL));
1399 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1400 g_assert_cmpstr (str, ==, "'see if this works'");
1403 g_key_file_free (keyfile);
1404 g_object_unref (settings);
1407 /* Test that getting child schemas works
1410 test_child_schema (void)
1412 GSettings *settings;
1416 /* first establish some known conditions */
1417 settings = g_settings_new ("org.gtk.test.basic-types");
1418 g_settings_set (settings, "test-byte", "y", 36);
1420 g_settings_get (settings, "test-byte", "y", &byte);
1421 g_assert_cmpint (byte, ==, 36);
1423 g_object_unref (settings);
1425 settings = g_settings_new ("org.gtk.test");
1426 child = g_settings_get_child (settings, "basic-types");
1427 g_assert (child != NULL);
1429 g_settings_get (child, "test-byte", "y", &byte);
1430 g_assert_cmpint (byte, ==, 36);
1432 g_object_unref (child);
1433 g_object_unref (settings);
1437 glib_translations_work (void)
1440 gchar *orig = "Unnamed";
1443 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
1444 setlocale (LC_MESSAGES, "de");
1445 str = dgettext ("glib20", orig);
1446 setlocale (LC_MESSAGES, locale);
1452 #include "../strinfo.c"
1457 /* "foo" has a value of 1
1458 * "bar" has a value of 2
1459 * "baz" is an alias for "bar"
1462 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1463 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1465 const guint32 *strinfo = (guint32 *) array;
1466 guint length = sizeof array / 4;
1470 /* build it and compare */
1473 builder = g_string_new (NULL);
1474 strinfo_builder_append_item (builder, "foo", 1);
1475 strinfo_builder_append_item (builder, "bar", 2);
1476 g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
1477 g_assert_cmpint (builder->len % 4, ==, 0);
1478 g_assert_cmpint (builder->len / 4, ==, length);
1479 g_assert (memcmp (builder->str, strinfo, length * 4) == 0);
1480 g_string_free (builder, TRUE);
1483 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
1485 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
1487 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
1489 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
1492 g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
1493 g_assert_cmpint (result, ==, 1);
1494 g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
1495 g_assert_cmpint (result, ==, 2);
1496 g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
1497 g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
1499 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
1500 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
1501 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
1502 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
1504 g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
1505 g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
1506 g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
1507 g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
1513 GSettings *settings, *direct;
1516 settings = g_settings_new ("org.gtk.test.enums");
1517 direct = g_settings_new ("org.gtk.test.enums.direct");
1521 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1522 g_settings_get_enum (direct, "test");
1523 g_test_trap_assert_failed ();
1524 g_test_trap_assert_stderr ("*not associated with an enum*");
1526 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1527 g_settings_set_enum (settings, "test", 42);
1528 g_test_trap_assert_failed ();
1529 g_test_trap_assert_stderr ("*invalid enum value 42*");
1531 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1532 g_settings_set_string (settings, "test", "qux");
1533 g_test_trap_assert_failed ();
1534 g_test_trap_assert_stderr ("*g_settings_range_check*");
1536 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1537 g_settings_get_flags (settings, "test");
1538 g_test_trap_assert_failed ();
1539 g_test_trap_assert_stderr ("*not associated with a flags*");
1542 str = g_settings_get_string (settings, "test");
1543 g_assert_cmpstr (str, ==, "bar");
1546 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
1548 str = g_settings_get_string (settings, "test");
1549 g_assert_cmpstr (str, ==, "foo");
1552 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
1554 g_settings_set_string (direct, "test", "qux");
1556 str = g_settings_get_string (direct, "test");
1557 g_assert_cmpstr (str, ==, "qux");
1560 str = g_settings_get_string (settings, "test");
1561 g_assert_cmpstr (str, ==, "quux");
1564 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
1570 GSettings *settings, *direct;
1574 settings = g_settings_new ("org.gtk.test.enums");
1575 direct = g_settings_new ("org.gtk.test.enums.direct");
1579 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1580 g_settings_get_flags (direct, "test");
1581 g_test_trap_assert_failed ();
1582 g_test_trap_assert_stderr ("*not associated with a flags*");
1584 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1585 g_settings_set_flags (settings, "f-test", 0x42);
1586 g_test_trap_assert_failed ();
1587 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
1589 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1590 g_settings_set_strv (settings, "f-test",
1591 (const gchar **) g_strsplit ("rock", ",", 0));
1592 g_test_trap_assert_failed ();
1593 g_test_trap_assert_stderr ("*g_settings_range_check*");
1595 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1596 g_settings_get_enum (settings, "f-test");
1597 g_test_trap_assert_failed ();
1598 g_test_trap_assert_stderr ("*not associated with an enum*");
1601 strv = g_settings_get_strv (settings, "f-test");
1602 str = g_strjoinv (",", strv);
1603 g_assert_cmpstr (str, ==, "");
1607 g_settings_set_flags (settings, "f-test",
1608 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1610 strv = g_settings_get_strv (settings, "f-test");
1611 str = g_strjoinv (",", strv);
1612 g_assert_cmpstr (str, ==, "talking,walking");
1616 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1617 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1619 strv = g_strsplit ("speaking,laughing", ",", 0);
1620 g_settings_set_strv (direct, "f-test", (const gchar **) strv);
1623 strv = g_settings_get_strv (direct, "f-test");
1624 str = g_strjoinv (",", strv);
1625 g_assert_cmpstr (str, ==, "speaking,laughing");
1629 strv = g_settings_get_strv (settings, "f-test");
1630 str = g_strjoinv (",", strv);
1631 g_assert_cmpstr (str, ==, "talking,laughing");
1635 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1636 TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
1642 GSettings *settings, *direct;
1644 settings = g_settings_new ("org.gtk.test.range");
1645 direct = g_settings_new ("org.gtk.test.range.direct");
1649 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1650 g_settings_set_int (settings, "val", 45);
1651 g_test_trap_assert_failed ();
1652 g_test_trap_assert_stderr ("*g_settings_range_check*");
1654 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1655 g_settings_set_int (settings, "val", 1);
1656 g_test_trap_assert_failed ();
1657 g_test_trap_assert_stderr ("*g_settings_range_check*");
1660 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1661 g_settings_set_int (direct, "val", 22);
1662 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
1663 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
1664 g_settings_set_int (direct, "val", 45);
1665 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
1666 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1667 g_settings_set_int (direct, "val", 1);
1668 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
1669 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1673 strv_has_string (const gchar **haystack,
1674 const gchar *needle)
1678 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
1680 if (g_strcmp0 (haystack[n], needle) == 0)
1687 strv_set_equal (const gchar **strv, ...)
1696 va_start (list, strv);
1699 str = va_arg (list, const gchar *);
1702 if (!strv_has_string (strv, str))
1712 res = g_strv_length ((gchar**)strv) == count;
1718 test_list_items (void)
1720 GSettings *settings;
1721 const gchar **items;
1723 settings = g_settings_new ("org.gtk.test");
1724 items = g_settings_list_items (settings);
1726 g_assert (strv_set_equal (items, "greeting", "farewell", "basic-types/", "complex-types/", "localized/", NULL));
1730 g_object_unref (settings);
1734 map_func (GVariant *value,
1738 gint *state = user_data;
1742 v = g_variant_get_int32 (value);
1748 g_assert_cmpint (v, ==, 1);
1752 else if (*state == 1)
1754 g_assert_cmpint (v, ==, 0);
1760 g_assert (value == NULL);
1761 *result = g_variant_new_int32 (5);
1767 test_get_mapped (void)
1769 GSettings *settings;
1774 settings = g_settings_new ("org.gtk.test.mapped");
1775 g_settings_set_int (settings, "val", 1);
1778 p = g_settings_get_mapped (settings, "val", map_func, &state);
1779 val = g_variant_get_int32 ((GVariant*)p);
1780 g_assert_cmpint (val, ==, 5);
1782 g_variant_unref (p);
1783 g_object_unref (settings);
1787 main (int argc, char *argv[])
1792 setlocale (LC_ALL, "");
1794 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
1796 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
1799 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
1802 g_test_init (&argc, &argv, NULL);
1804 g_remove ("org.gtk.test.enums.xml");
1805 g_assert (g_spawn_command_line_sync ("../../gobject/glib-mkenums "
1806 "--template " SRCDIR "/enums.xml.template "
1807 SRCDIR "/testenum.h",
1808 &enums, NULL, &result, NULL));
1809 g_assert (result == 0);
1810 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
1813 g_remove ("gschemas.compiled");
1814 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=. "
1815 "--schema-file=org.gtk.test.enums.xml "
1816 "--schema-file=" SRCDIR "/org.gtk.test.gschema.xml",
1817 NULL, NULL, &result, NULL));
1818 g_assert (result == 0);
1820 g_test_add_func ("/gsettings/basic", test_basic);
1824 g_test_add_func ("/gsettings/no-schema", test_no_schema);
1825 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
1826 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
1827 g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
1828 g_test_add_func ("/gsettings/no-path", test_no_path);
1831 g_test_add_func ("/gsettings/basic-types", test_basic_types);
1832 g_test_add_func ("/gsettings/complex-types", test_complex_types);
1833 g_test_add_func ("/gsettings/changes", test_changes);
1835 if (glib_translations_work ())
1837 g_test_add_func ("/gsettings/l10n", test_l10n);
1838 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
1841 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
1842 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
1843 g_test_add_func ("/gsettings/atomic", test_atomic);
1845 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
1846 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
1847 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
1848 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
1849 g_test_add_func ("/gsettings/unbinding", test_unbind);
1850 g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
1854 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
1855 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
1856 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
1859 g_test_add_func ("/gsettings/keyfile", test_keyfile);
1860 g_test_add_func ("/gsettings/child-schema", test_child_schema);
1861 g_test_add_func ("/gsettings/strinfo", test_strinfo);
1862 g_test_add_func ("/gsettings/enums", test_enums);
1863 g_test_add_func ("/gsettings/flags", test_flags);
1864 g_test_add_func ("/gsettings/range", test_range);
1865 g_test_add_func ("/gsettings/list-items", test_list_items);
1866 g_test_add_func ("/gsettings/mapped", test_get_mapped);
1868 result = g_test_run ();