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 (void)
88 if (!g_test_undefined ())
91 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
96 settings = g_settings_new ("org.gtk.test");
97 value = g_settings_get_value (settings, "no_such_key");
99 g_assert (value == NULL);
101 g_object_unref (settings);
103 g_test_trap_assert_failed ();
104 g_test_trap_assert_stderr ("*does not contain*");
107 /* Check that we get an error when the schema
108 * has not been installed
111 test_no_schema (void)
113 if (!g_test_undefined ())
116 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
120 settings = g_settings_new ("no.such.schema");
122 g_assert (settings == NULL);
125 g_test_trap_assert_failed ();
126 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
129 /* Check that we get an error when passing a type string
130 * that does not match the schema
133 test_wrong_type (void)
138 if (!g_test_undefined ())
141 settings = g_settings_new ("org.gtk.test");
143 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
144 "*given value has a type of*");
145 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
146 "*valid_format_string*");
147 g_settings_get (settings, "greeting", "o", &str);
148 g_test_assert_expected_messages ();
150 g_assert (str == NULL);
152 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
153 "*expects type 's'*");
154 g_settings_set (settings, "greeting", "o", "/a/path");
155 g_test_assert_expected_messages ();
157 g_object_unref (settings);
160 /* Check errors with explicit paths */
162 test_wrong_path (void)
164 if (!g_test_undefined ())
167 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
169 GSettings *settings G_GNUC_UNUSED;
171 settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
174 g_test_trap_assert_failed ();
175 g_test_trap_assert_stderr ("*but path * specified by schema*");
181 if (!g_test_undefined ())
184 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
186 GSettings *settings G_GNUC_UNUSED;
188 settings = g_settings_new ("org.gtk.test.no-path");
191 g_test_trap_assert_failed ();
192 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
196 /* Check that we can successfully read and set the full
197 * range of all basic types
200 test_basic_types (void)
214 settings = g_settings_new ("org.gtk.test.basic-types");
216 g_settings_get (settings, "test-boolean", "b", &b);
217 g_assert_cmpint (b, ==, 1);
219 g_settings_set (settings, "test-boolean", "b", 0);
220 g_settings_get (settings, "test-boolean", "b", &b);
221 g_assert_cmpint (b, ==, 0);
223 g_settings_get (settings, "test-byte", "y", &byte);
224 g_assert_cmpint (byte, ==, 25);
226 g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
227 g_settings_get (settings, "test-byte", "y", &byte);
228 g_assert_cmpint (byte, ==, G_MAXUINT8);
230 g_settings_get (settings, "test-int16", "n", &i16);
231 g_assert_cmpint (i16, ==, -1234);
233 g_settings_set (settings, "test-int16", "n", G_MININT16);
234 g_settings_get (settings, "test-int16", "n", &i16);
235 g_assert_cmpint (i16, ==, G_MININT16);
237 g_settings_set (settings, "test-int16", "n", G_MAXINT16);
238 g_settings_get (settings, "test-int16", "n", &i16);
239 g_assert_cmpint (i16, ==, G_MAXINT16);
241 g_settings_get (settings, "test-uint16", "q", &u16);
242 g_assert_cmpuint (u16, ==, 1234);
244 g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
245 g_settings_get (settings, "test-uint16", "q", &u16);
246 g_assert_cmpuint (u16, ==, G_MAXUINT16);
248 g_settings_get (settings, "test-int32", "i", &i32);
249 g_assert_cmpint (i32, ==, -123456);
251 g_settings_set (settings, "test-int32", "i", G_MININT32);
252 g_settings_get (settings, "test-int32", "i", &i32);
253 g_assert_cmpint (i32, ==, G_MININT32);
255 g_settings_set (settings, "test-int32", "i", G_MAXINT32);
256 g_settings_get (settings, "test-int32", "i", &i32);
257 g_assert_cmpint (i32, ==, G_MAXINT32);
259 g_settings_get (settings, "test-uint32", "u", &u32);
260 g_assert_cmpuint (u32, ==, 123456);
262 g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
263 g_settings_get (settings, "test-uint32", "u", &u32);
264 g_assert_cmpuint (u32, ==, G_MAXUINT32);
266 g_settings_get (settings, "test-int64", "x", &i64);
267 g_assert_cmpuint (i64, ==, -123456789);
269 g_settings_set (settings, "test-int64", "x", G_MININT64);
270 g_settings_get (settings, "test-int64", "x", &i64);
271 g_assert_cmpuint (i64, ==, G_MININT64);
273 g_settings_set (settings, "test-int64", "x", G_MAXINT64);
274 g_settings_get (settings, "test-int64", "x", &i64);
275 g_assert_cmpuint (i64, ==, G_MAXINT64);
277 g_settings_get (settings, "test-uint64", "t", &u64);
278 g_assert_cmpuint (u64, ==, 123456789);
280 g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
281 g_settings_get (settings, "test-uint64", "t", &u64);
282 g_assert_cmpuint (u64, ==, G_MAXUINT64);
284 g_settings_get (settings, "test-double", "d", &d);
285 g_assert_cmpfloat (d, ==, 123.456);
287 g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
288 g_settings_get (settings, "test-double", "d", &d);
289 g_assert_cmpfloat (d, ==, G_MINDOUBLE);
291 g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
292 g_settings_get (settings, "test-double", "d", &d);
293 g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
295 g_settings_get (settings, "test-string", "s", &str);
296 g_assert_cmpstr (str, ==, "a string, it seems");
300 g_settings_get (settings, "test-objectpath", "o", &str);
301 g_assert_cmpstr (str, ==, "/a/object/path");
302 g_object_unref (settings);
307 /* Check that we can read an set complex types like
308 * tuples, arrays and dictionaries
311 test_complex_types (void)
316 GVariantIter *iter = NULL;
318 settings = g_settings_new ("org.gtk.test.complex-types");
320 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
321 g_assert_cmpstr (s, ==, "one");
322 g_assert_cmpint (i1,==, 2);
323 g_assert_cmpint (i2,==, 3);
327 g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
328 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
329 g_assert_cmpstr (s, ==, "none");
330 g_assert_cmpint (i1,==, 0);
331 g_assert_cmpint (i2,==, 0);
335 g_settings_get (settings, "test-array", "ai", &iter);
336 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
337 g_assert (g_variant_iter_next (iter, "i", &i1));
338 g_assert_cmpint (i1, ==, 0);
339 g_assert (g_variant_iter_next (iter, "i", &i1));
340 g_assert_cmpint (i1, ==, 1);
341 g_assert (g_variant_iter_next (iter, "i", &i1));
342 g_assert_cmpint (i1, ==, 2);
343 g_assert (g_variant_iter_next (iter, "i", &i1));
344 g_assert_cmpint (i1, ==, 3);
345 g_assert (g_variant_iter_next (iter, "i", &i1));
346 g_assert_cmpint (i1, ==, 4);
347 g_assert (g_variant_iter_next (iter, "i", &i1));
348 g_assert_cmpint (i1, ==, 5);
349 g_assert (!g_variant_iter_next (iter, "i", &i1));
350 g_variant_iter_free (iter);
352 g_object_unref (settings);
355 static gboolean changed_cb_called;
358 changed_cb (GSettings *settings,
362 changed_cb_called = TRUE;
364 g_assert_cmpstr (key, ==, data);
367 /* Test that basic change notification with the changed signal works.
373 GSettings *settings2;
375 settings = g_settings_new ("org.gtk.test");
377 g_signal_connect (settings, "changed",
378 G_CALLBACK (changed_cb), "greeting");
380 changed_cb_called = FALSE;
382 g_settings_set (settings, "greeting", "s", "new greeting");
383 g_assert (changed_cb_called);
385 settings2 = g_settings_new ("org.gtk.test");
387 changed_cb_called = FALSE;
389 g_settings_set (settings2, "greeting", "s", "hi");
390 g_assert (changed_cb_called);
392 g_object_unref (settings2);
393 g_object_unref (settings);
396 static gboolean changed_cb_called2;
399 changed_cb2 (GSettings *settings,
408 /* Test that changes done to a delay-mode instance
409 * don't appear to the outside world until apply. Also
410 * check that we get change notification when they are
412 * Also test that the has-unapplied property is properly
416 test_delay_apply (void)
419 GSettings *settings2;
422 settings = g_settings_new ("org.gtk.test");
423 settings2 = g_settings_new ("org.gtk.test");
425 g_settings_set (settings2, "greeting", "s", "top o' the morning");
427 changed_cb_called = FALSE;
428 changed_cb_called2 = FALSE;
430 g_signal_connect (settings, "changed",
431 G_CALLBACK (changed_cb2), &changed_cb_called);
432 g_signal_connect (settings2, "changed",
433 G_CALLBACK (changed_cb2), &changed_cb_called2);
435 g_settings_delay (settings);
437 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
439 g_assert (changed_cb_called);
440 g_assert (!changed_cb_called2);
442 g_settings_get (settings, "greeting", "s", &str);
443 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
447 g_settings_get (settings2, "greeting", "s", &str);
448 g_assert_cmpstr (str, ==, "top o' the morning");
452 g_assert (g_settings_get_has_unapplied (settings));
453 g_assert (!g_settings_get_has_unapplied (settings2));
455 changed_cb_called = FALSE;
456 changed_cb_called2 = FALSE;
458 g_settings_apply (settings);
460 g_assert (!changed_cb_called);
461 g_assert (changed_cb_called2);
463 g_settings_get (settings, "greeting", "s", &str);
464 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
468 g_settings_get (settings2, "greeting", "s", &str);
469 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
473 g_assert (!g_settings_get_has_unapplied (settings));
474 g_assert (!g_settings_get_has_unapplied (settings2));
476 g_object_unref (settings2);
477 g_object_unref (settings);
480 /* Test that reverting unapplied changes in a delay-apply
481 * settings instance works.
484 test_delay_revert (void)
487 GSettings *settings2;
490 settings = g_settings_new ("org.gtk.test");
491 settings2 = g_settings_new ("org.gtk.test");
493 g_settings_set (settings2, "greeting", "s", "top o' the morning");
495 g_settings_delay (settings);
497 g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
499 g_settings_get (settings, "greeting", "s", &str);
500 g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
504 g_settings_get (settings2, "greeting", "s", &str);
505 g_assert_cmpstr (str, ==, "top o' the morning");
509 g_assert (g_settings_get_has_unapplied (settings));
511 g_settings_revert (settings);
513 g_assert (!g_settings_get_has_unapplied (settings));
515 g_settings_get (settings, "greeting", "s", &str);
516 g_assert_cmpstr (str, ==, "top o' the morning");
520 g_settings_get (settings2, "greeting", "s", &str);
521 g_assert_cmpstr (str, ==, "top o' the morning");
525 g_object_unref (settings2);
526 g_object_unref (settings);
530 keys_changed_cb (GSettings *settings,
536 g_assert_cmpint (n_keys, ==, 2);
538 g_assert ((keys[0] == g_quark_from_static_string ("greeting") &&
539 keys[1] == g_quark_from_static_string ("farewell")) ||
540 (keys[1] == g_quark_from_static_string ("greeting") &&
541 keys[0] == g_quark_from_static_string ("farewell")));
543 g_settings_get (settings, "greeting", "s", &str);
544 g_assert_cmpstr (str, ==, "greetings from test_atomic");
548 g_settings_get (settings, "farewell", "s", &str);
549 g_assert_cmpstr (str, ==, "atomic bye-bye");
554 /* Check that delay-applied changes appear atomically.
555 * More specifically, verify that all changed keys appear
556 * with their new value while handling the change-event signal.
562 GSettings *settings2;
565 settings = g_settings_new ("org.gtk.test");
566 settings2 = g_settings_new ("org.gtk.test");
568 g_settings_set (settings2, "greeting", "s", "top o' the morning");
570 changed_cb_called = FALSE;
571 changed_cb_called2 = FALSE;
573 g_signal_connect (settings2, "change-event",
574 G_CALLBACK (keys_changed_cb), NULL);
576 g_settings_delay (settings);
578 g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
579 g_settings_set (settings, "farewell", "s", "atomic bye-bye");
581 g_settings_apply (settings);
583 g_settings_get (settings, "greeting", "s", &str);
584 g_assert_cmpstr (str, ==, "greetings from test_atomic");
588 g_settings_get (settings, "farewell", "s", &str);
589 g_assert_cmpstr (str, ==, "atomic bye-bye");
593 g_settings_get (settings2, "greeting", "s", &str);
594 g_assert_cmpstr (str, ==, "greetings from test_atomic");
598 g_settings_get (settings2, "farewell", "s", &str);
599 g_assert_cmpstr (str, ==, "atomic bye-bye");
603 g_object_unref (settings2);
604 g_object_unref (settings);
607 /* On Windows the interaction between the C library locale and libintl
608 * (from GNU gettext) is not like on POSIX, so just skip these tests
611 * There are several issues:
613 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
614 * in libintl (defined in its <libintl.h>).
616 * 2) The locale names that setlocale() accepts and returns aren't in
617 * the "de_DE" style, but like "German_Germany".
619 * 3) libintl looks at the Win32 thread locale and not the C library
620 * locale. (And even if libintl would use the C library's locale, as
621 * there are several alternative C library DLLs, libintl might be
622 * linked to a different one than the application code, so they
623 * wouldn't have the same C library locale anyway.)
626 /* Test that translations work for schema defaults.
628 * This test relies on the de.po file in the same directory
629 * to be compiled into ./de/LC_MESSAGES/test.mo
638 bindtextdomain ("test", ".");
639 bind_textdomain_codeset ("test", "UTF-8");
641 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
643 settings = g_settings_new ("org.gtk.test.localized");
645 setlocale (LC_MESSAGES, "C");
646 str = g_settings_get_string (settings, "error-message");
647 setlocale (LC_MESSAGES, locale);
649 g_assert_cmpstr (str, ==, "Unnamed");
653 setlocale (LC_MESSAGES, "de_DE");
654 str = g_settings_get_string (settings, "error-message");
655 setlocale (LC_MESSAGES, locale);
657 g_assert_cmpstr (str, ==, "Unbenannt");
658 g_object_unref (settings);
665 /* Test that message context works as expected with translated
666 * schema defaults. Also, verify that non-ASCII UTF-8 content
669 * This test relies on the de.po file in the same directory
670 * to be compiled into ./de/LC_MESSAGES/test.mo
673 test_l10n_context (void)
679 bindtextdomain ("test", ".");
680 bind_textdomain_codeset ("test", "UTF-8");
682 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
684 settings = g_settings_new ("org.gtk.test.localized");
686 setlocale (LC_MESSAGES, "C");
687 g_settings_get (settings, "backspace", "s", &str);
688 setlocale (LC_MESSAGES, locale);
690 g_assert_cmpstr (str, ==, "BackSpace");
694 setlocale (LC_MESSAGES, "de_DE");
695 g_settings_get (settings, "backspace", "s", &str);
696 setlocale (LC_MESSAGES, locale);
698 g_assert_cmpstr (str, ==, "Löschen");
699 g_object_unref (settings);
729 GObject parent_instance;
732 gboolean anti_bool_prop;
743 gchar *no_write_prop;
751 GObjectClass parent_class;
754 static GType test_object_get_type (void);
755 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
758 test_object_init (TestObject *object)
763 test_object_finalize (GObject *object)
765 TestObject *testo = (TestObject*)object;
766 g_strfreev (testo->strv_prop);
767 g_free (testo->string_prop);
768 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
772 test_object_get_property (GObject *object,
777 TestObject *test_object = (TestObject *)object;
782 g_value_set_boolean (value, test_object->bool_prop);
785 g_value_set_boolean (value, test_object->anti_bool_prop);
788 g_value_set_schar (value, test_object->byte_prop);
791 g_value_set_uint (value, test_object->uint16_prop);
794 g_value_set_int (value, test_object->int16_prop);
797 g_value_set_int (value, test_object->int_prop);
800 g_value_set_uint (value, test_object->uint_prop);
803 g_value_set_int64 (value, test_object->int64_prop);
806 g_value_set_uint64 (value, test_object->uint64_prop);
809 g_value_set_double (value, test_object->double_prop);
812 g_value_set_string (value, test_object->string_prop);
815 g_value_set_string (value, test_object->no_write_prop);
818 g_value_set_boxed (value, test_object->strv_prop);
821 g_value_set_enum (value, test_object->enum_prop);
824 g_value_set_flags (value, test_object->flags_prop);
827 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
833 test_object_set_property (GObject *object,
838 TestObject *test_object = (TestObject *)object;
843 test_object->bool_prop = g_value_get_boolean (value);
846 test_object->anti_bool_prop = g_value_get_boolean (value);
849 test_object->byte_prop = g_value_get_schar (value);
852 test_object->int16_prop = g_value_get_int (value);
855 test_object->uint16_prop = g_value_get_uint (value);
858 test_object->int_prop = g_value_get_int (value);
861 test_object->uint_prop = g_value_get_uint (value);
864 test_object->int64_prop = g_value_get_int64 (value);
867 test_object->uint64_prop = g_value_get_uint64 (value);
870 test_object->double_prop = g_value_get_double (value);
873 g_free (test_object->string_prop);
874 test_object->string_prop = g_value_dup_string (value);
877 g_free (test_object->no_read_prop);
878 test_object->no_read_prop = g_value_dup_string (value);
881 g_strfreev (test_object->strv_prop);
882 test_object->strv_prop = g_value_dup_boxed (value);
885 test_object->enum_prop = g_value_get_enum (value);
888 test_object->flags_prop = g_value_get_flags (value);
891 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
897 test_enum_get_type (void)
899 static volatile gsize define_type_id = 0;
901 if (g_once_init_enter (&define_type_id))
903 static const GEnumValue values[] = {
904 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
905 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
906 { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
907 { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
911 GType type_id = g_enum_register_static ("TestEnum", values);
912 g_once_init_leave (&define_type_id, type_id);
915 return define_type_id;
919 test_flags_get_type (void)
921 static volatile gsize define_type_id = 0;
923 if (g_once_init_enter (&define_type_id))
925 static const GFlagsValue values[] = {
926 { TEST_FLAGS_NONE, "TEST_FLAGS_NONE", "none" },
927 { TEST_FLAGS_MOURNING, "TEST_FLAGS_MOURNING", "mourning" },
928 { TEST_FLAGS_LAUGHING, "TEST_FLAGS_LAUGHING", "laughing" },
929 { TEST_FLAGS_WALKING, "TEST_FLAGS_WALKING", "walking" },
933 GType type_id = g_flags_register_static ("TestFlags", values);
934 g_once_init_leave (&define_type_id, type_id);
937 return define_type_id;
941 test_object_class_init (TestObjectClass *class)
943 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
945 gobject_class->get_property = test_object_get_property;
946 gobject_class->set_property = test_object_set_property;
947 gobject_class->finalize = test_object_finalize;
949 g_object_class_install_property (gobject_class, PROP_BOOL,
950 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
951 g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
952 g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
953 g_object_class_install_property (gobject_class, PROP_BYTE,
954 g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
955 g_object_class_install_property (gobject_class, PROP_INT16,
956 g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
957 g_object_class_install_property (gobject_class, PROP_UINT16,
958 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
959 g_object_class_install_property (gobject_class, PROP_INT,
960 g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
961 g_object_class_install_property (gobject_class, PROP_UINT,
962 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
963 g_object_class_install_property (gobject_class, PROP_INT64,
964 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
965 g_object_class_install_property (gobject_class, PROP_UINT64,
966 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
967 g_object_class_install_property (gobject_class, PROP_DOUBLE,
968 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
969 g_object_class_install_property (gobject_class, PROP_STRING,
970 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
971 g_object_class_install_property (gobject_class, PROP_NO_WRITE,
972 g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
973 g_object_class_install_property (gobject_class, PROP_NO_READ,
974 g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
975 g_object_class_install_property (gobject_class, PROP_STRV,
976 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
977 g_object_class_install_property (gobject_class, PROP_ENUM,
978 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
979 g_object_class_install_property (gobject_class, PROP_FLAGS,
980 g_param_spec_flags ("flags", "", "", test_flags_get_type (), TEST_FLAGS_NONE, G_PARAM_READWRITE));
984 test_object_new (void)
986 return (TestObject*)g_object_new (test_object_get_type (), NULL);
989 /* Test basic binding functionality for simple types.
990 * Verify that with bidirectional bindings, changes on either side
991 * are notified on the other end.
994 test_simple_binding (void)
1013 settings = g_settings_new ("org.gtk.test.binding");
1014 obj = test_object_new ();
1016 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
1017 g_object_set (obj, "bool", TRUE, NULL);
1018 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1020 g_settings_set_boolean (settings, "bool", FALSE);
1022 g_object_get (obj, "bool", &b, NULL);
1023 g_assert_cmpint (b, ==, FALSE);
1025 g_settings_bind (settings, "anti-bool", obj, "anti-bool",
1026 G_SETTINGS_BIND_INVERT_BOOLEAN);
1027 g_object_set (obj, "anti-bool", FALSE, NULL);
1028 g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
1030 g_settings_set_boolean (settings, "anti-bool", FALSE);
1032 g_object_get (obj, "anti-bool", &b, NULL);
1033 g_assert_cmpint (b, ==, TRUE);
1035 g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
1037 g_object_set (obj, "byte", 123, NULL);
1039 g_settings_get (settings, "byte", "y", &y);
1040 g_assert_cmpint (y, ==, 123);
1042 g_settings_set (settings, "byte", "y", 54);
1044 g_object_get (obj, "byte", &y, NULL);
1045 g_assert_cmpint (y, ==, 54);
1047 g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
1049 g_object_set (obj, "int16", 1234, NULL);
1051 g_settings_get (settings, "int16", "n", &n);
1052 g_assert_cmpint (n, ==, 1234);
1054 g_settings_set (settings, "int16", "n", 4321);
1056 g_object_get (obj, "int16", &n2, NULL);
1057 g_assert_cmpint (n2, ==, 4321);
1059 g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
1061 g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1063 g_settings_get (settings, "uint16", "q", &q);
1064 g_assert_cmpuint (q, ==, G_MAXUINT16);
1066 g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1068 g_object_get (obj, "uint16", &q2, NULL);
1069 g_assert_cmpuint (q2, ==, (guint16) G_MAXINT16);
1071 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1073 g_object_set (obj, "int", 12345, NULL);
1074 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1076 g_settings_set_int (settings, "int", 54321);
1078 g_object_get (obj, "int", &i, NULL);
1079 g_assert_cmpint (i, ==, 54321);
1081 g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1083 g_object_set (obj, "uint", 12345, NULL);
1084 g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);
1086 g_settings_set_uint (settings, "uint", 54321);
1088 g_object_get (obj, "uint", &u, NULL);
1089 g_assert_cmpuint (u, ==, 54321);
1091 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1093 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1095 g_settings_get (settings, "int64", "x", &i64);
1096 g_assert_cmpint (i64, ==, G_MAXINT64);
1098 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1100 g_object_get (obj, "int64", &i64, NULL);
1101 g_assert_cmpint (i64, ==, G_MININT64);
1103 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1105 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1107 g_settings_get (settings, "uint64", "t", &u64);
1108 g_assert_cmpuint (u64, ==, G_MAXUINT64);
1110 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1112 g_object_get (obj, "uint64", &u64, NULL);
1113 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1115 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1117 g_object_set (obj, "string", "bu ba", NULL);
1118 s = g_settings_get_string (settings, "string");
1119 g_assert_cmpstr (s, ==, "bu ba");
1122 g_settings_set_string (settings, "string", "bla bla");
1123 g_object_get (obj, "string", &s, NULL);
1124 g_assert_cmpstr (s, ==, "bla bla");
1127 g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1129 g_object_set (obj, "string", "non-unicode:\315", NULL);
1130 value = g_settings_get_value (settings, "chararray");
1131 g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1132 g_variant_unref (value);
1134 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1136 g_object_set (obj, "double", G_MAXFLOAT, NULL);
1137 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1139 g_settings_set_double (settings, "double", G_MINFLOAT);
1141 g_object_get (obj, "double", &d, NULL);
1142 g_assert_cmpfloat (d, ==, G_MINFLOAT);
1144 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1145 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1147 g_settings_set_double (settings, "double", -G_MINDOUBLE);
1149 g_object_get (obj, "double", &d, NULL);
1150 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1152 strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1153 g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1154 g_object_set (obj, "strv", strv, NULL);
1156 strv = g_settings_get_strv (settings, "strv");
1157 s = g_strjoinv (",", strv);
1158 g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1161 strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1162 g_settings_set_strv (settings, "strv", (const gchar **) strv);
1164 g_object_get (obj, "strv", &strv, NULL);
1165 s = g_strjoinv (",", strv);
1166 g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1170 g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1171 g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1172 s = g_settings_get_string (settings, "enum");
1173 g_assert_cmpstr (s, ==, "baz");
1175 g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1177 g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1179 g_object_get (obj, "enum", &i, NULL);
1180 g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1182 g_settings_set_string (settings, "enum", "baz");
1184 g_object_get (obj, "enum", &i, NULL);
1185 g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1187 g_settings_bind (settings, "flags", obj, "flags", G_SETTINGS_BIND_DEFAULT);
1188 g_object_set (obj, "flags", TEST_FLAGS_MOURNING, NULL);
1189 strv = g_settings_get_strv (settings, "flags");
1190 g_assert_cmpint (g_strv_length (strv), ==, 1);
1191 g_assert_cmpstr (strv[0], ==, "mourning");
1194 g_assert_cmpint (g_settings_get_flags (settings, "flags"), ==, TEST_FLAGS_MOURNING);
1196 g_settings_set_flags (settings, "flags", TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1198 g_object_get (obj, "flags", &i, NULL);
1199 g_assert_cmpint (i, ==, TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1201 g_object_unref (obj);
1202 g_object_unref (settings);
1209 GSettings *settings;
1211 settings = g_settings_new ("org.gtk.test.binding");
1212 obj = test_object_new ();
1214 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1216 g_object_set (obj, "int", 12345, NULL);
1217 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1219 g_settings_unbind (obj, "int");
1221 g_object_set (obj, "int", 54321, NULL);
1222 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1224 g_object_unref (obj);
1225 g_object_unref (settings);
1229 test_bind_writable (void)
1232 GSettings *settings;
1235 settings = g_settings_new ("org.gtk.test.binding");
1236 obj = test_object_new ();
1238 g_object_set (obj, "bool", FALSE, NULL);
1240 g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1242 g_object_get (obj, "bool", &b, NULL);
1245 g_settings_unbind (obj, "bool");
1247 g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1249 g_object_get (obj, "bool", &b, NULL);
1252 g_object_unref (obj);
1253 g_object_unref (settings);
1256 /* Test one-way bindings.
1257 * Verify that changes on one side show up on the other,
1258 * but not vice versa
1261 test_directional_binding (void)
1264 GSettings *settings;
1268 settings = g_settings_new ("org.gtk.test.binding");
1269 obj = test_object_new ();
1271 g_object_set (obj, "bool", FALSE, NULL);
1272 g_settings_set_boolean (settings, "bool", FALSE);
1274 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1276 g_settings_set_boolean (settings, "bool", TRUE);
1277 g_object_get (obj, "bool", &b, NULL);
1278 g_assert_cmpint (b, ==, TRUE);
1280 g_object_set (obj, "bool", FALSE, NULL);
1281 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1283 g_object_set (obj, "int", 20, NULL);
1284 g_settings_set_int (settings, "int", 20);
1286 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1288 g_object_set (obj, "int", 32, NULL);
1289 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1291 g_settings_set_int (settings, "int", 20);
1292 g_object_get (obj, "int", &i, NULL);
1293 g_assert_cmpint (i, ==, 32);
1295 g_object_unref (obj);
1296 g_object_unref (settings);
1299 /* Test that type mismatch is caught when creating a binding
1302 test_typesafe_binding (void)
1304 if (!g_test_undefined ())
1307 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1310 GSettings *settings;
1312 settings = g_settings_new ("org.gtk.test.binding");
1313 obj = test_object_new ();
1315 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1317 g_object_unref (obj);
1318 g_object_unref (settings);
1320 g_test_trap_assert_failed ();
1321 g_test_trap_assert_stderr ("*not compatible*");
1325 string_to_bool (GValue *value,
1331 s = g_variant_get_string (variant, NULL);
1332 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1338 bool_to_string (const GValue *value,
1339 const GVariantType *expected_type,
1342 if (g_value_get_boolean (value))
1343 return g_variant_new_string ("true");
1345 return g_variant_new_string ("false");
1348 /* Test custom bindings.
1349 * Translate strings to booleans and back
1352 test_custom_binding (void)
1355 GSettings *settings;
1359 settings = g_settings_new ("org.gtk.test.binding");
1360 obj = test_object_new ();
1362 g_settings_set_string (settings, "string", "true");
1364 g_settings_bind_with_mapping (settings, "string",
1366 G_SETTINGS_BIND_DEFAULT,
1371 g_settings_set_string (settings, "string", "false");
1372 g_object_get (obj, "bool", &b, NULL);
1373 g_assert_cmpint (b, ==, FALSE);
1375 g_settings_set_string (settings, "string", "not true");
1376 g_object_get (obj, "bool", &b, NULL);
1377 g_assert_cmpint (b, ==, FALSE);
1379 g_object_set (obj, "bool", TRUE, NULL);
1380 s = g_settings_get_string (settings, "string");
1381 g_assert_cmpstr (s, ==, "true");
1384 g_object_unref (obj);
1385 g_object_unref (settings);
1388 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1389 * initial settings value is transported to the object
1390 * side, but later settings changes do not affect the
1394 test_no_change_binding (void)
1397 GSettings *settings;
1400 settings = g_settings_new ("org.gtk.test.binding");
1401 obj = test_object_new ();
1403 g_object_set (obj, "bool", TRUE, NULL);
1404 g_settings_set_boolean (settings, "bool", FALSE);
1406 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1408 g_object_get (obj, "bool", &b, NULL);
1409 g_assert_cmpint (b, ==, FALSE);
1411 g_settings_set_boolean (settings, "bool", TRUE);
1412 g_object_get (obj, "bool", &b, NULL);
1413 g_assert_cmpint (b, ==, FALSE);
1415 g_settings_set_boolean (settings, "bool", FALSE);
1416 g_object_set (obj, "bool", TRUE, NULL);
1417 b = g_settings_get_boolean (settings, "bool");
1418 g_assert_cmpint (b, ==, TRUE);
1420 g_object_unref (obj);
1421 g_object_unref (settings);
1424 /* Test that binding a non-readable property only
1425 * works in 'GET' mode.
1428 test_no_read_binding (void)
1430 if (g_test_undefined ())
1432 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1435 GSettings *settings;
1437 settings = g_settings_new ("org.gtk.test.binding");
1438 obj = test_object_new ();
1440 g_settings_bind (settings, "string", obj, "no-read", 0);
1442 g_test_trap_assert_failed ();
1443 g_test_trap_assert_stderr ("*property*is not readable*");
1446 if (g_test_trap_fork (0, 0))
1449 GSettings *settings;
1451 settings = g_settings_new ("org.gtk.test.binding");
1452 obj = test_object_new ();
1454 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1458 g_test_trap_assert_passed ();
1461 /* Test that binding a non-writable property only
1462 * works in 'SET' mode.
1465 test_no_write_binding (void)
1467 if (g_test_undefined ())
1469 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1472 GSettings *settings;
1474 settings = g_settings_new ("org.gtk.test.binding");
1475 obj = test_object_new ();
1477 g_settings_bind (settings, "string", obj, "no-write", 0);
1479 g_test_trap_assert_failed ();
1480 g_test_trap_assert_stderr ("*property*is not writable*");
1483 if (g_test_trap_fork (0, 0))
1486 GSettings *settings;
1488 settings = g_settings_new ("org.gtk.test.binding");
1489 obj = test_object_new ();
1491 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1495 g_test_trap_assert_passed ();
1499 * Test that using a keyfile works
1504 GSettingsBackend *kf_backend;
1505 GSettings *settings;
1509 g_remove ("gsettings.store");
1511 kf_backend = g_keyfile_settings_backend_new ("gsettings.store", "/", "root");
1512 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1513 g_object_unref (kf_backend);
1515 g_settings_set (settings, "greeting", "s", "see if this works");
1517 keyfile = g_key_file_new ();
1518 g_assert (g_key_file_load_from_file (keyfile, "gsettings.store", 0, NULL));
1520 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1521 g_assert_cmpstr (str, ==, "'see if this works'");
1524 g_key_file_free (keyfile);
1525 g_object_unref (settings);
1528 /* Test that getting child schemas works
1531 test_child_schema (void)
1533 GSettings *settings;
1537 /* first establish some known conditions */
1538 settings = g_settings_new ("org.gtk.test.basic-types");
1539 g_settings_set (settings, "test-byte", "y", 36);
1541 g_settings_get (settings, "test-byte", "y", &byte);
1542 g_assert_cmpint (byte, ==, 36);
1544 g_object_unref (settings);
1546 settings = g_settings_new ("org.gtk.test");
1547 child = g_settings_get_child (settings, "basic-types");
1548 g_assert (child != NULL);
1550 g_settings_get (child, "test-byte", "y", &byte);
1551 g_assert_cmpint (byte, ==, 36);
1553 g_object_unref (child);
1554 g_object_unref (settings);
1558 glib_translations_work (void)
1562 gchar *orig = "Unnamed";
1564 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
1565 if (!setlocale (LC_MESSAGES, "de"))
1568 works = dgettext ("glib20", orig) != orig;
1569 setlocale (LC_MESSAGES, locale);
1575 #include "../strinfo.c"
1580 /* "foo" has a value of 1
1581 * "bar" has a value of 2
1582 * "baz" is an alias for "bar"
1585 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1586 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1588 const guint32 *strinfo = (guint32 *) array;
1589 guint length = sizeof array / 4;
1593 /* build it and compare */
1596 builder = g_string_new (NULL);
1597 strinfo_builder_append_item (builder, "foo", 1);
1598 strinfo_builder_append_item (builder, "bar", 2);
1599 g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
1600 g_assert_cmpint (builder->len % 4, ==, 0);
1601 g_assert_cmpint (builder->len / 4, ==, length);
1602 g_assert (memcmp (builder->str, strinfo, length * 4) == 0);
1603 g_string_free (builder, TRUE);
1606 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
1608 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
1610 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
1612 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
1615 g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
1616 g_assert_cmpint (result, ==, 1);
1617 g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
1618 g_assert_cmpint (result, ==, 2);
1619 g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
1620 g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
1622 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
1623 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
1624 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
1625 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
1627 g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
1628 g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
1629 g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
1630 g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
1636 GSettings *settings, *direct;
1639 settings = g_settings_new ("org.gtk.test.enums");
1640 direct = g_settings_new ("org.gtk.test.enums.direct");
1642 if (g_test_undefined () && !backend_set)
1644 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1645 g_settings_get_enum (direct, "test");
1646 g_test_trap_assert_failed ();
1647 g_test_trap_assert_stderr ("*not associated with an enum*");
1649 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1650 g_settings_set_enum (settings, "test", 42);
1651 g_test_trap_assert_failed ();
1652 g_test_trap_assert_stderr ("*invalid enum value 42*");
1654 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1655 g_settings_set_string (settings, "test", "qux");
1656 g_test_trap_assert_failed ();
1657 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1659 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1660 g_settings_get_flags (settings, "test");
1661 g_test_trap_assert_failed ();
1662 g_test_trap_assert_stderr ("*not associated with a flags*");
1665 str = g_settings_get_string (settings, "test");
1666 g_assert_cmpstr (str, ==, "bar");
1669 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
1671 str = g_settings_get_string (settings, "test");
1672 g_assert_cmpstr (str, ==, "foo");
1675 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
1677 g_settings_set_string (direct, "test", "qux");
1679 str = g_settings_get_string (direct, "test");
1680 g_assert_cmpstr (str, ==, "qux");
1683 str = g_settings_get_string (settings, "test");
1684 g_assert_cmpstr (str, ==, "quux");
1687 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
1693 GSettings *settings, *direct;
1697 settings = g_settings_new ("org.gtk.test.enums");
1698 direct = g_settings_new ("org.gtk.test.enums.direct");
1700 if (g_test_undefined () && !backend_set)
1702 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1703 g_settings_get_flags (direct, "test");
1704 g_test_trap_assert_failed ();
1705 g_test_trap_assert_stderr ("*not associated with a flags*");
1707 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1708 g_settings_set_flags (settings, "f-test", 0x42);
1709 g_test_trap_assert_failed ();
1710 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
1712 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1713 g_settings_set_strv (settings, "f-test",
1714 (const gchar **) g_strsplit ("rock", ",", 0));
1715 g_test_trap_assert_failed ();
1716 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1718 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1719 g_settings_get_enum (settings, "f-test");
1720 g_test_trap_assert_failed ();
1721 g_test_trap_assert_stderr ("*not associated with an enum*");
1724 strv = g_settings_get_strv (settings, "f-test");
1725 str = g_strjoinv (",", strv);
1726 g_assert_cmpstr (str, ==, "");
1730 g_settings_set_flags (settings, "f-test",
1731 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1733 strv = g_settings_get_strv (settings, "f-test");
1734 str = g_strjoinv (",", strv);
1735 g_assert_cmpstr (str, ==, "talking,walking");
1739 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1740 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1742 strv = g_strsplit ("speaking,laughing", ",", 0);
1743 g_settings_set_strv (direct, "f-test", (const gchar **) strv);
1746 strv = g_settings_get_strv (direct, "f-test");
1747 str = g_strjoinv (",", strv);
1748 g_assert_cmpstr (str, ==, "speaking,laughing");
1752 strv = g_settings_get_strv (settings, "f-test");
1753 str = g_strjoinv (",", strv);
1754 g_assert_cmpstr (str, ==, "talking,laughing");
1758 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1759 TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
1765 GSettings *settings, *direct;
1768 settings = g_settings_new ("org.gtk.test.range");
1769 direct = g_settings_new ("org.gtk.test.range.direct");
1771 if (g_test_undefined () && !backend_set)
1773 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1774 g_settings_set_int (settings, "val", 45);
1775 g_test_trap_assert_failed ();
1776 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1778 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1779 g_settings_set_int (settings, "val", 1);
1780 g_test_trap_assert_failed ();
1781 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1784 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1785 g_settings_set_int (direct, "val", 22);
1786 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
1787 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
1788 g_settings_set_int (direct, "val", 45);
1789 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
1790 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1791 g_settings_set_int (direct, "val", 1);
1792 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
1793 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1795 value = g_variant_new_int32 (1);
1796 g_assert (!g_settings_range_check (settings, "val", value));
1797 g_variant_unref (value);
1798 value = g_variant_new_int32 (33);
1799 g_assert (g_settings_range_check (settings, "val", value));
1800 g_variant_unref (value);
1801 value = g_variant_new_int32 (45);
1802 g_assert (!g_settings_range_check (settings, "val", value));
1803 g_variant_unref (value);
1807 strv_has_string (gchar **haystack,
1808 const gchar *needle)
1812 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
1814 if (g_strcmp0 (haystack[n], needle) == 0)
1821 strv_set_equal (gchar **strv, ...)
1830 va_start (list, strv);
1833 str = va_arg (list, const gchar *);
1836 if (!strv_has_string (strv, str))
1846 res = g_strv_length ((gchar**)strv) == count;
1852 test_list_items (void)
1854 GSettings *settings;
1858 settings = g_settings_new ("org.gtk.test");
1859 children = g_settings_list_children (settings);
1860 keys = g_settings_list_keys (settings);
1862 g_assert (strv_set_equal (children, "basic-types", "complex-types", "localized", NULL));
1863 g_assert (strv_set_equal (keys, "greeting", "farewell", NULL));
1865 g_strfreev (children);
1868 g_object_unref (settings);
1872 test_list_schemas (void)
1874 const gchar * const *schemas;
1875 const gchar * const *relocs;
1877 relocs = g_settings_list_relocatable_schemas ();
1878 schemas = g_settings_list_schemas ();
1880 g_assert (strv_set_equal ((gchar **)relocs,
1881 "org.gtk.test.no-path",
1884 g_assert (strv_set_equal ((gchar **)schemas,
1886 "org.gtk.test.basic-types",
1887 "org.gtk.test.complex-types",
1888 "org.gtk.test.localized",
1889 "org.gtk.test.binding",
1890 "org.gtk.test.enums",
1891 "org.gtk.test.enums.direct",
1892 "org.gtk.test.range",
1893 "org.gtk.test.range.direct",
1894 "org.gtk.test.mapped",
1899 map_func (GVariant *value,
1903 gint *state = user_data;
1907 v = g_variant_get_int32 (value);
1913 g_assert_cmpint (v, ==, 1);
1917 else if (*state == 1)
1919 g_assert_cmpint (v, ==, 0);
1925 g_assert (value == NULL);
1926 *result = g_variant_new_int32 (5);
1932 test_get_mapped (void)
1934 GSettings *settings;
1939 settings = g_settings_new ("org.gtk.test.mapped");
1940 g_settings_set_int (settings, "val", 1);
1943 p = g_settings_get_mapped (settings, "val", map_func, &state);
1944 val = g_variant_get_int32 ((GVariant*)p);
1945 g_assert_cmpint (val, ==, 5);
1947 g_variant_unref (p);
1948 g_object_unref (settings);
1952 test_get_range (void)
1954 GSettings *settings;
1957 settings = g_settings_new ("org.gtk.test.range");
1958 range = g_settings_get_range (settings, "val");
1959 check_and_free (range, "('range', <(2, 44)>)");
1960 g_object_unref (settings);
1962 settings = g_settings_new ("org.gtk.test.enums");
1963 range = g_settings_get_range (settings, "test");
1964 check_and_free (range, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
1965 g_object_unref (settings);
1967 settings = g_settings_new ("org.gtk.test.enums");
1968 range = g_settings_get_range (settings, "f-test");
1969 check_and_free (range, "('flags', "
1970 "<['mourning', 'laughing', 'talking', 'walking']>)");
1971 g_object_unref (settings);
1973 settings = g_settings_new ("org.gtk.test");
1974 range = g_settings_get_range (settings, "greeting");
1975 check_and_free (range, "('type', <@as []>)");
1976 g_object_unref (settings);
1980 test_schema_source (void)
1982 GSettingsSchemaSource *parent;
1983 GSettingsSchemaSource *source;
1984 GSettingsBackend *backend;
1985 GSettingsSchema *schema;
1986 GError *error = NULL;
1987 GSettings *settings;
1990 backend = g_settings_backend_get_default ();
1992 /* make sure it fails properly */
1993 parent = g_settings_schema_source_get_default ();
1994 source = g_settings_schema_source_new_from_directory ("/path/that/does/not/exist", parent, TRUE, &error);
1995 g_assert (source == NULL);
1996 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
1997 g_clear_error (&error);
1999 /* create a source with the parent */
2000 source = g_settings_schema_source_new_from_directory ("schema-source", parent, TRUE, &error);
2001 g_assert_no_error (error);
2002 g_assert (source != NULL);
2004 /* check recursive lookups are working */
2005 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2006 g_assert (schema != NULL);
2007 g_settings_schema_unref (schema);
2009 /* check recursive lookups for non-existent schemas */
2010 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", TRUE);
2011 g_assert (schema == NULL);
2013 /* check non-recursive for schema that only exists in lower layers */
2014 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2015 g_assert (schema == NULL);
2017 /* check non-recursive lookup for non-existent */
2018 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", FALSE);
2019 g_assert (schema == NULL);
2021 /* check non-recursive for schema that exists in toplevel */
2022 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2023 g_assert (schema != NULL);
2024 g_settings_schema_unref (schema);
2026 /* check recursive for schema that exists in toplevel */
2027 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2028 g_assert (schema != NULL);
2030 /* try to use it for something */
2031 settings = g_settings_new_full (schema, backend, g_settings_schema_get_path (schema));
2032 g_settings_schema_unref (schema);
2034 g_settings_get (settings, "enabled", "b", &enabled);
2036 g_object_unref (settings);
2038 g_settings_schema_source_unref (source);
2040 /* try again, but with no parent */
2041 source = g_settings_schema_source_new_from_directory ("schema-source", NULL, FALSE, NULL);
2042 g_assert (source != NULL);
2044 /* should not find it this time, even if recursive... */
2045 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2046 g_assert (schema == NULL);
2047 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2048 g_assert (schema == NULL);
2050 /* should still find our own... */
2051 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2052 g_assert (schema != NULL);
2053 g_settings_schema_unref (schema);
2054 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2055 g_assert (schema != NULL);
2056 g_settings_schema_unref (schema);
2058 g_settings_schema_source_unref (source);
2064 GAction *string, *toggle;
2065 gboolean c1, c2, c3;
2066 GSettings *settings;
2068 GVariantType *param_type;
2070 GVariantType *state_type;
2073 settings = g_settings_new ("org.gtk.test.basic-types");
2074 string = g_settings_create_action (settings, "test-string");
2075 toggle = g_settings_create_action (settings, "test-boolean");
2076 g_object_unref (settings); /* should be held by the actions */
2078 g_signal_connect (settings, "changed", G_CALLBACK (changed_cb2), &c1);
2079 g_signal_connect (string, "notify::state", G_CALLBACK (changed_cb2), &c2);
2080 g_signal_connect (toggle, "notify::state", G_CALLBACK (changed_cb2), &c3);
2082 c1 = c2 = c3 = FALSE;
2083 g_settings_set_string (settings, "test-string", "hello world");
2084 check_and_free (g_action_get_state (string), "'hello world'");
2085 g_assert (c1 && c2 && !c3);
2086 c1 = c2 = c3 = FALSE;
2088 g_action_activate (string, g_variant_new_string ("hihi"));
2089 check_and_free (g_settings_get_value (settings, "test-string"), "'hihi'");
2090 g_assert (c1 && c2 && !c3);
2091 c1 = c2 = c3 = FALSE;
2093 g_action_change_state (string, g_variant_new_string ("kthxbye"));
2094 check_and_free (g_settings_get_value (settings, "test-string"), "'kthxbye'");
2095 g_assert (c1 && c2 && !c3);
2096 c1 = c2 = c3 = FALSE;
2098 g_action_change_state (toggle, g_variant_new_boolean (TRUE));
2099 g_assert (g_settings_get_boolean (settings, "test-boolean"));
2100 g_assert (c1 && !c2 && c3);
2101 c1 = c2 = c3 = FALSE;
2103 g_action_activate (toggle, NULL);
2104 g_assert (!g_settings_get_boolean (settings, "test-boolean"));
2105 g_assert (c1 && !c2 && c3);
2107 g_object_get (string,
2109 "parameter-type", ¶m_type,
2110 "enabled", &enabled,
2111 "state-type", &state_type,
2115 g_assert_cmpstr (name, ==, "test-string");
2116 g_assert (g_variant_type_equal (param_type, G_VARIANT_TYPE_STRING));
2118 g_assert (g_variant_type_equal (state_type, G_VARIANT_TYPE_STRING));
2119 g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "kthxbye");
2122 g_variant_unref (state);
2124 g_object_unref (string);
2125 g_object_unref (toggle);
2129 main (int argc, char *argv[])
2134 setlocale (LC_ALL, "");
2136 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
2138 g_setenv ("XDG_DATA_DIRS", ".", TRUE);
2139 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
2142 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
2144 g_test_init (&argc, &argv, NULL);
2146 g_remove ("org.gtk.test.enums.xml");
2147 g_assert (g_spawn_command_line_sync ("../../gobject/glib-mkenums "
2148 "--template " SRCDIR "/enums.xml.template "
2149 SRCDIR "/testenum.h",
2150 &enums, NULL, &result, NULL));
2151 g_assert (result == 0);
2152 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
2155 g_remove ("gschemas.compiled");
2156 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=. "
2157 "--schema-file=org.gtk.test.enums.xml "
2158 "--schema-file=" SRCDIR "/org.gtk.test.gschema.xml",
2159 NULL, NULL, &result, NULL));
2160 g_assert (result == 0);
2162 g_remove ("schema-source/gschemas.compiled");
2163 g_mkdir ("schema-source", 0777);
2164 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=schema-source "
2165 "--schema-file=" SRCDIR "/org.gtk.schemasourcecheck.gschema.xml",
2166 NULL, NULL, &result, NULL));
2167 g_assert (result == 0);
2169 g_test_add_func ("/gsettings/basic", test_basic);
2173 g_test_add_func ("/gsettings/no-schema", test_no_schema);
2174 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
2175 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
2176 g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
2177 g_test_add_func ("/gsettings/no-path", test_no_path);
2180 g_test_add_func ("/gsettings/basic-types", test_basic_types);
2181 g_test_add_func ("/gsettings/complex-types", test_complex_types);
2182 g_test_add_func ("/gsettings/changes", test_changes);
2184 if (glib_translations_work ())
2186 g_test_add_func ("/gsettings/l10n", test_l10n);
2187 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
2190 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
2191 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
2192 g_test_add_func ("/gsettings/atomic", test_atomic);
2194 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
2195 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
2196 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
2197 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
2198 g_test_add_func ("/gsettings/unbinding", test_unbind);
2199 g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
2203 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
2204 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
2205 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
2208 g_test_add_func ("/gsettings/keyfile", test_keyfile);
2209 g_test_add_func ("/gsettings/child-schema", test_child_schema);
2210 g_test_add_func ("/gsettings/strinfo", test_strinfo);
2211 g_test_add_func ("/gsettings/enums", test_enums);
2212 g_test_add_func ("/gsettings/flags", test_flags);
2213 g_test_add_func ("/gsettings/range", test_range);
2214 g_test_add_func ("/gsettings/list-items", test_list_items);
2215 g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
2216 g_test_add_func ("/gsettings/mapped", test_get_mapped);
2217 g_test_add_func ("/gsettings/get-range", test_get_range);
2218 g_test_add_func ("/gsettings/schema-source", test_schema_source);
2219 g_test_add_func ("/gsettings/actions", test_actions);
2221 result = g_test_run ();