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");
32 g_settings_get (settings, "greeting", "s", &str);
33 g_assert_cmpstr (str, ==, "Hello, earthlings");
36 g_settings_set (settings, "greeting", "s", "goodbye world");
37 g_settings_get (settings, "greeting", "s", &str);
38 g_assert_cmpstr (str, ==, "goodbye world");
44 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
46 settings = g_settings_new ("org.gtk.test");
47 g_settings_set (settings, "greeting", "i", 555);
50 g_test_trap_assert_failed ();
51 g_test_trap_assert_stderr ("*g_settings_type_check*");
54 g_settings_get (settings, "greeting", "s", &str);
55 g_assert_cmpstr (str, ==, "goodbye world");
59 g_settings_reset (settings, "greeting");
60 str = g_settings_get_string (settings, "greeting");
61 g_assert_cmpstr (str, ==, "Hello, earthlings");
64 g_settings_set (settings, "greeting", "s", "this is the end");
65 g_object_unref (settings);
68 /* Check that we get an error when getting a key
69 * that is not in the schema
72 test_unknown_key (void)
74 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
79 settings = g_settings_new ("org.gtk.test");
80 value = g_settings_get_value (settings, "no_such_key");
82 g_assert (value == NULL);
84 g_object_unref (settings);
86 g_test_trap_assert_failed ();
87 g_test_trap_assert_stderr ("*does not contain*");
90 /* Check that we get an error when the schema
91 * has not been installed
96 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
100 settings = g_settings_new ("no.such.schema");
102 g_assert (settings == NULL);
105 g_test_trap_assert_failed ();
106 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
109 /* Check that we get an error when passing a type string
110 * that does not match the schema
113 test_wrong_type (void)
115 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
120 settings = g_settings_new ("org.gtk.test");
122 g_settings_get (settings, "greeting", "o", &str);
124 g_assert (str == NULL);
126 g_test_trap_assert_failed ();
127 g_test_trap_assert_stderr ("*CRITICAL*");
129 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
133 settings = g_settings_new ("org.gtk.test");
135 g_settings_set (settings, "greeting", "o", "/a/path");
137 g_test_trap_assert_failed ();
138 g_test_trap_assert_stderr ("*CRITICAL*");
141 /* Check errors with explicit paths */
143 test_wrong_path (void)
145 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
149 settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
152 g_test_trap_assert_failed ();
153 g_test_trap_assert_stderr ("*but path * specified by schema*");
159 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
163 settings = g_settings_new ("org.gtk.test.no-path");
166 g_test_trap_assert_failed ();
167 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
171 /* Check that we can successfully read and set the full
172 * range of all basic types
175 test_basic_types (void)
189 settings = g_settings_new ("org.gtk.test.basic-types");
191 g_settings_get (settings, "test-boolean", "b", &b);
192 g_assert_cmpint (b, ==, 1);
194 g_settings_set (settings, "test-boolean", "b", 0);
195 g_settings_get (settings, "test-boolean", "b", &b);
196 g_assert_cmpint (b, ==, 0);
198 g_settings_get (settings, "test-byte", "y", &byte);
199 g_assert_cmpint (byte, ==, 25);
201 g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
202 g_settings_get (settings, "test-byte", "y", &byte);
203 g_assert_cmpint (byte, ==, G_MAXUINT8);
205 g_settings_get (settings, "test-int16", "n", &i16);
206 g_assert_cmpint (i16, ==, -1234);
208 g_settings_set (settings, "test-int16", "n", G_MININT16);
209 g_settings_get (settings, "test-int16", "n", &i16);
210 g_assert_cmpint (i16, ==, G_MININT16);
212 g_settings_set (settings, "test-int16", "n", G_MAXINT16);
213 g_settings_get (settings, "test-int16", "n", &i16);
214 g_assert_cmpint (i16, ==, G_MAXINT16);
216 g_settings_get (settings, "test-uint16", "q", &u16);
217 g_assert_cmpuint (u16, ==, 1234);
219 g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
220 g_settings_get (settings, "test-uint16", "q", &u16);
221 g_assert_cmpuint (u16, ==, G_MAXUINT16);
223 g_settings_get (settings, "test-int32", "i", &i32);
224 g_assert_cmpint (i32, ==, -123456);
226 g_settings_set (settings, "test-int32", "i", G_MININT32);
227 g_settings_get (settings, "test-int32", "i", &i32);
228 g_assert_cmpint (i32, ==, G_MININT32);
230 g_settings_set (settings, "test-int32", "i", G_MAXINT32);
231 g_settings_get (settings, "test-int32", "i", &i32);
232 g_assert_cmpint (i32, ==, G_MAXINT32);
234 g_settings_get (settings, "test-uint32", "u", &u32);
235 g_assert_cmpuint (u32, ==, 123456);
237 g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
238 g_settings_get (settings, "test-uint32", "u", &u32);
239 g_assert_cmpuint (u32, ==, G_MAXUINT32);
241 g_settings_get (settings, "test-int64", "x", &i64);
242 g_assert_cmpuint (i64, ==, -123456789);
244 g_settings_set (settings, "test-int64", "x", G_MININT64);
245 g_settings_get (settings, "test-int64", "x", &i64);
246 g_assert_cmpuint (i64, ==, G_MININT64);
248 g_settings_set (settings, "test-int64", "x", G_MAXINT64);
249 g_settings_get (settings, "test-int64", "x", &i64);
250 g_assert_cmpuint (i64, ==, G_MAXINT64);
252 g_settings_get (settings, "test-uint64", "t", &u64);
253 g_assert_cmpuint (u64, ==, 123456789);
255 g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
256 g_settings_get (settings, "test-uint64", "t", &u64);
257 g_assert_cmpuint (u64, ==, G_MAXUINT64);
259 g_settings_get (settings, "test-double", "d", &d);
260 g_assert_cmpfloat (d, ==, 123.456);
262 g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
263 g_settings_get (settings, "test-double", "d", &d);
264 g_assert_cmpfloat (d, ==, G_MINDOUBLE);
266 g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
267 g_settings_get (settings, "test-double", "d", &d);
268 g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
270 g_settings_get (settings, "test-string", "s", &str);
271 g_assert_cmpstr (str, ==, "a string, it seems");
275 g_settings_get (settings, "test-objectpath", "o", &str);
276 g_assert_cmpstr (str, ==, "/a/object/path");
277 g_object_unref (settings);
282 /* Check that we can read an set complex types like
283 * tuples, arrays and dictionaries
286 test_complex_types (void)
291 GVariantIter *iter = NULL;
293 settings = g_settings_new ("org.gtk.test.complex-types");
295 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
296 g_assert_cmpstr (s, ==, "one");
297 g_assert_cmpint (i1,==, 2);
298 g_assert_cmpint (i2,==, 3);
302 g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
303 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
304 g_assert_cmpstr (s, ==, "none");
305 g_assert_cmpint (i1,==, 0);
306 g_assert_cmpint (i2,==, 0);
310 g_settings_get (settings, "test-array", "ai", &iter);
311 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
312 g_assert (g_variant_iter_next (iter, "i", &i1));
313 g_assert_cmpint (i1, ==, 0);
314 g_assert (g_variant_iter_next (iter, "i", &i1));
315 g_assert_cmpint (i1, ==, 1);
316 g_assert (g_variant_iter_next (iter, "i", &i1));
317 g_assert_cmpint (i1, ==, 2);
318 g_assert (g_variant_iter_next (iter, "i", &i1));
319 g_assert_cmpint (i1, ==, 3);
320 g_assert (g_variant_iter_next (iter, "i", &i1));
321 g_assert_cmpint (i1, ==, 4);
322 g_assert (g_variant_iter_next (iter, "i", &i1));
323 g_assert_cmpint (i1, ==, 5);
324 g_assert (!g_variant_iter_next (iter, "i", &i1));
325 g_variant_iter_free (iter);
327 g_object_unref (settings);
330 static gboolean changed_cb_called;
333 changed_cb (GSettings *settings,
337 changed_cb_called = TRUE;
339 g_assert_cmpstr (key, ==, data);
342 /* Test that basic change notification with the changed signal works.
348 GSettings *settings2;
350 settings = g_settings_new ("org.gtk.test");
352 g_signal_connect (settings, "changed",
353 G_CALLBACK (changed_cb), "greeting");
355 changed_cb_called = FALSE;
357 g_settings_set (settings, "greeting", "s", "new greeting");
358 g_assert (changed_cb_called);
360 settings2 = g_settings_new ("org.gtk.test");
362 changed_cb_called = FALSE;
364 g_settings_set (settings2, "greeting", "s", "hi");
365 g_assert (changed_cb_called);
367 g_object_unref (settings2);
368 g_object_unref (settings);
371 static gboolean changed_cb_called2;
374 changed_cb2 (GSettings *settings,
383 /* Test that changes done to a delay-mode instance
384 * don't appear to the outside world until apply. Also
385 * check that we get change notification when they are
387 * Also test that the has-unapplied property is properly
391 test_delay_apply (void)
394 GSettings *settings2;
397 settings = g_settings_new ("org.gtk.test");
398 settings2 = g_settings_new ("org.gtk.test");
400 g_settings_set (settings2, "greeting", "s", "top o' the morning");
402 changed_cb_called = FALSE;
403 changed_cb_called2 = FALSE;
405 g_signal_connect (settings, "changed",
406 G_CALLBACK (changed_cb2), &changed_cb_called);
407 g_signal_connect (settings2, "changed",
408 G_CALLBACK (changed_cb2), &changed_cb_called2);
410 g_settings_delay (settings);
412 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
414 g_assert (changed_cb_called);
415 g_assert (!changed_cb_called2);
417 g_settings_get (settings, "greeting", "s", &str);
418 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
422 g_settings_get (settings2, "greeting", "s", &str);
423 g_assert_cmpstr (str, ==, "top o' the morning");
427 g_assert (g_settings_get_has_unapplied (settings));
428 g_assert (!g_settings_get_has_unapplied (settings2));
430 changed_cb_called = FALSE;
431 changed_cb_called2 = FALSE;
433 g_settings_apply (settings);
435 g_assert (!changed_cb_called);
436 g_assert (changed_cb_called2);
438 g_settings_get (settings, "greeting", "s", &str);
439 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
443 g_settings_get (settings2, "greeting", "s", &str);
444 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
448 g_assert (!g_settings_get_has_unapplied (settings));
449 g_assert (!g_settings_get_has_unapplied (settings2));
451 g_object_unref (settings2);
452 g_object_unref (settings);
455 /* Test that reverting unapplied changes in a delay-apply
456 * settings instance works.
459 test_delay_revert (void)
462 GSettings *settings2;
465 settings = g_settings_new ("org.gtk.test");
466 settings2 = g_settings_new ("org.gtk.test");
468 g_settings_set (settings2, "greeting", "s", "top o' the morning");
470 g_settings_delay (settings);
472 g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
474 g_settings_get (settings, "greeting", "s", &str);
475 g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
479 g_settings_get (settings2, "greeting", "s", &str);
480 g_assert_cmpstr (str, ==, "top o' the morning");
484 g_assert (g_settings_get_has_unapplied (settings));
486 g_settings_revert (settings);
488 g_assert (!g_settings_get_has_unapplied (settings));
490 g_settings_get (settings, "greeting", "s", &str);
491 g_assert_cmpstr (str, ==, "top o' the morning");
495 g_settings_get (settings2, "greeting", "s", &str);
496 g_assert_cmpstr (str, ==, "top o' the morning");
500 g_object_unref (settings2);
501 g_object_unref (settings);
505 keys_changed_cb (GSettings *settings,
511 g_assert_cmpint (n_keys, ==, 2);
513 g_assert ((keys[0] == g_quark_from_static_string ("greeting") &&
514 keys[1] == g_quark_from_static_string ("farewell")) ||
515 (keys[1] == g_quark_from_static_string ("greeting") &&
516 keys[0] == g_quark_from_static_string ("farewell")));
518 g_settings_get (settings, "greeting", "s", &str);
519 g_assert_cmpstr (str, ==, "greetings from test_atomic");
523 g_settings_get (settings, "farewell", "s", &str);
524 g_assert_cmpstr (str, ==, "atomic bye-bye");
529 /* Check that delay-applied changes appear atomically.
530 * More specifically, verify that all changed keys appear
531 * with their new value while handling the change-event signal.
537 GSettings *settings2;
540 settings = g_settings_new ("org.gtk.test");
541 settings2 = g_settings_new ("org.gtk.test");
543 g_settings_set (settings2, "greeting", "s", "top o' the morning");
545 changed_cb_called = FALSE;
546 changed_cb_called2 = FALSE;
548 g_signal_connect (settings2, "change-event",
549 G_CALLBACK (keys_changed_cb), NULL);
551 g_settings_delay (settings);
553 g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
554 g_settings_set (settings, "farewell", "s", "atomic bye-bye");
556 g_settings_apply (settings);
558 g_settings_get (settings, "greeting", "s", &str);
559 g_assert_cmpstr (str, ==, "greetings from test_atomic");
563 g_settings_get (settings, "farewell", "s", &str);
564 g_assert_cmpstr (str, ==, "atomic bye-bye");
568 g_settings_get (settings2, "greeting", "s", &str);
569 g_assert_cmpstr (str, ==, "greetings from test_atomic");
573 g_settings_get (settings2, "farewell", "s", &str);
574 g_assert_cmpstr (str, ==, "atomic bye-bye");
578 g_object_unref (settings2);
579 g_object_unref (settings);
582 /* On Windows the interaction between the C library locale and libintl
583 * (from GNU gettext) is not like on POSIX, so just skip these tests
586 * There are several issues:
588 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
589 * in libintl (defined in its <libintl.h>).
591 * 2) The locale names that setlocale() accepts and returns aren't in
592 * the "de_DE" style, but like "German_Germany".
594 * 3) libintl looks at the Win32 thread locale and not the C library
595 * locale. (And even if libintl would use the C library's locale, as
596 * there are several alternative C library DLLs, libintl might be
597 * linked to a different one than the application code, so they
598 * wouldn't have the same C library locale anyway.)
601 /* Test that translations work for schema defaults.
603 * This test relies on the de.po file in the same directory
604 * to be compiled into ./de/LC_MESSAGES/test.mo
613 bindtextdomain ("test", ".");
614 bind_textdomain_codeset ("test", "UTF-8");
616 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
618 settings = g_settings_new ("org.gtk.test.localized");
620 setlocale (LC_MESSAGES, "C");
621 str = g_settings_get_string (settings, "error-message");
622 setlocale (LC_MESSAGES, locale);
624 g_assert_cmpstr (str, ==, "Unnamed");
628 setlocale (LC_MESSAGES, "de_DE");
629 str = g_settings_get_string (settings, "error-message");
630 setlocale (LC_MESSAGES, locale);
632 g_assert_cmpstr (str, ==, "Unbenannt");
633 g_object_unref (settings);
640 /* Test that message context works as expected with translated
641 * schema defaults. Also, verify that non-ASCII UTF-8 content
644 * This test relies on the de.po file in the same directory
645 * to be compiled into ./de/LC_MESSAGES/test.mo
648 test_l10n_context (void)
654 bindtextdomain ("test", ".");
655 bind_textdomain_codeset ("test", "UTF-8");
657 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
659 settings = g_settings_new ("org.gtk.test.localized");
661 setlocale (LC_MESSAGES, "C");
662 g_settings_get (settings, "backspace", "s", &str);
663 setlocale (LC_MESSAGES, locale);
665 g_assert_cmpstr (str, ==, "BackSpace");
669 setlocale (LC_MESSAGES, "de_DE");
670 g_settings_get (settings, "backspace", "s", &str);
671 setlocale (LC_MESSAGES, locale);
673 g_assert_cmpstr (str, ==, "Löschen");
674 g_object_unref (settings);
703 GObject parent_instance;
706 gboolean anti_bool_prop;
717 gchar *no_write_prop;
724 GObjectClass parent_class;
727 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
730 test_object_init (TestObject *object)
735 test_object_finalize (GObject *object)
737 TestObject *testo = (TestObject*)object;
738 g_strfreev (testo->strv_prop);
739 g_free (testo->string_prop);
740 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
744 test_object_get_property (GObject *object,
749 TestObject *test_object = (TestObject *)object;
754 g_value_set_boolean (value, test_object->bool_prop);
757 g_value_set_boolean (value, test_object->anti_bool_prop);
760 g_value_set_char (value, test_object->byte_prop);
763 g_value_set_uint (value, test_object->uint16_prop);
766 g_value_set_int (value, test_object->int16_prop);
769 g_value_set_int (value, test_object->int_prop);
772 g_value_set_uint (value, test_object->uint_prop);
775 g_value_set_int64 (value, test_object->int64_prop);
778 g_value_set_uint64 (value, test_object->uint64_prop);
781 g_value_set_double (value, test_object->double_prop);
784 g_value_set_string (value, test_object->string_prop);
787 g_value_set_string (value, test_object->no_write_prop);
790 g_value_set_boxed (value, test_object->strv_prop);
793 g_value_set_enum (value, test_object->enum_prop);
796 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
802 test_object_set_property (GObject *object,
807 TestObject *test_object = (TestObject *)object;
812 test_object->bool_prop = g_value_get_boolean (value);
815 test_object->anti_bool_prop = g_value_get_boolean (value);
818 test_object->byte_prop = g_value_get_char (value);
821 test_object->int16_prop = g_value_get_int (value);
824 test_object->uint16_prop = g_value_get_uint (value);
827 test_object->int_prop = g_value_get_int (value);
830 test_object->uint_prop = g_value_get_uint (value);
833 test_object->int64_prop = g_value_get_int64 (value);
836 test_object->uint64_prop = g_value_get_uint64 (value);
839 test_object->double_prop = g_value_get_double (value);
842 g_free (test_object->string_prop);
843 test_object->string_prop = g_value_dup_string (value);
846 g_free (test_object->no_read_prop);
847 test_object->no_read_prop = g_value_dup_string (value);
850 g_strfreev (test_object->strv_prop);
851 test_object->strv_prop = g_value_dup_boxed (value);
854 test_object->enum_prop = g_value_get_enum (value);
857 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
863 test_enum_get_type (void)
865 static volatile gsize define_type_id = 0;
867 if (g_once_init_enter (&define_type_id))
869 static const GEnumValue values[] = {
870 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
871 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
872 { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
873 { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
877 GType type_id = g_enum_register_static ("TestEnum", values);
878 g_once_init_leave (&define_type_id, type_id);
881 return define_type_id;
885 test_object_class_init (TestObjectClass *class)
887 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
889 gobject_class->get_property = test_object_get_property;
890 gobject_class->set_property = test_object_set_property;
891 gobject_class->finalize = test_object_finalize;
893 g_object_class_install_property (gobject_class, PROP_BOOL,
894 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
895 g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
896 g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
897 g_object_class_install_property (gobject_class, PROP_BYTE,
898 g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
899 g_object_class_install_property (gobject_class, PROP_INT16,
900 g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
901 g_object_class_install_property (gobject_class, PROP_UINT16,
902 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
903 g_object_class_install_property (gobject_class, PROP_INT,
904 g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
905 g_object_class_install_property (gobject_class, PROP_UINT,
906 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
907 g_object_class_install_property (gobject_class, PROP_INT64,
908 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
909 g_object_class_install_property (gobject_class, PROP_UINT64,
910 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
911 g_object_class_install_property (gobject_class, PROP_DOUBLE,
912 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
913 g_object_class_install_property (gobject_class, PROP_STRING,
914 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
915 g_object_class_install_property (gobject_class, PROP_NO_WRITE,
916 g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
917 g_object_class_install_property (gobject_class, PROP_NO_READ,
918 g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
919 g_object_class_install_property (gobject_class, PROP_STRV,
920 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
921 g_object_class_install_property (gobject_class, PROP_ENUM,
922 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
926 test_object_new (void)
928 return (TestObject*)g_object_new (test_object_get_type (), NULL);
931 /* Test basic binding functionality for simple types.
932 * Verify that with bidirectional bindings, changes on either side
933 * are notified on the other end.
936 test_simple_binding (void)
952 settings = g_settings_new ("org.gtk.test.binding");
953 obj = test_object_new ();
955 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
956 g_object_set (obj, "bool", TRUE, NULL);
957 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
959 g_settings_set_boolean (settings, "bool", FALSE);
961 g_object_get (obj, "bool", &b, NULL);
962 g_assert_cmpint (b, ==, FALSE);
964 g_settings_bind (settings, "anti-bool", obj, "anti-bool",
965 G_SETTINGS_BIND_INVERT_BOOLEAN);
966 g_object_set (obj, "anti-bool", FALSE, NULL);
967 g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
969 g_settings_set_boolean (settings, "anti-bool", FALSE);
971 g_object_get (obj, "anti-bool", &b, NULL);
972 g_assert_cmpint (b, ==, TRUE);
974 g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
976 g_object_set (obj, "byte", 123, NULL);
978 g_settings_get (settings, "byte", "y", &y);
979 g_assert_cmpint (y, ==, 123);
981 g_settings_set (settings, "byte", "y", 54);
983 g_object_get (obj, "byte", &y, NULL);
984 g_assert_cmpint (y, ==, 54);
986 g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
988 g_object_set (obj, "int16", 1234, NULL);
990 g_settings_get (settings, "int16", "n", &n);
991 g_assert_cmpint (n, ==, 1234);
993 g_settings_set (settings, "int16", "n", 4321);
995 g_object_get (obj, "int16", &n, NULL);
996 g_assert_cmpint (n, ==, 4321);
998 g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
1000 g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1002 g_settings_get (settings, "uint16", "q", &q);
1003 g_assert_cmpuint (q, ==, G_MAXUINT16);
1005 g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1007 g_object_get (obj, "uint16", &q, NULL);
1008 g_assert_cmpuint (q, ==, (guint16) G_MAXINT16);
1010 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1012 g_object_set (obj, "int", 12345, NULL);
1013 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1015 g_settings_set_int (settings, "int", 54321);
1017 g_object_get (obj, "int", &i, NULL);
1018 g_assert_cmpint (i, ==, 54321);
1020 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1022 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1024 g_settings_get (settings, "int64", "x", &i64);
1025 g_assert_cmpint (i64, ==, G_MAXINT64);
1027 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1029 g_object_get (obj, "int64", &i64, NULL);
1030 g_assert_cmpint (i64, ==, G_MININT64);
1032 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1034 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1036 g_settings_get (settings, "uint64", "t", &u64);
1037 g_assert_cmpuint (u64, ==, G_MAXUINT64);
1039 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1041 g_object_get (obj, "uint64", &u64, NULL);
1042 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1044 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1046 g_object_set (obj, "string", "bu ba", NULL);
1047 s = g_settings_get_string (settings, "string");
1048 g_assert_cmpstr (s, ==, "bu ba");
1051 g_settings_set_string (settings, "string", "bla bla");
1052 g_object_get (obj, "string", &s, NULL);
1053 g_assert_cmpstr (s, ==, "bla bla");
1056 g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1058 g_object_set (obj, "string", "non-unicode:\315", NULL);
1059 value = g_settings_get_value (settings, "chararray");
1060 g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1061 g_variant_unref (value);
1063 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1065 g_object_set (obj, "double", G_MAXFLOAT, NULL);
1066 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1068 g_settings_set_double (settings, "double", G_MINFLOAT);
1070 g_object_get (obj, "double", &d, NULL);
1071 g_assert_cmpfloat (d, ==, G_MINFLOAT);
1073 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1074 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1076 g_settings_set_double (settings, "double", -G_MINDOUBLE);
1078 g_object_get (obj, "double", &d, NULL);
1079 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1081 strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1082 g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1083 g_object_set (obj, "strv", strv, NULL);
1085 strv = g_settings_get_strv (settings, "strv");
1086 s = g_strjoinv (",", strv);
1087 g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1090 strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1091 g_settings_set_strv (settings, "strv", (const gchar **) strv);
1093 g_object_get (obj, "strv", &strv, NULL);
1094 s = g_strjoinv (",", strv);
1095 g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1099 g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1100 g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1101 s = g_settings_get_string (settings, "enum");
1102 g_assert_cmpstr (s, ==, "baz");
1104 g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1106 g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1108 g_object_get (obj, "enum", &i, NULL);
1109 g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1111 g_settings_set_string (settings, "enum", "baz");
1113 g_object_get (obj, "enum", &i, NULL);
1114 g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1116 g_object_unref (obj);
1117 g_object_unref (settings);
1124 GSettings *settings;
1126 settings = g_settings_new ("org.gtk.test.binding");
1127 obj = test_object_new ();
1129 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1131 g_object_set (obj, "int", 12345, NULL);
1132 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1134 g_settings_unbind (obj, "int");
1136 g_object_set (obj, "int", 54321, NULL);
1137 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1139 g_object_unref (obj);
1140 g_object_unref (settings);
1144 test_bind_writable (void)
1147 GSettings *settings;
1150 settings = g_settings_new ("org.gtk.test.binding");
1151 obj = test_object_new ();
1153 g_object_set (obj, "bool", FALSE, NULL);
1155 g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1157 g_object_get (obj, "bool", &b, NULL);
1160 g_settings_unbind (obj, "bool");
1162 g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1164 g_object_get (obj, "bool", &b, NULL);
1167 g_object_unref (obj);
1168 g_object_unref (settings);
1171 /* Test one-way bindings.
1172 * Verify that changes on one side show up on the other,
1173 * but not vice versa
1176 test_directional_binding (void)
1179 GSettings *settings;
1183 settings = g_settings_new ("org.gtk.test.binding");
1184 obj = test_object_new ();
1186 g_object_set (obj, "bool", FALSE, NULL);
1187 g_settings_set_boolean (settings, "bool", FALSE);
1189 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1191 g_settings_set_boolean (settings, "bool", TRUE);
1192 g_object_get (obj, "bool", &b, NULL);
1193 g_assert_cmpint (b, ==, TRUE);
1195 g_object_set (obj, "bool", FALSE, NULL);
1196 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1198 g_object_set (obj, "int", 20, NULL);
1199 g_settings_set_int (settings, "int", 20);
1201 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1203 g_object_set (obj, "int", 32, NULL);
1204 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1206 g_settings_set_int (settings, "int", 20);
1207 g_object_get (obj, "int", &i, NULL);
1208 g_assert_cmpint (i, ==, 32);
1210 g_object_unref (obj);
1211 g_object_unref (settings);
1214 /* Test that type mismatch is caught when creating a binding
1217 test_typesafe_binding (void)
1219 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1222 GSettings *settings;
1224 settings = g_settings_new ("org.gtk.test.binding");
1225 obj = test_object_new ();
1227 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1229 g_object_unref (obj);
1230 g_object_unref (settings);
1232 g_test_trap_assert_failed ();
1233 g_test_trap_assert_stderr ("*not compatible*");
1237 string_to_bool (GValue *value,
1243 s = g_variant_get_string (variant, NULL);
1244 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1250 bool_to_string (const GValue *value,
1251 const GVariantType *expected_type,
1254 if (g_value_get_boolean (value))
1255 return g_variant_new_string ("true");
1257 return g_variant_new_string ("false");
1260 /* Test custom bindings.
1261 * Translate strings to booleans and back
1264 test_custom_binding (void)
1267 GSettings *settings;
1271 settings = g_settings_new ("org.gtk.test.binding");
1272 obj = test_object_new ();
1274 g_settings_set_string (settings, "string", "true");
1276 g_settings_bind_with_mapping (settings, "string",
1278 G_SETTINGS_BIND_DEFAULT,
1283 g_settings_set_string (settings, "string", "false");
1284 g_object_get (obj, "bool", &b, NULL);
1285 g_assert_cmpint (b, ==, FALSE);
1287 g_settings_set_string (settings, "string", "not true");
1288 g_object_get (obj, "bool", &b, NULL);
1289 g_assert_cmpint (b, ==, FALSE);
1291 g_object_set (obj, "bool", TRUE, NULL);
1292 s = g_settings_get_string (settings, "string");
1293 g_assert_cmpstr (s, ==, "true");
1296 g_object_unref (obj);
1297 g_object_unref (settings);
1300 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1301 * initial settings value is transported to the object
1302 * side, but later settings changes do not affect the
1306 test_no_change_binding (void)
1309 GSettings *settings;
1312 settings = g_settings_new ("org.gtk.test.binding");
1313 obj = test_object_new ();
1315 g_object_set (obj, "bool", TRUE, NULL);
1316 g_settings_set_boolean (settings, "bool", FALSE);
1318 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1320 g_object_get (obj, "bool", &b, NULL);
1321 g_assert_cmpint (b, ==, FALSE);
1323 g_settings_set_boolean (settings, "bool", TRUE);
1324 g_object_get (obj, "bool", &b, NULL);
1325 g_assert_cmpint (b, ==, FALSE);
1327 g_settings_set_boolean (settings, "bool", FALSE);
1328 g_object_set (obj, "bool", TRUE, NULL);
1329 b = g_settings_get_boolean (settings, "bool");
1330 g_assert_cmpint (b, ==, TRUE);
1332 g_object_unref (obj);
1333 g_object_unref (settings);
1336 /* Test that binding a non-readable property only
1337 * works in 'GET' mode.
1340 test_no_read_binding (void)
1342 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1345 GSettings *settings;
1347 settings = g_settings_new ("org.gtk.test.binding");
1348 obj = test_object_new ();
1350 g_settings_bind (settings, "string", obj, "no-read", 0);
1352 g_test_trap_assert_failed ();
1353 g_test_trap_assert_stderr ("*property*is not readable*");
1355 if (g_test_trap_fork (0, 0))
1358 GSettings *settings;
1360 settings = g_settings_new ("org.gtk.test.binding");
1361 obj = test_object_new ();
1363 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1367 g_test_trap_assert_passed ();
1370 /* Test that binding a non-writable property only
1371 * works in 'SET' mode.
1374 test_no_write_binding (void)
1376 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1379 GSettings *settings;
1381 settings = g_settings_new ("org.gtk.test.binding");
1382 obj = test_object_new ();
1384 g_settings_bind (settings, "string", obj, "no-write", 0);
1386 g_test_trap_assert_failed ();
1387 g_test_trap_assert_stderr ("*property*is not writable*");
1389 if (g_test_trap_fork (0, 0))
1392 GSettings *settings;
1394 settings = g_settings_new ("org.gtk.test.binding");
1395 obj = test_object_new ();
1397 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1401 g_test_trap_assert_passed ();
1405 * Test that using a keyfile works
1410 GSettingsBackend *kf_backend;
1411 GSettings *settings;
1415 g_remove ("gsettings.store");
1417 kf_backend = g_keyfile_settings_backend_new ("gsettings.store", "/", "root");
1418 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1419 g_object_unref (kf_backend);
1421 g_settings_set (settings, "greeting", "s", "see if this works");
1423 keyfile = g_key_file_new ();
1424 g_assert (g_key_file_load_from_file (keyfile, "gsettings.store", 0, NULL));
1426 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1427 g_assert_cmpstr (str, ==, "'see if this works'");
1430 g_key_file_free (keyfile);
1431 g_object_unref (settings);
1434 /* Test that getting child schemas works
1437 test_child_schema (void)
1439 GSettings *settings;
1443 /* first establish some known conditions */
1444 settings = g_settings_new ("org.gtk.test.basic-types");
1445 g_settings_set (settings, "test-byte", "y", 36);
1447 g_settings_get (settings, "test-byte", "y", &byte);
1448 g_assert_cmpint (byte, ==, 36);
1450 g_object_unref (settings);
1452 settings = g_settings_new ("org.gtk.test");
1453 child = g_settings_get_child (settings, "basic-types");
1454 g_assert (child != NULL);
1456 g_settings_get (child, "test-byte", "y", &byte);
1457 g_assert_cmpint (byte, ==, 36);
1459 g_object_unref (child);
1460 g_object_unref (settings);
1464 glib_translations_work (void)
1467 gchar *orig = "Unnamed";
1470 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
1471 setlocale (LC_MESSAGES, "de");
1472 str = dgettext ("glib20", orig);
1473 setlocale (LC_MESSAGES, locale);
1479 #include "../strinfo.c"
1484 /* "foo" has a value of 1
1485 * "bar" has a value of 2
1486 * "baz" is an alias for "bar"
1489 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1490 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1492 const guint32 *strinfo = (guint32 *) array;
1493 guint length = sizeof array / 4;
1497 /* build it and compare */
1500 builder = g_string_new (NULL);
1501 strinfo_builder_append_item (builder, "foo", 1);
1502 strinfo_builder_append_item (builder, "bar", 2);
1503 g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
1504 g_assert_cmpint (builder->len % 4, ==, 0);
1505 g_assert_cmpint (builder->len / 4, ==, length);
1506 g_assert (memcmp (builder->str, strinfo, length * 4) == 0);
1507 g_string_free (builder, TRUE);
1510 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
1512 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
1514 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
1516 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
1519 g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
1520 g_assert_cmpint (result, ==, 1);
1521 g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
1522 g_assert_cmpint (result, ==, 2);
1523 g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
1524 g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
1526 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
1527 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
1528 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
1529 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
1531 g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
1532 g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
1533 g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
1534 g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
1540 GSettings *settings, *direct;
1543 settings = g_settings_new ("org.gtk.test.enums");
1544 direct = g_settings_new ("org.gtk.test.enums.direct");
1548 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1549 g_settings_get_enum (direct, "test");
1550 g_test_trap_assert_failed ();
1551 g_test_trap_assert_stderr ("*not associated with an enum*");
1553 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1554 g_settings_set_enum (settings, "test", 42);
1555 g_test_trap_assert_failed ();
1556 g_test_trap_assert_stderr ("*invalid enum value 42*");
1558 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1559 g_settings_set_string (settings, "test", "qux");
1560 g_test_trap_assert_failed ();
1561 g_test_trap_assert_stderr ("*g_settings_range_check*");
1563 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1564 g_settings_get_flags (settings, "test");
1565 g_test_trap_assert_failed ();
1566 g_test_trap_assert_stderr ("*not associated with a flags*");
1569 str = g_settings_get_string (settings, "test");
1570 g_assert_cmpstr (str, ==, "bar");
1573 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
1575 str = g_settings_get_string (settings, "test");
1576 g_assert_cmpstr (str, ==, "foo");
1579 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
1581 g_settings_set_string (direct, "test", "qux");
1583 str = g_settings_get_string (direct, "test");
1584 g_assert_cmpstr (str, ==, "qux");
1587 str = g_settings_get_string (settings, "test");
1588 g_assert_cmpstr (str, ==, "quux");
1591 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
1597 GSettings *settings, *direct;
1601 settings = g_settings_new ("org.gtk.test.enums");
1602 direct = g_settings_new ("org.gtk.test.enums.direct");
1606 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1607 g_settings_get_flags (direct, "test");
1608 g_test_trap_assert_failed ();
1609 g_test_trap_assert_stderr ("*not associated with a flags*");
1611 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1612 g_settings_set_flags (settings, "f-test", 0x42);
1613 g_test_trap_assert_failed ();
1614 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
1616 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1617 g_settings_set_strv (settings, "f-test",
1618 (const gchar **) g_strsplit ("rock", ",", 0));
1619 g_test_trap_assert_failed ();
1620 g_test_trap_assert_stderr ("*g_settings_range_check*");
1622 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1623 g_settings_get_enum (settings, "f-test");
1624 g_test_trap_assert_failed ();
1625 g_test_trap_assert_stderr ("*not associated with an enum*");
1628 strv = g_settings_get_strv (settings, "f-test");
1629 str = g_strjoinv (",", strv);
1630 g_assert_cmpstr (str, ==, "");
1634 g_settings_set_flags (settings, "f-test",
1635 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1637 strv = g_settings_get_strv (settings, "f-test");
1638 str = g_strjoinv (",", strv);
1639 g_assert_cmpstr (str, ==, "talking,walking");
1643 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1644 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1646 strv = g_strsplit ("speaking,laughing", ",", 0);
1647 g_settings_set_strv (direct, "f-test", (const gchar **) strv);
1650 strv = g_settings_get_strv (direct, "f-test");
1651 str = g_strjoinv (",", strv);
1652 g_assert_cmpstr (str, ==, "speaking,laughing");
1656 strv = g_settings_get_strv (settings, "f-test");
1657 str = g_strjoinv (",", strv);
1658 g_assert_cmpstr (str, ==, "talking,laughing");
1662 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1663 TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
1669 GSettings *settings, *direct;
1671 settings = g_settings_new ("org.gtk.test.range");
1672 direct = g_settings_new ("org.gtk.test.range.direct");
1676 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1677 g_settings_set_int (settings, "val", 45);
1678 g_test_trap_assert_failed ();
1679 g_test_trap_assert_stderr ("*g_settings_range_check*");
1681 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1682 g_settings_set_int (settings, "val", 1);
1683 g_test_trap_assert_failed ();
1684 g_test_trap_assert_stderr ("*g_settings_range_check*");
1687 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1688 g_settings_set_int (direct, "val", 22);
1689 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
1690 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
1691 g_settings_set_int (direct, "val", 45);
1692 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
1693 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1694 g_settings_set_int (direct, "val", 1);
1695 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
1696 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1700 strv_has_string (gchar **haystack,
1701 const gchar *needle)
1705 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
1707 if (g_strcmp0 (haystack[n], needle) == 0)
1714 strv_set_equal (gchar **strv, ...)
1723 va_start (list, strv);
1726 str = va_arg (list, const gchar *);
1729 if (!strv_has_string (strv, str))
1739 res = g_strv_length ((gchar**)strv) == count;
1745 test_list_items (void)
1747 GSettings *settings;
1751 settings = g_settings_new ("org.gtk.test");
1752 children = g_settings_list_children (settings);
1753 keys = g_settings_list_keys (settings);
1755 g_assert (strv_set_equal (children, "basic-types", "complex-types", "localized", NULL));
1756 g_assert (strv_set_equal (keys, "greeting", "farewell", NULL));
1758 g_strfreev (children);
1761 g_object_unref (settings);
1765 test_list_schemas (void)
1767 const gchar * const *schemas;
1769 schemas = g_settings_list_schemas ();
1771 g_assert (strv_set_equal ((gchar **)schemas,
1773 "org.gtk.test.no-path",
1774 "org.gtk.test.basic-types",
1775 "org.gtk.test.complex-types",
1776 "org.gtk.test.localized",
1777 "org.gtk.test.binding",
1778 "org.gtk.test.enums",
1779 "org.gtk.test.enums.direct",
1780 "org.gtk.test.range",
1781 "org.gtk.test.range.direct",
1782 "org.gtk.test.mapped",
1787 map_func (GVariant *value,
1791 gint *state = user_data;
1795 v = g_variant_get_int32 (value);
1801 g_assert_cmpint (v, ==, 1);
1805 else if (*state == 1)
1807 g_assert_cmpint (v, ==, 0);
1813 g_assert (value == NULL);
1814 *result = g_variant_new_int32 (5);
1820 test_get_mapped (void)
1822 GSettings *settings;
1827 settings = g_settings_new ("org.gtk.test.mapped");
1828 g_settings_set_int (settings, "val", 1);
1831 p = g_settings_get_mapped (settings, "val", map_func, &state);
1832 val = g_variant_get_int32 ((GVariant*)p);
1833 g_assert_cmpint (val, ==, 5);
1835 g_variant_unref (p);
1836 g_object_unref (settings);
1840 main (int argc, char *argv[])
1845 setlocale (LC_ALL, "");
1847 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
1849 g_setenv ("XDG_DATA_DIRS", ".", TRUE);
1850 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
1853 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
1856 g_test_init (&argc, &argv, NULL);
1858 g_remove ("org.gtk.test.enums.xml");
1859 g_assert (g_spawn_command_line_sync ("../../gobject/glib-mkenums "
1860 "--template " SRCDIR "/enums.xml.template "
1861 SRCDIR "/testenum.h",
1862 &enums, NULL, &result, NULL));
1863 g_assert (result == 0);
1864 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
1867 g_remove ("gschemas.compiled");
1868 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=. "
1869 "--schema-file=org.gtk.test.enums.xml "
1870 "--schema-file=" SRCDIR "/org.gtk.test.gschema.xml",
1871 NULL, NULL, &result, NULL));
1872 g_assert (result == 0);
1874 g_test_add_func ("/gsettings/basic", test_basic);
1878 g_test_add_func ("/gsettings/no-schema", test_no_schema);
1879 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
1880 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
1881 g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
1882 g_test_add_func ("/gsettings/no-path", test_no_path);
1885 g_test_add_func ("/gsettings/basic-types", test_basic_types);
1886 g_test_add_func ("/gsettings/complex-types", test_complex_types);
1887 g_test_add_func ("/gsettings/changes", test_changes);
1889 if (glib_translations_work ())
1891 g_test_add_func ("/gsettings/l10n", test_l10n);
1892 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
1895 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
1896 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
1897 g_test_add_func ("/gsettings/atomic", test_atomic);
1899 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
1900 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
1901 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
1902 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
1903 g_test_add_func ("/gsettings/unbinding", test_unbind);
1904 g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
1908 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
1909 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
1910 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
1913 g_test_add_func ("/gsettings/keyfile", test_keyfile);
1914 g_test_add_func ("/gsettings/child-schema", test_child_schema);
1915 g_test_add_func ("/gsettings/strinfo", test_strinfo);
1916 g_test_add_func ("/gsettings/enums", test_enums);
1917 g_test_add_func ("/gsettings/flags", test_flags);
1918 g_test_add_func ("/gsettings/range", test_range);
1919 g_test_add_func ("/gsettings/list-items", test_list_items);
1920 g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
1921 g_test_add_func ("/gsettings/mapped", test_get_mapped);
1923 result = g_test_run ();