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.
18 check_and_free (GVariant *value,
19 const gchar *expected)
23 printed = g_variant_print (value, TRUE);
24 g_assert_cmpstr (printed, ==, expected);
27 g_variant_unref (value);
31 /* Just to get warmed up: Read and set a string, and
32 * verify that can read the changed string back
40 settings = g_settings_new ("org.gtk.test");
42 g_object_get (settings, "schema", &str, NULL);
43 g_assert_cmpstr (str, ==, "org.gtk.test");
46 g_settings_get (settings, "greeting", "s", &str);
47 g_assert_cmpstr (str, ==, "Hello, earthlings");
50 g_settings_set (settings, "greeting", "s", "goodbye world");
51 g_settings_get (settings, "greeting", "s", &str);
52 g_assert_cmpstr (str, ==, "goodbye world");
58 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
60 settings = g_settings_new ("org.gtk.test");
61 g_settings_set (settings, "greeting", "i", 555);
64 g_test_trap_assert_failed ();
65 g_test_trap_assert_stderr ("*g_settings_set_value*expects type*");
68 g_settings_get (settings, "greeting", "s", &str);
69 g_assert_cmpstr (str, ==, "goodbye world");
73 g_settings_reset (settings, "greeting");
74 str = g_settings_get_string (settings, "greeting");
75 g_assert_cmpstr (str, ==, "Hello, earthlings");
78 g_settings_set (settings, "greeting", "s", "this is the end");
79 g_object_unref (settings);
82 /* Check that we get an error when getting a key
83 * that is not in the schema
86 test_unknown_key (void)
88 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
93 settings = g_settings_new ("org.gtk.test");
94 value = g_settings_get_value (settings, "no_such_key");
96 g_assert (value == NULL);
98 g_object_unref (settings);
100 g_test_trap_assert_failed ();
101 g_test_trap_assert_stderr ("*does not contain*");
104 /* Check that we get an error when the schema
105 * has not been installed
108 test_no_schema (void)
110 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
114 settings = g_settings_new ("no.such.schema");
116 g_assert (settings == NULL);
119 g_test_trap_assert_failed ();
120 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
123 /* Check that we get an error when passing a type string
124 * that does not match the schema
127 test_wrong_type (void)
129 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
134 settings = g_settings_new ("org.gtk.test");
136 g_settings_get (settings, "greeting", "o", &str);
138 g_assert (str == NULL);
140 g_test_trap_assert_failed ();
141 g_test_trap_assert_stderr ("*CRITICAL*");
143 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
147 settings = g_settings_new ("org.gtk.test");
149 g_settings_set (settings, "greeting", "o", "/a/path");
151 g_test_trap_assert_failed ();
152 g_test_trap_assert_stderr ("*CRITICAL*");
155 /* Check errors with explicit paths */
157 test_wrong_path (void)
159 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
161 GSettings *settings G_GNUC_UNUSED;
163 settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
166 g_test_trap_assert_failed ();
167 g_test_trap_assert_stderr ("*but path * specified by schema*");
173 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
175 GSettings *settings G_GNUC_UNUSED;
177 settings = g_settings_new ("org.gtk.test.no-path");
180 g_test_trap_assert_failed ();
181 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
185 /* Check that we can successfully read and set the full
186 * range of all basic types
189 test_basic_types (void)
203 settings = g_settings_new ("org.gtk.test.basic-types");
205 g_settings_get (settings, "test-boolean", "b", &b);
206 g_assert_cmpint (b, ==, 1);
208 g_settings_set (settings, "test-boolean", "b", 0);
209 g_settings_get (settings, "test-boolean", "b", &b);
210 g_assert_cmpint (b, ==, 0);
212 g_settings_get (settings, "test-byte", "y", &byte);
213 g_assert_cmpint (byte, ==, 25);
215 g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
216 g_settings_get (settings, "test-byte", "y", &byte);
217 g_assert_cmpint (byte, ==, G_MAXUINT8);
219 g_settings_get (settings, "test-int16", "n", &i16);
220 g_assert_cmpint (i16, ==, -1234);
222 g_settings_set (settings, "test-int16", "n", G_MININT16);
223 g_settings_get (settings, "test-int16", "n", &i16);
224 g_assert_cmpint (i16, ==, G_MININT16);
226 g_settings_set (settings, "test-int16", "n", G_MAXINT16);
227 g_settings_get (settings, "test-int16", "n", &i16);
228 g_assert_cmpint (i16, ==, G_MAXINT16);
230 g_settings_get (settings, "test-uint16", "q", &u16);
231 g_assert_cmpuint (u16, ==, 1234);
233 g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
234 g_settings_get (settings, "test-uint16", "q", &u16);
235 g_assert_cmpuint (u16, ==, G_MAXUINT16);
237 g_settings_get (settings, "test-int32", "i", &i32);
238 g_assert_cmpint (i32, ==, -123456);
240 g_settings_set (settings, "test-int32", "i", G_MININT32);
241 g_settings_get (settings, "test-int32", "i", &i32);
242 g_assert_cmpint (i32, ==, G_MININT32);
244 g_settings_set (settings, "test-int32", "i", G_MAXINT32);
245 g_settings_get (settings, "test-int32", "i", &i32);
246 g_assert_cmpint (i32, ==, G_MAXINT32);
248 g_settings_get (settings, "test-uint32", "u", &u32);
249 g_assert_cmpuint (u32, ==, 123456);
251 g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
252 g_settings_get (settings, "test-uint32", "u", &u32);
253 g_assert_cmpuint (u32, ==, G_MAXUINT32);
255 g_settings_get (settings, "test-int64", "x", &i64);
256 g_assert_cmpuint (i64, ==, -123456789);
258 g_settings_set (settings, "test-int64", "x", G_MININT64);
259 g_settings_get (settings, "test-int64", "x", &i64);
260 g_assert_cmpuint (i64, ==, G_MININT64);
262 g_settings_set (settings, "test-int64", "x", G_MAXINT64);
263 g_settings_get (settings, "test-int64", "x", &i64);
264 g_assert_cmpuint (i64, ==, G_MAXINT64);
266 g_settings_get (settings, "test-uint64", "t", &u64);
267 g_assert_cmpuint (u64, ==, 123456789);
269 g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
270 g_settings_get (settings, "test-uint64", "t", &u64);
271 g_assert_cmpuint (u64, ==, G_MAXUINT64);
273 g_settings_get (settings, "test-double", "d", &d);
274 g_assert_cmpfloat (d, ==, 123.456);
276 g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
277 g_settings_get (settings, "test-double", "d", &d);
278 g_assert_cmpfloat (d, ==, G_MINDOUBLE);
280 g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
281 g_settings_get (settings, "test-double", "d", &d);
282 g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
284 g_settings_get (settings, "test-string", "s", &str);
285 g_assert_cmpstr (str, ==, "a string, it seems");
289 g_settings_get (settings, "test-objectpath", "o", &str);
290 g_assert_cmpstr (str, ==, "/a/object/path");
291 g_object_unref (settings);
296 /* Check that we can read an set complex types like
297 * tuples, arrays and dictionaries
300 test_complex_types (void)
305 GVariantIter *iter = NULL;
307 settings = g_settings_new ("org.gtk.test.complex-types");
309 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
310 g_assert_cmpstr (s, ==, "one");
311 g_assert_cmpint (i1,==, 2);
312 g_assert_cmpint (i2,==, 3);
316 g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
317 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
318 g_assert_cmpstr (s, ==, "none");
319 g_assert_cmpint (i1,==, 0);
320 g_assert_cmpint (i2,==, 0);
324 g_settings_get (settings, "test-array", "ai", &iter);
325 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
326 g_assert (g_variant_iter_next (iter, "i", &i1));
327 g_assert_cmpint (i1, ==, 0);
328 g_assert (g_variant_iter_next (iter, "i", &i1));
329 g_assert_cmpint (i1, ==, 1);
330 g_assert (g_variant_iter_next (iter, "i", &i1));
331 g_assert_cmpint (i1, ==, 2);
332 g_assert (g_variant_iter_next (iter, "i", &i1));
333 g_assert_cmpint (i1, ==, 3);
334 g_assert (g_variant_iter_next (iter, "i", &i1));
335 g_assert_cmpint (i1, ==, 4);
336 g_assert (g_variant_iter_next (iter, "i", &i1));
337 g_assert_cmpint (i1, ==, 5);
338 g_assert (!g_variant_iter_next (iter, "i", &i1));
339 g_variant_iter_free (iter);
341 g_object_unref (settings);
344 static gboolean changed_cb_called;
347 changed_cb (GSettings *settings,
351 changed_cb_called = TRUE;
353 g_assert_cmpstr (key, ==, data);
356 /* Test that basic change notification with the changed signal works.
362 GSettings *settings2;
364 settings = g_settings_new ("org.gtk.test");
366 g_signal_connect (settings, "changed",
367 G_CALLBACK (changed_cb), "greeting");
369 changed_cb_called = FALSE;
371 g_settings_set (settings, "greeting", "s", "new greeting");
372 g_assert (changed_cb_called);
374 settings2 = g_settings_new ("org.gtk.test");
376 changed_cb_called = FALSE;
378 g_settings_set (settings2, "greeting", "s", "hi");
379 g_assert (changed_cb_called);
381 g_object_unref (settings2);
382 g_object_unref (settings);
385 static gboolean changed_cb_called2;
388 changed_cb2 (GSettings *settings,
397 /* Test that changes done to a delay-mode instance
398 * don't appear to the outside world until apply. Also
399 * check that we get change notification when they are
401 * Also test that the has-unapplied property is properly
405 test_delay_apply (void)
408 GSettings *settings2;
411 settings = g_settings_new ("org.gtk.test");
412 settings2 = g_settings_new ("org.gtk.test");
414 g_settings_set (settings2, "greeting", "s", "top o' the morning");
416 changed_cb_called = FALSE;
417 changed_cb_called2 = FALSE;
419 g_signal_connect (settings, "changed",
420 G_CALLBACK (changed_cb2), &changed_cb_called);
421 g_signal_connect (settings2, "changed",
422 G_CALLBACK (changed_cb2), &changed_cb_called2);
424 g_settings_delay (settings);
426 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
428 g_assert (changed_cb_called);
429 g_assert (!changed_cb_called2);
431 g_settings_get (settings, "greeting", "s", &str);
432 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
436 g_settings_get (settings2, "greeting", "s", &str);
437 g_assert_cmpstr (str, ==, "top o' the morning");
441 g_assert (g_settings_get_has_unapplied (settings));
442 g_assert (!g_settings_get_has_unapplied (settings2));
444 changed_cb_called = FALSE;
445 changed_cb_called2 = FALSE;
447 g_settings_apply (settings);
449 g_assert (!changed_cb_called);
450 g_assert (changed_cb_called2);
452 g_settings_get (settings, "greeting", "s", &str);
453 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
457 g_settings_get (settings2, "greeting", "s", &str);
458 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
462 g_assert (!g_settings_get_has_unapplied (settings));
463 g_assert (!g_settings_get_has_unapplied (settings2));
465 g_object_unref (settings2);
466 g_object_unref (settings);
469 /* Test that reverting unapplied changes in a delay-apply
470 * settings instance works.
473 test_delay_revert (void)
476 GSettings *settings2;
479 settings = g_settings_new ("org.gtk.test");
480 settings2 = g_settings_new ("org.gtk.test");
482 g_settings_set (settings2, "greeting", "s", "top o' the morning");
484 g_settings_delay (settings);
486 g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
488 g_settings_get (settings, "greeting", "s", &str);
489 g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
493 g_settings_get (settings2, "greeting", "s", &str);
494 g_assert_cmpstr (str, ==, "top o' the morning");
498 g_assert (g_settings_get_has_unapplied (settings));
500 g_settings_revert (settings);
502 g_assert (!g_settings_get_has_unapplied (settings));
504 g_settings_get (settings, "greeting", "s", &str);
505 g_assert_cmpstr (str, ==, "top o' the morning");
509 g_settings_get (settings2, "greeting", "s", &str);
510 g_assert_cmpstr (str, ==, "top o' the morning");
514 g_object_unref (settings2);
515 g_object_unref (settings);
519 keys_changed_cb (GSettings *settings,
525 g_assert_cmpint (n_keys, ==, 2);
527 g_assert ((keys[0] == g_quark_from_static_string ("greeting") &&
528 keys[1] == g_quark_from_static_string ("farewell")) ||
529 (keys[1] == g_quark_from_static_string ("greeting") &&
530 keys[0] == g_quark_from_static_string ("farewell")));
532 g_settings_get (settings, "greeting", "s", &str);
533 g_assert_cmpstr (str, ==, "greetings from test_atomic");
537 g_settings_get (settings, "farewell", "s", &str);
538 g_assert_cmpstr (str, ==, "atomic bye-bye");
543 /* Check that delay-applied changes appear atomically.
544 * More specifically, verify that all changed keys appear
545 * with their new value while handling the change-event signal.
551 GSettings *settings2;
554 settings = g_settings_new ("org.gtk.test");
555 settings2 = g_settings_new ("org.gtk.test");
557 g_settings_set (settings2, "greeting", "s", "top o' the morning");
559 changed_cb_called = FALSE;
560 changed_cb_called2 = FALSE;
562 g_signal_connect (settings2, "change-event",
563 G_CALLBACK (keys_changed_cb), NULL);
565 g_settings_delay (settings);
567 g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
568 g_settings_set (settings, "farewell", "s", "atomic bye-bye");
570 g_settings_apply (settings);
572 g_settings_get (settings, "greeting", "s", &str);
573 g_assert_cmpstr (str, ==, "greetings from test_atomic");
577 g_settings_get (settings, "farewell", "s", &str);
578 g_assert_cmpstr (str, ==, "atomic bye-bye");
582 g_settings_get (settings2, "greeting", "s", &str);
583 g_assert_cmpstr (str, ==, "greetings from test_atomic");
587 g_settings_get (settings2, "farewell", "s", &str);
588 g_assert_cmpstr (str, ==, "atomic bye-bye");
592 g_object_unref (settings2);
593 g_object_unref (settings);
596 /* On Windows the interaction between the C library locale and libintl
597 * (from GNU gettext) is not like on POSIX, so just skip these tests
600 * There are several issues:
602 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
603 * in libintl (defined in its <libintl.h>).
605 * 2) The locale names that setlocale() accepts and returns aren't in
606 * the "de_DE" style, but like "German_Germany".
608 * 3) libintl looks at the Win32 thread locale and not the C library
609 * locale. (And even if libintl would use the C library's locale, as
610 * there are several alternative C library DLLs, libintl might be
611 * linked to a different one than the application code, so they
612 * wouldn't have the same C library locale anyway.)
615 /* Test that translations work for schema defaults.
617 * This test relies on the de.po file in the same directory
618 * to be compiled into ./de/LC_MESSAGES/test.mo
627 bindtextdomain ("test", ".");
628 bind_textdomain_codeset ("test", "UTF-8");
630 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
632 settings = g_settings_new ("org.gtk.test.localized");
634 setlocale (LC_MESSAGES, "C");
635 str = g_settings_get_string (settings, "error-message");
636 setlocale (LC_MESSAGES, locale);
638 g_assert_cmpstr (str, ==, "Unnamed");
642 setlocale (LC_MESSAGES, "de_DE");
643 str = g_settings_get_string (settings, "error-message");
644 setlocale (LC_MESSAGES, locale);
646 g_assert_cmpstr (str, ==, "Unbenannt");
647 g_object_unref (settings);
654 /* Test that message context works as expected with translated
655 * schema defaults. Also, verify that non-ASCII UTF-8 content
658 * This test relies on the de.po file in the same directory
659 * to be compiled into ./de/LC_MESSAGES/test.mo
662 test_l10n_context (void)
668 bindtextdomain ("test", ".");
669 bind_textdomain_codeset ("test", "UTF-8");
671 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
673 settings = g_settings_new ("org.gtk.test.localized");
675 setlocale (LC_MESSAGES, "C");
676 g_settings_get (settings, "backspace", "s", &str);
677 setlocale (LC_MESSAGES, locale);
679 g_assert_cmpstr (str, ==, "BackSpace");
683 setlocale (LC_MESSAGES, "de_DE");
684 g_settings_get (settings, "backspace", "s", &str);
685 setlocale (LC_MESSAGES, locale);
687 g_assert_cmpstr (str, ==, "Löschen");
688 g_object_unref (settings);
717 GObject parent_instance;
720 gboolean anti_bool_prop;
731 gchar *no_write_prop;
738 GObjectClass parent_class;
741 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
744 test_object_init (TestObject *object)
749 test_object_finalize (GObject *object)
751 TestObject *testo = (TestObject*)object;
752 g_strfreev (testo->strv_prop);
753 g_free (testo->string_prop);
754 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
758 test_object_get_property (GObject *object,
763 TestObject *test_object = (TestObject *)object;
768 g_value_set_boolean (value, test_object->bool_prop);
771 g_value_set_boolean (value, test_object->anti_bool_prop);
774 g_value_set_char (value, test_object->byte_prop);
777 g_value_set_uint (value, test_object->uint16_prop);
780 g_value_set_int (value, test_object->int16_prop);
783 g_value_set_int (value, test_object->int_prop);
786 g_value_set_uint (value, test_object->uint_prop);
789 g_value_set_int64 (value, test_object->int64_prop);
792 g_value_set_uint64 (value, test_object->uint64_prop);
795 g_value_set_double (value, test_object->double_prop);
798 g_value_set_string (value, test_object->string_prop);
801 g_value_set_string (value, test_object->no_write_prop);
804 g_value_set_boxed (value, test_object->strv_prop);
807 g_value_set_enum (value, test_object->enum_prop);
810 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
816 test_object_set_property (GObject *object,
821 TestObject *test_object = (TestObject *)object;
826 test_object->bool_prop = g_value_get_boolean (value);
829 test_object->anti_bool_prop = g_value_get_boolean (value);
832 test_object->byte_prop = g_value_get_char (value);
835 test_object->int16_prop = g_value_get_int (value);
838 test_object->uint16_prop = g_value_get_uint (value);
841 test_object->int_prop = g_value_get_int (value);
844 test_object->uint_prop = g_value_get_uint (value);
847 test_object->int64_prop = g_value_get_int64 (value);
850 test_object->uint64_prop = g_value_get_uint64 (value);
853 test_object->double_prop = g_value_get_double (value);
856 g_free (test_object->string_prop);
857 test_object->string_prop = g_value_dup_string (value);
860 g_free (test_object->no_read_prop);
861 test_object->no_read_prop = g_value_dup_string (value);
864 g_strfreev (test_object->strv_prop);
865 test_object->strv_prop = g_value_dup_boxed (value);
868 test_object->enum_prop = g_value_get_enum (value);
871 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
877 test_enum_get_type (void)
879 static volatile gsize define_type_id = 0;
881 if (g_once_init_enter (&define_type_id))
883 static const GEnumValue values[] = {
884 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
885 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
886 { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
887 { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
891 GType type_id = g_enum_register_static ("TestEnum", values);
892 g_once_init_leave (&define_type_id, type_id);
895 return define_type_id;
899 test_object_class_init (TestObjectClass *class)
901 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
903 gobject_class->get_property = test_object_get_property;
904 gobject_class->set_property = test_object_set_property;
905 gobject_class->finalize = test_object_finalize;
907 g_object_class_install_property (gobject_class, PROP_BOOL,
908 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
909 g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
910 g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
911 g_object_class_install_property (gobject_class, PROP_BYTE,
912 g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
913 g_object_class_install_property (gobject_class, PROP_INT16,
914 g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
915 g_object_class_install_property (gobject_class, PROP_UINT16,
916 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
917 g_object_class_install_property (gobject_class, PROP_INT,
918 g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
919 g_object_class_install_property (gobject_class, PROP_UINT,
920 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
921 g_object_class_install_property (gobject_class, PROP_INT64,
922 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
923 g_object_class_install_property (gobject_class, PROP_UINT64,
924 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
925 g_object_class_install_property (gobject_class, PROP_DOUBLE,
926 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
927 g_object_class_install_property (gobject_class, PROP_STRING,
928 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
929 g_object_class_install_property (gobject_class, PROP_NO_WRITE,
930 g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
931 g_object_class_install_property (gobject_class, PROP_NO_READ,
932 g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
933 g_object_class_install_property (gobject_class, PROP_STRV,
934 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
935 g_object_class_install_property (gobject_class, PROP_ENUM,
936 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
940 test_object_new (void)
942 return (TestObject*)g_object_new (test_object_get_type (), NULL);
945 /* Test basic binding functionality for simple types.
946 * Verify that with bidirectional bindings, changes on either side
947 * are notified on the other end.
950 test_simple_binding (void)
968 settings = g_settings_new ("org.gtk.test.binding");
969 obj = test_object_new ();
971 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
972 g_object_set (obj, "bool", TRUE, NULL);
973 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
975 g_settings_set_boolean (settings, "bool", FALSE);
977 g_object_get (obj, "bool", &b, NULL);
978 g_assert_cmpint (b, ==, FALSE);
980 g_settings_bind (settings, "anti-bool", obj, "anti-bool",
981 G_SETTINGS_BIND_INVERT_BOOLEAN);
982 g_object_set (obj, "anti-bool", FALSE, NULL);
983 g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
985 g_settings_set_boolean (settings, "anti-bool", FALSE);
987 g_object_get (obj, "anti-bool", &b, NULL);
988 g_assert_cmpint (b, ==, TRUE);
990 g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
992 g_object_set (obj, "byte", 123, NULL);
994 g_settings_get (settings, "byte", "y", &y);
995 g_assert_cmpint (y, ==, 123);
997 g_settings_set (settings, "byte", "y", 54);
999 g_object_get (obj, "byte", &y, NULL);
1000 g_assert_cmpint (y, ==, 54);
1002 g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
1004 g_object_set (obj, "int16", 1234, NULL);
1006 g_settings_get (settings, "int16", "n", &n);
1007 g_assert_cmpint (n, ==, 1234);
1009 g_settings_set (settings, "int16", "n", 4321);
1011 g_object_get (obj, "int16", &n2, NULL);
1012 g_assert_cmpint (n2, ==, 4321);
1014 g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
1016 g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1018 g_settings_get (settings, "uint16", "q", &q);
1019 g_assert_cmpuint (q, ==, G_MAXUINT16);
1021 g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1023 g_object_get (obj, "uint16", &q2, NULL);
1024 g_assert_cmpuint (q2, ==, (guint16) G_MAXINT16);
1026 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1028 g_object_set (obj, "int", 12345, NULL);
1029 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1031 g_settings_set_int (settings, "int", 54321);
1033 g_object_get (obj, "int", &i, NULL);
1034 g_assert_cmpint (i, ==, 54321);
1036 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1038 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1040 g_settings_get (settings, "int64", "x", &i64);
1041 g_assert_cmpint (i64, ==, G_MAXINT64);
1043 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1045 g_object_get (obj, "int64", &i64, NULL);
1046 g_assert_cmpint (i64, ==, G_MININT64);
1048 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1050 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1052 g_settings_get (settings, "uint64", "t", &u64);
1053 g_assert_cmpuint (u64, ==, G_MAXUINT64);
1055 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1057 g_object_get (obj, "uint64", &u64, NULL);
1058 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1060 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1062 g_object_set (obj, "string", "bu ba", NULL);
1063 s = g_settings_get_string (settings, "string");
1064 g_assert_cmpstr (s, ==, "bu ba");
1067 g_settings_set_string (settings, "string", "bla bla");
1068 g_object_get (obj, "string", &s, NULL);
1069 g_assert_cmpstr (s, ==, "bla bla");
1072 g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1074 g_object_set (obj, "string", "non-unicode:\315", NULL);
1075 value = g_settings_get_value (settings, "chararray");
1076 g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1077 g_variant_unref (value);
1079 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1081 g_object_set (obj, "double", G_MAXFLOAT, NULL);
1082 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1084 g_settings_set_double (settings, "double", G_MINFLOAT);
1086 g_object_get (obj, "double", &d, NULL);
1087 g_assert_cmpfloat (d, ==, G_MINFLOAT);
1089 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1090 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1092 g_settings_set_double (settings, "double", -G_MINDOUBLE);
1094 g_object_get (obj, "double", &d, NULL);
1095 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1097 strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1098 g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1099 g_object_set (obj, "strv", strv, NULL);
1101 strv = g_settings_get_strv (settings, "strv");
1102 s = g_strjoinv (",", strv);
1103 g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1106 strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1107 g_settings_set_strv (settings, "strv", (const gchar **) strv);
1109 g_object_get (obj, "strv", &strv, NULL);
1110 s = g_strjoinv (",", strv);
1111 g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1115 g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1116 g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1117 s = g_settings_get_string (settings, "enum");
1118 g_assert_cmpstr (s, ==, "baz");
1120 g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1122 g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1124 g_object_get (obj, "enum", &i, NULL);
1125 g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1127 g_settings_set_string (settings, "enum", "baz");
1129 g_object_get (obj, "enum", &i, NULL);
1130 g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1132 g_object_unref (obj);
1133 g_object_unref (settings);
1140 GSettings *settings;
1142 settings = g_settings_new ("org.gtk.test.binding");
1143 obj = test_object_new ();
1145 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1147 g_object_set (obj, "int", 12345, NULL);
1148 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1150 g_settings_unbind (obj, "int");
1152 g_object_set (obj, "int", 54321, NULL);
1153 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1155 g_object_unref (obj);
1156 g_object_unref (settings);
1160 test_bind_writable (void)
1163 GSettings *settings;
1166 settings = g_settings_new ("org.gtk.test.binding");
1167 obj = test_object_new ();
1169 g_object_set (obj, "bool", FALSE, NULL);
1171 g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1173 g_object_get (obj, "bool", &b, NULL);
1176 g_settings_unbind (obj, "bool");
1178 g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1180 g_object_get (obj, "bool", &b, NULL);
1183 g_object_unref (obj);
1184 g_object_unref (settings);
1187 /* Test one-way bindings.
1188 * Verify that changes on one side show up on the other,
1189 * but not vice versa
1192 test_directional_binding (void)
1195 GSettings *settings;
1199 settings = g_settings_new ("org.gtk.test.binding");
1200 obj = test_object_new ();
1202 g_object_set (obj, "bool", FALSE, NULL);
1203 g_settings_set_boolean (settings, "bool", FALSE);
1205 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1207 g_settings_set_boolean (settings, "bool", TRUE);
1208 g_object_get (obj, "bool", &b, NULL);
1209 g_assert_cmpint (b, ==, TRUE);
1211 g_object_set (obj, "bool", FALSE, NULL);
1212 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1214 g_object_set (obj, "int", 20, NULL);
1215 g_settings_set_int (settings, "int", 20);
1217 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1219 g_object_set (obj, "int", 32, NULL);
1220 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1222 g_settings_set_int (settings, "int", 20);
1223 g_object_get (obj, "int", &i, NULL);
1224 g_assert_cmpint (i, ==, 32);
1226 g_object_unref (obj);
1227 g_object_unref (settings);
1230 /* Test that type mismatch is caught when creating a binding
1233 test_typesafe_binding (void)
1235 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1238 GSettings *settings;
1240 settings = g_settings_new ("org.gtk.test.binding");
1241 obj = test_object_new ();
1243 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1245 g_object_unref (obj);
1246 g_object_unref (settings);
1248 g_test_trap_assert_failed ();
1249 g_test_trap_assert_stderr ("*not compatible*");
1253 string_to_bool (GValue *value,
1259 s = g_variant_get_string (variant, NULL);
1260 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1266 bool_to_string (const GValue *value,
1267 const GVariantType *expected_type,
1270 if (g_value_get_boolean (value))
1271 return g_variant_new_string ("true");
1273 return g_variant_new_string ("false");
1276 /* Test custom bindings.
1277 * Translate strings to booleans and back
1280 test_custom_binding (void)
1283 GSettings *settings;
1287 settings = g_settings_new ("org.gtk.test.binding");
1288 obj = test_object_new ();
1290 g_settings_set_string (settings, "string", "true");
1292 g_settings_bind_with_mapping (settings, "string",
1294 G_SETTINGS_BIND_DEFAULT,
1299 g_settings_set_string (settings, "string", "false");
1300 g_object_get (obj, "bool", &b, NULL);
1301 g_assert_cmpint (b, ==, FALSE);
1303 g_settings_set_string (settings, "string", "not true");
1304 g_object_get (obj, "bool", &b, NULL);
1305 g_assert_cmpint (b, ==, FALSE);
1307 g_object_set (obj, "bool", TRUE, NULL);
1308 s = g_settings_get_string (settings, "string");
1309 g_assert_cmpstr (s, ==, "true");
1312 g_object_unref (obj);
1313 g_object_unref (settings);
1316 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1317 * initial settings value is transported to the object
1318 * side, but later settings changes do not affect the
1322 test_no_change_binding (void)
1325 GSettings *settings;
1328 settings = g_settings_new ("org.gtk.test.binding");
1329 obj = test_object_new ();
1331 g_object_set (obj, "bool", TRUE, NULL);
1332 g_settings_set_boolean (settings, "bool", FALSE);
1334 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1336 g_object_get (obj, "bool", &b, NULL);
1337 g_assert_cmpint (b, ==, FALSE);
1339 g_settings_set_boolean (settings, "bool", TRUE);
1340 g_object_get (obj, "bool", &b, NULL);
1341 g_assert_cmpint (b, ==, FALSE);
1343 g_settings_set_boolean (settings, "bool", FALSE);
1344 g_object_set (obj, "bool", TRUE, NULL);
1345 b = g_settings_get_boolean (settings, "bool");
1346 g_assert_cmpint (b, ==, TRUE);
1348 g_object_unref (obj);
1349 g_object_unref (settings);
1352 /* Test that binding a non-readable property only
1353 * works in 'GET' mode.
1356 test_no_read_binding (void)
1358 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1361 GSettings *settings;
1363 settings = g_settings_new ("org.gtk.test.binding");
1364 obj = test_object_new ();
1366 g_settings_bind (settings, "string", obj, "no-read", 0);
1368 g_test_trap_assert_failed ();
1369 g_test_trap_assert_stderr ("*property*is not readable*");
1371 if (g_test_trap_fork (0, 0))
1374 GSettings *settings;
1376 settings = g_settings_new ("org.gtk.test.binding");
1377 obj = test_object_new ();
1379 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1383 g_test_trap_assert_passed ();
1386 /* Test that binding a non-writable property only
1387 * works in 'SET' mode.
1390 test_no_write_binding (void)
1392 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1395 GSettings *settings;
1397 settings = g_settings_new ("org.gtk.test.binding");
1398 obj = test_object_new ();
1400 g_settings_bind (settings, "string", obj, "no-write", 0);
1402 g_test_trap_assert_failed ();
1403 g_test_trap_assert_stderr ("*property*is not writable*");
1405 if (g_test_trap_fork (0, 0))
1408 GSettings *settings;
1410 settings = g_settings_new ("org.gtk.test.binding");
1411 obj = test_object_new ();
1413 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1417 g_test_trap_assert_passed ();
1421 * Test that using a keyfile works
1426 GSettingsBackend *kf_backend;
1427 GSettings *settings;
1431 g_remove ("gsettings.store");
1433 kf_backend = g_keyfile_settings_backend_new ("gsettings.store", "/", "root");
1434 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1435 g_object_unref (kf_backend);
1437 g_settings_set (settings, "greeting", "s", "see if this works");
1439 keyfile = g_key_file_new ();
1440 g_assert (g_key_file_load_from_file (keyfile, "gsettings.store", 0, NULL));
1442 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1443 g_assert_cmpstr (str, ==, "'see if this works'");
1446 g_key_file_free (keyfile);
1447 g_object_unref (settings);
1450 /* Test that getting child schemas works
1453 test_child_schema (void)
1455 GSettings *settings;
1459 /* first establish some known conditions */
1460 settings = g_settings_new ("org.gtk.test.basic-types");
1461 g_settings_set (settings, "test-byte", "y", 36);
1463 g_settings_get (settings, "test-byte", "y", &byte);
1464 g_assert_cmpint (byte, ==, 36);
1466 g_object_unref (settings);
1468 settings = g_settings_new ("org.gtk.test");
1469 child = g_settings_get_child (settings, "basic-types");
1470 g_assert (child != NULL);
1472 g_settings_get (child, "test-byte", "y", &byte);
1473 g_assert_cmpint (byte, ==, 36);
1475 g_object_unref (child);
1476 g_object_unref (settings);
1480 glib_translations_work (void)
1484 gchar *orig = "Unnamed";
1486 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
1487 if (!setlocale (LC_MESSAGES, "de"))
1490 works = dgettext ("glib20", orig) != orig;
1491 setlocale (LC_MESSAGES, locale);
1497 #include "../strinfo.c"
1502 /* "foo" has a value of 1
1503 * "bar" has a value of 2
1504 * "baz" is an alias for "bar"
1507 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1508 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1510 const guint32 *strinfo = (guint32 *) array;
1511 guint length = sizeof array / 4;
1515 /* build it and compare */
1518 builder = g_string_new (NULL);
1519 strinfo_builder_append_item (builder, "foo", 1);
1520 strinfo_builder_append_item (builder, "bar", 2);
1521 g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
1522 g_assert_cmpint (builder->len % 4, ==, 0);
1523 g_assert_cmpint (builder->len / 4, ==, length);
1524 g_assert (memcmp (builder->str, strinfo, length * 4) == 0);
1525 g_string_free (builder, TRUE);
1528 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
1530 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
1532 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
1534 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
1537 g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
1538 g_assert_cmpint (result, ==, 1);
1539 g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
1540 g_assert_cmpint (result, ==, 2);
1541 g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
1542 g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
1544 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
1545 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
1546 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
1547 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
1549 g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
1550 g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
1551 g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
1552 g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
1558 GSettings *settings, *direct;
1561 settings = g_settings_new ("org.gtk.test.enums");
1562 direct = g_settings_new ("org.gtk.test.enums.direct");
1566 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1567 g_settings_get_enum (direct, "test");
1568 g_test_trap_assert_failed ();
1569 g_test_trap_assert_stderr ("*not associated with an enum*");
1571 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1572 g_settings_set_enum (settings, "test", 42);
1573 g_test_trap_assert_failed ();
1574 g_test_trap_assert_stderr ("*invalid enum value 42*");
1576 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1577 g_settings_set_string (settings, "test", "qux");
1578 g_test_trap_assert_failed ();
1579 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1581 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1582 g_settings_get_flags (settings, "test");
1583 g_test_trap_assert_failed ();
1584 g_test_trap_assert_stderr ("*not associated with a flags*");
1587 str = g_settings_get_string (settings, "test");
1588 g_assert_cmpstr (str, ==, "bar");
1591 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
1593 str = g_settings_get_string (settings, "test");
1594 g_assert_cmpstr (str, ==, "foo");
1597 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
1599 g_settings_set_string (direct, "test", "qux");
1601 str = g_settings_get_string (direct, "test");
1602 g_assert_cmpstr (str, ==, "qux");
1605 str = g_settings_get_string (settings, "test");
1606 g_assert_cmpstr (str, ==, "quux");
1609 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
1615 GSettings *settings, *direct;
1619 settings = g_settings_new ("org.gtk.test.enums");
1620 direct = g_settings_new ("org.gtk.test.enums.direct");
1624 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1625 g_settings_get_flags (direct, "test");
1626 g_test_trap_assert_failed ();
1627 g_test_trap_assert_stderr ("*not associated with a flags*");
1629 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1630 g_settings_set_flags (settings, "f-test", 0x42);
1631 g_test_trap_assert_failed ();
1632 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
1634 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1635 g_settings_set_strv (settings, "f-test",
1636 (const gchar **) g_strsplit ("rock", ",", 0));
1637 g_test_trap_assert_failed ();
1638 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1640 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1641 g_settings_get_enum (settings, "f-test");
1642 g_test_trap_assert_failed ();
1643 g_test_trap_assert_stderr ("*not associated with an enum*");
1646 strv = g_settings_get_strv (settings, "f-test");
1647 str = g_strjoinv (",", strv);
1648 g_assert_cmpstr (str, ==, "");
1652 g_settings_set_flags (settings, "f-test",
1653 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1655 strv = g_settings_get_strv (settings, "f-test");
1656 str = g_strjoinv (",", strv);
1657 g_assert_cmpstr (str, ==, "talking,walking");
1661 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1662 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1664 strv = g_strsplit ("speaking,laughing", ",", 0);
1665 g_settings_set_strv (direct, "f-test", (const gchar **) strv);
1668 strv = g_settings_get_strv (direct, "f-test");
1669 str = g_strjoinv (",", strv);
1670 g_assert_cmpstr (str, ==, "speaking,laughing");
1674 strv = g_settings_get_strv (settings, "f-test");
1675 str = g_strjoinv (",", strv);
1676 g_assert_cmpstr (str, ==, "talking,laughing");
1680 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1681 TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
1687 GSettings *settings, *direct;
1689 settings = g_settings_new ("org.gtk.test.range");
1690 direct = g_settings_new ("org.gtk.test.range.direct");
1694 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1695 g_settings_set_int (settings, "val", 45);
1696 g_test_trap_assert_failed ();
1697 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1699 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1700 g_settings_set_int (settings, "val", 1);
1701 g_test_trap_assert_failed ();
1702 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1705 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1706 g_settings_set_int (direct, "val", 22);
1707 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
1708 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
1709 g_settings_set_int (direct, "val", 45);
1710 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
1711 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1712 g_settings_set_int (direct, "val", 1);
1713 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
1714 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1718 strv_has_string (gchar **haystack,
1719 const gchar *needle)
1723 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
1725 if (g_strcmp0 (haystack[n], needle) == 0)
1732 strv_set_equal (gchar **strv, ...)
1741 va_start (list, strv);
1744 str = va_arg (list, const gchar *);
1747 if (!strv_has_string (strv, str))
1757 res = g_strv_length ((gchar**)strv) == count;
1763 test_list_items (void)
1765 GSettings *settings;
1769 settings = g_settings_new ("org.gtk.test");
1770 children = g_settings_list_children (settings);
1771 keys = g_settings_list_keys (settings);
1773 g_assert (strv_set_equal (children, "basic-types", "complex-types", "localized", NULL));
1774 g_assert (strv_set_equal (keys, "greeting", "farewell", NULL));
1776 g_strfreev (children);
1779 g_object_unref (settings);
1783 test_list_schemas (void)
1785 const gchar * const *schemas;
1786 const gchar * const *relocs;
1788 relocs = g_settings_list_relocatable_schemas ();
1789 schemas = g_settings_list_schemas ();
1791 g_assert (strv_set_equal ((gchar **)relocs,
1792 "org.gtk.test.no-path",
1795 g_assert (strv_set_equal ((gchar **)schemas,
1797 "org.gtk.test.basic-types",
1798 "org.gtk.test.complex-types",
1799 "org.gtk.test.localized",
1800 "org.gtk.test.binding",
1801 "org.gtk.test.enums",
1802 "org.gtk.test.enums.direct",
1803 "org.gtk.test.range",
1804 "org.gtk.test.range.direct",
1805 "org.gtk.test.mapped",
1810 map_func (GVariant *value,
1814 gint *state = user_data;
1818 v = g_variant_get_int32 (value);
1824 g_assert_cmpint (v, ==, 1);
1828 else if (*state == 1)
1830 g_assert_cmpint (v, ==, 0);
1836 g_assert (value == NULL);
1837 *result = g_variant_new_int32 (5);
1843 test_get_mapped (void)
1845 GSettings *settings;
1850 settings = g_settings_new ("org.gtk.test.mapped");
1851 g_settings_set_int (settings, "val", 1);
1854 p = g_settings_get_mapped (settings, "val", map_func, &state);
1855 val = g_variant_get_int32 ((GVariant*)p);
1856 g_assert_cmpint (val, ==, 5);
1858 g_variant_unref (p);
1859 g_object_unref (settings);
1863 test_get_range (void)
1865 GSettings *settings;
1868 settings = g_settings_new ("org.gtk.test.range");
1869 range = g_settings_get_range (settings, "val");
1870 check_and_free (range, "('range', <(2, 44)>)");
1871 g_object_unref (settings);
1873 settings = g_settings_new ("org.gtk.test.enums");
1874 range = g_settings_get_range (settings, "test");
1875 check_and_free (range, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
1876 g_object_unref (settings);
1878 settings = g_settings_new ("org.gtk.test.enums");
1879 range = g_settings_get_range (settings, "f-test");
1880 check_and_free (range, "('flags', "
1881 "<['mourning', 'laughing', 'talking', 'walking']>)");
1882 g_object_unref (settings);
1884 settings = g_settings_new ("org.gtk.test");
1885 range = g_settings_get_range (settings, "greeting");
1886 check_and_free (range, "('type', <@as []>)");
1887 g_object_unref (settings);
1891 main (int argc, char *argv[])
1896 setlocale (LC_ALL, "");
1898 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
1900 g_setenv ("XDG_DATA_DIRS", ".", TRUE);
1901 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
1904 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
1907 g_test_init (&argc, &argv, NULL);
1909 g_remove ("org.gtk.test.enums.xml");
1910 g_assert (g_spawn_command_line_sync ("../../gobject/glib-mkenums "
1911 "--template " SRCDIR "/enums.xml.template "
1912 SRCDIR "/testenum.h",
1913 &enums, NULL, &result, NULL));
1914 g_assert (result == 0);
1915 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
1918 g_remove ("gschemas.compiled");
1919 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=. "
1920 "--schema-file=org.gtk.test.enums.xml "
1921 "--schema-file=" SRCDIR "/org.gtk.test.gschema.xml",
1922 NULL, NULL, &result, NULL));
1923 g_assert (result == 0);
1925 g_test_add_func ("/gsettings/basic", test_basic);
1929 g_test_add_func ("/gsettings/no-schema", test_no_schema);
1930 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
1931 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
1932 g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
1933 g_test_add_func ("/gsettings/no-path", test_no_path);
1936 g_test_add_func ("/gsettings/basic-types", test_basic_types);
1937 g_test_add_func ("/gsettings/complex-types", test_complex_types);
1938 g_test_add_func ("/gsettings/changes", test_changes);
1940 if (glib_translations_work ())
1942 g_test_add_func ("/gsettings/l10n", test_l10n);
1943 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
1946 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
1947 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
1948 g_test_add_func ("/gsettings/atomic", test_atomic);
1950 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
1951 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
1952 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
1953 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
1954 g_test_add_func ("/gsettings/unbinding", test_unbind);
1955 g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
1959 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
1960 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
1961 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
1964 g_test_add_func ("/gsettings/keyfile", test_keyfile);
1965 g_test_add_func ("/gsettings/child-schema", test_child_schema);
1966 g_test_add_func ("/gsettings/strinfo", test_strinfo);
1967 g_test_add_func ("/gsettings/enums", test_enums);
1968 g_test_add_func ("/gsettings/flags", test_flags);
1969 g_test_add_func ("/gsettings/range", test_range);
1970 g_test_add_func ("/gsettings/list-items", test_list_items);
1971 g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
1972 g_test_add_func ("/gsettings/mapped", test_get_mapped);
1973 g_test_add_func ("/gsettings/get-range", test_get_range);
1975 result = g_test_run ();