6 #define G_SETTINGS_ENABLE_BACKEND
7 #include <gio/gsettingsbackend.h>
9 /* These tests rely on the schemas in org.gtk.test.gschema.xml
10 * to be compiled and installed in the same directory.
13 /* Just to get warmed up: Read and set a string, and
14 * verify that can read the changed string back
22 settings = g_settings_new ("org.gtk.test");
24 g_settings_get (settings, "greeting", "s", &str);
25 g_assert_cmpstr (str, ==, "Hello, earthlings");
27 g_settings_set (settings, "greeting", "s", "goodbye world");
28 g_settings_get (settings, "greeting", "s", &str);
29 g_assert_cmpstr (str, ==, "goodbye world");
33 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
35 settings = g_settings_new ("org.gtk.test");
36 g_settings_set (settings, "greeting", "i", 555);
39 g_test_trap_assert_failed ();
40 g_test_trap_assert_stderr ("*correct_type*");
42 g_settings_get (settings, "greeting", "s", &str);
43 g_assert_cmpstr (str, ==, "goodbye world");
47 g_settings_set (settings, "greeting", "s", "this is the end");
48 g_object_unref (settings);
51 /* Check that we get an error when getting a key
52 * that is not in the schema
55 test_unknown_key (void)
57 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
62 settings = g_settings_new ("org.gtk.test");
63 value = g_settings_get_value (settings, "no_such_key");
65 g_assert (value == NULL);
67 g_object_unref (settings);
69 g_test_trap_assert_failed ();
70 g_test_trap_assert_stderr ("*does not contain*");
73 /* Check that we get an error when the schema
74 * has not been installed
79 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
83 settings = g_settings_new ("no.such.schema");
85 g_assert (settings == NULL);
88 g_test_trap_assert_failed ();
89 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
92 /* Check that we get an error when passing a type string
93 * that does not match the schema
96 test_wrong_type (void)
98 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
103 settings = g_settings_new ("org.gtk.test");
105 g_settings_get (settings, "greeting", "o", &str);
107 g_assert (str == NULL);
109 g_test_trap_assert_failed ();
110 g_test_trap_assert_stderr ("*CRITICAL*");
112 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
116 settings = g_settings_new ("org.gtk.test");
118 g_settings_set (settings, "greetings", "o", "/a/path");
120 g_test_trap_assert_failed ();
121 g_test_trap_assert_stderr ("*CRITICAL*");
124 /* Check that we can successfully read and set the full
125 * range of all basic types
128 test_basic_types (void)
142 settings = g_settings_new ("org.gtk.test.basic-types");
144 g_settings_get (settings, "test-boolean", "b", &b);
145 g_assert_cmpint (b, ==, 1);
147 g_settings_set (settings, "test-boolean", "b", 0);
148 g_settings_get (settings, "test-boolean", "b", &b);
149 g_assert_cmpint (b, ==, 0);
151 g_settings_get (settings, "test-byte", "y", &byte);
152 g_assert_cmpint (byte, ==, 25);
154 g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
155 g_settings_get (settings, "test-byte", "y", &byte);
156 g_assert_cmpint (byte, ==, G_MAXUINT8);
158 g_settings_get (settings, "test-int16", "n", &i16);
159 g_assert_cmpint (i16, ==, -1234);
161 g_settings_set (settings, "test-int16", "n", G_MININT16);
162 g_settings_get (settings, "test-int16", "n", &i16);
163 g_assert_cmpint (i16, ==, G_MININT16);
165 g_settings_set (settings, "test-int16", "n", G_MAXINT16);
166 g_settings_get (settings, "test-int16", "n", &i16);
167 g_assert_cmpint (i16, ==, G_MAXINT16);
169 g_settings_get (settings, "test-uint16", "q", &u16);
170 g_assert_cmpuint (u16, ==, 1234);
172 g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
173 g_settings_get (settings, "test-uint16", "q", &u16);
174 g_assert_cmpuint (u16, ==, G_MAXUINT16);
176 g_settings_get (settings, "test-int32", "i", &i32);
177 g_assert_cmpint (i32, ==, -123456);
179 g_settings_set (settings, "test-int32", "i", G_MININT32);
180 g_settings_get (settings, "test-int32", "i", &i32);
181 g_assert_cmpint (i32, ==, G_MININT32);
183 g_settings_set (settings, "test-int32", "i", G_MAXINT32);
184 g_settings_get (settings, "test-int32", "i", &i32);
185 g_assert_cmpint (i32, ==, G_MAXINT32);
187 g_settings_get (settings, "test-uint32", "u", &u32);
188 g_assert_cmpuint (u32, ==, 123456);
190 g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
191 g_settings_get (settings, "test-uint32", "u", &u32);
192 g_assert_cmpuint (u32, ==, G_MAXUINT32);
194 g_settings_get (settings, "test-int64", "x", &i64);
195 g_assert_cmpuint (i64, ==, -123456789);
197 g_settings_set (settings, "test-int64", "x", G_MININT64);
198 g_settings_get (settings, "test-int64", "x", &i64);
199 g_assert_cmpuint (i64, ==, G_MININT64);
201 g_settings_set (settings, "test-int64", "x", G_MAXINT64);
202 g_settings_get (settings, "test-int64", "x", &i64);
203 g_assert_cmpuint (i64, ==, G_MAXINT64);
205 g_settings_get (settings, "test-uint64", "t", &u64);
206 g_assert_cmpuint (u64, ==, 123456789);
208 g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
209 g_settings_get (settings, "test-uint64", "t", &u64);
210 g_assert_cmpuint (u64, ==, G_MAXUINT64);
212 g_settings_get (settings, "test-double", "d", &d);
213 g_assert_cmpfloat (d, ==, 123.456);
215 g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
216 g_settings_get (settings, "test-double", "d", &d);
217 g_assert_cmpfloat (d, ==, G_MINDOUBLE);
219 g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
220 g_settings_get (settings, "test-double", "d", &d);
221 g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
223 g_settings_get (settings, "test-string", "s", &str);
224 g_assert_cmpstr (str, ==, "a string, it seems");
228 g_settings_get (settings, "test-objectpath", "o", &str);
229 g_assert_cmpstr (str, ==, "/a/object/path");
230 g_object_unref (settings);
235 /* Check that we can read an set complex types like
236 * tuples, arrays and dictionaries
239 test_complex_types (void)
244 GVariantIter *iter = NULL;
246 settings = g_settings_new ("org.gtk.test.complex-types");
248 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
249 g_assert_cmpstr (s, ==, "one");
250 g_assert_cmpint (i1,==, 2);
251 g_assert_cmpint (i2,==, 3);
255 g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
256 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
257 g_assert_cmpstr (s, ==, "none");
258 g_assert_cmpint (i1,==, 0);
259 g_assert_cmpint (i2,==, 0);
263 g_settings_get (settings, "test-array", "ai", &iter);
264 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
265 g_assert (g_variant_iter_next (iter, "i", &i1));
266 g_assert_cmpint (i1, ==, 0);
267 g_assert (g_variant_iter_next (iter, "i", &i1));
268 g_assert_cmpint (i1, ==, 1);
269 g_assert (g_variant_iter_next (iter, "i", &i1));
270 g_assert_cmpint (i1, ==, 2);
271 g_assert (g_variant_iter_next (iter, "i", &i1));
272 g_assert_cmpint (i1, ==, 3);
273 g_assert (g_variant_iter_next (iter, "i", &i1));
274 g_assert_cmpint (i1, ==, 4);
275 g_assert (g_variant_iter_next (iter, "i", &i1));
276 g_assert_cmpint (i1, ==, 5);
277 g_assert (!g_variant_iter_next (iter, "i", &i1));
278 g_variant_iter_free (iter);
280 g_object_unref (settings);
283 static gboolean changed_cb_called;
286 changed_cb (GSettings *settings,
290 changed_cb_called = TRUE;
292 g_assert_cmpstr (key, ==, data);
295 /* Test that basic change notification with the changed signal works.
301 GSettings *settings2;
303 settings = g_settings_new ("org.gtk.test");
305 g_signal_connect (settings, "changed",
306 G_CALLBACK (changed_cb), "greeting");
308 changed_cb_called = FALSE;
310 g_settings_set (settings, "greeting", "s", "new greeting");
311 g_assert (changed_cb_called);
313 settings2 = g_settings_new ("org.gtk.test");
315 changed_cb_called = FALSE;
317 g_settings_set (settings2, "greeting", "s", "hi");
318 g_assert (changed_cb_called);
320 g_object_unref (settings2);
321 g_object_unref (settings);
324 static gboolean changed_cb_called2;
327 changed_cb2 (GSettings *settings,
336 /* Test that changes done to a delay-mode instance
337 * don't appear to the outside world until apply. Also
338 * check that we get change notification when they are
340 * Also test that the has-unapplied property is properly
344 test_delay_apply (void)
347 GSettings *settings2;
350 settings = g_settings_new ("org.gtk.test");
351 settings2 = g_settings_new ("org.gtk.test");
353 g_settings_set (settings2, "greeting", "s", "top o' the morning");
355 changed_cb_called = FALSE;
356 changed_cb_called2 = FALSE;
358 g_signal_connect (settings, "changed",
359 G_CALLBACK (changed_cb2), &changed_cb_called);
360 g_signal_connect (settings2, "changed",
361 G_CALLBACK (changed_cb2), &changed_cb_called2);
363 g_settings_delay (settings);
365 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
367 g_assert (changed_cb_called);
368 g_assert (!changed_cb_called2);
370 g_settings_get (settings, "greeting", "s", &str);
371 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
375 g_settings_get (settings2, "greeting", "s", &str);
376 g_assert_cmpstr (str, ==, "top o' the morning");
380 g_assert (g_settings_get_has_unapplied (settings));
381 g_assert (!g_settings_get_has_unapplied (settings2));
383 changed_cb_called = FALSE;
384 changed_cb_called2 = FALSE;
386 g_settings_apply (settings);
388 g_assert (!changed_cb_called);
389 g_assert (changed_cb_called2);
391 g_settings_get (settings, "greeting", "s", &str);
392 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
396 g_settings_get (settings2, "greeting", "s", &str);
397 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
401 g_assert (!g_settings_get_has_unapplied (settings));
402 g_assert (!g_settings_get_has_unapplied (settings2));
404 g_object_unref (settings2);
405 g_object_unref (settings);
408 /* Test that reverting unapplied changes in a delay-apply
409 * settings instance works.
412 test_delay_revert (void)
415 GSettings *settings2;
418 settings = g_settings_new ("org.gtk.test");
419 settings2 = g_settings_new ("org.gtk.test");
421 g_settings_set (settings2, "greeting", "s", "top o' the morning");
423 g_settings_delay (settings);
425 g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
427 g_settings_get (settings, "greeting", "s", &str);
428 g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
432 g_settings_get (settings2, "greeting", "s", &str);
433 g_assert_cmpstr (str, ==, "top o' the morning");
437 g_assert (g_settings_get_has_unapplied (settings));
439 g_settings_revert (settings);
441 g_assert (!g_settings_get_has_unapplied (settings));
443 g_settings_get (settings, "greeting", "s", &str);
444 g_assert_cmpstr (str, ==, "top o' the morning");
448 g_settings_get (settings2, "greeting", "s", &str);
449 g_assert_cmpstr (str, ==, "top o' the morning");
453 g_object_unref (settings2);
454 g_object_unref (settings);
458 keys_changed_cb (GSettings *settings,
464 g_assert_cmpint (n_keys, ==, 2);
466 g_assert ((keys[0] == g_quark_from_static_string ("greeting") &&
467 keys[1] == g_quark_from_static_string ("farewell")) ||
468 (keys[1] == g_quark_from_static_string ("greeting") &&
469 keys[0] == g_quark_from_static_string ("farewell")));
471 g_settings_get (settings, "greeting", "s", &str);
472 g_assert_cmpstr (str, ==, "greetings from test_atomic");
476 g_settings_get (settings, "farewell", "s", &str);
477 g_assert_cmpstr (str, ==, "atomic bye-bye");
482 /* Check that delay-applied changes appear atomically.
483 * More specifically, verify that all changed keys appear
484 * with their new value while handling the change-event signal.
490 GSettings *settings2;
493 settings = g_settings_new ("org.gtk.test");
494 settings2 = g_settings_new ("org.gtk.test");
496 g_settings_set (settings2, "greeting", "s", "top o' the morning");
498 changed_cb_called = FALSE;
499 changed_cb_called2 = FALSE;
501 g_signal_connect (settings2, "change-event",
502 G_CALLBACK (keys_changed_cb), NULL);
504 g_settings_delay (settings);
506 g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
507 g_settings_set (settings, "farewell", "s", "atomic bye-bye");
509 g_settings_apply (settings);
511 g_settings_get (settings, "greeting", "s", &str);
512 g_assert_cmpstr (str, ==, "greetings from test_atomic");
516 g_settings_get (settings, "farewell", "s", &str);
517 g_assert_cmpstr (str, ==, "atomic bye-bye");
521 g_settings_get (settings2, "greeting", "s", &str);
522 g_assert_cmpstr (str, ==, "greetings from test_atomic");
526 g_settings_get (settings2, "farewell", "s", &str);
527 g_assert_cmpstr (str, ==, "atomic bye-bye");
531 g_object_unref (settings2);
532 g_object_unref (settings);
537 /* On Windows the interaction between the C library locale and libintl
538 * (from GNU gettext) is not like on POSIX, so just skip these tests
541 * There are several issues:
543 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
544 * in libintl (defined in its <libintl.h>).
546 * 2) The locale names that setlocale() accepts and returns aren't in
547 * the "de_DE" style, but like "German_Germany".
549 * 3) libintl looks at the Win32 thread locale and not the C library
550 * locale. (And even if libintl would use the C library's locale, as
551 * there are several alternative C library DLLs, libintl might be
552 * linked to a different one than the application code, so they
553 * wouldn't have the same C library locale anyway.)
556 /* Test that translations work for schema defaults.
558 * This test relies on the de.po file in the same directory
559 * to be compiled into ./de/LC_MESSAGES/test.mo
568 bindtextdomain ("test", ".");
569 bind_textdomain_codeset ("test", "UTF-8");
571 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
573 settings = g_settings_new ("org.gtk.test.localized");
575 setlocale (LC_MESSAGES, "C");
576 str = g_settings_get_string (settings, "error-message");
577 setlocale (LC_MESSAGES, locale);
579 g_assert_cmpstr (str, ==, "Unnamed");
583 setlocale (LC_MESSAGES, "de_DE");
584 str = g_settings_get_string (settings, "error-message");
585 setlocale (LC_MESSAGES, locale);
587 g_assert_cmpstr (str, ==, "Unbenannt");
588 g_object_unref (settings);
595 /* Test that message context works as expected with translated
596 * schema defaults. Also, verify that non-ASCII UTF-8 content
599 * This test relies on the de.po file in the same directory
600 * to be compiled into ./de/LC_MESSAGES/test.mo
603 test_l10n_context (void)
609 bindtextdomain ("test", ".");
610 bind_textdomain_codeset ("test", "UTF-8");
612 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
614 settings = g_settings_new ("org.gtk.test.localized");
616 setlocale (LC_MESSAGES, "C");
617 g_settings_get (settings, "backspace", "s", &str);
618 setlocale (LC_MESSAGES, locale);
620 g_assert_cmpstr (str, ==, "BackSpace");
624 setlocale (LC_MESSAGES, "de_DE");
625 g_settings_get (settings, "backspace", "s", &str);
626 setlocale (LC_MESSAGES, locale);
628 g_assert_cmpstr (str, ==, "Löschen");
629 g_object_unref (settings);
651 GObject parent_instance;
663 GObjectClass parent_class;
666 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
669 test_object_init (TestObject *object)
674 test_object_finalize (GObject *object)
676 TestObject *testo = (TestObject*)object;
677 g_free (testo->string_prop);
678 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
682 test_object_get_property (GObject *object,
687 TestObject *test_object = (TestObject *)object;
692 g_value_set_boolean (value, test_object->bool_prop);
695 g_value_set_int (value, test_object->int_prop);
698 g_value_set_int64 (value, test_object->int64_prop);
701 g_value_set_uint64 (value, test_object->uint64_prop);
704 g_value_set_double (value, test_object->double_prop);
707 g_value_set_string (value, test_object->string_prop);
710 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
716 test_object_set_property (GObject *object,
721 TestObject *test_object = (TestObject *)object;
726 test_object->bool_prop = g_value_get_boolean (value);
729 test_object->int_prop = g_value_get_int (value);
732 test_object->int64_prop = g_value_get_int64 (value);
735 test_object->uint64_prop = g_value_get_uint64 (value);
738 test_object->double_prop = g_value_get_double (value);
741 g_free (test_object->string_prop);
742 test_object->string_prop = g_value_dup_string (value);
745 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
751 test_object_class_init (TestObjectClass *class)
753 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
755 gobject_class->get_property = test_object_get_property;
756 gobject_class->set_property = test_object_set_property;
757 gobject_class->finalize = test_object_finalize;
759 g_object_class_install_property (gobject_class, PROP_BOOL,
760 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
761 g_object_class_install_property (gobject_class, PROP_INT,
762 g_param_spec_int ("int", "", "", -G_MAXINT, G_MAXINT, 0, G_PARAM_READWRITE));
763 g_object_class_install_property (gobject_class, PROP_INT64,
764 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
765 g_object_class_install_property (gobject_class, PROP_UINT64,
766 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
767 g_object_class_install_property (gobject_class, PROP_DOUBLE,
768 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
769 g_object_class_install_property (gobject_class, PROP_STRING,
770 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
774 test_object_new (void)
776 return (TestObject*)g_object_new (test_object_get_type (), NULL);
779 /* Test basic binding functionality for simple types.
780 * Verify that with bidirectional bindings, changes on either side
781 * are notified on the other end.
784 test_simple_binding (void)
795 settings = g_settings_new ("org.gtk.test.binding");
796 obj = test_object_new ();
798 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
800 g_object_set (obj, "bool", TRUE, NULL);
801 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
803 g_settings_set_boolean (settings, "bool", FALSE);
804 g_object_get (obj, "bool", &b, NULL);
805 g_assert_cmpint (b, ==, FALSE);
807 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
809 g_object_set (obj, "int", 12345, NULL);
810 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
812 g_settings_set_int (settings, "int", 54321);
813 g_object_get (obj, "int", &i, NULL);
814 g_assert_cmpint (i, ==, 54321);
816 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
818 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
819 g_settings_get (settings, "int64", "x", &i64);
820 g_assert_cmpint (i64, ==, G_MAXINT64);
822 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
823 g_object_get (obj, "int64", &i64, NULL);
824 g_assert_cmpint (i64, ==, G_MININT64);
826 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
828 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
829 g_settings_get (settings, "uint64", "t", &u64);
830 g_assert_cmpuint (u64, ==, G_MAXUINT64);
832 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
833 g_object_get (obj, "uint64", &u64, NULL);
834 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
836 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
838 g_object_set (obj, "string", "bu ba", NULL);
839 s = g_settings_get_string (settings, "string");
840 g_assert_cmpstr (s, ==, "bu ba");
843 g_settings_set_string (settings, "string", "bla bla");
844 g_object_get (obj, "string", &s, NULL);
845 g_assert_cmpstr (s, ==, "bla bla");
848 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
850 g_object_set (obj, "double", G_MAXFLOAT, NULL);
851 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
853 g_settings_set_double (settings, "double", G_MINFLOAT);
854 g_object_get (obj, "double", &d, NULL);
855 g_assert_cmpfloat (d, ==, G_MINFLOAT);
857 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
858 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
860 g_settings_set_double (settings, "double", -G_MINDOUBLE);
861 g_object_get (obj, "double", &d, NULL);
862 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
863 g_object_unref (obj);
864 g_object_unref (settings);
867 /* Test one-way bindings.
868 * Verify that changes on one side show up on the other,
872 test_directional_binding (void)
879 settings = g_settings_new ("org.gtk.test.binding");
880 obj = test_object_new ();
882 g_object_set (obj, "bool", FALSE, NULL);
883 g_settings_set_boolean (settings, "bool", FALSE);
885 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
887 g_settings_set_boolean (settings, "bool", TRUE);
888 g_object_get (obj, "bool", &b, NULL);
889 g_assert_cmpint (b, ==, TRUE);
891 g_object_set (obj, "bool", FALSE, NULL);
892 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
894 g_object_set (obj, "int", 20, NULL);
895 g_settings_set_int (settings, "int", 20);
897 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
899 g_object_set (obj, "int", 32, NULL);
900 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
902 g_settings_set_int (settings, "int", 20);
903 g_object_get (obj, "int", &i, NULL);
904 g_assert_cmpint (i, ==, 32);
906 g_object_unref (obj);
907 g_object_unref (settings);
910 /* Test that type mismatch is caught when creating a binding
913 test_typesafe_binding (void)
915 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
920 settings = g_settings_new ("org.gtk.test.binding");
921 obj = test_object_new ();
923 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
925 g_object_unref (obj);
926 g_object_unref (settings);
928 g_test_trap_assert_failed ();
929 g_test_trap_assert_stderr ("*not compatible*");
933 string_to_bool (GValue *value,
939 s = g_variant_get_string (variant, NULL);
940 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
946 bool_to_string (const GValue *value,
947 const GVariantType *expected_type,
950 if (g_value_get_boolean (value))
951 return g_variant_new_string ("true");
953 return g_variant_new_string ("false");
956 /* Test custom bindings.
957 * Translate strings to booleans and back
960 test_custom_binding (void)
967 settings = g_settings_new ("org.gtk.test.binding");
968 obj = test_object_new ();
970 g_settings_set_string (settings, "string", "true");
972 g_settings_bind_with_mapping (settings, "string",
974 G_SETTINGS_BIND_DEFAULT,
979 g_settings_set_string (settings, "string", "false");
980 g_object_get (obj, "bool", &b, NULL);
981 g_assert_cmpint (b, ==, FALSE);
983 g_settings_set_string (settings, "string", "not true");
984 g_object_get (obj, "bool", &b, NULL);
985 g_assert_cmpint (b, ==, FALSE);
987 g_object_set (obj, "bool", TRUE, NULL);
988 s = g_settings_get_string (settings, "string");
989 g_assert_cmpstr (s, ==, "true");
991 g_object_unref (obj);
992 g_object_unref (settings);
995 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
996 * initial settings value is transported to the object
997 * side, but later settings changes do not affect the
1001 test_no_change_binding (void)
1004 GSettings *settings;
1007 settings = g_settings_new ("org.gtk.test.binding");
1008 obj = test_object_new ();
1010 g_object_set (obj, "bool", TRUE, NULL);
1011 g_settings_set_boolean (settings, "bool", FALSE);
1013 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1015 g_object_get (obj, "bool", &b, NULL);
1016 g_assert_cmpint (b, ==, FALSE);
1018 g_settings_set_boolean (settings, "bool", TRUE);
1019 g_object_get (obj, "bool", &b, NULL);
1020 g_assert_cmpint (b, ==, FALSE);
1022 g_settings_set_boolean (settings, "bool", FALSE);
1023 g_object_set (obj, "bool", TRUE, NULL);
1024 b = g_settings_get_boolean (settings, "bool");
1025 g_assert_cmpint (b, ==, TRUE);
1027 g_object_unref (obj);
1028 g_object_unref (settings);
1032 * Test that using a keyfile works
1037 GSettings *settings;
1041 g_remove ("gsettings.store");
1043 g_settings_backend_setup_keyfile ("blah", "gsettings.store");
1045 settings = g_settings_new_with_context ("org.gtk.test", "blah");
1047 g_settings_set (settings, "greeting", "s", "see if this works");
1049 keyfile = g_key_file_new ();
1050 g_assert (g_key_file_load_from_file (keyfile, "gsettings.store", 0, NULL));
1052 str = g_key_file_get_string (keyfile, "/tests/", "greeting", NULL);
1053 g_assert_cmpstr (str, ==, "'see if this works'");
1056 g_key_file_free (keyfile);
1057 g_object_unref (settings);
1061 main (int argc, char *argv[])
1063 setlocale (LC_ALL, "");
1065 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
1066 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
1069 g_test_init (&argc, &argv, NULL);
1071 g_remove ("gschemas.compiled");
1072 g_assert (g_spawn_command_line_sync ("../gschema-compile --targetdir=. " SRCDIR,
1073 NULL, NULL, NULL, NULL));
1075 g_test_add_func ("/gsettings/basic", test_basic);
1076 g_test_add_func ("/gsettings/no-schema", test_no_schema);
1077 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
1078 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
1079 g_test_add_func ("/gsettings/basic-types", test_basic_types);
1080 g_test_add_func ("/gsettings/complex-types", test_complex_types);
1081 g_test_add_func ("/gsettings/changes", test_changes);
1083 g_test_add_func ("/gsettings/l10n", test_l10n);
1084 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
1086 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
1087 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
1088 g_test_add_func ("/gsettings/atomic", test_atomic);
1089 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
1090 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
1091 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
1092 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
1093 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
1094 g_test_add_func ("/gsettings/keyfile", test_keyfile);
1096 return g_test_run ();