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)
954 settings = g_settings_new ("org.gtk.test.binding");
955 obj = test_object_new ();
957 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
958 g_object_set (obj, "bool", TRUE, NULL);
959 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
961 g_settings_set_boolean (settings, "bool", FALSE);
963 g_object_get (obj, "bool", &b, NULL);
964 g_assert_cmpint (b, ==, FALSE);
966 g_settings_bind (settings, "anti-bool", obj, "anti-bool",
967 G_SETTINGS_BIND_INVERT_BOOLEAN);
968 g_object_set (obj, "anti-bool", FALSE, NULL);
969 g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
971 g_settings_set_boolean (settings, "anti-bool", FALSE);
973 g_object_get (obj, "anti-bool", &b, NULL);
974 g_assert_cmpint (b, ==, TRUE);
976 g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
978 g_object_set (obj, "byte", 123, NULL);
980 g_settings_get (settings, "byte", "y", &y);
981 g_assert_cmpint (y, ==, 123);
983 g_settings_set (settings, "byte", "y", 54);
985 g_object_get (obj, "byte", &y, NULL);
986 g_assert_cmpint (y, ==, 54);
988 g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
990 g_object_set (obj, "int16", 1234, NULL);
992 g_settings_get (settings, "int16", "n", &n);
993 g_assert_cmpint (n, ==, 1234);
995 g_settings_set (settings, "int16", "n", 4321);
997 g_object_get (obj, "int16", &n2, NULL);
998 g_assert_cmpint (n2, ==, 4321);
1000 g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
1002 g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1004 g_settings_get (settings, "uint16", "q", &q);
1005 g_assert_cmpuint (q, ==, G_MAXUINT16);
1007 g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1009 g_object_get (obj, "uint16", &q2, NULL);
1010 g_assert_cmpuint (q2, ==, (guint16) G_MAXINT16);
1012 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1014 g_object_set (obj, "int", 12345, NULL);
1015 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1017 g_settings_set_int (settings, "int", 54321);
1019 g_object_get (obj, "int", &i, NULL);
1020 g_assert_cmpint (i, ==, 54321);
1022 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1024 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1026 g_settings_get (settings, "int64", "x", &i64);
1027 g_assert_cmpint (i64, ==, G_MAXINT64);
1029 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1031 g_object_get (obj, "int64", &i64, NULL);
1032 g_assert_cmpint (i64, ==, G_MININT64);
1034 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1036 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1038 g_settings_get (settings, "uint64", "t", &u64);
1039 g_assert_cmpuint (u64, ==, G_MAXUINT64);
1041 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1043 g_object_get (obj, "uint64", &u64, NULL);
1044 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1046 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1048 g_object_set (obj, "string", "bu ba", NULL);
1049 s = g_settings_get_string (settings, "string");
1050 g_assert_cmpstr (s, ==, "bu ba");
1053 g_settings_set_string (settings, "string", "bla bla");
1054 g_object_get (obj, "string", &s, NULL);
1055 g_assert_cmpstr (s, ==, "bla bla");
1058 g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1060 g_object_set (obj, "string", "non-unicode:\315", NULL);
1061 value = g_settings_get_value (settings, "chararray");
1062 g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1063 g_variant_unref (value);
1065 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1067 g_object_set (obj, "double", G_MAXFLOAT, NULL);
1068 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1070 g_settings_set_double (settings, "double", G_MINFLOAT);
1072 g_object_get (obj, "double", &d, NULL);
1073 g_assert_cmpfloat (d, ==, G_MINFLOAT);
1075 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1076 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1078 g_settings_set_double (settings, "double", -G_MINDOUBLE);
1080 g_object_get (obj, "double", &d, NULL);
1081 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1083 strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1084 g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1085 g_object_set (obj, "strv", strv, NULL);
1087 strv = g_settings_get_strv (settings, "strv");
1088 s = g_strjoinv (",", strv);
1089 g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1092 strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1093 g_settings_set_strv (settings, "strv", (const gchar **) strv);
1095 g_object_get (obj, "strv", &strv, NULL);
1096 s = g_strjoinv (",", strv);
1097 g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1101 g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1102 g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1103 s = g_settings_get_string (settings, "enum");
1104 g_assert_cmpstr (s, ==, "baz");
1106 g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1108 g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1110 g_object_get (obj, "enum", &i, NULL);
1111 g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1113 g_settings_set_string (settings, "enum", "baz");
1115 g_object_get (obj, "enum", &i, NULL);
1116 g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1118 g_object_unref (obj);
1119 g_object_unref (settings);
1126 GSettings *settings;
1128 settings = g_settings_new ("org.gtk.test.binding");
1129 obj = test_object_new ();
1131 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1133 g_object_set (obj, "int", 12345, NULL);
1134 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1136 g_settings_unbind (obj, "int");
1138 g_object_set (obj, "int", 54321, NULL);
1139 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1141 g_object_unref (obj);
1142 g_object_unref (settings);
1146 test_bind_writable (void)
1149 GSettings *settings;
1152 settings = g_settings_new ("org.gtk.test.binding");
1153 obj = test_object_new ();
1155 g_object_set (obj, "bool", FALSE, NULL);
1157 g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1159 g_object_get (obj, "bool", &b, NULL);
1162 g_settings_unbind (obj, "bool");
1164 g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1166 g_object_get (obj, "bool", &b, NULL);
1169 g_object_unref (obj);
1170 g_object_unref (settings);
1173 /* Test one-way bindings.
1174 * Verify that changes on one side show up on the other,
1175 * but not vice versa
1178 test_directional_binding (void)
1181 GSettings *settings;
1185 settings = g_settings_new ("org.gtk.test.binding");
1186 obj = test_object_new ();
1188 g_object_set (obj, "bool", FALSE, NULL);
1189 g_settings_set_boolean (settings, "bool", FALSE);
1191 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1193 g_settings_set_boolean (settings, "bool", TRUE);
1194 g_object_get (obj, "bool", &b, NULL);
1195 g_assert_cmpint (b, ==, TRUE);
1197 g_object_set (obj, "bool", FALSE, NULL);
1198 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1200 g_object_set (obj, "int", 20, NULL);
1201 g_settings_set_int (settings, "int", 20);
1203 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1205 g_object_set (obj, "int", 32, NULL);
1206 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1208 g_settings_set_int (settings, "int", 20);
1209 g_object_get (obj, "int", &i, NULL);
1210 g_assert_cmpint (i, ==, 32);
1212 g_object_unref (obj);
1213 g_object_unref (settings);
1216 /* Test that type mismatch is caught when creating a binding
1219 test_typesafe_binding (void)
1221 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1224 GSettings *settings;
1226 settings = g_settings_new ("org.gtk.test.binding");
1227 obj = test_object_new ();
1229 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1231 g_object_unref (obj);
1232 g_object_unref (settings);
1234 g_test_trap_assert_failed ();
1235 g_test_trap_assert_stderr ("*not compatible*");
1239 string_to_bool (GValue *value,
1245 s = g_variant_get_string (variant, NULL);
1246 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1252 bool_to_string (const GValue *value,
1253 const GVariantType *expected_type,
1256 if (g_value_get_boolean (value))
1257 return g_variant_new_string ("true");
1259 return g_variant_new_string ("false");
1262 /* Test custom bindings.
1263 * Translate strings to booleans and back
1266 test_custom_binding (void)
1269 GSettings *settings;
1273 settings = g_settings_new ("org.gtk.test.binding");
1274 obj = test_object_new ();
1276 g_settings_set_string (settings, "string", "true");
1278 g_settings_bind_with_mapping (settings, "string",
1280 G_SETTINGS_BIND_DEFAULT,
1285 g_settings_set_string (settings, "string", "false");
1286 g_object_get (obj, "bool", &b, NULL);
1287 g_assert_cmpint (b, ==, FALSE);
1289 g_settings_set_string (settings, "string", "not true");
1290 g_object_get (obj, "bool", &b, NULL);
1291 g_assert_cmpint (b, ==, FALSE);
1293 g_object_set (obj, "bool", TRUE, NULL);
1294 s = g_settings_get_string (settings, "string");
1295 g_assert_cmpstr (s, ==, "true");
1298 g_object_unref (obj);
1299 g_object_unref (settings);
1302 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1303 * initial settings value is transported to the object
1304 * side, but later settings changes do not affect the
1308 test_no_change_binding (void)
1311 GSettings *settings;
1314 settings = g_settings_new ("org.gtk.test.binding");
1315 obj = test_object_new ();
1317 g_object_set (obj, "bool", TRUE, NULL);
1318 g_settings_set_boolean (settings, "bool", FALSE);
1320 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1322 g_object_get (obj, "bool", &b, NULL);
1323 g_assert_cmpint (b, ==, FALSE);
1325 g_settings_set_boolean (settings, "bool", TRUE);
1326 g_object_get (obj, "bool", &b, NULL);
1327 g_assert_cmpint (b, ==, FALSE);
1329 g_settings_set_boolean (settings, "bool", FALSE);
1330 g_object_set (obj, "bool", TRUE, NULL);
1331 b = g_settings_get_boolean (settings, "bool");
1332 g_assert_cmpint (b, ==, TRUE);
1334 g_object_unref (obj);
1335 g_object_unref (settings);
1338 /* Test that binding a non-readable property only
1339 * works in 'GET' mode.
1342 test_no_read_binding (void)
1344 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1347 GSettings *settings;
1349 settings = g_settings_new ("org.gtk.test.binding");
1350 obj = test_object_new ();
1352 g_settings_bind (settings, "string", obj, "no-read", 0);
1354 g_test_trap_assert_failed ();
1355 g_test_trap_assert_stderr ("*property*is not readable*");
1357 if (g_test_trap_fork (0, 0))
1360 GSettings *settings;
1362 settings = g_settings_new ("org.gtk.test.binding");
1363 obj = test_object_new ();
1365 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1369 g_test_trap_assert_passed ();
1372 /* Test that binding a non-writable property only
1373 * works in 'SET' mode.
1376 test_no_write_binding (void)
1378 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1381 GSettings *settings;
1383 settings = g_settings_new ("org.gtk.test.binding");
1384 obj = test_object_new ();
1386 g_settings_bind (settings, "string", obj, "no-write", 0);
1388 g_test_trap_assert_failed ();
1389 g_test_trap_assert_stderr ("*property*is not writable*");
1391 if (g_test_trap_fork (0, 0))
1394 GSettings *settings;
1396 settings = g_settings_new ("org.gtk.test.binding");
1397 obj = test_object_new ();
1399 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1403 g_test_trap_assert_passed ();
1407 * Test that using a keyfile works
1412 GSettingsBackend *kf_backend;
1413 GSettings *settings;
1417 g_remove ("gsettings.store");
1419 kf_backend = g_keyfile_settings_backend_new ("gsettings.store", "/", "root");
1420 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1421 g_object_unref (kf_backend);
1423 g_settings_set (settings, "greeting", "s", "see if this works");
1425 keyfile = g_key_file_new ();
1426 g_assert (g_key_file_load_from_file (keyfile, "gsettings.store", 0, NULL));
1428 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1429 g_assert_cmpstr (str, ==, "'see if this works'");
1432 g_key_file_free (keyfile);
1433 g_object_unref (settings);
1436 /* Test that getting child schemas works
1439 test_child_schema (void)
1441 GSettings *settings;
1445 /* first establish some known conditions */
1446 settings = g_settings_new ("org.gtk.test.basic-types");
1447 g_settings_set (settings, "test-byte", "y", 36);
1449 g_settings_get (settings, "test-byte", "y", &byte);
1450 g_assert_cmpint (byte, ==, 36);
1452 g_object_unref (settings);
1454 settings = g_settings_new ("org.gtk.test");
1455 child = g_settings_get_child (settings, "basic-types");
1456 g_assert (child != NULL);
1458 g_settings_get (child, "test-byte", "y", &byte);
1459 g_assert_cmpint (byte, ==, 36);
1461 g_object_unref (child);
1462 g_object_unref (settings);
1466 glib_translations_work (void)
1469 gchar *orig = "Unnamed";
1472 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
1473 setlocale (LC_MESSAGES, "de");
1474 str = dgettext ("glib20", orig);
1475 setlocale (LC_MESSAGES, locale);
1481 #include "../strinfo.c"
1486 /* "foo" has a value of 1
1487 * "bar" has a value of 2
1488 * "baz" is an alias for "bar"
1491 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1492 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1494 const guint32 *strinfo = (guint32 *) array;
1495 guint length = sizeof array / 4;
1499 /* build it and compare */
1502 builder = g_string_new (NULL);
1503 strinfo_builder_append_item (builder, "foo", 1);
1504 strinfo_builder_append_item (builder, "bar", 2);
1505 g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
1506 g_assert_cmpint (builder->len % 4, ==, 0);
1507 g_assert_cmpint (builder->len / 4, ==, length);
1508 g_assert (memcmp (builder->str, strinfo, length * 4) == 0);
1509 g_string_free (builder, TRUE);
1512 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
1514 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
1516 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
1518 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
1521 g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
1522 g_assert_cmpint (result, ==, 1);
1523 g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
1524 g_assert_cmpint (result, ==, 2);
1525 g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
1526 g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
1528 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
1529 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
1530 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
1531 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
1533 g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
1534 g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
1535 g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
1536 g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
1542 GSettings *settings, *direct;
1545 settings = g_settings_new ("org.gtk.test.enums");
1546 direct = g_settings_new ("org.gtk.test.enums.direct");
1550 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1551 g_settings_get_enum (direct, "test");
1552 g_test_trap_assert_failed ();
1553 g_test_trap_assert_stderr ("*not associated with an enum*");
1555 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1556 g_settings_set_enum (settings, "test", 42);
1557 g_test_trap_assert_failed ();
1558 g_test_trap_assert_stderr ("*invalid enum value 42*");
1560 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1561 g_settings_set_string (settings, "test", "qux");
1562 g_test_trap_assert_failed ();
1563 g_test_trap_assert_stderr ("*g_settings_range_check*");
1565 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1566 g_settings_get_flags (settings, "test");
1567 g_test_trap_assert_failed ();
1568 g_test_trap_assert_stderr ("*not associated with a flags*");
1571 str = g_settings_get_string (settings, "test");
1572 g_assert_cmpstr (str, ==, "bar");
1575 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
1577 str = g_settings_get_string (settings, "test");
1578 g_assert_cmpstr (str, ==, "foo");
1581 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
1583 g_settings_set_string (direct, "test", "qux");
1585 str = g_settings_get_string (direct, "test");
1586 g_assert_cmpstr (str, ==, "qux");
1589 str = g_settings_get_string (settings, "test");
1590 g_assert_cmpstr (str, ==, "quux");
1593 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
1599 GSettings *settings, *direct;
1603 settings = g_settings_new ("org.gtk.test.enums");
1604 direct = g_settings_new ("org.gtk.test.enums.direct");
1608 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1609 g_settings_get_flags (direct, "test");
1610 g_test_trap_assert_failed ();
1611 g_test_trap_assert_stderr ("*not associated with a flags*");
1613 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1614 g_settings_set_flags (settings, "f-test", 0x42);
1615 g_test_trap_assert_failed ();
1616 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
1618 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1619 g_settings_set_strv (settings, "f-test",
1620 (const gchar **) g_strsplit ("rock", ",", 0));
1621 g_test_trap_assert_failed ();
1622 g_test_trap_assert_stderr ("*g_settings_range_check*");
1624 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1625 g_settings_get_enum (settings, "f-test");
1626 g_test_trap_assert_failed ();
1627 g_test_trap_assert_stderr ("*not associated with an enum*");
1630 strv = g_settings_get_strv (settings, "f-test");
1631 str = g_strjoinv (",", strv);
1632 g_assert_cmpstr (str, ==, "");
1636 g_settings_set_flags (settings, "f-test",
1637 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1639 strv = g_settings_get_strv (settings, "f-test");
1640 str = g_strjoinv (",", strv);
1641 g_assert_cmpstr (str, ==, "talking,walking");
1645 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1646 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1648 strv = g_strsplit ("speaking,laughing", ",", 0);
1649 g_settings_set_strv (direct, "f-test", (const gchar **) strv);
1652 strv = g_settings_get_strv (direct, "f-test");
1653 str = g_strjoinv (",", strv);
1654 g_assert_cmpstr (str, ==, "speaking,laughing");
1658 strv = g_settings_get_strv (settings, "f-test");
1659 str = g_strjoinv (",", strv);
1660 g_assert_cmpstr (str, ==, "talking,laughing");
1664 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1665 TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
1671 GSettings *settings, *direct;
1673 settings = g_settings_new ("org.gtk.test.range");
1674 direct = g_settings_new ("org.gtk.test.range.direct");
1678 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1679 g_settings_set_int (settings, "val", 45);
1680 g_test_trap_assert_failed ();
1681 g_test_trap_assert_stderr ("*g_settings_range_check*");
1683 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1684 g_settings_set_int (settings, "val", 1);
1685 g_test_trap_assert_failed ();
1686 g_test_trap_assert_stderr ("*g_settings_range_check*");
1689 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1690 g_settings_set_int (direct, "val", 22);
1691 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
1692 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
1693 g_settings_set_int (direct, "val", 45);
1694 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
1695 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1696 g_settings_set_int (direct, "val", 1);
1697 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
1698 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1702 strv_has_string (gchar **haystack,
1703 const gchar *needle)
1707 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
1709 if (g_strcmp0 (haystack[n], needle) == 0)
1716 strv_set_equal (gchar **strv, ...)
1725 va_start (list, strv);
1728 str = va_arg (list, const gchar *);
1731 if (!strv_has_string (strv, str))
1741 res = g_strv_length ((gchar**)strv) == count;
1747 test_list_items (void)
1749 GSettings *settings;
1753 settings = g_settings_new ("org.gtk.test");
1754 children = g_settings_list_children (settings);
1755 keys = g_settings_list_keys (settings);
1757 g_assert (strv_set_equal (children, "basic-types", "complex-types", "localized", NULL));
1758 g_assert (strv_set_equal (keys, "greeting", "farewell", NULL));
1760 g_strfreev (children);
1763 g_object_unref (settings);
1767 test_list_schemas (void)
1769 const gchar * const *schemas;
1770 const gchar * const *relocs;
1772 relocs = g_settings_list_relocatable_schemas ();
1773 schemas = g_settings_list_schemas ();
1775 g_assert (strv_set_equal ((gchar **)relocs,
1776 "org.gtk.test.no-path",
1779 g_assert (strv_set_equal ((gchar **)schemas,
1781 "org.gtk.test.basic-types",
1782 "org.gtk.test.complex-types",
1783 "org.gtk.test.localized",
1784 "org.gtk.test.binding",
1785 "org.gtk.test.enums",
1786 "org.gtk.test.enums.direct",
1787 "org.gtk.test.range",
1788 "org.gtk.test.range.direct",
1789 "org.gtk.test.mapped",
1794 map_func (GVariant *value,
1798 gint *state = user_data;
1802 v = g_variant_get_int32 (value);
1808 g_assert_cmpint (v, ==, 1);
1812 else if (*state == 1)
1814 g_assert_cmpint (v, ==, 0);
1820 g_assert (value == NULL);
1821 *result = g_variant_new_int32 (5);
1827 test_get_mapped (void)
1829 GSettings *settings;
1834 settings = g_settings_new ("org.gtk.test.mapped");
1835 g_settings_set_int (settings, "val", 1);
1838 p = g_settings_get_mapped (settings, "val", map_func, &state);
1839 val = g_variant_get_int32 ((GVariant*)p);
1840 g_assert_cmpint (val, ==, 5);
1842 g_variant_unref (p);
1843 g_object_unref (settings);
1847 main (int argc, char *argv[])
1852 setlocale (LC_ALL, "");
1854 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
1856 g_setenv ("XDG_DATA_DIRS", ".", TRUE);
1857 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
1860 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
1863 g_test_init (&argc, &argv, NULL);
1865 g_remove ("org.gtk.test.enums.xml");
1866 g_assert (g_spawn_command_line_sync ("../../gobject/glib-mkenums "
1867 "--template " SRCDIR "/enums.xml.template "
1868 SRCDIR "/testenum.h",
1869 &enums, NULL, &result, NULL));
1870 g_assert (result == 0);
1871 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
1874 g_remove ("gschemas.compiled");
1875 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=. "
1876 "--schema-file=org.gtk.test.enums.xml "
1877 "--schema-file=" SRCDIR "/org.gtk.test.gschema.xml",
1878 NULL, NULL, &result, NULL));
1879 g_assert (result == 0);
1881 g_test_add_func ("/gsettings/basic", test_basic);
1885 g_test_add_func ("/gsettings/no-schema", test_no_schema);
1886 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
1887 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
1888 g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
1889 g_test_add_func ("/gsettings/no-path", test_no_path);
1892 g_test_add_func ("/gsettings/basic-types", test_basic_types);
1893 g_test_add_func ("/gsettings/complex-types", test_complex_types);
1894 g_test_add_func ("/gsettings/changes", test_changes);
1896 if (glib_translations_work ())
1898 g_test_add_func ("/gsettings/l10n", test_l10n);
1899 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
1902 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
1903 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
1904 g_test_add_func ("/gsettings/atomic", test_atomic);
1906 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
1907 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
1908 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
1909 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
1910 g_test_add_func ("/gsettings/unbinding", test_unbind);
1911 g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
1915 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
1916 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
1917 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
1920 g_test_add_func ("/gsettings/keyfile", test_keyfile);
1921 g_test_add_func ("/gsettings/child-schema", test_child_schema);
1922 g_test_add_func ("/gsettings/strinfo", test_strinfo);
1923 g_test_add_func ("/gsettings/enums", test_enums);
1924 g_test_add_func ("/gsettings/flags", test_flags);
1925 g_test_add_func ("/gsettings/range", test_range);
1926 g_test_add_func ("/gsettings/list-items", test_list_items);
1927 g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
1928 g_test_add_func ("/gsettings/mapped", test_get_mapped);
1930 result = g_test_run ();