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_settings_get (settings, "greeting", "s", &str);
29 g_assert_cmpstr (str, ==, "Hello, earthlings");
32 g_settings_set (settings, "greeting", "s", "goodbye world");
33 g_settings_get (settings, "greeting", "s", &str);
34 g_assert_cmpstr (str, ==, "goodbye world");
40 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
42 settings = g_settings_new ("org.gtk.test");
43 g_settings_set (settings, "greeting", "i", 555);
46 g_test_trap_assert_failed ();
47 g_test_trap_assert_stderr ("*g_settings_type_check*");
50 g_settings_get (settings, "greeting", "s", &str);
51 g_assert_cmpstr (str, ==, "goodbye world");
55 g_settings_set (settings, "greeting", "s", "this is the end");
56 g_object_unref (settings);
59 /* Check that we get an error when getting a key
60 * that is not in the schema
63 test_unknown_key (void)
65 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
70 settings = g_settings_new ("org.gtk.test");
71 value = g_settings_get_value (settings, "no_such_key");
73 g_assert (value == NULL);
75 g_object_unref (settings);
77 g_test_trap_assert_failed ();
78 g_test_trap_assert_stderr ("*does not contain*");
81 /* Check that we get an error when the schema
82 * has not been installed
87 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
91 settings = g_settings_new ("no.such.schema");
93 g_assert (settings == NULL);
96 g_test_trap_assert_failed ();
97 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
100 /* Check that we get an error when passing a type string
101 * that does not match the schema
104 test_wrong_type (void)
106 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
111 settings = g_settings_new ("org.gtk.test");
113 g_settings_get (settings, "greeting", "o", &str);
115 g_assert (str == NULL);
117 g_test_trap_assert_failed ();
118 g_test_trap_assert_stderr ("*CRITICAL*");
120 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
124 settings = g_settings_new ("org.gtk.test");
126 g_settings_set (settings, "greeting", "o", "/a/path");
128 g_test_trap_assert_failed ();
129 g_test_trap_assert_stderr ("*CRITICAL*");
132 /* Check that we can successfully read and set the full
133 * range of all basic types
136 test_basic_types (void)
150 settings = g_settings_new ("org.gtk.test.basic-types");
152 g_settings_get (settings, "test-boolean", "b", &b);
153 g_assert_cmpint (b, ==, 1);
155 g_settings_set (settings, "test-boolean", "b", 0);
156 g_settings_get (settings, "test-boolean", "b", &b);
157 g_assert_cmpint (b, ==, 0);
159 g_settings_get (settings, "test-byte", "y", &byte);
160 g_assert_cmpint (byte, ==, 25);
162 g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
163 g_settings_get (settings, "test-byte", "y", &byte);
164 g_assert_cmpint (byte, ==, G_MAXUINT8);
166 g_settings_get (settings, "test-int16", "n", &i16);
167 g_assert_cmpint (i16, ==, -1234);
169 g_settings_set (settings, "test-int16", "n", G_MININT16);
170 g_settings_get (settings, "test-int16", "n", &i16);
171 g_assert_cmpint (i16, ==, G_MININT16);
173 g_settings_set (settings, "test-int16", "n", G_MAXINT16);
174 g_settings_get (settings, "test-int16", "n", &i16);
175 g_assert_cmpint (i16, ==, G_MAXINT16);
177 g_settings_get (settings, "test-uint16", "q", &u16);
178 g_assert_cmpuint (u16, ==, 1234);
180 g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
181 g_settings_get (settings, "test-uint16", "q", &u16);
182 g_assert_cmpuint (u16, ==, G_MAXUINT16);
184 g_settings_get (settings, "test-int32", "i", &i32);
185 g_assert_cmpint (i32, ==, -123456);
187 g_settings_set (settings, "test-int32", "i", G_MININT32);
188 g_settings_get (settings, "test-int32", "i", &i32);
189 g_assert_cmpint (i32, ==, G_MININT32);
191 g_settings_set (settings, "test-int32", "i", G_MAXINT32);
192 g_settings_get (settings, "test-int32", "i", &i32);
193 g_assert_cmpint (i32, ==, G_MAXINT32);
195 g_settings_get (settings, "test-uint32", "u", &u32);
196 g_assert_cmpuint (u32, ==, 123456);
198 g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
199 g_settings_get (settings, "test-uint32", "u", &u32);
200 g_assert_cmpuint (u32, ==, G_MAXUINT32);
202 g_settings_get (settings, "test-int64", "x", &i64);
203 g_assert_cmpuint (i64, ==, -123456789);
205 g_settings_set (settings, "test-int64", "x", G_MININT64);
206 g_settings_get (settings, "test-int64", "x", &i64);
207 g_assert_cmpuint (i64, ==, G_MININT64);
209 g_settings_set (settings, "test-int64", "x", G_MAXINT64);
210 g_settings_get (settings, "test-int64", "x", &i64);
211 g_assert_cmpuint (i64, ==, G_MAXINT64);
213 g_settings_get (settings, "test-uint64", "t", &u64);
214 g_assert_cmpuint (u64, ==, 123456789);
216 g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
217 g_settings_get (settings, "test-uint64", "t", &u64);
218 g_assert_cmpuint (u64, ==, G_MAXUINT64);
220 g_settings_get (settings, "test-double", "d", &d);
221 g_assert_cmpfloat (d, ==, 123.456);
223 g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
224 g_settings_get (settings, "test-double", "d", &d);
225 g_assert_cmpfloat (d, ==, G_MINDOUBLE);
227 g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
228 g_settings_get (settings, "test-double", "d", &d);
229 g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
231 g_settings_get (settings, "test-string", "s", &str);
232 g_assert_cmpstr (str, ==, "a string, it seems");
236 g_settings_get (settings, "test-objectpath", "o", &str);
237 g_assert_cmpstr (str, ==, "/a/object/path");
238 g_object_unref (settings);
243 /* Check that we can read an set complex types like
244 * tuples, arrays and dictionaries
247 test_complex_types (void)
252 GVariantIter *iter = NULL;
254 settings = g_settings_new ("org.gtk.test.complex-types");
256 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
257 g_assert_cmpstr (s, ==, "one");
258 g_assert_cmpint (i1,==, 2);
259 g_assert_cmpint (i2,==, 3);
263 g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
264 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
265 g_assert_cmpstr (s, ==, "none");
266 g_assert_cmpint (i1,==, 0);
267 g_assert_cmpint (i2,==, 0);
271 g_settings_get (settings, "test-array", "ai", &iter);
272 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
273 g_assert (g_variant_iter_next (iter, "i", &i1));
274 g_assert_cmpint (i1, ==, 0);
275 g_assert (g_variant_iter_next (iter, "i", &i1));
276 g_assert_cmpint (i1, ==, 1);
277 g_assert (g_variant_iter_next (iter, "i", &i1));
278 g_assert_cmpint (i1, ==, 2);
279 g_assert (g_variant_iter_next (iter, "i", &i1));
280 g_assert_cmpint (i1, ==, 3);
281 g_assert (g_variant_iter_next (iter, "i", &i1));
282 g_assert_cmpint (i1, ==, 4);
283 g_assert (g_variant_iter_next (iter, "i", &i1));
284 g_assert_cmpint (i1, ==, 5);
285 g_assert (!g_variant_iter_next (iter, "i", &i1));
286 g_variant_iter_free (iter);
288 g_object_unref (settings);
291 static gboolean changed_cb_called;
294 changed_cb (GSettings *settings,
298 changed_cb_called = TRUE;
300 g_assert_cmpstr (key, ==, data);
303 /* Test that basic change notification with the changed signal works.
309 GSettings *settings2;
311 settings = g_settings_new ("org.gtk.test");
313 g_signal_connect (settings, "changed",
314 G_CALLBACK (changed_cb), "greeting");
316 changed_cb_called = FALSE;
318 g_settings_set (settings, "greeting", "s", "new greeting");
319 g_assert (changed_cb_called);
321 settings2 = g_settings_new ("org.gtk.test");
323 changed_cb_called = FALSE;
325 g_settings_set (settings2, "greeting", "s", "hi");
326 g_assert (changed_cb_called);
328 g_object_unref (settings2);
329 g_object_unref (settings);
332 static gboolean changed_cb_called2;
335 changed_cb2 (GSettings *settings,
344 /* Test that changes done to a delay-mode instance
345 * don't appear to the outside world until apply. Also
346 * check that we get change notification when they are
348 * Also test that the has-unapplied property is properly
352 test_delay_apply (void)
355 GSettings *settings2;
358 settings = g_settings_new ("org.gtk.test");
359 settings2 = g_settings_new ("org.gtk.test");
361 g_settings_set (settings2, "greeting", "s", "top o' the morning");
363 changed_cb_called = FALSE;
364 changed_cb_called2 = FALSE;
366 g_signal_connect (settings, "changed",
367 G_CALLBACK (changed_cb2), &changed_cb_called);
368 g_signal_connect (settings2, "changed",
369 G_CALLBACK (changed_cb2), &changed_cb_called2);
371 g_settings_delay (settings);
373 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
375 g_assert (changed_cb_called);
376 g_assert (!changed_cb_called2);
378 g_settings_get (settings, "greeting", "s", &str);
379 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
383 g_settings_get (settings2, "greeting", "s", &str);
384 g_assert_cmpstr (str, ==, "top o' the morning");
388 g_assert (g_settings_get_has_unapplied (settings));
389 g_assert (!g_settings_get_has_unapplied (settings2));
391 changed_cb_called = FALSE;
392 changed_cb_called2 = FALSE;
394 g_settings_apply (settings);
396 g_assert (!changed_cb_called);
397 g_assert (changed_cb_called2);
399 g_settings_get (settings, "greeting", "s", &str);
400 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
404 g_settings_get (settings2, "greeting", "s", &str);
405 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
409 g_assert (!g_settings_get_has_unapplied (settings));
410 g_assert (!g_settings_get_has_unapplied (settings2));
412 g_object_unref (settings2);
413 g_object_unref (settings);
416 /* Test that reverting unapplied changes in a delay-apply
417 * settings instance works.
420 test_delay_revert (void)
423 GSettings *settings2;
426 settings = g_settings_new ("org.gtk.test");
427 settings2 = g_settings_new ("org.gtk.test");
429 g_settings_set (settings2, "greeting", "s", "top o' the morning");
431 g_settings_delay (settings);
433 g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
435 g_settings_get (settings, "greeting", "s", &str);
436 g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
440 g_settings_get (settings2, "greeting", "s", &str);
441 g_assert_cmpstr (str, ==, "top o' the morning");
445 g_assert (g_settings_get_has_unapplied (settings));
447 g_settings_revert (settings);
449 g_assert (!g_settings_get_has_unapplied (settings));
451 g_settings_get (settings, "greeting", "s", &str);
452 g_assert_cmpstr (str, ==, "top o' the morning");
456 g_settings_get (settings2, "greeting", "s", &str);
457 g_assert_cmpstr (str, ==, "top o' the morning");
461 g_object_unref (settings2);
462 g_object_unref (settings);
466 keys_changed_cb (GSettings *settings,
472 g_assert_cmpint (n_keys, ==, 2);
474 g_assert ((keys[0] == g_quark_from_static_string ("greeting") &&
475 keys[1] == g_quark_from_static_string ("farewell")) ||
476 (keys[1] == g_quark_from_static_string ("greeting") &&
477 keys[0] == g_quark_from_static_string ("farewell")));
479 g_settings_get (settings, "greeting", "s", &str);
480 g_assert_cmpstr (str, ==, "greetings from test_atomic");
484 g_settings_get (settings, "farewell", "s", &str);
485 g_assert_cmpstr (str, ==, "atomic bye-bye");
490 /* Check that delay-applied changes appear atomically.
491 * More specifically, verify that all changed keys appear
492 * with their new value while handling the change-event signal.
498 GSettings *settings2;
501 settings = g_settings_new ("org.gtk.test");
502 settings2 = g_settings_new ("org.gtk.test");
504 g_settings_set (settings2, "greeting", "s", "top o' the morning");
506 changed_cb_called = FALSE;
507 changed_cb_called2 = FALSE;
509 g_signal_connect (settings2, "change-event",
510 G_CALLBACK (keys_changed_cb), NULL);
512 g_settings_delay (settings);
514 g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
515 g_settings_set (settings, "farewell", "s", "atomic bye-bye");
517 g_settings_apply (settings);
519 g_settings_get (settings, "greeting", "s", &str);
520 g_assert_cmpstr (str, ==, "greetings from test_atomic");
524 g_settings_get (settings, "farewell", "s", &str);
525 g_assert_cmpstr (str, ==, "atomic bye-bye");
529 g_settings_get (settings2, "greeting", "s", &str);
530 g_assert_cmpstr (str, ==, "greetings from test_atomic");
534 g_settings_get (settings2, "farewell", "s", &str);
535 g_assert_cmpstr (str, ==, "atomic bye-bye");
539 g_object_unref (settings2);
540 g_object_unref (settings);
543 /* On Windows the interaction between the C library locale and libintl
544 * (from GNU gettext) is not like on POSIX, so just skip these tests
547 * There are several issues:
549 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
550 * in libintl (defined in its <libintl.h>).
552 * 2) The locale names that setlocale() accepts and returns aren't in
553 * the "de_DE" style, but like "German_Germany".
555 * 3) libintl looks at the Win32 thread locale and not the C library
556 * locale. (And even if libintl would use the C library's locale, as
557 * there are several alternative C library DLLs, libintl might be
558 * linked to a different one than the application code, so they
559 * wouldn't have the same C library locale anyway.)
562 /* Test that translations work for schema defaults.
564 * This test relies on the de.po file in the same directory
565 * to be compiled into ./de/LC_MESSAGES/test.mo
574 bindtextdomain ("test", ".");
575 bind_textdomain_codeset ("test", "UTF-8");
577 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
579 settings = g_settings_new ("org.gtk.test.localized");
581 setlocale (LC_MESSAGES, "C");
582 str = g_settings_get_string (settings, "error-message");
583 setlocale (LC_MESSAGES, locale);
585 g_assert_cmpstr (str, ==, "Unnamed");
589 setlocale (LC_MESSAGES, "de_DE");
590 str = g_settings_get_string (settings, "error-message");
591 setlocale (LC_MESSAGES, locale);
593 g_assert_cmpstr (str, ==, "Unbenannt");
594 g_object_unref (settings);
601 /* Test that message context works as expected with translated
602 * schema defaults. Also, verify that non-ASCII UTF-8 content
605 * This test relies on the de.po file in the same directory
606 * to be compiled into ./de/LC_MESSAGES/test.mo
609 test_l10n_context (void)
615 bindtextdomain ("test", ".");
616 bind_textdomain_codeset ("test", "UTF-8");
618 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
620 settings = g_settings_new ("org.gtk.test.localized");
622 setlocale (LC_MESSAGES, "C");
623 g_settings_get (settings, "backspace", "s", &str);
624 setlocale (LC_MESSAGES, locale);
626 g_assert_cmpstr (str, ==, "BackSpace");
630 setlocale (LC_MESSAGES, "de_DE");
631 g_settings_get (settings, "backspace", "s", &str);
632 setlocale (LC_MESSAGES, locale);
634 g_assert_cmpstr (str, ==, "Löschen");
635 g_object_unref (settings);
657 GObject parent_instance;
666 gchar *no_write_prop;
671 GObjectClass parent_class;
674 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
677 test_object_init (TestObject *object)
682 test_object_finalize (GObject *object)
684 TestObject *testo = (TestObject*)object;
685 g_free (testo->string_prop);
686 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
690 test_object_get_property (GObject *object,
695 TestObject *test_object = (TestObject *)object;
700 g_value_set_boolean (value, test_object->bool_prop);
703 g_value_set_int (value, test_object->int_prop);
706 g_value_set_int64 (value, test_object->int64_prop);
709 g_value_set_uint64 (value, test_object->uint64_prop);
712 g_value_set_double (value, test_object->double_prop);
715 g_value_set_string (value, test_object->string_prop);
718 g_value_set_string (value, test_object->no_write_prop);
721 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
727 test_object_set_property (GObject *object,
732 TestObject *test_object = (TestObject *)object;
737 test_object->bool_prop = g_value_get_boolean (value);
740 test_object->int_prop = g_value_get_int (value);
743 test_object->int64_prop = g_value_get_int64 (value);
746 test_object->uint64_prop = g_value_get_uint64 (value);
749 test_object->double_prop = g_value_get_double (value);
752 g_free (test_object->string_prop);
753 test_object->string_prop = g_value_dup_string (value);
756 g_free (test_object->no_read_prop);
757 test_object->no_read_prop = g_value_dup_string (value);
760 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
766 test_object_class_init (TestObjectClass *class)
768 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
770 gobject_class->get_property = test_object_get_property;
771 gobject_class->set_property = test_object_set_property;
772 gobject_class->finalize = test_object_finalize;
774 g_object_class_install_property (gobject_class, PROP_BOOL,
775 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
776 g_object_class_install_property (gobject_class, PROP_INT,
777 g_param_spec_int ("int", "", "", -G_MAXINT, G_MAXINT, 0, G_PARAM_READWRITE));
778 g_object_class_install_property (gobject_class, PROP_INT64,
779 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
780 g_object_class_install_property (gobject_class, PROP_UINT64,
781 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
782 g_object_class_install_property (gobject_class, PROP_DOUBLE,
783 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
784 g_object_class_install_property (gobject_class, PROP_STRING,
785 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
786 g_object_class_install_property (gobject_class, PROP_NO_WRITE,
787 g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
788 g_object_class_install_property (gobject_class, PROP_NO_READ,
789 g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
793 test_object_new (void)
795 return (TestObject*)g_object_new (test_object_get_type (), NULL);
798 /* Test basic binding functionality for simple types.
799 * Verify that with bidirectional bindings, changes on either side
800 * are notified on the other end.
803 test_simple_binding (void)
814 settings = g_settings_new ("org.gtk.test.binding");
815 obj = test_object_new ();
817 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
819 g_object_set (obj, "bool", TRUE, NULL);
820 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
822 g_settings_set_boolean (settings, "bool", FALSE);
823 g_object_get (obj, "bool", &b, NULL);
824 g_assert_cmpint (b, ==, FALSE);
826 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
828 g_object_set (obj, "int", 12345, NULL);
829 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
831 g_settings_set_int (settings, "int", 54321);
832 g_object_get (obj, "int", &i, NULL);
833 g_assert_cmpint (i, ==, 54321);
835 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
837 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
838 g_settings_get (settings, "int64", "x", &i64);
839 g_assert_cmpint (i64, ==, G_MAXINT64);
841 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
842 g_object_get (obj, "int64", &i64, NULL);
843 g_assert_cmpint (i64, ==, G_MININT64);
845 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
847 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
848 g_settings_get (settings, "uint64", "t", &u64);
849 g_assert_cmpuint (u64, ==, G_MAXUINT64);
851 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
852 g_object_get (obj, "uint64", &u64, NULL);
853 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
855 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
857 g_object_set (obj, "string", "bu ba", NULL);
858 s = g_settings_get_string (settings, "string");
859 g_assert_cmpstr (s, ==, "bu ba");
862 g_settings_set_string (settings, "string", "bla bla");
863 g_object_get (obj, "string", &s, NULL);
864 g_assert_cmpstr (s, ==, "bla bla");
867 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
869 g_object_set (obj, "double", G_MAXFLOAT, NULL);
870 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
872 g_settings_set_double (settings, "double", G_MINFLOAT);
873 g_object_get (obj, "double", &d, NULL);
874 g_assert_cmpfloat (d, ==, G_MINFLOAT);
876 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
877 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
879 g_settings_set_double (settings, "double", -G_MINDOUBLE);
880 g_object_get (obj, "double", &d, NULL);
881 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
882 g_object_unref (obj);
883 g_object_unref (settings);
886 /* Test one-way bindings.
887 * Verify that changes on one side show up on the other,
891 test_directional_binding (void)
898 settings = g_settings_new ("org.gtk.test.binding");
899 obj = test_object_new ();
901 g_object_set (obj, "bool", FALSE, NULL);
902 g_settings_set_boolean (settings, "bool", FALSE);
904 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
906 g_settings_set_boolean (settings, "bool", TRUE);
907 g_object_get (obj, "bool", &b, NULL);
908 g_assert_cmpint (b, ==, TRUE);
910 g_object_set (obj, "bool", FALSE, NULL);
911 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
913 g_object_set (obj, "int", 20, NULL);
914 g_settings_set_int (settings, "int", 20);
916 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
918 g_object_set (obj, "int", 32, NULL);
919 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
921 g_settings_set_int (settings, "int", 20);
922 g_object_get (obj, "int", &i, NULL);
923 g_assert_cmpint (i, ==, 32);
925 g_object_unref (obj);
926 g_object_unref (settings);
929 /* Test that type mismatch is caught when creating a binding
932 test_typesafe_binding (void)
934 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
939 settings = g_settings_new ("org.gtk.test.binding");
940 obj = test_object_new ();
942 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
944 g_object_unref (obj);
945 g_object_unref (settings);
947 g_test_trap_assert_failed ();
948 g_test_trap_assert_stderr ("*not compatible*");
952 string_to_bool (GValue *value,
958 s = g_variant_get_string (variant, NULL);
959 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
965 bool_to_string (const GValue *value,
966 const GVariantType *expected_type,
969 if (g_value_get_boolean (value))
970 return g_variant_new_string ("true");
972 return g_variant_new_string ("false");
975 /* Test custom bindings.
976 * Translate strings to booleans and back
979 test_custom_binding (void)
986 settings = g_settings_new ("org.gtk.test.binding");
987 obj = test_object_new ();
989 g_settings_set_string (settings, "string", "true");
991 g_settings_bind_with_mapping (settings, "string",
993 G_SETTINGS_BIND_DEFAULT,
998 g_settings_set_string (settings, "string", "false");
999 g_object_get (obj, "bool", &b, NULL);
1000 g_assert_cmpint (b, ==, FALSE);
1002 g_settings_set_string (settings, "string", "not true");
1003 g_object_get (obj, "bool", &b, NULL);
1004 g_assert_cmpint (b, ==, FALSE);
1006 g_object_set (obj, "bool", TRUE, NULL);
1007 s = g_settings_get_string (settings, "string");
1008 g_assert_cmpstr (s, ==, "true");
1011 g_object_unref (obj);
1012 g_object_unref (settings);
1015 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1016 * initial settings value is transported to the object
1017 * side, but later settings changes do not affect the
1021 test_no_change_binding (void)
1024 GSettings *settings;
1027 settings = g_settings_new ("org.gtk.test.binding");
1028 obj = test_object_new ();
1030 g_object_set (obj, "bool", TRUE, NULL);
1031 g_settings_set_boolean (settings, "bool", FALSE);
1033 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1035 g_object_get (obj, "bool", &b, NULL);
1036 g_assert_cmpint (b, ==, FALSE);
1038 g_settings_set_boolean (settings, "bool", TRUE);
1039 g_object_get (obj, "bool", &b, NULL);
1040 g_assert_cmpint (b, ==, FALSE);
1042 g_settings_set_boolean (settings, "bool", FALSE);
1043 g_object_set (obj, "bool", TRUE, NULL);
1044 b = g_settings_get_boolean (settings, "bool");
1045 g_assert_cmpint (b, ==, TRUE);
1047 g_object_unref (obj);
1048 g_object_unref (settings);
1051 /* Test that binding a non-readable property only
1052 * works in 'GET' mode.
1055 test_no_read_binding (void)
1057 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1060 GSettings *settings;
1062 settings = g_settings_new ("org.gtk.test.binding");
1063 obj = test_object_new ();
1065 g_settings_bind (settings, "string", obj, "no-read", 0);
1067 g_test_trap_assert_failed ();
1068 g_test_trap_assert_stderr ("*property*is not readable*");
1070 if (g_test_trap_fork (0, 0))
1073 GSettings *settings;
1075 settings = g_settings_new ("org.gtk.test.binding");
1076 obj = test_object_new ();
1078 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1082 g_test_trap_assert_passed ();
1085 /* Test that binding a non-writable property only
1086 * works in 'SET' mode.
1089 test_no_write_binding (void)
1091 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1094 GSettings *settings;
1096 settings = g_settings_new ("org.gtk.test.binding");
1097 obj = test_object_new ();
1099 g_settings_bind (settings, "string", obj, "no-write", 0);
1101 g_test_trap_assert_failed ();
1102 g_test_trap_assert_stderr ("*property*is not writable*");
1104 if (g_test_trap_fork (0, 0))
1107 GSettings *settings;
1109 settings = g_settings_new ("org.gtk.test.binding");
1110 obj = test_object_new ();
1112 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1116 g_test_trap_assert_passed ();
1120 * Test that using a keyfile works
1125 GSettingsBackend *kf_backend;
1126 GSettings *settings;
1130 g_remove ("gsettings.store");
1132 kf_backend = g_keyfile_settings_backend_new ("gsettings.store", "/", "root");
1133 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1134 g_object_unref (kf_backend);
1136 g_settings_set (settings, "greeting", "s", "see if this works");
1138 keyfile = g_key_file_new ();
1139 g_assert (g_key_file_load_from_file (keyfile, "gsettings.store", 0, NULL));
1141 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1142 g_assert_cmpstr (str, ==, "'see if this works'");
1145 g_key_file_free (keyfile);
1146 g_object_unref (settings);
1149 /* Test that getting child schemas works
1152 test_child_schema (void)
1154 GSettings *settings;
1158 /* first establish some known conditions */
1159 settings = g_settings_new ("org.gtk.test.basic-types");
1160 g_settings_set (settings, "test-byte", "y", 36);
1162 g_settings_get (settings, "test-byte", "y", &byte);
1163 g_assert_cmpint (byte, ==, 36);
1165 g_object_unref (settings);
1167 settings = g_settings_new ("org.gtk.test");
1168 child = g_settings_get_child (settings, "basic-types");
1169 g_assert (child != NULL);
1171 g_settings_get (child, "test-byte", "y", &byte);
1172 g_assert_cmpint (byte, ==, 36);
1174 g_object_unref (child);
1175 g_object_unref (settings);
1179 glib_translations_work (void)
1182 gchar *orig = "Unnamed";
1185 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
1186 setlocale (LC_MESSAGES, "de");
1187 str = dgettext ("glib20", orig);
1188 setlocale (LC_MESSAGES, locale);
1194 #include "../strinfo.c"
1199 /* "foo" has a value of 1
1200 * "bar" has a value of 2
1201 * "baz" is an alias for "bar"
1204 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1205 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1207 const guint32 *strinfo = (guint32 *) array;
1208 guint length = sizeof array / 4;
1212 /* build it and compare */
1215 builder = g_string_new (NULL);
1216 strinfo_builder_append_item (builder, "foo", 1);
1217 strinfo_builder_append_item (builder, "bar", 2);
1218 g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
1219 g_assert_cmpint (builder->len % 4, ==, 0);
1220 g_assert_cmpint (builder->len / 4, ==, length);
1221 g_assert (memcmp (builder->str, strinfo, length * 4) == 0);
1222 g_string_free (builder, TRUE);
1225 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
1227 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
1229 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
1231 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
1234 g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
1235 g_assert_cmpint (result, ==, 1);
1236 g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
1237 g_assert_cmpint (result, ==, 2);
1238 g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
1239 g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
1241 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
1242 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
1243 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
1244 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
1246 g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
1247 g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
1248 g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
1249 g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
1257 GSettings *settings, *direct;
1260 settings = g_settings_new ("org.gtk.test.enums");
1261 direct = g_settings_new ("org.gtk.test.enums.direct");
1265 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1266 g_settings_get_enum (direct, "test");
1267 g_test_trap_assert_failed ();
1268 g_test_trap_assert_stderr ("*not associated with an enum*");
1270 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1271 g_settings_set_enum (settings, "test", 42);
1272 g_test_trap_assert_failed ();
1273 g_test_trap_assert_stderr ("*invalid enum value 42*");
1275 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1276 g_settings_set_string (settings, "test", "qux");
1277 g_test_trap_assert_failed ();
1278 g_test_trap_assert_stderr ("*g_settings_range_check*");
1281 str = g_settings_get_string (settings, "test");
1282 g_assert_cmpstr (str, ==, "bar");
1285 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
1287 str = g_settings_get_string (settings, "test");
1288 g_assert_cmpstr (str, ==, "foo");
1291 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
1293 g_settings_set_string (direct, "test", "qux");
1295 str = g_settings_get_string (direct, "test");
1296 g_assert_cmpstr (str, ==, "qux");
1299 str = g_settings_get_string (settings, "test");
1300 g_assert_cmpstr (str, ==, "quux");
1303 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
1309 GSettings *settings, *direct;
1311 settings = g_settings_new ("org.gtk.test.range");
1312 direct = g_settings_new ("org.gtk.test.range.direct");
1316 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1317 g_settings_set_int (settings, "val", 45);
1318 g_test_trap_assert_failed ();
1319 g_test_trap_assert_stderr ("*g_settings_range_check*");
1321 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1322 g_settings_set_int (settings, "val", 1);
1323 g_test_trap_assert_failed ();
1324 g_test_trap_assert_stderr ("*g_settings_range_check*");
1327 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1328 g_settings_set_int (direct, "val", 22);
1329 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
1330 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
1331 g_settings_set_int (direct, "val", 45);
1332 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
1333 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1334 g_settings_set_int (direct, "val", 1);
1335 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
1336 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1340 main (int argc, char *argv[])
1345 setlocale (LC_ALL, "");
1347 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
1349 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
1352 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
1355 g_test_init (&argc, &argv, NULL);
1357 g_remove ("org.gtk.test.enums.xml");
1358 g_assert (g_spawn_command_line_sync ("../../gobject/glib-mkenums "
1359 "--template " SRCDIR "/enums.xml.template "
1360 SRCDIR "/testenum.h",
1361 &enums, NULL, &result, NULL));
1362 g_assert (result == 0);
1363 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
1366 g_remove ("gschemas.compiled");
1367 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=. "
1368 "--schema-file=org.gtk.test.enums.xml "
1369 "--schema-file=" SRCDIR "/org.gtk.test.gschema.xml",
1370 NULL, NULL, &result, NULL));
1371 g_assert (result == 0);
1373 g_test_add_func ("/gsettings/basic", test_basic);
1377 g_test_add_func ("/gsettings/no-schema", test_no_schema);
1378 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
1379 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
1382 g_test_add_func ("/gsettings/basic-types", test_basic_types);
1383 g_test_add_func ("/gsettings/complex-types", test_complex_types);
1384 g_test_add_func ("/gsettings/changes", test_changes);
1386 if (glib_translations_work ())
1388 g_test_add_func ("/gsettings/l10n", test_l10n);
1389 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
1392 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
1393 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
1394 g_test_add_func ("/gsettings/atomic", test_atomic);
1395 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
1396 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
1397 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
1398 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
1402 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
1403 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
1404 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
1407 g_test_add_func ("/gsettings/keyfile", test_keyfile);
1408 g_test_add_func ("/gsettings/child-schema", test_child_schema);
1409 g_test_add_func ("/gsettings/strinfo", test_strinfo);
1410 g_test_add_func ("/gsettings/enums", test_enums);
1411 g_test_add_func ("/gsettings/range", test_range);
1413 result = g_test_run ();