6 #define G_SETTINGS_ENABLE_BACKEND
7 #include <gio/gsettingsbackend.h>
11 static const gchar *datapath;
12 static gboolean backend_set;
14 /* These tests rely on the schemas in org.gtk.test.gschema.xml
15 * to be compiled and installed in the same directory.
19 check_and_free (GVariant *value,
20 const gchar *expected)
24 printed = g_variant_print (value, TRUE);
25 g_assert_cmpstr (printed, ==, expected);
28 g_variant_unref (value);
32 /* Just to get warmed up: Read and set a string, and
33 * verify that can read the changed string back
41 settings = g_settings_new ("org.gtk.test");
43 g_object_get (settings, "schema", &str, NULL);
44 g_assert_cmpstr (str, ==, "org.gtk.test");
47 g_settings_get (settings, "greeting", "s", &str);
48 g_assert_cmpstr (str, ==, "Hello, earthlings");
51 g_settings_set (settings, "greeting", "s", "goodbye world");
52 g_settings_get (settings, "greeting", "s", &str);
53 g_assert_cmpstr (str, ==, "goodbye world");
57 if (!backend_set && g_test_undefined ())
59 GSettings *tmp_settings = g_settings_new ("org.gtk.test");
61 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
62 "*g_settings_set_value*expects type*");
63 g_settings_set (tmp_settings, "greeting", "i", 555);
64 g_test_assert_expected_messages ();
66 g_object_unref (tmp_settings);
69 g_settings_get (settings, "greeting", "s", &str);
70 g_assert_cmpstr (str, ==, "goodbye world");
74 g_settings_reset (settings, "greeting");
75 str = g_settings_get_string (settings, "greeting");
76 g_assert_cmpstr (str, ==, "Hello, earthlings");
79 g_settings_set (settings, "greeting", "s", "this is the end");
80 g_object_unref (settings);
83 /* Check that we get an error when getting a key
84 * that is not in the schema
87 test_unknown_key_subprocess (void)
92 settings = g_settings_new ("org.gtk.test");
93 value = g_settings_get_value (settings, "no_such_key");
95 g_assert (value == NULL);
97 g_object_unref (settings);
101 test_unknown_key (void)
103 if (!g_test_undefined ())
106 g_test_trap_subprocess ("/gsettings/unknown-key/subprocess", 0, 0);
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_subprocess (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/subprocess", 0, 0);
131 g_test_trap_assert_failed ();
132 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
135 /* Check that we get an error when passing a type string
136 * that does not match the schema
139 test_wrong_type (void)
144 if (!g_test_undefined ())
147 settings = g_settings_new ("org.gtk.test");
149 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
150 "*given value has a type of*");
151 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
152 "*valid_format_string*");
153 g_settings_get (settings, "greeting", "o", &str);
154 g_test_assert_expected_messages ();
156 g_assert (str == NULL);
158 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
159 "*expects type 's'*");
160 g_settings_set (settings, "greeting", "o", "/a/path");
161 g_test_assert_expected_messages ();
163 g_object_unref (settings);
166 /* Check errors with explicit paths */
168 test_wrong_path_subprocess (void)
170 GSettings *settings G_GNUC_UNUSED;
172 settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
176 test_wrong_path (void)
178 if (!g_test_undefined ())
181 g_test_trap_subprocess ("/gsettings/wrong-path/subprocess", 0, 0);
182 g_test_trap_assert_failed ();
183 g_test_trap_assert_stderr ("*but path * specified by schema*");
187 test_no_path_subprocess (void)
189 GSettings *settings G_GNUC_UNUSED;
191 settings = g_settings_new ("org.gtk.test.no-path");
197 if (!g_test_undefined ())
200 g_test_trap_subprocess ("/gsettings/no-path/subprocess", 0, 0);
201 g_test_trap_assert_failed ();
202 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
206 /* Check that we can successfully read and set the full
207 * range of all basic types
210 test_basic_types (void)
224 settings = g_settings_new ("org.gtk.test.basic-types");
226 g_settings_get (settings, "test-boolean", "b", &b);
227 g_assert_cmpint (b, ==, 1);
229 g_settings_set (settings, "test-boolean", "b", 0);
230 g_settings_get (settings, "test-boolean", "b", &b);
231 g_assert_cmpint (b, ==, 0);
233 g_settings_get (settings, "test-byte", "y", &byte);
234 g_assert_cmpint (byte, ==, 25);
236 g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
237 g_settings_get (settings, "test-byte", "y", &byte);
238 g_assert_cmpint (byte, ==, G_MAXUINT8);
240 g_settings_get (settings, "test-int16", "n", &i16);
241 g_assert_cmpint (i16, ==, -1234);
243 g_settings_set (settings, "test-int16", "n", G_MININT16);
244 g_settings_get (settings, "test-int16", "n", &i16);
245 g_assert_cmpint (i16, ==, G_MININT16);
247 g_settings_set (settings, "test-int16", "n", G_MAXINT16);
248 g_settings_get (settings, "test-int16", "n", &i16);
249 g_assert_cmpint (i16, ==, G_MAXINT16);
251 g_settings_get (settings, "test-uint16", "q", &u16);
252 g_assert_cmpuint (u16, ==, 1234);
254 g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
255 g_settings_get (settings, "test-uint16", "q", &u16);
256 g_assert_cmpuint (u16, ==, G_MAXUINT16);
258 g_settings_get (settings, "test-int32", "i", &i32);
259 g_assert_cmpint (i32, ==, -123456);
261 g_settings_set (settings, "test-int32", "i", G_MININT32);
262 g_settings_get (settings, "test-int32", "i", &i32);
263 g_assert_cmpint (i32, ==, G_MININT32);
265 g_settings_set (settings, "test-int32", "i", G_MAXINT32);
266 g_settings_get (settings, "test-int32", "i", &i32);
267 g_assert_cmpint (i32, ==, G_MAXINT32);
269 g_settings_get (settings, "test-uint32", "u", &u32);
270 g_assert_cmpuint (u32, ==, 123456);
272 g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
273 g_settings_get (settings, "test-uint32", "u", &u32);
274 g_assert_cmpuint (u32, ==, G_MAXUINT32);
276 g_settings_get (settings, "test-int64", "x", &i64);
277 g_assert_cmpuint (i64, ==, -123456789);
279 g_settings_set (settings, "test-int64", "x", G_MININT64);
280 g_settings_get (settings, "test-int64", "x", &i64);
281 g_assert_cmpuint (i64, ==, G_MININT64);
283 g_settings_set (settings, "test-int64", "x", G_MAXINT64);
284 g_settings_get (settings, "test-int64", "x", &i64);
285 g_assert_cmpuint (i64, ==, G_MAXINT64);
287 g_settings_get (settings, "test-uint64", "t", &u64);
288 g_assert_cmpuint (u64, ==, 123456789);
290 g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
291 g_settings_get (settings, "test-uint64", "t", &u64);
292 g_assert_cmpuint (u64, ==, G_MAXUINT64);
294 g_settings_get (settings, "test-double", "d", &d);
295 g_assert_cmpfloat (d, ==, 123.456);
297 g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
298 g_settings_get (settings, "test-double", "d", &d);
299 g_assert_cmpfloat (d, ==, G_MINDOUBLE);
301 g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
302 g_settings_get (settings, "test-double", "d", &d);
303 g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
305 g_settings_get (settings, "test-string", "s", &str);
306 g_assert_cmpstr (str, ==, "a string, it seems");
310 g_settings_get (settings, "test-objectpath", "o", &str);
311 g_assert_cmpstr (str, ==, "/a/object/path");
312 g_object_unref (settings);
317 /* Check that we can read an set complex types like
318 * tuples, arrays and dictionaries
321 test_complex_types (void)
326 GVariantIter *iter = NULL;
328 settings = g_settings_new ("org.gtk.test.complex-types");
330 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
331 g_assert_cmpstr (s, ==, "one");
332 g_assert_cmpint (i1,==, 2);
333 g_assert_cmpint (i2,==, 3);
337 g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
338 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
339 g_assert_cmpstr (s, ==, "none");
340 g_assert_cmpint (i1,==, 0);
341 g_assert_cmpint (i2,==, 0);
345 g_settings_get (settings, "test-array", "ai", &iter);
346 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
347 g_assert (g_variant_iter_next (iter, "i", &i1));
348 g_assert_cmpint (i1, ==, 0);
349 g_assert (g_variant_iter_next (iter, "i", &i1));
350 g_assert_cmpint (i1, ==, 1);
351 g_assert (g_variant_iter_next (iter, "i", &i1));
352 g_assert_cmpint (i1, ==, 2);
353 g_assert (g_variant_iter_next (iter, "i", &i1));
354 g_assert_cmpint (i1, ==, 3);
355 g_assert (g_variant_iter_next (iter, "i", &i1));
356 g_assert_cmpint (i1, ==, 4);
357 g_assert (g_variant_iter_next (iter, "i", &i1));
358 g_assert_cmpint (i1, ==, 5);
359 g_assert (!g_variant_iter_next (iter, "i", &i1));
360 g_variant_iter_free (iter);
362 g_object_unref (settings);
365 static gboolean changed_cb_called;
368 changed_cb (GSettings *settings,
372 changed_cb_called = TRUE;
374 g_assert_cmpstr (key, ==, data);
377 /* Test that basic change notification with the changed signal works.
383 GSettings *settings2;
385 settings = g_settings_new ("org.gtk.test");
387 g_signal_connect (settings, "changed",
388 G_CALLBACK (changed_cb), "greeting");
390 changed_cb_called = FALSE;
392 g_settings_set (settings, "greeting", "s", "new greeting");
393 g_assert (changed_cb_called);
395 settings2 = g_settings_new ("org.gtk.test");
397 changed_cb_called = FALSE;
399 g_settings_set (settings2, "greeting", "s", "hi");
400 g_assert (changed_cb_called);
402 g_object_unref (settings2);
403 g_object_unref (settings);
406 static gboolean changed_cb_called2;
409 changed_cb2 (GSettings *settings,
418 /* Test that changes done to a delay-mode instance
419 * don't appear to the outside world until apply. Also
420 * check that we get change notification when they are
422 * Also test that the has-unapplied property is properly
426 test_delay_apply (void)
429 GSettings *settings2;
432 settings = g_settings_new ("org.gtk.test");
433 settings2 = g_settings_new ("org.gtk.test");
435 g_settings_set (settings2, "greeting", "s", "top o' the morning");
437 changed_cb_called = FALSE;
438 changed_cb_called2 = FALSE;
440 g_signal_connect (settings, "changed",
441 G_CALLBACK (changed_cb2), &changed_cb_called);
442 g_signal_connect (settings2, "changed",
443 G_CALLBACK (changed_cb2), &changed_cb_called2);
445 g_settings_delay (settings);
447 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
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, ==, "top o' the morning");
462 g_assert (g_settings_get_has_unapplied (settings));
463 g_assert (!g_settings_get_has_unapplied (settings2));
465 changed_cb_called = FALSE;
466 changed_cb_called2 = FALSE;
468 g_settings_apply (settings);
470 g_assert (!changed_cb_called);
471 g_assert (changed_cb_called2);
473 g_settings_get (settings, "greeting", "s", &str);
474 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
478 g_settings_get (settings2, "greeting", "s", &str);
479 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
483 g_assert (!g_settings_get_has_unapplied (settings));
484 g_assert (!g_settings_get_has_unapplied (settings2));
486 g_object_unref (settings2);
487 g_object_unref (settings);
490 /* Test that reverting unapplied changes in a delay-apply
491 * settings instance works.
494 test_delay_revert (void)
497 GSettings *settings2;
500 settings = g_settings_new ("org.gtk.test");
501 settings2 = g_settings_new ("org.gtk.test");
503 g_settings_set (settings2, "greeting", "s", "top o' the morning");
505 g_settings_delay (settings);
507 g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
509 g_settings_get (settings, "greeting", "s", &str);
510 g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
514 g_settings_get (settings2, "greeting", "s", &str);
515 g_assert_cmpstr (str, ==, "top o' the morning");
519 g_assert (g_settings_get_has_unapplied (settings));
521 g_settings_revert (settings);
523 g_assert (!g_settings_get_has_unapplied (settings));
525 g_settings_get (settings, "greeting", "s", &str);
526 g_assert_cmpstr (str, ==, "top o' the morning");
530 g_settings_get (settings2, "greeting", "s", &str);
531 g_assert_cmpstr (str, ==, "top o' the morning");
535 g_object_unref (settings2);
536 g_object_unref (settings);
540 keys_changed_cb (GSettings *settings,
546 g_assert_cmpint (n_keys, ==, 2);
548 g_assert ((keys[0] == g_quark_from_static_string ("greeting") &&
549 keys[1] == g_quark_from_static_string ("farewell")) ||
550 (keys[1] == g_quark_from_static_string ("greeting") &&
551 keys[0] == g_quark_from_static_string ("farewell")));
553 g_settings_get (settings, "greeting", "s", &str);
554 g_assert_cmpstr (str, ==, "greetings from test_atomic");
558 g_settings_get (settings, "farewell", "s", &str);
559 g_assert_cmpstr (str, ==, "atomic bye-bye");
564 /* Check that delay-applied changes appear atomically.
565 * More specifically, verify that all changed keys appear
566 * with their new value while handling the change-event signal.
572 GSettings *settings2;
575 settings = g_settings_new ("org.gtk.test");
576 settings2 = g_settings_new ("org.gtk.test");
578 g_settings_set (settings2, "greeting", "s", "top o' the morning");
580 changed_cb_called = FALSE;
581 changed_cb_called2 = FALSE;
583 g_signal_connect (settings2, "change-event",
584 G_CALLBACK (keys_changed_cb), NULL);
586 g_settings_delay (settings);
588 g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
589 g_settings_set (settings, "farewell", "s", "atomic bye-bye");
591 g_settings_apply (settings);
593 g_settings_get (settings, "greeting", "s", &str);
594 g_assert_cmpstr (str, ==, "greetings from test_atomic");
598 g_settings_get (settings, "farewell", "s", &str);
599 g_assert_cmpstr (str, ==, "atomic bye-bye");
603 g_settings_get (settings2, "greeting", "s", &str);
604 g_assert_cmpstr (str, ==, "greetings from test_atomic");
608 g_settings_get (settings2, "farewell", "s", &str);
609 g_assert_cmpstr (str, ==, "atomic bye-bye");
613 g_object_unref (settings2);
614 g_object_unref (settings);
617 /* On Windows the interaction between the C library locale and libintl
618 * (from GNU gettext) is not like on POSIX, so just skip these tests
621 * There are several issues:
623 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
624 * in libintl (defined in its <libintl.h>).
626 * 2) The locale names that setlocale() accepts and returns aren't in
627 * the "de_DE" style, but like "German_Germany".
629 * 3) libintl looks at the Win32 thread locale and not the C library
630 * locale. (And even if libintl would use the C library's locale, as
631 * there are several alternative C library DLLs, libintl might be
632 * linked to a different one than the application code, so they
633 * wouldn't have the same C library locale anyway.)
636 /* Test that translations work for schema defaults.
638 * This test relies on the de.po file in the same directory
639 * to be compiled into ./de/LC_MESSAGES/test.mo
648 bindtextdomain ("test", ".");
649 bind_textdomain_codeset ("test", "UTF-8");
651 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
653 settings = g_settings_new ("org.gtk.test.localized");
655 setlocale (LC_MESSAGES, "C");
656 str = g_settings_get_string (settings, "error-message");
657 setlocale (LC_MESSAGES, locale);
659 g_assert_cmpstr (str, ==, "Unnamed");
663 setlocale (LC_MESSAGES, "de_DE");
664 str = g_settings_get_string (settings, "error-message");
665 setlocale (LC_MESSAGES, locale);
667 g_assert_cmpstr (str, ==, "Unbenannt");
668 g_object_unref (settings);
675 /* Test that message context works as expected with translated
676 * schema defaults. Also, verify that non-ASCII UTF-8 content
679 * This test relies on the de.po file in the same directory
680 * to be compiled into ./de/LC_MESSAGES/test.mo
683 test_l10n_context (void)
689 bindtextdomain ("test", ".");
690 bind_textdomain_codeset ("test", "UTF-8");
692 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
694 settings = g_settings_new ("org.gtk.test.localized");
696 setlocale (LC_MESSAGES, "C");
697 g_settings_get (settings, "backspace", "s", &str);
698 setlocale (LC_MESSAGES, locale);
700 g_assert_cmpstr (str, ==, "BackSpace");
704 setlocale (LC_MESSAGES, "de_DE");
705 g_settings_get (settings, "backspace", "s", &str);
706 setlocale (LC_MESSAGES, locale);
708 g_assert_cmpstr (str, ==, "Löschen");
709 g_object_unref (settings);
739 GObject parent_instance;
742 gboolean anti_bool_prop;
753 gchar *no_write_prop;
761 GObjectClass parent_class;
764 static GType test_object_get_type (void);
765 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
768 test_object_init (TestObject *object)
773 test_object_finalize (GObject *object)
775 TestObject *testo = (TestObject*)object;
776 g_strfreev (testo->strv_prop);
777 g_free (testo->string_prop);
778 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
782 test_object_get_property (GObject *object,
787 TestObject *test_object = (TestObject *)object;
792 g_value_set_boolean (value, test_object->bool_prop);
795 g_value_set_boolean (value, test_object->anti_bool_prop);
798 g_value_set_schar (value, test_object->byte_prop);
801 g_value_set_uint (value, test_object->uint16_prop);
804 g_value_set_int (value, test_object->int16_prop);
807 g_value_set_int (value, test_object->int_prop);
810 g_value_set_uint (value, test_object->uint_prop);
813 g_value_set_int64 (value, test_object->int64_prop);
816 g_value_set_uint64 (value, test_object->uint64_prop);
819 g_value_set_double (value, test_object->double_prop);
822 g_value_set_string (value, test_object->string_prop);
825 g_value_set_string (value, test_object->no_write_prop);
828 g_value_set_boxed (value, test_object->strv_prop);
831 g_value_set_enum (value, test_object->enum_prop);
834 g_value_set_flags (value, test_object->flags_prop);
837 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
843 test_object_set_property (GObject *object,
848 TestObject *test_object = (TestObject *)object;
853 test_object->bool_prop = g_value_get_boolean (value);
856 test_object->anti_bool_prop = g_value_get_boolean (value);
859 test_object->byte_prop = g_value_get_schar (value);
862 test_object->int16_prop = g_value_get_int (value);
865 test_object->uint16_prop = g_value_get_uint (value);
868 test_object->int_prop = g_value_get_int (value);
871 test_object->uint_prop = g_value_get_uint (value);
874 test_object->int64_prop = g_value_get_int64 (value);
877 test_object->uint64_prop = g_value_get_uint64 (value);
880 test_object->double_prop = g_value_get_double (value);
883 g_free (test_object->string_prop);
884 test_object->string_prop = g_value_dup_string (value);
887 g_free (test_object->no_read_prop);
888 test_object->no_read_prop = g_value_dup_string (value);
891 g_strfreev (test_object->strv_prop);
892 test_object->strv_prop = g_value_dup_boxed (value);
895 test_object->enum_prop = g_value_get_enum (value);
898 test_object->flags_prop = g_value_get_flags (value);
901 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
907 test_enum_get_type (void)
909 static volatile gsize define_type_id = 0;
911 if (g_once_init_enter (&define_type_id))
913 static const GEnumValue values[] = {
914 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
915 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
916 { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
917 { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
921 GType type_id = g_enum_register_static ("TestEnum", values);
922 g_once_init_leave (&define_type_id, type_id);
925 return define_type_id;
929 test_flags_get_type (void)
931 static volatile gsize define_type_id = 0;
933 if (g_once_init_enter (&define_type_id))
935 static const GFlagsValue values[] = {
936 { TEST_FLAGS_NONE, "TEST_FLAGS_NONE", "none" },
937 { TEST_FLAGS_MOURNING, "TEST_FLAGS_MOURNING", "mourning" },
938 { TEST_FLAGS_LAUGHING, "TEST_FLAGS_LAUGHING", "laughing" },
939 { TEST_FLAGS_WALKING, "TEST_FLAGS_WALKING", "walking" },
943 GType type_id = g_flags_register_static ("TestFlags", values);
944 g_once_init_leave (&define_type_id, type_id);
947 return define_type_id;
951 test_object_class_init (TestObjectClass *class)
953 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
955 gobject_class->get_property = test_object_get_property;
956 gobject_class->set_property = test_object_set_property;
957 gobject_class->finalize = test_object_finalize;
959 g_object_class_install_property (gobject_class, PROP_BOOL,
960 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
961 g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
962 g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
963 g_object_class_install_property (gobject_class, PROP_BYTE,
964 g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
965 g_object_class_install_property (gobject_class, PROP_INT16,
966 g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
967 g_object_class_install_property (gobject_class, PROP_UINT16,
968 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
969 g_object_class_install_property (gobject_class, PROP_INT,
970 g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
971 g_object_class_install_property (gobject_class, PROP_UINT,
972 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
973 g_object_class_install_property (gobject_class, PROP_INT64,
974 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
975 g_object_class_install_property (gobject_class, PROP_UINT64,
976 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
977 g_object_class_install_property (gobject_class, PROP_DOUBLE,
978 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
979 g_object_class_install_property (gobject_class, PROP_STRING,
980 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
981 g_object_class_install_property (gobject_class, PROP_NO_WRITE,
982 g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
983 g_object_class_install_property (gobject_class, PROP_NO_READ,
984 g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
985 g_object_class_install_property (gobject_class, PROP_STRV,
986 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
987 g_object_class_install_property (gobject_class, PROP_ENUM,
988 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
989 g_object_class_install_property (gobject_class, PROP_FLAGS,
990 g_param_spec_flags ("flags", "", "", test_flags_get_type (), TEST_FLAGS_NONE, G_PARAM_READWRITE));
994 test_object_new (void)
996 return (TestObject*)g_object_new (test_object_get_type (), NULL);
999 /* Test basic binding functionality for simple types.
1000 * Verify that with bidirectional bindings, changes on either side
1001 * are notified on the other end.
1004 test_simple_binding (void)
1007 GSettings *settings;
1023 settings = g_settings_new ("org.gtk.test.binding");
1024 obj = test_object_new ();
1026 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
1027 g_object_set (obj, "bool", TRUE, NULL);
1028 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1030 g_settings_set_boolean (settings, "bool", FALSE);
1032 g_object_get (obj, "bool", &b, NULL);
1033 g_assert_cmpint (b, ==, FALSE);
1035 g_settings_bind (settings, "anti-bool", obj, "anti-bool",
1036 G_SETTINGS_BIND_INVERT_BOOLEAN);
1037 g_object_set (obj, "anti-bool", FALSE, NULL);
1038 g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
1040 g_settings_set_boolean (settings, "anti-bool", FALSE);
1042 g_object_get (obj, "anti-bool", &b, NULL);
1043 g_assert_cmpint (b, ==, TRUE);
1045 g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
1047 g_object_set (obj, "byte", 123, NULL);
1049 g_settings_get (settings, "byte", "y", &y);
1050 g_assert_cmpint (y, ==, 123);
1052 g_settings_set (settings, "byte", "y", 54);
1054 g_object_get (obj, "byte", &y, NULL);
1055 g_assert_cmpint (y, ==, 54);
1057 g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
1059 g_object_set (obj, "int16", 1234, NULL);
1061 g_settings_get (settings, "int16", "n", &n);
1062 g_assert_cmpint (n, ==, 1234);
1064 g_settings_set (settings, "int16", "n", 4321);
1066 g_object_get (obj, "int16", &n2, NULL);
1067 g_assert_cmpint (n2, ==, 4321);
1069 g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
1071 g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1073 g_settings_get (settings, "uint16", "q", &q);
1074 g_assert_cmpuint (q, ==, G_MAXUINT16);
1076 g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1078 g_object_get (obj, "uint16", &q2, NULL);
1079 g_assert_cmpuint (q2, ==, (guint16) G_MAXINT16);
1081 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1083 g_object_set (obj, "int", 12345, NULL);
1084 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1086 g_settings_set_int (settings, "int", 54321);
1088 g_object_get (obj, "int", &i, NULL);
1089 g_assert_cmpint (i, ==, 54321);
1091 g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1093 g_object_set (obj, "uint", 12345, NULL);
1094 g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);
1096 g_settings_set_uint (settings, "uint", 54321);
1098 g_object_get (obj, "uint", &u, NULL);
1099 g_assert_cmpuint (u, ==, 54321);
1101 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1103 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1105 g_settings_get (settings, "int64", "x", &i64);
1106 g_assert_cmpint (i64, ==, G_MAXINT64);
1108 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1110 g_object_get (obj, "int64", &i64, NULL);
1111 g_assert_cmpint (i64, ==, G_MININT64);
1113 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1115 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1117 g_settings_get (settings, "uint64", "t", &u64);
1118 g_assert_cmpuint (u64, ==, G_MAXUINT64);
1120 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1122 g_object_get (obj, "uint64", &u64, NULL);
1123 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1125 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1127 g_object_set (obj, "string", "bu ba", NULL);
1128 s = g_settings_get_string (settings, "string");
1129 g_assert_cmpstr (s, ==, "bu ba");
1132 g_settings_set_string (settings, "string", "bla bla");
1133 g_object_get (obj, "string", &s, NULL);
1134 g_assert_cmpstr (s, ==, "bla bla");
1137 g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1139 g_object_set (obj, "string", "non-unicode:\315", NULL);
1140 value = g_settings_get_value (settings, "chararray");
1141 g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1142 g_variant_unref (value);
1144 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1146 g_object_set (obj, "double", G_MAXFLOAT, NULL);
1147 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1149 g_settings_set_double (settings, "double", G_MINFLOAT);
1151 g_object_get (obj, "double", &d, NULL);
1152 g_assert_cmpfloat (d, ==, G_MINFLOAT);
1154 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1155 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1157 g_settings_set_double (settings, "double", -G_MINDOUBLE);
1159 g_object_get (obj, "double", &d, NULL);
1160 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1162 strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1163 g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1164 g_object_set (obj, "strv", strv, NULL);
1166 strv = g_settings_get_strv (settings, "strv");
1167 s = g_strjoinv (",", strv);
1168 g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1171 strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1172 g_settings_set_strv (settings, "strv", (const gchar **) strv);
1174 g_object_get (obj, "strv", &strv, NULL);
1175 s = g_strjoinv (",", strv);
1176 g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1180 g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1181 g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1182 s = g_settings_get_string (settings, "enum");
1183 g_assert_cmpstr (s, ==, "baz");
1185 g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1187 g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1189 g_object_get (obj, "enum", &i, NULL);
1190 g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1192 g_settings_set_string (settings, "enum", "baz");
1194 g_object_get (obj, "enum", &i, NULL);
1195 g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1197 g_settings_bind (settings, "flags", obj, "flags", G_SETTINGS_BIND_DEFAULT);
1198 g_object_set (obj, "flags", TEST_FLAGS_MOURNING, NULL);
1199 strv = g_settings_get_strv (settings, "flags");
1200 g_assert_cmpint (g_strv_length (strv), ==, 1);
1201 g_assert_cmpstr (strv[0], ==, "mourning");
1204 g_assert_cmpint (g_settings_get_flags (settings, "flags"), ==, TEST_FLAGS_MOURNING);
1206 g_settings_set_flags (settings, "flags", TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1208 g_object_get (obj, "flags", &i, NULL);
1209 g_assert_cmpint (i, ==, TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1211 g_object_unref (obj);
1212 g_object_unref (settings);
1219 GSettings *settings;
1221 settings = g_settings_new ("org.gtk.test.binding");
1222 obj = test_object_new ();
1224 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1226 g_object_set (obj, "int", 12345, NULL);
1227 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1229 g_settings_unbind (obj, "int");
1231 g_object_set (obj, "int", 54321, NULL);
1232 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1234 g_object_unref (obj);
1235 g_object_unref (settings);
1239 test_bind_writable (void)
1242 GSettings *settings;
1245 settings = g_settings_new ("org.gtk.test.binding");
1246 obj = test_object_new ();
1248 g_object_set (obj, "bool", FALSE, NULL);
1250 g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1252 g_object_get (obj, "bool", &b, NULL);
1255 g_settings_unbind (obj, "bool");
1257 g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1259 g_object_get (obj, "bool", &b, NULL);
1262 g_object_unref (obj);
1263 g_object_unref (settings);
1266 /* Test one-way bindings.
1267 * Verify that changes on one side show up on the other,
1268 * but not vice versa
1271 test_directional_binding (void)
1274 GSettings *settings;
1278 settings = g_settings_new ("org.gtk.test.binding");
1279 obj = test_object_new ();
1281 g_object_set (obj, "bool", FALSE, NULL);
1282 g_settings_set_boolean (settings, "bool", FALSE);
1284 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1286 g_settings_set_boolean (settings, "bool", TRUE);
1287 g_object_get (obj, "bool", &b, NULL);
1288 g_assert_cmpint (b, ==, TRUE);
1290 g_object_set (obj, "bool", FALSE, NULL);
1291 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1293 g_object_set (obj, "int", 20, NULL);
1294 g_settings_set_int (settings, "int", 20);
1296 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1298 g_object_set (obj, "int", 32, NULL);
1299 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1301 g_settings_set_int (settings, "int", 20);
1302 g_object_get (obj, "int", &i, NULL);
1303 g_assert_cmpint (i, ==, 32);
1305 g_object_unref (obj);
1306 g_object_unref (settings);
1309 /* Test that type mismatch is caught when creating a binding
1312 test_typesafe_binding_subprocess (void)
1315 GSettings *settings;
1317 settings = g_settings_new ("org.gtk.test.binding");
1318 obj = test_object_new ();
1320 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1322 g_object_unref (obj);
1323 g_object_unref (settings);
1327 test_typesafe_binding (void)
1329 if (!g_test_undefined ())
1332 g_test_trap_subprocess ("/gsettings/typesafe-binding/subprocess", 0, 0);
1333 g_test_trap_assert_failed ();
1334 g_test_trap_assert_stderr ("*not compatible*");
1338 string_to_bool (GValue *value,
1344 s = g_variant_get_string (variant, NULL);
1345 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1351 bool_to_string (const GValue *value,
1352 const GVariantType *expected_type,
1355 if (g_value_get_boolean (value))
1356 return g_variant_new_string ("true");
1358 return g_variant_new_string ("false");
1361 /* Test custom bindings.
1362 * Translate strings to booleans and back
1365 test_custom_binding (void)
1368 GSettings *settings;
1372 settings = g_settings_new ("org.gtk.test.binding");
1373 obj = test_object_new ();
1375 g_settings_set_string (settings, "string", "true");
1377 g_settings_bind_with_mapping (settings, "string",
1379 G_SETTINGS_BIND_DEFAULT,
1384 g_settings_set_string (settings, "string", "false");
1385 g_object_get (obj, "bool", &b, NULL);
1386 g_assert_cmpint (b, ==, FALSE);
1388 g_settings_set_string (settings, "string", "not true");
1389 g_object_get (obj, "bool", &b, NULL);
1390 g_assert_cmpint (b, ==, FALSE);
1392 g_object_set (obj, "bool", TRUE, NULL);
1393 s = g_settings_get_string (settings, "string");
1394 g_assert_cmpstr (s, ==, "true");
1397 g_object_unref (obj);
1398 g_object_unref (settings);
1401 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1402 * initial settings value is transported to the object
1403 * side, but later settings changes do not affect the
1407 test_no_change_binding (void)
1410 GSettings *settings;
1413 settings = g_settings_new ("org.gtk.test.binding");
1414 obj = test_object_new ();
1416 g_object_set (obj, "bool", TRUE, NULL);
1417 g_settings_set_boolean (settings, "bool", FALSE);
1419 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1421 g_object_get (obj, "bool", &b, NULL);
1422 g_assert_cmpint (b, ==, FALSE);
1424 g_settings_set_boolean (settings, "bool", TRUE);
1425 g_object_get (obj, "bool", &b, NULL);
1426 g_assert_cmpint (b, ==, FALSE);
1428 g_settings_set_boolean (settings, "bool", FALSE);
1429 g_object_set (obj, "bool", TRUE, NULL);
1430 b = g_settings_get_boolean (settings, "bool");
1431 g_assert_cmpint (b, ==, TRUE);
1433 g_object_unref (obj);
1434 g_object_unref (settings);
1437 /* Test that binding a non-readable property only
1438 * works in 'GET' mode.
1441 test_no_read_binding_fail (void)
1444 GSettings *settings;
1446 settings = g_settings_new ("org.gtk.test.binding");
1447 obj = test_object_new ();
1449 g_settings_bind (settings, "string", obj, "no-read", 0);
1453 test_no_read_binding_pass (void)
1456 GSettings *settings;
1458 settings = g_settings_new ("org.gtk.test.binding");
1459 obj = test_object_new ();
1461 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1467 test_no_read_binding (void)
1469 if (g_test_undefined ())
1471 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/fail", 0, 0);
1472 g_test_trap_assert_failed ();
1473 g_test_trap_assert_stderr ("*property*is not readable*");
1476 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/pass", 0, 0);
1477 g_test_trap_assert_passed ();
1480 /* Test that binding a non-writable property only
1481 * works in 'SET' mode.
1484 test_no_write_binding_fail (void)
1487 GSettings *settings;
1489 settings = g_settings_new ("org.gtk.test.binding");
1490 obj = test_object_new ();
1492 g_settings_bind (settings, "string", obj, "no-write", 0);
1496 test_no_write_binding_pass (void)
1499 GSettings *settings;
1501 settings = g_settings_new ("org.gtk.test.binding");
1502 obj = test_object_new ();
1504 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1510 test_no_write_binding (void)
1512 if (g_test_undefined ())
1514 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/fail", 0, 0);
1515 g_test_trap_assert_failed ();
1516 g_test_trap_assert_stderr ("*property*is not writable*");
1519 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/pass", 0, 0);
1520 g_test_trap_assert_passed ();
1524 * Test that using a keyfile works
1529 GSettingsBackend *kf_backend;
1530 GSettings *settings;
1534 g_remove ("gsettings.store");
1536 kf_backend = g_keyfile_settings_backend_new ("gsettings.store", "/", "root");
1537 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1538 g_object_unref (kf_backend);
1540 g_settings_set (settings, "greeting", "s", "see if this works");
1542 keyfile = g_key_file_new ();
1543 g_assert (g_key_file_load_from_file (keyfile, "gsettings.store", 0, NULL));
1545 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1546 g_assert_cmpstr (str, ==, "'see if this works'");
1549 g_key_file_free (keyfile);
1550 g_object_unref (settings);
1553 /* Test that getting child schemas works
1556 test_child_schema (void)
1558 GSettings *settings;
1562 /* first establish some known conditions */
1563 settings = g_settings_new ("org.gtk.test.basic-types");
1564 g_settings_set (settings, "test-byte", "y", 36);
1566 g_settings_get (settings, "test-byte", "y", &byte);
1567 g_assert_cmpint (byte, ==, 36);
1569 g_object_unref (settings);
1571 settings = g_settings_new ("org.gtk.test");
1572 child = g_settings_get_child (settings, "basic-types");
1573 g_assert (child != NULL);
1575 g_settings_get (child, "test-byte", "y", &byte);
1576 g_assert_cmpint (byte, ==, 36);
1578 g_object_unref (child);
1579 g_object_unref (settings);
1583 glib_translations_work (void)
1587 gchar *orig = "Unnamed";
1589 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
1590 if (!setlocale (LC_MESSAGES, "de"))
1593 works = dgettext ("glib20", orig) != orig;
1594 setlocale (LC_MESSAGES, locale);
1600 #include "../strinfo.c"
1605 /* "foo" has a value of 1
1606 * "bar" has a value of 2
1607 * "baz" is an alias for "bar"
1610 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1611 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1613 const guint32 *strinfo = (guint32 *) array;
1614 guint length = sizeof array / 4;
1618 /* build it and compare */
1621 builder = g_string_new (NULL);
1622 strinfo_builder_append_item (builder, "foo", 1);
1623 strinfo_builder_append_item (builder, "bar", 2);
1624 g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
1625 g_assert_cmpint (builder->len % 4, ==, 0);
1626 g_assert_cmpint (builder->len / 4, ==, length);
1627 g_assert (memcmp (builder->str, strinfo, length * 4) == 0);
1628 g_string_free (builder, TRUE);
1631 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
1633 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
1635 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
1637 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
1640 g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
1641 g_assert_cmpint (result, ==, 1);
1642 g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
1643 g_assert_cmpint (result, ==, 2);
1644 g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
1645 g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
1647 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
1648 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
1649 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
1650 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
1652 g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
1653 g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
1654 g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
1655 g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
1659 test_enums_non_enum_key (void)
1663 direct = g_settings_new ("org.gtk.test.enums.direct");
1664 g_settings_get_enum (direct, "test");
1665 g_assert_not_reached ();
1669 test_enums_non_enum_value (void)
1671 GSettings *settings;
1673 settings = g_settings_new ("org.gtk.test.enums");
1674 g_settings_set_enum (settings, "test", 42);
1675 g_assert_not_reached ();
1679 test_enums_range (void)
1681 GSettings *settings;
1683 settings = g_settings_new ("org.gtk.test.enums");
1684 g_settings_set_string (settings, "test", "qux");
1685 g_assert_not_reached ();
1689 test_enums_non_flags (void)
1691 GSettings *settings;
1693 settings = g_settings_new ("org.gtk.test.enums");
1694 g_settings_get_flags (settings, "test");
1695 g_assert_not_reached ();
1701 GSettings *settings, *direct;
1704 settings = g_settings_new ("org.gtk.test.enums");
1705 direct = g_settings_new ("org.gtk.test.enums.direct");
1707 if (g_test_undefined () && !backend_set)
1709 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-key", 0, 0);
1710 g_test_trap_assert_failed ();
1711 g_test_trap_assert_stderr ("*not associated with an enum*");
1713 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-value", 0, 0);
1714 g_test_trap_assert_failed ();
1715 g_test_trap_assert_stderr ("*invalid enum value 42*");
1717 g_test_trap_subprocess ("/gsettings/enums/subprocess/range", 0, 0);
1718 g_test_trap_assert_failed ();
1719 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1721 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-flags", 0, 0);
1722 g_test_trap_assert_failed ();
1723 g_test_trap_assert_stderr ("*not associated with a flags*");
1726 str = g_settings_get_string (settings, "test");
1727 g_assert_cmpstr (str, ==, "bar");
1730 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
1732 str = g_settings_get_string (settings, "test");
1733 g_assert_cmpstr (str, ==, "foo");
1736 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
1738 g_settings_set_string (direct, "test", "qux");
1740 str = g_settings_get_string (direct, "test");
1741 g_assert_cmpstr (str, ==, "qux");
1744 str = g_settings_get_string (settings, "test");
1745 g_assert_cmpstr (str, ==, "quux");
1748 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
1752 test_flags_non_flags_key (void)
1756 direct = g_settings_new ("org.gtk.test.enums.direct");
1757 g_settings_get_flags (direct, "test");
1758 g_assert_not_reached ();
1762 test_flags_non_flags_value (void)
1764 GSettings *settings;
1766 settings = g_settings_new ("org.gtk.test.enums");
1767 g_settings_set_flags (settings, "f-test", 0x42);
1768 g_assert_not_reached ();
1772 test_flags_range (void)
1774 GSettings *settings;
1776 settings = g_settings_new ("org.gtk.test.enums");
1777 g_settings_set_strv (settings, "f-test",
1778 (const gchar **) g_strsplit ("rock", ",", 0));
1779 g_assert_not_reached ();
1783 test_flags_non_enum (void)
1785 GSettings *settings;
1787 settings = g_settings_new ("org.gtk.test.enums");
1788 g_settings_get_enum (settings, "f-test");
1789 g_assert_not_reached ();
1795 GSettings *settings, *direct;
1799 settings = g_settings_new ("org.gtk.test.enums");
1800 direct = g_settings_new ("org.gtk.test.enums.direct");
1802 if (g_test_undefined () && !backend_set)
1804 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-key", 0, 0);
1805 g_test_trap_assert_failed ();
1806 g_test_trap_assert_stderr ("*not associated with a flags*");
1808 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-value", 0, 0);
1809 g_test_trap_assert_failed ();
1810 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
1812 g_test_trap_subprocess ("/gsettings/flags/subprocess/range", 0, 0);
1813 g_test_trap_assert_failed ();
1814 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1816 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-enum", 0, 0);
1817 g_test_trap_assert_failed ();
1818 g_test_trap_assert_stderr ("*not associated with an enum*");
1821 strv = g_settings_get_strv (settings, "f-test");
1822 str = g_strjoinv (",", strv);
1823 g_assert_cmpstr (str, ==, "");
1827 g_settings_set_flags (settings, "f-test",
1828 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1830 strv = g_settings_get_strv (settings, "f-test");
1831 str = g_strjoinv (",", strv);
1832 g_assert_cmpstr (str, ==, "talking,walking");
1836 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1837 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1839 strv = g_strsplit ("speaking,laughing", ",", 0);
1840 g_settings_set_strv (direct, "f-test", (const gchar **) strv);
1843 strv = g_settings_get_strv (direct, "f-test");
1844 str = g_strjoinv (",", strv);
1845 g_assert_cmpstr (str, ==, "speaking,laughing");
1849 strv = g_settings_get_strv (settings, "f-test");
1850 str = g_strjoinv (",", strv);
1851 g_assert_cmpstr (str, ==, "talking,laughing");
1855 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1856 TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
1860 test_range_high (void)
1862 GSettings *settings;
1864 settings = g_settings_new ("org.gtk.test.range");
1865 g_settings_set_int (settings, "val", 45);
1866 g_assert_not_reached ();
1870 test_range_low (void)
1872 GSettings *settings;
1874 settings = g_settings_new ("org.gtk.test.range");
1875 g_settings_set_int (settings, "val", 1);
1876 g_assert_not_reached ();
1882 GSettings *settings, *direct;
1885 settings = g_settings_new ("org.gtk.test.range");
1886 direct = g_settings_new ("org.gtk.test.range.direct");
1888 if (g_test_undefined () && !backend_set)
1890 g_test_trap_subprocess ("/gsettings/range/subprocess/high", 0, 0);
1891 g_test_trap_assert_failed ();
1892 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1894 g_test_trap_subprocess ("/gsettings/range/subprocess/low", 0, 0);
1895 g_test_trap_assert_failed ();
1896 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1899 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1900 g_settings_set_int (direct, "val", 22);
1901 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
1902 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
1903 g_settings_set_int (direct, "val", 45);
1904 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
1905 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1906 g_settings_set_int (direct, "val", 1);
1907 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
1908 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1910 value = g_variant_new_int32 (1);
1911 g_assert (!g_settings_range_check (settings, "val", value));
1912 g_variant_unref (value);
1913 value = g_variant_new_int32 (33);
1914 g_assert (g_settings_range_check (settings, "val", value));
1915 g_variant_unref (value);
1916 value = g_variant_new_int32 (45);
1917 g_assert (!g_settings_range_check (settings, "val", value));
1918 g_variant_unref (value);
1922 strv_has_string (gchar **haystack,
1923 const gchar *needle)
1927 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
1929 if (g_strcmp0 (haystack[n], needle) == 0)
1936 strv_set_equal (gchar **strv, ...)
1945 va_start (list, strv);
1948 str = va_arg (list, const gchar *);
1951 if (!strv_has_string (strv, str))
1961 res = g_strv_length ((gchar**)strv) == count;
1967 test_list_items (void)
1969 GSettings *settings;
1973 settings = g_settings_new ("org.gtk.test");
1974 children = g_settings_list_children (settings);
1975 keys = g_settings_list_keys (settings);
1977 g_assert (strv_set_equal (children, "basic-types", "complex-types", "localized", NULL));
1978 g_assert (strv_set_equal (keys, "greeting", "farewell", NULL));
1980 g_strfreev (children);
1983 g_object_unref (settings);
1987 test_list_schemas (void)
1989 const gchar * const *schemas;
1990 const gchar * const *relocs;
1992 relocs = g_settings_list_relocatable_schemas ();
1993 schemas = g_settings_list_schemas ();
1995 g_assert (strv_set_equal ((gchar **)relocs,
1996 "org.gtk.test.no-path",
1999 g_assert (strv_set_equal ((gchar **)schemas,
2001 "org.gtk.test.basic-types",
2002 "org.gtk.test.complex-types",
2003 "org.gtk.test.localized",
2004 "org.gtk.test.binding",
2005 "org.gtk.test.enums",
2006 "org.gtk.test.enums.direct",
2007 "org.gtk.test.range",
2008 "org.gtk.test.range.direct",
2009 "org.gtk.test.mapped",
2014 map_func (GVariant *value,
2018 gint *state = user_data;
2022 v = g_variant_get_int32 (value);
2028 g_assert_cmpint (v, ==, 1);
2032 else if (*state == 1)
2034 g_assert_cmpint (v, ==, 0);
2040 g_assert (value == NULL);
2041 *result = g_variant_new_int32 (5);
2047 test_get_mapped (void)
2049 GSettings *settings;
2054 settings = g_settings_new ("org.gtk.test.mapped");
2055 g_settings_set_int (settings, "val", 1);
2058 p = g_settings_get_mapped (settings, "val", map_func, &state);
2059 val = g_variant_get_int32 ((GVariant*)p);
2060 g_assert_cmpint (val, ==, 5);
2062 g_variant_unref (p);
2063 g_object_unref (settings);
2067 test_get_range (void)
2069 GSettings *settings;
2072 settings = g_settings_new ("org.gtk.test.range");
2073 range = g_settings_get_range (settings, "val");
2074 check_and_free (range, "('range', <(2, 44)>)");
2075 g_object_unref (settings);
2077 settings = g_settings_new ("org.gtk.test.enums");
2078 range = g_settings_get_range (settings, "test");
2079 check_and_free (range, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
2080 g_object_unref (settings);
2082 settings = g_settings_new ("org.gtk.test.enums");
2083 range = g_settings_get_range (settings, "f-test");
2084 check_and_free (range, "('flags', "
2085 "<['mourning', 'laughing', 'talking', 'walking']>)");
2086 g_object_unref (settings);
2088 settings = g_settings_new ("org.gtk.test");
2089 range = g_settings_get_range (settings, "greeting");
2090 check_and_free (range, "('type', <@as []>)");
2091 g_object_unref (settings);
2095 test_schema_source (void)
2097 GSettingsSchemaSource *parent;
2098 GSettingsSchemaSource *source;
2099 GSettingsBackend *backend;
2100 GSettingsSchema *schema;
2101 GError *error = NULL;
2102 GSettings *settings;
2105 backend = g_settings_backend_get_default ();
2107 /* make sure it fails properly */
2108 parent = g_settings_schema_source_get_default ();
2109 source = g_settings_schema_source_new_from_directory ("/path/that/does/not/exist", parent, TRUE, &error);
2110 g_assert (source == NULL);
2111 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
2112 g_clear_error (&error);
2114 /* create a source with the parent */
2115 source = g_settings_schema_source_new_from_directory ("schema-source", parent, TRUE, &error);
2116 g_assert_no_error (error);
2117 g_assert (source != NULL);
2119 /* check recursive lookups are working */
2120 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2121 g_assert (schema != NULL);
2122 g_settings_schema_unref (schema);
2124 /* check recursive lookups for non-existent schemas */
2125 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", TRUE);
2126 g_assert (schema == NULL);
2128 /* check non-recursive for schema that only exists in lower layers */
2129 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2130 g_assert (schema == NULL);
2132 /* check non-recursive lookup for non-existent */
2133 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", FALSE);
2134 g_assert (schema == NULL);
2136 /* check non-recursive for schema that exists in toplevel */
2137 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2138 g_assert (schema != NULL);
2139 g_settings_schema_unref (schema);
2141 /* check recursive for schema that exists in toplevel */
2142 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2143 g_assert (schema != NULL);
2145 /* try to use it for something */
2146 settings = g_settings_new_full (schema, backend, g_settings_schema_get_path (schema));
2147 g_settings_schema_unref (schema);
2149 g_settings_get (settings, "enabled", "b", &enabled);
2151 g_object_unref (settings);
2153 g_settings_schema_source_unref (source);
2155 /* try again, but with no parent */
2156 source = g_settings_schema_source_new_from_directory ("schema-source", NULL, FALSE, NULL);
2157 g_assert (source != NULL);
2159 /* should not find it this time, even if recursive... */
2160 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2161 g_assert (schema == NULL);
2162 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2163 g_assert (schema == NULL);
2165 /* should still find our own... */
2166 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2167 g_assert (schema != NULL);
2168 g_settings_schema_unref (schema);
2169 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2170 g_assert (schema != NULL);
2171 g_settings_schema_unref (schema);
2173 g_settings_schema_source_unref (source);
2179 GAction *string, *toggle;
2180 gboolean c1, c2, c3;
2181 GSettings *settings;
2183 GVariantType *param_type;
2185 GVariantType *state_type;
2188 settings = g_settings_new ("org.gtk.test.basic-types");
2189 string = g_settings_create_action (settings, "test-string");
2190 toggle = g_settings_create_action (settings, "test-boolean");
2191 g_object_unref (settings); /* should be held by the actions */
2193 g_signal_connect (settings, "changed", G_CALLBACK (changed_cb2), &c1);
2194 g_signal_connect (string, "notify::state", G_CALLBACK (changed_cb2), &c2);
2195 g_signal_connect (toggle, "notify::state", G_CALLBACK (changed_cb2), &c3);
2197 c1 = c2 = c3 = FALSE;
2198 g_settings_set_string (settings, "test-string", "hello world");
2199 check_and_free (g_action_get_state (string), "'hello world'");
2200 g_assert (c1 && c2 && !c3);
2201 c1 = c2 = c3 = FALSE;
2203 g_action_activate (string, g_variant_new_string ("hihi"));
2204 check_and_free (g_settings_get_value (settings, "test-string"), "'hihi'");
2205 g_assert (c1 && c2 && !c3);
2206 c1 = c2 = c3 = FALSE;
2208 g_action_change_state (string, g_variant_new_string ("kthxbye"));
2209 check_and_free (g_settings_get_value (settings, "test-string"), "'kthxbye'");
2210 g_assert (c1 && c2 && !c3);
2211 c1 = c2 = c3 = FALSE;
2213 g_action_change_state (toggle, g_variant_new_boolean (TRUE));
2214 g_assert (g_settings_get_boolean (settings, "test-boolean"));
2215 g_assert (c1 && !c2 && c3);
2216 c1 = c2 = c3 = FALSE;
2218 g_action_activate (toggle, NULL);
2219 g_assert (!g_settings_get_boolean (settings, "test-boolean"));
2220 g_assert (c1 && !c2 && c3);
2222 g_object_get (string,
2224 "parameter-type", ¶m_type,
2225 "enabled", &enabled,
2226 "state-type", &state_type,
2230 g_assert_cmpstr (name, ==, "test-string");
2231 g_assert (g_variant_type_equal (param_type, G_VARIANT_TYPE_STRING));
2233 g_assert (g_variant_type_equal (state_type, G_VARIANT_TYPE_STRING));
2234 g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "kthxbye");
2237 g_variant_unref (state);
2239 g_object_unref (string);
2240 g_object_unref (toggle);
2244 main (int argc, char *argv[])
2249 setlocale (LC_ALL, "");
2251 if (g_getenv ("G_TEST_DATA"))
2252 datapath = g_getenv ("G_TEST_DATA");
2256 g_test_init (&argc, &argv, NULL);
2258 if (!g_test_subprocess ())
2260 const gchar *glib_mkenums;
2261 const gchar *glib_compile_schemas;
2264 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
2266 g_setenv ("XDG_DATA_DIRS", datapath, TRUE);
2267 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
2270 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
2272 if (g_getenv ("GLIB_MKENUMS"))
2273 glib_mkenums = g_getenv ("GLIB_MKENUMS");
2275 glib_mkenums = "glib-mkenums";
2277 cmdline = g_strdup_printf ("%s --template %s/enums.xml.template %s/testenum.h", glib_mkenums, datapath, datapath);
2279 g_assert (g_spawn_command_line_sync (cmdline, &enums, NULL, &result, NULL));
2280 g_assert (result == 0);
2281 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
2286 if (g_getenv ("GLIB_COMPILE_SCHEMAS"))
2287 glib_compile_schemas = g_getenv ("GLIB_COMPILE_SCHEMAS");
2289 glib_compile_schemas = "glib-compile-schemas";
2291 cmdline = g_strdup_printf ("%s --targetdir=. --schema-file=org.gtk.test.enums.xml --schema-file=%s/org.gtk.test.gschema.xml", glib_compile_schemas, datapath);
2292 g_assert (g_spawn_command_line_sync (cmdline, NULL, NULL, &result, NULL));
2293 g_assert (result == 0);
2296 g_mkdir ("schema-source", 0777);
2297 g_remove ("schema-source/gschemas.compiled");
2299 cmdline = g_strdup_printf ("%s --targetdir=schema-source --schema-file=%s/org.gtk.schemasourcecheck.gschema.xml", glib_compile_schemas, datapath);
2300 g_assert (g_spawn_command_line_sync (cmdline, NULL, NULL, &result, NULL));
2301 g_assert (result == 0);
2305 g_test_add_func ("/gsettings/basic", test_basic);
2309 g_test_add_func ("/gsettings/no-schema", test_no_schema);
2310 g_test_add_func ("/gsettings/no-schema/subprocess", test_no_schema_subprocess);
2311 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
2312 g_test_add_func ("/gsettings/unknown-key/subprocess", test_unknown_key_subprocess);
2313 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
2314 g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
2315 g_test_add_func ("/gsettings/wrong-path/subprocess", test_wrong_path_subprocess);
2316 g_test_add_func ("/gsettings/no-path", test_no_path);
2317 g_test_add_func ("/gsettings/no-path/subprocess", test_no_path_subprocess);
2320 g_test_add_func ("/gsettings/basic-types", test_basic_types);
2321 g_test_add_func ("/gsettings/complex-types", test_complex_types);
2322 g_test_add_func ("/gsettings/changes", test_changes);
2324 if (glib_translations_work ())
2326 g_test_add_func ("/gsettings/l10n", test_l10n);
2327 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
2330 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
2331 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
2332 g_test_add_func ("/gsettings/atomic", test_atomic);
2334 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
2335 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
2336 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
2337 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
2338 g_test_add_func ("/gsettings/unbinding", test_unbind);
2339 g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
2343 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
2344 g_test_add_func ("/gsettings/typesafe-binding/subprocess", test_typesafe_binding_subprocess);
2345 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
2346 g_test_add_func ("/gsettings/no-read-binding/subprocess/fail", test_no_read_binding_fail);
2347 g_test_add_func ("/gsettings/no-read-binding/subprocess/pass", test_no_read_binding_pass);
2348 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
2349 g_test_add_func ("/gsettings/no-write-binding/subprocess/fail", test_no_write_binding_fail);
2350 g_test_add_func ("/gsettings/no-write-binding/subprocess/pass", test_no_write_binding_pass);
2353 g_test_add_func ("/gsettings/keyfile", test_keyfile);
2354 g_test_add_func ("/gsettings/child-schema", test_child_schema);
2355 g_test_add_func ("/gsettings/strinfo", test_strinfo);
2356 g_test_add_func ("/gsettings/enums", test_enums);
2357 g_test_add_func ("/gsettings/enums/subprocess/non-enum-key", test_enums_non_enum_key);
2358 g_test_add_func ("/gsettings/enums/subprocess/non-enum-value", test_enums_non_enum_value);
2359 g_test_add_func ("/gsettings/enums/subprocess/range", test_enums_range);
2360 g_test_add_func ("/gsettings/enums/subprocess/non-flags", test_enums_non_flags);
2361 g_test_add_func ("/gsettings/flags", test_flags);
2362 g_test_add_func ("/gsettings/flags/subprocess/non-flags-key", test_flags_non_flags_key);
2363 g_test_add_func ("/gsettings/flags/subprocess/non-flags-value", test_flags_non_flags_value);
2364 g_test_add_func ("/gsettings/flags/subprocess/range", test_flags_range);
2365 g_test_add_func ("/gsettings/flags/subprocess/non-enum", test_flags_non_enum);
2366 g_test_add_func ("/gsettings/range", test_range);
2367 g_test_add_func ("/gsettings/range/subprocess/high", test_range_high);
2368 g_test_add_func ("/gsettings/range/subprocess/low", test_range_low);
2369 g_test_add_func ("/gsettings/list-items", test_list_items);
2370 g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
2371 g_test_add_func ("/gsettings/mapped", test_get_mapped);
2372 g_test_add_func ("/gsettings/get-range", test_get_range);
2373 g_test_add_func ("/gsettings/schema-source", test_schema_source);
2374 g_test_add_func ("/gsettings/actions", test_actions);
2376 result = g_test_run ();