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");
56 if (!backend_set && g_test_undefined ())
58 GSettings *tmp_settings = g_settings_new ("org.gtk.test");
60 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
61 "*g_settings_set_value*expects type*");
62 g_settings_set (tmp_settings, "greeting", "i", 555);
63 g_test_assert_expected_messages ();
65 g_object_unref (tmp_settings);
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_child (void)
91 settings = g_settings_new ("org.gtk.test");
92 value = g_settings_get_value (settings, "no_such_key");
94 g_assert (value == NULL);
96 g_object_unref (settings);
100 test_unknown_key (void)
102 if (!g_test_undefined ())
105 g_test_trap_subprocess ("/gsettings/unknown-key:child",
106 0, G_TEST_TRAP_SILENCE_STDERR);
107 g_test_trap_assert_failed ();
108 g_test_trap_assert_stderr ("*does not contain*");
111 /* Check that we get an error when the schema
112 * has not been installed
115 test_no_schema_child (void)
119 settings = g_settings_new ("no.such.schema");
121 g_assert (settings == NULL);
125 test_no_schema (void)
127 if (!g_test_undefined ())
130 g_test_trap_subprocess ("/gsettings/no-schema:child",
131 0, G_TEST_TRAP_SILENCE_STDERR);
132 g_test_trap_assert_failed ();
133 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
136 /* Check that we get an error when passing a type string
137 * that does not match the schema
140 test_wrong_type (void)
145 if (!g_test_undefined ())
148 settings = g_settings_new ("org.gtk.test");
150 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
151 "*given value has a type of*");
152 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
153 "*valid_format_string*");
154 g_settings_get (settings, "greeting", "o", &str);
155 g_test_assert_expected_messages ();
157 g_assert (str == NULL);
159 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
160 "*expects type 's'*");
161 g_settings_set (settings, "greeting", "o", "/a/path");
162 g_test_assert_expected_messages ();
164 g_object_unref (settings);
167 /* Check errors with explicit paths */
169 test_wrong_path_child (void)
171 GSettings *settings G_GNUC_UNUSED;
173 settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
177 test_wrong_path (void)
179 if (!g_test_undefined ())
182 g_test_trap_subprocess ("/gsettings/wrong-path:child",
183 0, G_TEST_TRAP_SILENCE_STDERR);
184 g_test_trap_assert_failed ();
185 g_test_trap_assert_stderr ("*but path * specified by schema*");
189 test_no_path_child (void)
191 GSettings *settings G_GNUC_UNUSED;
193 settings = g_settings_new ("org.gtk.test.no-path");
199 if (!g_test_undefined ())
202 g_test_trap_subprocess ("/gsettings/no-path:child",
203 0, G_TEST_TRAP_SILENCE_STDERR);
204 g_test_trap_assert_failed ();
205 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
209 /* Check that we can successfully read and set the full
210 * range of all basic types
213 test_basic_types (void)
227 settings = g_settings_new ("org.gtk.test.basic-types");
229 g_settings_get (settings, "test-boolean", "b", &b);
230 g_assert_cmpint (b, ==, 1);
232 g_settings_set (settings, "test-boolean", "b", 0);
233 g_settings_get (settings, "test-boolean", "b", &b);
234 g_assert_cmpint (b, ==, 0);
236 g_settings_get (settings, "test-byte", "y", &byte);
237 g_assert_cmpint (byte, ==, 25);
239 g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
240 g_settings_get (settings, "test-byte", "y", &byte);
241 g_assert_cmpint (byte, ==, G_MAXUINT8);
243 g_settings_get (settings, "test-int16", "n", &i16);
244 g_assert_cmpint (i16, ==, -1234);
246 g_settings_set (settings, "test-int16", "n", G_MININT16);
247 g_settings_get (settings, "test-int16", "n", &i16);
248 g_assert_cmpint (i16, ==, G_MININT16);
250 g_settings_set (settings, "test-int16", "n", G_MAXINT16);
251 g_settings_get (settings, "test-int16", "n", &i16);
252 g_assert_cmpint (i16, ==, G_MAXINT16);
254 g_settings_get (settings, "test-uint16", "q", &u16);
255 g_assert_cmpuint (u16, ==, 1234);
257 g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
258 g_settings_get (settings, "test-uint16", "q", &u16);
259 g_assert_cmpuint (u16, ==, G_MAXUINT16);
261 g_settings_get (settings, "test-int32", "i", &i32);
262 g_assert_cmpint (i32, ==, -123456);
264 g_settings_set (settings, "test-int32", "i", G_MININT32);
265 g_settings_get (settings, "test-int32", "i", &i32);
266 g_assert_cmpint (i32, ==, G_MININT32);
268 g_settings_set (settings, "test-int32", "i", G_MAXINT32);
269 g_settings_get (settings, "test-int32", "i", &i32);
270 g_assert_cmpint (i32, ==, G_MAXINT32);
272 g_settings_get (settings, "test-uint32", "u", &u32);
273 g_assert_cmpuint (u32, ==, 123456);
275 g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
276 g_settings_get (settings, "test-uint32", "u", &u32);
277 g_assert_cmpuint (u32, ==, G_MAXUINT32);
279 g_settings_get (settings, "test-int64", "x", &i64);
280 g_assert_cmpuint (i64, ==, -123456789);
282 g_settings_set (settings, "test-int64", "x", G_MININT64);
283 g_settings_get (settings, "test-int64", "x", &i64);
284 g_assert_cmpuint (i64, ==, G_MININT64);
286 g_settings_set (settings, "test-int64", "x", G_MAXINT64);
287 g_settings_get (settings, "test-int64", "x", &i64);
288 g_assert_cmpuint (i64, ==, G_MAXINT64);
290 g_settings_get (settings, "test-uint64", "t", &u64);
291 g_assert_cmpuint (u64, ==, 123456789);
293 g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
294 g_settings_get (settings, "test-uint64", "t", &u64);
295 g_assert_cmpuint (u64, ==, G_MAXUINT64);
297 g_settings_get (settings, "test-double", "d", &d);
298 g_assert_cmpfloat (d, ==, 123.456);
300 g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
301 g_settings_get (settings, "test-double", "d", &d);
302 g_assert_cmpfloat (d, ==, G_MINDOUBLE);
304 g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
305 g_settings_get (settings, "test-double", "d", &d);
306 g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
308 g_settings_get (settings, "test-string", "s", &str);
309 g_assert_cmpstr (str, ==, "a string, it seems");
313 g_settings_get (settings, "test-objectpath", "o", &str);
314 g_assert_cmpstr (str, ==, "/a/object/path");
315 g_object_unref (settings);
320 /* Check that we can read an set complex types like
321 * tuples, arrays and dictionaries
324 test_complex_types (void)
329 GVariantIter *iter = NULL;
331 settings = g_settings_new ("org.gtk.test.complex-types");
333 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
334 g_assert_cmpstr (s, ==, "one");
335 g_assert_cmpint (i1,==, 2);
336 g_assert_cmpint (i2,==, 3);
340 g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
341 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
342 g_assert_cmpstr (s, ==, "none");
343 g_assert_cmpint (i1,==, 0);
344 g_assert_cmpint (i2,==, 0);
348 g_settings_get (settings, "test-array", "ai", &iter);
349 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
350 g_assert (g_variant_iter_next (iter, "i", &i1));
351 g_assert_cmpint (i1, ==, 0);
352 g_assert (g_variant_iter_next (iter, "i", &i1));
353 g_assert_cmpint (i1, ==, 1);
354 g_assert (g_variant_iter_next (iter, "i", &i1));
355 g_assert_cmpint (i1, ==, 2);
356 g_assert (g_variant_iter_next (iter, "i", &i1));
357 g_assert_cmpint (i1, ==, 3);
358 g_assert (g_variant_iter_next (iter, "i", &i1));
359 g_assert_cmpint (i1, ==, 4);
360 g_assert (g_variant_iter_next (iter, "i", &i1));
361 g_assert_cmpint (i1, ==, 5);
362 g_assert (!g_variant_iter_next (iter, "i", &i1));
363 g_variant_iter_free (iter);
365 g_object_unref (settings);
368 static gboolean changed_cb_called;
371 changed_cb (GSettings *settings,
375 changed_cb_called = TRUE;
377 g_assert_cmpstr (key, ==, data);
380 /* Test that basic change notification with the changed signal works.
386 GSettings *settings2;
388 settings = g_settings_new ("org.gtk.test");
390 g_signal_connect (settings, "changed",
391 G_CALLBACK (changed_cb), "greeting");
393 changed_cb_called = FALSE;
395 g_settings_set (settings, "greeting", "s", "new greeting");
396 g_assert (changed_cb_called);
398 settings2 = g_settings_new ("org.gtk.test");
400 changed_cb_called = FALSE;
402 g_settings_set (settings2, "greeting", "s", "hi");
403 g_assert (changed_cb_called);
405 g_object_unref (settings2);
406 g_object_unref (settings);
409 static gboolean changed_cb_called2;
412 changed_cb2 (GSettings *settings,
421 /* Test that changes done to a delay-mode instance
422 * don't appear to the outside world until apply. Also
423 * check that we get change notification when they are
425 * Also test that the has-unapplied property is properly
429 test_delay_apply (void)
432 GSettings *settings2;
435 settings = g_settings_new ("org.gtk.test");
436 settings2 = g_settings_new ("org.gtk.test");
438 g_settings_set (settings2, "greeting", "s", "top o' the morning");
440 changed_cb_called = FALSE;
441 changed_cb_called2 = FALSE;
443 g_signal_connect (settings, "changed",
444 G_CALLBACK (changed_cb2), &changed_cb_called);
445 g_signal_connect (settings2, "changed",
446 G_CALLBACK (changed_cb2), &changed_cb_called2);
448 g_settings_delay (settings);
450 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
452 g_assert (changed_cb_called);
453 g_assert (!changed_cb_called2);
455 g_settings_get (settings, "greeting", "s", &str);
456 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
460 g_settings_get (settings2, "greeting", "s", &str);
461 g_assert_cmpstr (str, ==, "top o' the morning");
465 g_assert (g_settings_get_has_unapplied (settings));
466 g_assert (!g_settings_get_has_unapplied (settings2));
468 changed_cb_called = FALSE;
469 changed_cb_called2 = FALSE;
471 g_settings_apply (settings);
473 g_assert (!changed_cb_called);
474 g_assert (changed_cb_called2);
476 g_settings_get (settings, "greeting", "s", &str);
477 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
481 g_settings_get (settings2, "greeting", "s", &str);
482 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
486 g_assert (!g_settings_get_has_unapplied (settings));
487 g_assert (!g_settings_get_has_unapplied (settings2));
489 g_object_unref (settings2);
490 g_object_unref (settings);
493 /* Test that reverting unapplied changes in a delay-apply
494 * settings instance works.
497 test_delay_revert (void)
500 GSettings *settings2;
503 settings = g_settings_new ("org.gtk.test");
504 settings2 = g_settings_new ("org.gtk.test");
506 g_settings_set (settings2, "greeting", "s", "top o' the morning");
508 g_settings_delay (settings);
510 g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
512 g_settings_get (settings, "greeting", "s", &str);
513 g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
517 g_settings_get (settings2, "greeting", "s", &str);
518 g_assert_cmpstr (str, ==, "top o' the morning");
522 g_assert (g_settings_get_has_unapplied (settings));
524 g_settings_revert (settings);
526 g_assert (!g_settings_get_has_unapplied (settings));
528 g_settings_get (settings, "greeting", "s", &str);
529 g_assert_cmpstr (str, ==, "top o' the morning");
533 g_settings_get (settings2, "greeting", "s", &str);
534 g_assert_cmpstr (str, ==, "top o' the morning");
538 g_object_unref (settings2);
539 g_object_unref (settings);
543 keys_changed_cb (GSettings *settings,
549 g_assert_cmpint (n_keys, ==, 2);
551 g_assert ((keys[0] == g_quark_from_static_string ("greeting") &&
552 keys[1] == g_quark_from_static_string ("farewell")) ||
553 (keys[1] == g_quark_from_static_string ("greeting") &&
554 keys[0] == g_quark_from_static_string ("farewell")));
556 g_settings_get (settings, "greeting", "s", &str);
557 g_assert_cmpstr (str, ==, "greetings from test_atomic");
561 g_settings_get (settings, "farewell", "s", &str);
562 g_assert_cmpstr (str, ==, "atomic bye-bye");
567 /* Check that delay-applied changes appear atomically.
568 * More specifically, verify that all changed keys appear
569 * with their new value while handling the change-event signal.
575 GSettings *settings2;
578 settings = g_settings_new ("org.gtk.test");
579 settings2 = g_settings_new ("org.gtk.test");
581 g_settings_set (settings2, "greeting", "s", "top o' the morning");
583 changed_cb_called = FALSE;
584 changed_cb_called2 = FALSE;
586 g_signal_connect (settings2, "change-event",
587 G_CALLBACK (keys_changed_cb), NULL);
589 g_settings_delay (settings);
591 g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
592 g_settings_set (settings, "farewell", "s", "atomic bye-bye");
594 g_settings_apply (settings);
596 g_settings_get (settings, "greeting", "s", &str);
597 g_assert_cmpstr (str, ==, "greetings from test_atomic");
601 g_settings_get (settings, "farewell", "s", &str);
602 g_assert_cmpstr (str, ==, "atomic bye-bye");
606 g_settings_get (settings2, "greeting", "s", &str);
607 g_assert_cmpstr (str, ==, "greetings from test_atomic");
611 g_settings_get (settings2, "farewell", "s", &str);
612 g_assert_cmpstr (str, ==, "atomic bye-bye");
616 g_object_unref (settings2);
617 g_object_unref (settings);
620 /* On Windows the interaction between the C library locale and libintl
621 * (from GNU gettext) is not like on POSIX, so just skip these tests
624 * There are several issues:
626 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
627 * in libintl (defined in its <libintl.h>).
629 * 2) The locale names that setlocale() accepts and returns aren't in
630 * the "de_DE" style, but like "German_Germany".
632 * 3) libintl looks at the Win32 thread locale and not the C library
633 * locale. (And even if libintl would use the C library's locale, as
634 * there are several alternative C library DLLs, libintl might be
635 * linked to a different one than the application code, so they
636 * wouldn't have the same C library locale anyway.)
639 /* Test that translations work for schema defaults.
641 * This test relies on the de.po file in the same directory
642 * to be compiled into ./de/LC_MESSAGES/test.mo
651 bindtextdomain ("test", ".");
652 bind_textdomain_codeset ("test", "UTF-8");
654 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
656 settings = g_settings_new ("org.gtk.test.localized");
658 setlocale (LC_MESSAGES, "C");
659 str = g_settings_get_string (settings, "error-message");
660 setlocale (LC_MESSAGES, locale);
662 g_assert_cmpstr (str, ==, "Unnamed");
666 setlocale (LC_MESSAGES, "de_DE");
667 str = g_settings_get_string (settings, "error-message");
668 setlocale (LC_MESSAGES, locale);
670 g_assert_cmpstr (str, ==, "Unbenannt");
671 g_object_unref (settings);
678 /* Test that message context works as expected with translated
679 * schema defaults. Also, verify that non-ASCII UTF-8 content
682 * This test relies on the de.po file in the same directory
683 * to be compiled into ./de/LC_MESSAGES/test.mo
686 test_l10n_context (void)
692 bindtextdomain ("test", ".");
693 bind_textdomain_codeset ("test", "UTF-8");
695 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
697 settings = g_settings_new ("org.gtk.test.localized");
699 setlocale (LC_MESSAGES, "C");
700 g_settings_get (settings, "backspace", "s", &str);
701 setlocale (LC_MESSAGES, locale);
703 g_assert_cmpstr (str, ==, "BackSpace");
707 setlocale (LC_MESSAGES, "de_DE");
708 g_settings_get (settings, "backspace", "s", &str);
709 setlocale (LC_MESSAGES, locale);
711 g_assert_cmpstr (str, ==, "Löschen");
712 g_object_unref (settings);
742 GObject parent_instance;
745 gboolean anti_bool_prop;
756 gchar *no_write_prop;
764 GObjectClass parent_class;
767 static GType test_object_get_type (void);
768 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
771 test_object_init (TestObject *object)
776 test_object_finalize (GObject *object)
778 TestObject *testo = (TestObject*)object;
779 g_strfreev (testo->strv_prop);
780 g_free (testo->string_prop);
781 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
785 test_object_get_property (GObject *object,
790 TestObject *test_object = (TestObject *)object;
795 g_value_set_boolean (value, test_object->bool_prop);
798 g_value_set_boolean (value, test_object->anti_bool_prop);
801 g_value_set_schar (value, test_object->byte_prop);
804 g_value_set_uint (value, test_object->uint16_prop);
807 g_value_set_int (value, test_object->int16_prop);
810 g_value_set_int (value, test_object->int_prop);
813 g_value_set_uint (value, test_object->uint_prop);
816 g_value_set_int64 (value, test_object->int64_prop);
819 g_value_set_uint64 (value, test_object->uint64_prop);
822 g_value_set_double (value, test_object->double_prop);
825 g_value_set_string (value, test_object->string_prop);
828 g_value_set_string (value, test_object->no_write_prop);
831 g_value_set_boxed (value, test_object->strv_prop);
834 g_value_set_enum (value, test_object->enum_prop);
837 g_value_set_flags (value, test_object->flags_prop);
840 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
846 test_object_set_property (GObject *object,
851 TestObject *test_object = (TestObject *)object;
856 test_object->bool_prop = g_value_get_boolean (value);
859 test_object->anti_bool_prop = g_value_get_boolean (value);
862 test_object->byte_prop = g_value_get_schar (value);
865 test_object->int16_prop = g_value_get_int (value);
868 test_object->uint16_prop = g_value_get_uint (value);
871 test_object->int_prop = g_value_get_int (value);
874 test_object->uint_prop = g_value_get_uint (value);
877 test_object->int64_prop = g_value_get_int64 (value);
880 test_object->uint64_prop = g_value_get_uint64 (value);
883 test_object->double_prop = g_value_get_double (value);
886 g_free (test_object->string_prop);
887 test_object->string_prop = g_value_dup_string (value);
890 g_free (test_object->no_read_prop);
891 test_object->no_read_prop = g_value_dup_string (value);
894 g_strfreev (test_object->strv_prop);
895 test_object->strv_prop = g_value_dup_boxed (value);
898 test_object->enum_prop = g_value_get_enum (value);
901 test_object->flags_prop = g_value_get_flags (value);
904 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
910 test_enum_get_type (void)
912 static volatile gsize define_type_id = 0;
914 if (g_once_init_enter (&define_type_id))
916 static const GEnumValue values[] = {
917 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
918 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
919 { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
920 { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
924 GType type_id = g_enum_register_static ("TestEnum", values);
925 g_once_init_leave (&define_type_id, type_id);
928 return define_type_id;
932 test_flags_get_type (void)
934 static volatile gsize define_type_id = 0;
936 if (g_once_init_enter (&define_type_id))
938 static const GFlagsValue values[] = {
939 { TEST_FLAGS_NONE, "TEST_FLAGS_NONE", "none" },
940 { TEST_FLAGS_MOURNING, "TEST_FLAGS_MOURNING", "mourning" },
941 { TEST_FLAGS_LAUGHING, "TEST_FLAGS_LAUGHING", "laughing" },
942 { TEST_FLAGS_WALKING, "TEST_FLAGS_WALKING", "walking" },
946 GType type_id = g_flags_register_static ("TestFlags", values);
947 g_once_init_leave (&define_type_id, type_id);
950 return define_type_id;
954 test_object_class_init (TestObjectClass *class)
956 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
958 gobject_class->get_property = test_object_get_property;
959 gobject_class->set_property = test_object_set_property;
960 gobject_class->finalize = test_object_finalize;
962 g_object_class_install_property (gobject_class, PROP_BOOL,
963 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
964 g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
965 g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
966 g_object_class_install_property (gobject_class, PROP_BYTE,
967 g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
968 g_object_class_install_property (gobject_class, PROP_INT16,
969 g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
970 g_object_class_install_property (gobject_class, PROP_UINT16,
971 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
972 g_object_class_install_property (gobject_class, PROP_INT,
973 g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
974 g_object_class_install_property (gobject_class, PROP_UINT,
975 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
976 g_object_class_install_property (gobject_class, PROP_INT64,
977 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
978 g_object_class_install_property (gobject_class, PROP_UINT64,
979 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
980 g_object_class_install_property (gobject_class, PROP_DOUBLE,
981 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
982 g_object_class_install_property (gobject_class, PROP_STRING,
983 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
984 g_object_class_install_property (gobject_class, PROP_NO_WRITE,
985 g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
986 g_object_class_install_property (gobject_class, PROP_NO_READ,
987 g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
988 g_object_class_install_property (gobject_class, PROP_STRV,
989 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
990 g_object_class_install_property (gobject_class, PROP_ENUM,
991 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
992 g_object_class_install_property (gobject_class, PROP_FLAGS,
993 g_param_spec_flags ("flags", "", "", test_flags_get_type (), TEST_FLAGS_NONE, G_PARAM_READWRITE));
997 test_object_new (void)
999 return (TestObject*)g_object_new (test_object_get_type (), NULL);
1002 /* Test basic binding functionality for simple types.
1003 * Verify that with bidirectional bindings, changes on either side
1004 * are notified on the other end.
1007 test_simple_binding (void)
1010 GSettings *settings;
1026 settings = g_settings_new ("org.gtk.test.binding");
1027 obj = test_object_new ();
1029 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
1030 g_object_set (obj, "bool", TRUE, NULL);
1031 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1033 g_settings_set_boolean (settings, "bool", FALSE);
1035 g_object_get (obj, "bool", &b, NULL);
1036 g_assert_cmpint (b, ==, FALSE);
1038 g_settings_bind (settings, "anti-bool", obj, "anti-bool",
1039 G_SETTINGS_BIND_INVERT_BOOLEAN);
1040 g_object_set (obj, "anti-bool", FALSE, NULL);
1041 g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
1043 g_settings_set_boolean (settings, "anti-bool", FALSE);
1045 g_object_get (obj, "anti-bool", &b, NULL);
1046 g_assert_cmpint (b, ==, TRUE);
1048 g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
1050 g_object_set (obj, "byte", 123, NULL);
1052 g_settings_get (settings, "byte", "y", &y);
1053 g_assert_cmpint (y, ==, 123);
1055 g_settings_set (settings, "byte", "y", 54);
1057 g_object_get (obj, "byte", &y, NULL);
1058 g_assert_cmpint (y, ==, 54);
1060 g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
1062 g_object_set (obj, "int16", 1234, NULL);
1064 g_settings_get (settings, "int16", "n", &n);
1065 g_assert_cmpint (n, ==, 1234);
1067 g_settings_set (settings, "int16", "n", 4321);
1069 g_object_get (obj, "int16", &n2, NULL);
1070 g_assert_cmpint (n2, ==, 4321);
1072 g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
1074 g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1076 g_settings_get (settings, "uint16", "q", &q);
1077 g_assert_cmpuint (q, ==, G_MAXUINT16);
1079 g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1081 g_object_get (obj, "uint16", &q2, NULL);
1082 g_assert_cmpuint (q2, ==, (guint16) G_MAXINT16);
1084 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1086 g_object_set (obj, "int", 12345, NULL);
1087 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1089 g_settings_set_int (settings, "int", 54321);
1091 g_object_get (obj, "int", &i, NULL);
1092 g_assert_cmpint (i, ==, 54321);
1094 g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1096 g_object_set (obj, "uint", 12345, NULL);
1097 g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);
1099 g_settings_set_uint (settings, "uint", 54321);
1101 g_object_get (obj, "uint", &u, NULL);
1102 g_assert_cmpuint (u, ==, 54321);
1104 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1106 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1108 g_settings_get (settings, "int64", "x", &i64);
1109 g_assert_cmpint (i64, ==, G_MAXINT64);
1111 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1113 g_object_get (obj, "int64", &i64, NULL);
1114 g_assert_cmpint (i64, ==, G_MININT64);
1116 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1118 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1120 g_settings_get (settings, "uint64", "t", &u64);
1121 g_assert_cmpuint (u64, ==, G_MAXUINT64);
1123 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1125 g_object_get (obj, "uint64", &u64, NULL);
1126 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1128 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1130 g_object_set (obj, "string", "bu ba", NULL);
1131 s = g_settings_get_string (settings, "string");
1132 g_assert_cmpstr (s, ==, "bu ba");
1135 g_settings_set_string (settings, "string", "bla bla");
1136 g_object_get (obj, "string", &s, NULL);
1137 g_assert_cmpstr (s, ==, "bla bla");
1140 g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1142 g_object_set (obj, "string", "non-unicode:\315", NULL);
1143 value = g_settings_get_value (settings, "chararray");
1144 g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1145 g_variant_unref (value);
1147 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1149 g_object_set (obj, "double", G_MAXFLOAT, NULL);
1150 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1152 g_settings_set_double (settings, "double", G_MINFLOAT);
1154 g_object_get (obj, "double", &d, NULL);
1155 g_assert_cmpfloat (d, ==, G_MINFLOAT);
1157 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1158 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1160 g_settings_set_double (settings, "double", -G_MINDOUBLE);
1162 g_object_get (obj, "double", &d, NULL);
1163 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1165 strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1166 g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1167 g_object_set (obj, "strv", strv, NULL);
1169 strv = g_settings_get_strv (settings, "strv");
1170 s = g_strjoinv (",", strv);
1171 g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1174 strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1175 g_settings_set_strv (settings, "strv", (const gchar **) strv);
1177 g_object_get (obj, "strv", &strv, NULL);
1178 s = g_strjoinv (",", strv);
1179 g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1183 g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1184 g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1185 s = g_settings_get_string (settings, "enum");
1186 g_assert_cmpstr (s, ==, "baz");
1188 g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1190 g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1192 g_object_get (obj, "enum", &i, NULL);
1193 g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1195 g_settings_set_string (settings, "enum", "baz");
1197 g_object_get (obj, "enum", &i, NULL);
1198 g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1200 g_settings_bind (settings, "flags", obj, "flags", G_SETTINGS_BIND_DEFAULT);
1201 g_object_set (obj, "flags", TEST_FLAGS_MOURNING, NULL);
1202 strv = g_settings_get_strv (settings, "flags");
1203 g_assert_cmpint (g_strv_length (strv), ==, 1);
1204 g_assert_cmpstr (strv[0], ==, "mourning");
1207 g_assert_cmpint (g_settings_get_flags (settings, "flags"), ==, TEST_FLAGS_MOURNING);
1209 g_settings_set_flags (settings, "flags", TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1211 g_object_get (obj, "flags", &i, NULL);
1212 g_assert_cmpint (i, ==, TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1214 g_object_unref (obj);
1215 g_object_unref (settings);
1222 GSettings *settings;
1224 settings = g_settings_new ("org.gtk.test.binding");
1225 obj = test_object_new ();
1227 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1229 g_object_set (obj, "int", 12345, NULL);
1230 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1232 g_settings_unbind (obj, "int");
1234 g_object_set (obj, "int", 54321, NULL);
1235 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1237 g_object_unref (obj);
1238 g_object_unref (settings);
1242 test_bind_writable (void)
1245 GSettings *settings;
1248 settings = g_settings_new ("org.gtk.test.binding");
1249 obj = test_object_new ();
1251 g_object_set (obj, "bool", FALSE, NULL);
1253 g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1255 g_object_get (obj, "bool", &b, NULL);
1258 g_settings_unbind (obj, "bool");
1260 g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1262 g_object_get (obj, "bool", &b, NULL);
1265 g_object_unref (obj);
1266 g_object_unref (settings);
1269 /* Test one-way bindings.
1270 * Verify that changes on one side show up on the other,
1271 * but not vice versa
1274 test_directional_binding (void)
1277 GSettings *settings;
1281 settings = g_settings_new ("org.gtk.test.binding");
1282 obj = test_object_new ();
1284 g_object_set (obj, "bool", FALSE, NULL);
1285 g_settings_set_boolean (settings, "bool", FALSE);
1287 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1289 g_settings_set_boolean (settings, "bool", TRUE);
1290 g_object_get (obj, "bool", &b, NULL);
1291 g_assert_cmpint (b, ==, TRUE);
1293 g_object_set (obj, "bool", FALSE, NULL);
1294 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1296 g_object_set (obj, "int", 20, NULL);
1297 g_settings_set_int (settings, "int", 20);
1299 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1301 g_object_set (obj, "int", 32, NULL);
1302 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1304 g_settings_set_int (settings, "int", 20);
1305 g_object_get (obj, "int", &i, NULL);
1306 g_assert_cmpint (i, ==, 32);
1308 g_object_unref (obj);
1309 g_object_unref (settings);
1312 /* Test that type mismatch is caught when creating a binding
1315 test_typesafe_binding_child (void)
1318 GSettings *settings;
1320 settings = g_settings_new ("org.gtk.test.binding");
1321 obj = test_object_new ();
1323 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1325 g_object_unref (obj);
1326 g_object_unref (settings);
1330 test_typesafe_binding (void)
1332 if (!g_test_undefined ())
1335 g_test_trap_subprocess ("/gsettings/typesafe-binding:child",
1336 0, G_TEST_TRAP_SILENCE_STDERR);
1337 g_test_trap_assert_failed ();
1338 g_test_trap_assert_stderr ("*not compatible*");
1342 string_to_bool (GValue *value,
1348 s = g_variant_get_string (variant, NULL);
1349 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1355 bool_to_string (const GValue *value,
1356 const GVariantType *expected_type,
1359 if (g_value_get_boolean (value))
1360 return g_variant_new_string ("true");
1362 return g_variant_new_string ("false");
1365 /* Test custom bindings.
1366 * Translate strings to booleans and back
1369 test_custom_binding (void)
1372 GSettings *settings;
1376 settings = g_settings_new ("org.gtk.test.binding");
1377 obj = test_object_new ();
1379 g_settings_set_string (settings, "string", "true");
1381 g_settings_bind_with_mapping (settings, "string",
1383 G_SETTINGS_BIND_DEFAULT,
1388 g_settings_set_string (settings, "string", "false");
1389 g_object_get (obj, "bool", &b, NULL);
1390 g_assert_cmpint (b, ==, FALSE);
1392 g_settings_set_string (settings, "string", "not true");
1393 g_object_get (obj, "bool", &b, NULL);
1394 g_assert_cmpint (b, ==, FALSE);
1396 g_object_set (obj, "bool", TRUE, NULL);
1397 s = g_settings_get_string (settings, "string");
1398 g_assert_cmpstr (s, ==, "true");
1401 g_object_unref (obj);
1402 g_object_unref (settings);
1405 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1406 * initial settings value is transported to the object
1407 * side, but later settings changes do not affect the
1411 test_no_change_binding (void)
1414 GSettings *settings;
1417 settings = g_settings_new ("org.gtk.test.binding");
1418 obj = test_object_new ();
1420 g_object_set (obj, "bool", TRUE, NULL);
1421 g_settings_set_boolean (settings, "bool", FALSE);
1423 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1425 g_object_get (obj, "bool", &b, NULL);
1426 g_assert_cmpint (b, ==, FALSE);
1428 g_settings_set_boolean (settings, "bool", TRUE);
1429 g_object_get (obj, "bool", &b, NULL);
1430 g_assert_cmpint (b, ==, FALSE);
1432 g_settings_set_boolean (settings, "bool", FALSE);
1433 g_object_set (obj, "bool", TRUE, NULL);
1434 b = g_settings_get_boolean (settings, "bool");
1435 g_assert_cmpint (b, ==, TRUE);
1437 g_object_unref (obj);
1438 g_object_unref (settings);
1441 /* Test that binding a non-readable property only
1442 * works in 'GET' mode.
1445 test_no_read_binding_fail (void)
1448 GSettings *settings;
1450 settings = g_settings_new ("org.gtk.test.binding");
1451 obj = test_object_new ();
1453 g_settings_bind (settings, "string", obj, "no-read", 0);
1457 test_no_read_binding_pass (void)
1460 GSettings *settings;
1462 settings = g_settings_new ("org.gtk.test.binding");
1463 obj = test_object_new ();
1465 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1471 test_no_read_binding (void)
1473 if (g_test_undefined ())
1475 g_test_trap_subprocess ("/gsettings/no-read-binding:fail",
1476 0, G_TEST_TRAP_SILENCE_STDERR);
1477 g_test_trap_assert_failed ();
1478 g_test_trap_assert_stderr ("*property*is not readable*");
1481 g_test_trap_subprocess ("/gsettings/no-read-binding:pass", 0, 0);
1482 g_test_trap_assert_passed ();
1485 /* Test that binding a non-writable property only
1486 * works in 'SET' mode.
1489 test_no_write_binding_fail (void)
1492 GSettings *settings;
1494 settings = g_settings_new ("org.gtk.test.binding");
1495 obj = test_object_new ();
1497 g_settings_bind (settings, "string", obj, "no-write", 0);
1501 test_no_write_binding_pass (void)
1504 GSettings *settings;
1506 settings = g_settings_new ("org.gtk.test.binding");
1507 obj = test_object_new ();
1509 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1515 test_no_write_binding (void)
1517 if (g_test_undefined ())
1519 g_test_trap_subprocess ("/gsettings/no-write-binding:fail",
1520 0, G_TEST_TRAP_SILENCE_STDERR);
1521 g_test_trap_assert_failed ();
1522 g_test_trap_assert_stderr ("*property*is not writable*");
1525 g_test_trap_subprocess ("/gsettings/no-write-binding:pass", 0, 0);
1526 g_test_trap_assert_passed ();
1530 * Test that using a keyfile works
1535 GSettingsBackend *kf_backend;
1536 GSettings *settings;
1540 g_remove ("gsettings.store");
1542 kf_backend = g_keyfile_settings_backend_new ("gsettings.store", "/", "root");
1543 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1544 g_object_unref (kf_backend);
1546 g_settings_set (settings, "greeting", "s", "see if this works");
1548 keyfile = g_key_file_new ();
1549 g_assert (g_key_file_load_from_file (keyfile, "gsettings.store", 0, NULL));
1551 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1552 g_assert_cmpstr (str, ==, "'see if this works'");
1555 g_key_file_free (keyfile);
1556 g_object_unref (settings);
1559 /* Test that getting child schemas works
1562 test_child_schema (void)
1564 GSettings *settings;
1568 /* first establish some known conditions */
1569 settings = g_settings_new ("org.gtk.test.basic-types");
1570 g_settings_set (settings, "test-byte", "y", 36);
1572 g_settings_get (settings, "test-byte", "y", &byte);
1573 g_assert_cmpint (byte, ==, 36);
1575 g_object_unref (settings);
1577 settings = g_settings_new ("org.gtk.test");
1578 child = g_settings_get_child (settings, "basic-types");
1579 g_assert (child != NULL);
1581 g_settings_get (child, "test-byte", "y", &byte);
1582 g_assert_cmpint (byte, ==, 36);
1584 g_object_unref (child);
1585 g_object_unref (settings);
1589 glib_translations_work (void)
1593 gchar *orig = "Unnamed";
1595 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
1596 if (!setlocale (LC_MESSAGES, "de"))
1599 works = dgettext ("glib20", orig) != orig;
1600 setlocale (LC_MESSAGES, locale);
1606 #include "../strinfo.c"
1611 /* "foo" has a value of 1
1612 * "bar" has a value of 2
1613 * "baz" is an alias for "bar"
1616 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1617 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1619 const guint32 *strinfo = (guint32 *) array;
1620 guint length = sizeof array / 4;
1624 /* build it and compare */
1627 builder = g_string_new (NULL);
1628 strinfo_builder_append_item (builder, "foo", 1);
1629 strinfo_builder_append_item (builder, "bar", 2);
1630 g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
1631 g_assert_cmpint (builder->len % 4, ==, 0);
1632 g_assert_cmpint (builder->len / 4, ==, length);
1633 g_assert (memcmp (builder->str, strinfo, length * 4) == 0);
1634 g_string_free (builder, TRUE);
1637 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
1639 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
1641 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
1643 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
1646 g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
1647 g_assert_cmpint (result, ==, 1);
1648 g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
1649 g_assert_cmpint (result, ==, 2);
1650 g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
1651 g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
1653 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
1654 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
1655 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
1656 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
1658 g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
1659 g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
1660 g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
1661 g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
1665 test_enums_non_enum_key (void)
1669 direct = g_settings_new ("org.gtk.test.enums.direct");
1670 g_settings_get_enum (direct, "test");
1671 g_assert_not_reached ();
1675 test_enums_non_enum_value (void)
1677 GSettings *settings;
1679 settings = g_settings_new ("org.gtk.test.enums");
1680 g_settings_set_enum (settings, "test", 42);
1681 g_assert_not_reached ();
1685 test_enums_range (void)
1687 GSettings *settings;
1689 settings = g_settings_new ("org.gtk.test.enums");
1690 g_settings_set_string (settings, "test", "qux");
1691 g_assert_not_reached ();
1695 test_enums_non_flags (void)
1697 GSettings *settings;
1699 settings = g_settings_new ("org.gtk.test.enums");
1700 g_settings_get_flags (settings, "test");
1701 g_assert_not_reached ();
1707 GSettings *settings, *direct;
1710 settings = g_settings_new ("org.gtk.test.enums");
1711 direct = g_settings_new ("org.gtk.test.enums.direct");
1713 if (g_test_undefined () && !backend_set)
1715 g_test_trap_subprocess ("/gsettings/enums:non-enum-key",
1716 0, G_TEST_TRAP_SILENCE_STDERR);
1717 g_test_trap_assert_failed ();
1718 g_test_trap_assert_stderr ("*not associated with an enum*");
1720 g_test_trap_subprocess ("/gsettings/enums:non-enum-value",
1721 0, G_TEST_TRAP_SILENCE_STDERR);
1722 g_test_trap_assert_failed ();
1723 g_test_trap_assert_stderr ("*invalid enum value 42*");
1725 g_test_trap_subprocess ("/gsettings/enums:range",
1726 0, G_TEST_TRAP_SILENCE_STDERR);
1727 g_test_trap_assert_failed ();
1728 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1730 g_test_trap_subprocess ("/gsettings/enums:non-flags",
1731 0, G_TEST_TRAP_SILENCE_STDERR);
1732 g_test_trap_assert_failed ();
1733 g_test_trap_assert_stderr ("*not associated with a flags*");
1736 str = g_settings_get_string (settings, "test");
1737 g_assert_cmpstr (str, ==, "bar");
1740 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
1742 str = g_settings_get_string (settings, "test");
1743 g_assert_cmpstr (str, ==, "foo");
1746 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
1748 g_settings_set_string (direct, "test", "qux");
1750 str = g_settings_get_string (direct, "test");
1751 g_assert_cmpstr (str, ==, "qux");
1754 str = g_settings_get_string (settings, "test");
1755 g_assert_cmpstr (str, ==, "quux");
1758 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
1762 test_flags_non_flags_key (void)
1766 direct = g_settings_new ("org.gtk.test.enums.direct");
1767 g_settings_get_flags (direct, "test");
1768 g_assert_not_reached ();
1772 test_flags_non_flags_value (void)
1774 GSettings *settings;
1776 settings = g_settings_new ("org.gtk.test.enums");
1777 g_settings_set_flags (settings, "f-test", 0x42);
1778 g_assert_not_reached ();
1782 test_flags_range (void)
1784 GSettings *settings;
1786 settings = g_settings_new ("org.gtk.test.enums");
1787 g_settings_set_strv (settings, "f-test",
1788 (const gchar **) g_strsplit ("rock", ",", 0));
1789 g_assert_not_reached ();
1793 test_flags_non_enum (void)
1795 GSettings *settings;
1797 settings = g_settings_new ("org.gtk.test.enums");
1798 g_settings_get_enum (settings, "f-test");
1799 g_assert_not_reached ();
1805 GSettings *settings, *direct;
1809 settings = g_settings_new ("org.gtk.test.enums");
1810 direct = g_settings_new ("org.gtk.test.enums.direct");
1812 if (g_test_undefined () && !backend_set)
1814 g_test_trap_subprocess ("/gsettings/flags:non-flags-key",
1815 0, G_TEST_TRAP_SILENCE_STDERR);
1816 g_test_trap_assert_failed ();
1817 g_test_trap_assert_stderr ("*not associated with a flags*");
1819 g_test_trap_subprocess ("/gsettings/flags:non-flags-value",
1820 0, G_TEST_TRAP_SILENCE_STDERR);
1821 g_test_trap_assert_failed ();
1822 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
1824 g_test_trap_subprocess ("/gsettings/flags:range",
1825 0, G_TEST_TRAP_SILENCE_STDERR);
1826 g_test_trap_assert_failed ();
1827 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1829 g_test_trap_subprocess ("/gsettings/flags:non-enum",
1830 0, G_TEST_TRAP_SILENCE_STDERR);
1831 g_test_trap_assert_failed ();
1832 g_test_trap_assert_stderr ("*not associated with an enum*");
1835 strv = g_settings_get_strv (settings, "f-test");
1836 str = g_strjoinv (",", strv);
1837 g_assert_cmpstr (str, ==, "");
1841 g_settings_set_flags (settings, "f-test",
1842 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1844 strv = g_settings_get_strv (settings, "f-test");
1845 str = g_strjoinv (",", strv);
1846 g_assert_cmpstr (str, ==, "talking,walking");
1850 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1851 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1853 strv = g_strsplit ("speaking,laughing", ",", 0);
1854 g_settings_set_strv (direct, "f-test", (const gchar **) strv);
1857 strv = g_settings_get_strv (direct, "f-test");
1858 str = g_strjoinv (",", strv);
1859 g_assert_cmpstr (str, ==, "speaking,laughing");
1863 strv = g_settings_get_strv (settings, "f-test");
1864 str = g_strjoinv (",", strv);
1865 g_assert_cmpstr (str, ==, "talking,laughing");
1869 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1870 TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
1874 test_range_high (void)
1876 GSettings *settings;
1878 settings = g_settings_new ("org.gtk.test.range");
1879 g_settings_set_int (settings, "val", 45);
1880 g_assert_not_reached ();
1884 test_range_low (void)
1886 GSettings *settings;
1888 settings = g_settings_new ("org.gtk.test.range");
1889 g_settings_set_int (settings, "val", 1);
1890 g_assert_not_reached ();
1896 GSettings *settings, *direct;
1899 settings = g_settings_new ("org.gtk.test.range");
1900 direct = g_settings_new ("org.gtk.test.range.direct");
1902 if (g_test_undefined () && !backend_set)
1904 g_test_trap_subprocess ("/gsettings/range:high",
1905 0, G_TEST_TRAP_SILENCE_STDERR);
1906 g_test_trap_assert_failed ();
1907 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1909 g_test_trap_subprocess ("/gsettings/range:low",
1910 0, G_TEST_TRAP_SILENCE_STDERR);
1911 g_test_trap_assert_failed ();
1912 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1915 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1916 g_settings_set_int (direct, "val", 22);
1917 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
1918 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
1919 g_settings_set_int (direct, "val", 45);
1920 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
1921 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1922 g_settings_set_int (direct, "val", 1);
1923 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
1924 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1926 value = g_variant_new_int32 (1);
1927 g_assert (!g_settings_range_check (settings, "val", value));
1928 g_variant_unref (value);
1929 value = g_variant_new_int32 (33);
1930 g_assert (g_settings_range_check (settings, "val", value));
1931 g_variant_unref (value);
1932 value = g_variant_new_int32 (45);
1933 g_assert (!g_settings_range_check (settings, "val", value));
1934 g_variant_unref (value);
1938 strv_has_string (gchar **haystack,
1939 const gchar *needle)
1943 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
1945 if (g_strcmp0 (haystack[n], needle) == 0)
1952 strv_set_equal (gchar **strv, ...)
1961 va_start (list, strv);
1964 str = va_arg (list, const gchar *);
1967 if (!strv_has_string (strv, str))
1977 res = g_strv_length ((gchar**)strv) == count;
1983 test_list_items (void)
1985 GSettings *settings;
1989 settings = g_settings_new ("org.gtk.test");
1990 children = g_settings_list_children (settings);
1991 keys = g_settings_list_keys (settings);
1993 g_assert (strv_set_equal (children, "basic-types", "complex-types", "localized", NULL));
1994 g_assert (strv_set_equal (keys, "greeting", "farewell", NULL));
1996 g_strfreev (children);
1999 g_object_unref (settings);
2003 test_list_schemas (void)
2005 const gchar * const *schemas;
2006 const gchar * const *relocs;
2008 relocs = g_settings_list_relocatable_schemas ();
2009 schemas = g_settings_list_schemas ();
2011 g_assert (strv_set_equal ((gchar **)relocs,
2012 "org.gtk.test.no-path",
2015 g_assert (strv_set_equal ((gchar **)schemas,
2017 "org.gtk.test.basic-types",
2018 "org.gtk.test.complex-types",
2019 "org.gtk.test.localized",
2020 "org.gtk.test.binding",
2021 "org.gtk.test.enums",
2022 "org.gtk.test.enums.direct",
2023 "org.gtk.test.range",
2024 "org.gtk.test.range.direct",
2025 "org.gtk.test.mapped",
2030 map_func (GVariant *value,
2034 gint *state = user_data;
2038 v = g_variant_get_int32 (value);
2044 g_assert_cmpint (v, ==, 1);
2048 else if (*state == 1)
2050 g_assert_cmpint (v, ==, 0);
2056 g_assert (value == NULL);
2057 *result = g_variant_new_int32 (5);
2063 test_get_mapped (void)
2065 GSettings *settings;
2070 settings = g_settings_new ("org.gtk.test.mapped");
2071 g_settings_set_int (settings, "val", 1);
2074 p = g_settings_get_mapped (settings, "val", map_func, &state);
2075 val = g_variant_get_int32 ((GVariant*)p);
2076 g_assert_cmpint (val, ==, 5);
2078 g_variant_unref (p);
2079 g_object_unref (settings);
2083 test_get_range (void)
2085 GSettings *settings;
2088 settings = g_settings_new ("org.gtk.test.range");
2089 range = g_settings_get_range (settings, "val");
2090 check_and_free (range, "('range', <(2, 44)>)");
2091 g_object_unref (settings);
2093 settings = g_settings_new ("org.gtk.test.enums");
2094 range = g_settings_get_range (settings, "test");
2095 check_and_free (range, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
2096 g_object_unref (settings);
2098 settings = g_settings_new ("org.gtk.test.enums");
2099 range = g_settings_get_range (settings, "f-test");
2100 check_and_free (range, "('flags', "
2101 "<['mourning', 'laughing', 'talking', 'walking']>)");
2102 g_object_unref (settings);
2104 settings = g_settings_new ("org.gtk.test");
2105 range = g_settings_get_range (settings, "greeting");
2106 check_and_free (range, "('type', <@as []>)");
2107 g_object_unref (settings);
2111 test_schema_source (void)
2113 GSettingsSchemaSource *parent;
2114 GSettingsSchemaSource *source;
2115 GSettingsBackend *backend;
2116 GSettingsSchema *schema;
2117 GError *error = NULL;
2118 GSettings *settings;
2121 backend = g_settings_backend_get_default ();
2123 /* make sure it fails properly */
2124 parent = g_settings_schema_source_get_default ();
2125 source = g_settings_schema_source_new_from_directory ("/path/that/does/not/exist", parent, TRUE, &error);
2126 g_assert (source == NULL);
2127 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
2128 g_clear_error (&error);
2130 /* create a source with the parent */
2131 source = g_settings_schema_source_new_from_directory ("schema-source", parent, TRUE, &error);
2132 g_assert_no_error (error);
2133 g_assert (source != NULL);
2135 /* check recursive lookups are working */
2136 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2137 g_assert (schema != NULL);
2138 g_settings_schema_unref (schema);
2140 /* check recursive lookups for non-existent schemas */
2141 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", TRUE);
2142 g_assert (schema == NULL);
2144 /* check non-recursive for schema that only exists in lower layers */
2145 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2146 g_assert (schema == NULL);
2148 /* check non-recursive lookup for non-existent */
2149 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", FALSE);
2150 g_assert (schema == NULL);
2152 /* check non-recursive for schema that exists in toplevel */
2153 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2154 g_assert (schema != NULL);
2155 g_settings_schema_unref (schema);
2157 /* check recursive for schema that exists in toplevel */
2158 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2159 g_assert (schema != NULL);
2161 /* try to use it for something */
2162 settings = g_settings_new_full (schema, backend, g_settings_schema_get_path (schema));
2163 g_settings_schema_unref (schema);
2165 g_settings_get (settings, "enabled", "b", &enabled);
2167 g_object_unref (settings);
2169 g_settings_schema_source_unref (source);
2171 /* try again, but with no parent */
2172 source = g_settings_schema_source_new_from_directory ("schema-source", NULL, FALSE, NULL);
2173 g_assert (source != NULL);
2175 /* should not find it this time, even if recursive... */
2176 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2177 g_assert (schema == NULL);
2178 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2179 g_assert (schema == NULL);
2181 /* should still find our own... */
2182 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2183 g_assert (schema != NULL);
2184 g_settings_schema_unref (schema);
2185 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2186 g_assert (schema != NULL);
2187 g_settings_schema_unref (schema);
2189 g_settings_schema_source_unref (source);
2195 GAction *string, *toggle;
2196 gboolean c1, c2, c3;
2197 GSettings *settings;
2199 GVariantType *param_type;
2201 GVariantType *state_type;
2204 settings = g_settings_new ("org.gtk.test.basic-types");
2205 string = g_settings_create_action (settings, "test-string");
2206 toggle = g_settings_create_action (settings, "test-boolean");
2207 g_object_unref (settings); /* should be held by the actions */
2209 g_signal_connect (settings, "changed", G_CALLBACK (changed_cb2), &c1);
2210 g_signal_connect (string, "notify::state", G_CALLBACK (changed_cb2), &c2);
2211 g_signal_connect (toggle, "notify::state", G_CALLBACK (changed_cb2), &c3);
2213 c1 = c2 = c3 = FALSE;
2214 g_settings_set_string (settings, "test-string", "hello world");
2215 check_and_free (g_action_get_state (string), "'hello world'");
2216 g_assert (c1 && c2 && !c3);
2217 c1 = c2 = c3 = FALSE;
2219 g_action_activate (string, g_variant_new_string ("hihi"));
2220 check_and_free (g_settings_get_value (settings, "test-string"), "'hihi'");
2221 g_assert (c1 && c2 && !c3);
2222 c1 = c2 = c3 = FALSE;
2224 g_action_change_state (string, g_variant_new_string ("kthxbye"));
2225 check_and_free (g_settings_get_value (settings, "test-string"), "'kthxbye'");
2226 g_assert (c1 && c2 && !c3);
2227 c1 = c2 = c3 = FALSE;
2229 g_action_change_state (toggle, g_variant_new_boolean (TRUE));
2230 g_assert (g_settings_get_boolean (settings, "test-boolean"));
2231 g_assert (c1 && !c2 && c3);
2232 c1 = c2 = c3 = FALSE;
2234 g_action_activate (toggle, NULL);
2235 g_assert (!g_settings_get_boolean (settings, "test-boolean"));
2236 g_assert (c1 && !c2 && c3);
2238 g_object_get (string,
2240 "parameter-type", ¶m_type,
2241 "enabled", &enabled,
2242 "state-type", &state_type,
2246 g_assert_cmpstr (name, ==, "test-string");
2247 g_assert (g_variant_type_equal (param_type, G_VARIANT_TYPE_STRING));
2249 g_assert (g_variant_type_equal (state_type, G_VARIANT_TYPE_STRING));
2250 g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "kthxbye");
2253 g_variant_unref (state);
2255 g_object_unref (string);
2256 g_object_unref (toggle);
2260 main (int argc, char *argv[])
2265 setlocale (LC_ALL, "");
2267 g_test_init (&argc, &argv, NULL);
2269 if (!g_test_subprocess ())
2271 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
2273 g_setenv ("XDG_DATA_DIRS", ".", TRUE);
2274 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
2277 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
2279 g_remove ("org.gtk.test.enums.xml");
2280 g_assert (g_spawn_command_line_sync ("../../gobject/glib-mkenums "
2281 "--template " SRCDIR "/enums.xml.template "
2282 SRCDIR "/testenum.h",
2283 &enums, NULL, &result, NULL));
2284 g_assert (result == 0);
2285 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
2288 g_remove ("gschemas.compiled");
2289 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=. "
2290 "--schema-file=org.gtk.test.enums.xml "
2291 "--schema-file=" SRCDIR "/org.gtk.test.gschema.xml",
2292 NULL, NULL, &result, NULL));
2293 g_assert (result == 0);
2295 g_remove ("schema-source/gschemas.compiled");
2296 g_mkdir ("schema-source", 0777);
2297 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=schema-source "
2298 "--schema-file=" SRCDIR "/org.gtk.schemasourcecheck.gschema.xml",
2299 NULL, NULL, &result, NULL));
2300 g_assert (result == 0);
2303 g_test_add_func ("/gsettings/basic", test_basic);
2307 g_test_add_func ("/gsettings/no-schema", test_no_schema);
2308 g_test_add_func ("/gsettings/no-schema:child", test_no_schema_child);
2309 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
2310 g_test_add_func ("/gsettings/unknown-key:child", test_unknown_key_child);
2311 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
2312 g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
2313 g_test_add_func ("/gsettings/wrong-path:child", test_wrong_path_child);
2314 g_test_add_func ("/gsettings/no-path", test_no_path);
2315 g_test_add_func ("/gsettings/no-path:child", test_no_path_child);
2318 g_test_add_func ("/gsettings/basic-types", test_basic_types);
2319 g_test_add_func ("/gsettings/complex-types", test_complex_types);
2320 g_test_add_func ("/gsettings/changes", test_changes);
2322 if (glib_translations_work ())
2324 g_test_add_func ("/gsettings/l10n", test_l10n);
2325 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
2328 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
2329 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
2330 g_test_add_func ("/gsettings/atomic", test_atomic);
2332 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
2333 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
2334 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
2335 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
2336 g_test_add_func ("/gsettings/unbinding", test_unbind);
2337 g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
2341 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
2342 g_test_add_func ("/gsettings/typesafe-binding:child", test_typesafe_binding_child);
2343 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
2344 g_test_add_func ("/gsettings/no-read-binding:fail", test_no_read_binding_fail);
2345 g_test_add_func ("/gsettings/no-read-binding:pass", test_no_read_binding_pass);
2346 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
2347 g_test_add_func ("/gsettings/no-write-binding:fail", test_no_write_binding_fail);
2348 g_test_add_func ("/gsettings/no-write-binding:pass", test_no_write_binding_pass);
2351 g_test_add_func ("/gsettings/keyfile", test_keyfile);
2352 g_test_add_func ("/gsettings/child-schema", test_child_schema);
2353 g_test_add_func ("/gsettings/strinfo", test_strinfo);
2354 g_test_add_func ("/gsettings/enums", test_enums);
2355 g_test_add_func ("/gsettings/enums:non-enum-key", test_enums_non_enum_key);
2356 g_test_add_func ("/gsettings/enums:non-enum-value", test_enums_non_enum_value);
2357 g_test_add_func ("/gsettings/enums:range", test_enums_range);
2358 g_test_add_func ("/gsettings/enums:non-flags", test_enums_non_flags);
2359 g_test_add_func ("/gsettings/flags", test_flags);
2360 g_test_add_func ("/gsettings/flags:non-flags-key", test_flags_non_flags_key);
2361 g_test_add_func ("/gsettings/flags:non-flags-value", test_flags_non_flags_value);
2362 g_test_add_func ("/gsettings/flags:range", test_flags_range);
2363 g_test_add_func ("/gsettings/flags:non-enum", test_flags_non_enum);
2364 g_test_add_func ("/gsettings/range", test_range);
2365 g_test_add_func ("/gsettings/range:high", test_range_high);
2366 g_test_add_func ("/gsettings/range:low", test_range_low);
2367 g_test_add_func ("/gsettings/list-items", test_list_items);
2368 g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
2369 g_test_add_func ("/gsettings/mapped", test_get_mapped);
2370 g_test_add_func ("/gsettings/get-range", test_get_range);
2371 g_test_add_func ("/gsettings/schema-source", test_schema_source);
2372 g_test_add_func ("/gsettings/actions", test_actions);
2374 result = g_test_run ();