8 #define G_SETTINGS_ENABLE_BACKEND
9 #include <gio/gsettingsbackend.h>
13 static const gchar *locale_dir = ".";
15 static gboolean backend_set;
17 /* These tests rely on the schemas in org.gtk.test.gschema.xml
18 * to be compiled and installed in the same directory.
27 setup (Fixture *fixture,
28 gconstpointer user_data)
32 fixture->tmp_dir = g_dir_make_tmp ("gio-test-gsettings_XXXXXX", &error);
33 g_assert_no_error (error);
35 g_test_message ("Using temporary directory: %s", fixture->tmp_dir);
39 teardown (Fixture *fixture,
40 gconstpointer user_data)
42 g_assert_no_errno (g_rmdir (fixture->tmp_dir));
43 g_clear_pointer (&fixture->tmp_dir, g_free);
47 check_and_free (GVariant *value,
48 const gchar *expected)
52 printed = g_variant_print (value, TRUE);
53 g_assert_cmpstr (printed, ==, expected);
56 g_variant_unref (value);
60 /* Just to get warmed up: Read and set a string, and
61 * verify that can read the changed string back
69 gboolean has_unapplied;
73 settings = g_settings_new ("org.gtk.test");
75 g_object_get (settings,
79 "has-unapplied", &has_unapplied,
80 "delay-apply", &delay_apply,
82 g_assert_cmpstr (str, ==, "org.gtk.test");
84 g_assert_cmpstr (path, ==, "/tests/");
85 g_assert_false (has_unapplied);
86 g_assert_false (delay_apply);
91 g_settings_get (settings, "greeting", "s", &str);
92 g_assert_cmpstr (str, ==, "Hello, earthlings");
95 g_settings_set (settings, "greeting", "s", "goodbye world");
96 g_settings_get (settings, "greeting", "s", &str);
97 g_assert_cmpstr (str, ==, "goodbye world");
101 if (!backend_set && g_test_undefined ())
103 GSettings *tmp_settings = g_settings_new ("org.gtk.test");
105 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
106 "*g_settings_set_value*expects type*");
107 g_settings_set (tmp_settings, "greeting", "i", 555);
108 g_test_assert_expected_messages ();
110 g_object_unref (tmp_settings);
113 g_settings_get (settings, "greeting", "s", &str);
114 g_assert_cmpstr (str, ==, "goodbye world");
118 g_settings_reset (settings, "greeting");
119 str = g_settings_get_string (settings, "greeting");
120 g_assert_cmpstr (str, ==, "Hello, earthlings");
123 g_settings_set (settings, "greeting", "s", "this is the end");
124 g_object_unref (settings);
127 /* Check that we get an error when getting a key
128 * that is not in the schema
131 test_unknown_key (void)
133 if (!g_test_undefined ())
136 if (g_test_subprocess ())
141 settings = g_settings_new ("org.gtk.test");
142 value = g_settings_get_value (settings, "no_such_key");
144 g_assert_null (value);
146 g_object_unref (settings);
149 g_test_trap_subprocess (NULL, 0, 0);
150 g_test_trap_assert_failed ();
151 g_test_trap_assert_stderr ("*does not contain*");
154 /* Check that we get an error when the schema
155 * has not been installed
158 test_no_schema (void)
160 if (!g_test_undefined ())
163 if (g_test_subprocess ())
167 settings = g_settings_new ("no.such.schema");
169 g_assert_null (settings);
172 g_test_trap_subprocess (NULL, 0, 0);
173 g_test_trap_assert_failed ();
174 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
177 /* Check that we get an error when passing a type string
178 * that does not match the schema
181 test_wrong_type (void)
186 if (!g_test_undefined ())
189 settings = g_settings_new ("org.gtk.test");
191 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
192 "*given value has a type of*");
193 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
194 "*valid_format_string*");
195 g_settings_get (settings, "greeting", "o", &str);
196 g_test_assert_expected_messages ();
200 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
201 "*expects type 's'*");
202 g_settings_set (settings, "greeting", "o", "/a/path");
203 g_test_assert_expected_messages ();
205 g_object_unref (settings);
208 /* Check errors with explicit paths */
210 test_wrong_path (void)
212 if (!g_test_undefined ())
215 if (g_test_subprocess ())
217 GSettings *settings G_GNUC_UNUSED;
219 settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
222 g_test_trap_subprocess (NULL, 0, 0);
223 g_test_trap_assert_failed ();
224 g_test_trap_assert_stderr ("*but path * specified by schema*");
230 if (!g_test_undefined ())
233 if (g_test_subprocess ())
235 GSettings *settings G_GNUC_UNUSED;
237 settings = g_settings_new ("org.gtk.test.no-path");
240 g_test_trap_subprocess (NULL, 0, 0);
241 g_test_trap_assert_failed ();
242 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
246 /* Check that we can successfully read and set the full
247 * range of all basic types
250 test_basic_types (void)
264 settings = g_settings_new ("org.gtk.test.basic-types");
266 g_settings_get (settings, "test-boolean", "b", &b);
267 g_assert_cmpint (b, ==, 1);
269 g_settings_set (settings, "test-boolean", "b", 0);
270 g_settings_get (settings, "test-boolean", "b", &b);
271 g_assert_cmpint (b, ==, 0);
273 g_settings_get (settings, "test-byte", "y", &byte);
274 g_assert_cmpint (byte, ==, 25);
276 g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
277 g_settings_get (settings, "test-byte", "y", &byte);
278 g_assert_cmpint (byte, ==, G_MAXUINT8);
280 g_settings_get (settings, "test-int16", "n", &i16);
281 g_assert_cmpint (i16, ==, -1234);
283 g_settings_set (settings, "test-int16", "n", G_MININT16);
284 g_settings_get (settings, "test-int16", "n", &i16);
285 g_assert_cmpint (i16, ==, G_MININT16);
287 g_settings_set (settings, "test-int16", "n", G_MAXINT16);
288 g_settings_get (settings, "test-int16", "n", &i16);
289 g_assert_cmpint (i16, ==, G_MAXINT16);
291 g_settings_get (settings, "test-uint16", "q", &u16);
292 g_assert_cmpuint (u16, ==, 1234);
294 g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
295 g_settings_get (settings, "test-uint16", "q", &u16);
296 g_assert_cmpuint (u16, ==, G_MAXUINT16);
298 g_settings_get (settings, "test-int32", "i", &i32);
299 g_assert_cmpint (i32, ==, -123456);
301 g_settings_set (settings, "test-int32", "i", G_MININT32);
302 g_settings_get (settings, "test-int32", "i", &i32);
303 g_assert_cmpint (i32, ==, G_MININT32);
305 g_settings_set (settings, "test-int32", "i", G_MAXINT32);
306 g_settings_get (settings, "test-int32", "i", &i32);
307 g_assert_cmpint (i32, ==, G_MAXINT32);
309 g_settings_get (settings, "test-uint32", "u", &u32);
310 g_assert_cmpuint (u32, ==, 123456);
312 g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
313 g_settings_get (settings, "test-uint32", "u", &u32);
314 g_assert_cmpuint (u32, ==, G_MAXUINT32);
316 g_settings_get (settings, "test-int64", "x", &i64);
317 g_assert_cmpuint (i64, ==, -123456789);
319 g_settings_set (settings, "test-int64", "x", G_MININT64);
320 g_settings_get (settings, "test-int64", "x", &i64);
321 g_assert_cmpuint (i64, ==, G_MININT64);
323 g_settings_set (settings, "test-int64", "x", G_MAXINT64);
324 g_settings_get (settings, "test-int64", "x", &i64);
325 g_assert_cmpuint (i64, ==, G_MAXINT64);
327 g_settings_get (settings, "test-uint64", "t", &u64);
328 g_assert_cmpuint (u64, ==, 123456789);
330 g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
331 g_settings_get (settings, "test-uint64", "t", &u64);
332 g_assert_cmpuint (u64, ==, G_MAXUINT64);
334 g_settings_get (settings, "test-double", "d", &d);
335 g_assert_cmpfloat (d, ==, 123.456);
337 g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
338 g_settings_get (settings, "test-double", "d", &d);
339 g_assert_cmpfloat (d, ==, G_MINDOUBLE);
341 g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
342 g_settings_get (settings, "test-double", "d", &d);
343 g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
345 g_settings_get (settings, "test-string", "s", &str);
346 g_assert_cmpstr (str, ==, "a string, it seems");
350 g_settings_get (settings, "test-objectpath", "o", &str);
351 g_assert_cmpstr (str, ==, "/a/object/path");
352 g_object_unref (settings);
357 /* Check that we can read an set complex types like
358 * tuples, arrays and dictionaries
361 test_complex_types (void)
366 GVariantIter *iter = NULL;
369 settings = g_settings_new ("org.gtk.test.complex-types");
371 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
372 g_assert_cmpstr (s, ==, "one");
373 g_assert_cmpint (i1,==, 2);
374 g_assert_cmpint (i2,==, 3);
378 g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
379 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
380 g_assert_cmpstr (s, ==, "none");
381 g_assert_cmpint (i1,==, 0);
382 g_assert_cmpint (i2,==, 0);
386 g_settings_get (settings, "test-array", "ai", &iter);
387 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
388 g_assert_true (g_variant_iter_next (iter, "i", &i1));
389 g_assert_cmpint (i1, ==, 0);
390 g_assert_true (g_variant_iter_next (iter, "i", &i1));
391 g_assert_cmpint (i1, ==, 1);
392 g_assert_true (g_variant_iter_next (iter, "i", &i1));
393 g_assert_cmpint (i1, ==, 2);
394 g_assert_true (g_variant_iter_next (iter, "i", &i1));
395 g_assert_cmpint (i1, ==, 3);
396 g_assert_true (g_variant_iter_next (iter, "i", &i1));
397 g_assert_cmpint (i1, ==, 4);
398 g_assert_true (g_variant_iter_next (iter, "i", &i1));
399 g_assert_cmpint (i1, ==, 5);
400 g_assert_false (g_variant_iter_next (iter, "i", &i1));
401 g_variant_iter_free (iter);
403 g_settings_get (settings, "test-dict", "a{sau}", &iter);
404 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 2);
405 g_assert_true (g_variant_iter_next (iter, "{&s@au}", &s, &v));
406 g_assert_cmpstr (s, ==, "AC");
407 g_assert_cmpstr ((char *)g_variant_get_type (v), ==, "au");
409 g_assert_true (g_variant_iter_next (iter, "{&s@au}", &s, &v));
410 g_assert_cmpstr (s, ==, "IV");
411 g_assert_cmpstr ((char *)g_variant_get_type (v), ==, "au");
413 g_variant_iter_free (iter);
415 v = g_settings_get_value (settings, "test-dict");
416 g_assert_cmpstr ((char *)g_variant_get_type (v), ==, "a{sau}");
419 g_object_unref (settings);
422 static gboolean changed_cb_called;
425 changed_cb (GSettings *settings,
429 changed_cb_called = TRUE;
431 g_assert_cmpstr (key, ==, data);
434 /* Test that basic change notification with the changed signal works.
440 GSettings *settings2;
442 settings = g_settings_new ("org.gtk.test");
444 g_signal_connect (settings, "changed",
445 G_CALLBACK (changed_cb), "greeting");
447 changed_cb_called = FALSE;
449 g_settings_set (settings, "greeting", "s", "new greeting");
450 g_assert_true (changed_cb_called);
452 settings2 = g_settings_new ("org.gtk.test");
454 changed_cb_called = FALSE;
456 g_settings_set (settings2, "greeting", "s", "hi");
457 g_assert_true (changed_cb_called);
459 g_object_unref (settings2);
460 g_object_unref (settings);
463 static gboolean changed_cb_called2;
466 changed_cb2 (GSettings *settings,
475 /* Test that changes done to a delay-mode instance
476 * don't appear to the outside world until apply. Also
477 * check that we get change notification when they are
479 * Also test that the has-unapplied property is properly
483 test_delay_apply (void)
486 GSettings *settings2;
492 settings = g_settings_new ("org.gtk.test");
493 settings2 = g_settings_new ("org.gtk.test");
495 g_settings_set (settings2, "greeting", "s", "top o' the morning");
497 changed_cb_called = FALSE;
498 changed_cb_called2 = FALSE;
500 g_signal_connect (settings, "changed",
501 G_CALLBACK (changed_cb2), &changed_cb_called);
502 g_signal_connect (settings2, "changed",
503 G_CALLBACK (changed_cb2), &changed_cb_called2);
505 g_settings_delay (settings);
507 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
509 g_assert_true (changed_cb_called);
510 g_assert_false (changed_cb_called2);
512 /* Try resetting the key and ensure a notification is emitted on the delayed #GSettings object. */
513 changed_cb_called = FALSE;
514 changed_cb_called2 = FALSE;
516 g_settings_reset (settings, "greeting");
518 g_assert_true (changed_cb_called);
519 g_assert_false (changed_cb_called2);
521 /* Locally change the greeting again. */
522 changed_cb_called = FALSE;
523 changed_cb_called2 = FALSE;
525 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
527 g_assert_true (changed_cb_called);
528 g_assert_false (changed_cb_called2);
530 writable = g_settings_is_writable (settings, "greeting");
531 g_assert_true (writable);
533 g_settings_get (settings, "greeting", "s", &str);
534 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
538 v = g_settings_get_user_value (settings, "greeting");
539 s = g_variant_get_string (v, NULL);
540 g_assert_cmpstr (s, ==, "greetings from test_delay_apply");
543 g_settings_get (settings2, "greeting", "s", &str);
544 g_assert_cmpstr (str, ==, "top o' the morning");
548 g_assert_true (g_settings_get_has_unapplied (settings));
549 g_assert_false (g_settings_get_has_unapplied (settings2));
551 changed_cb_called = FALSE;
552 changed_cb_called2 = FALSE;
554 g_settings_apply (settings);
556 g_assert_false (changed_cb_called);
557 g_assert_true (changed_cb_called2);
559 g_settings_get (settings, "greeting", "s", &str);
560 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
564 g_settings_get (settings2, "greeting", "s", &str);
565 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
569 g_assert_false (g_settings_get_has_unapplied (settings));
570 g_assert_false (g_settings_get_has_unapplied (settings2));
572 g_settings_reset (settings, "greeting");
573 g_settings_apply (settings);
575 g_settings_get (settings, "greeting", "s", &str);
576 g_assert_cmpstr (str, ==, "Hello, earthlings");
579 g_object_unref (settings2);
580 g_object_unref (settings);
583 /* Test that reverting unapplied changes in a delay-apply
584 * settings instance works.
587 test_delay_revert (void)
590 GSettings *settings2;
593 settings = g_settings_new ("org.gtk.test");
594 settings2 = g_settings_new ("org.gtk.test");
596 g_settings_set (settings2, "greeting", "s", "top o' the morning");
598 g_settings_get (settings, "greeting", "s", &str);
599 g_assert_cmpstr (str, ==, "top o' the morning");
602 g_settings_delay (settings);
604 g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
606 g_settings_get (settings, "greeting", "s", &str);
607 g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
611 g_settings_get (settings2, "greeting", "s", &str);
612 g_assert_cmpstr (str, ==, "top o' the morning");
616 g_assert_true (g_settings_get_has_unapplied (settings));
618 g_settings_revert (settings);
620 g_assert_false (g_settings_get_has_unapplied (settings));
622 g_settings_get (settings, "greeting", "s", &str);
623 g_assert_cmpstr (str, ==, "top o' the morning");
627 g_settings_get (settings2, "greeting", "s", &str);
628 g_assert_cmpstr (str, ==, "top o' the morning");
632 g_object_unref (settings2);
633 g_object_unref (settings);
637 test_delay_child (void)
645 base = g_settings_new ("org.gtk.test.basic-types");
646 g_settings_set (base, "test-byte", "y", 36);
648 settings = g_settings_new ("org.gtk.test");
649 g_settings_delay (settings);
650 g_object_get (settings, "delay-apply", &delay, NULL);
651 g_assert_true (delay);
653 child = g_settings_get_child (settings, "basic-types");
654 g_assert_nonnull (child);
656 g_object_get (child, "delay-apply", &delay, NULL);
657 g_assert_false (delay);
659 g_settings_get (child, "test-byte", "y", &byte);
660 g_assert_cmpuint (byte, ==, 36);
662 g_settings_set (child, "test-byte", "y", 42);
664 /* make sure the child was delayed too */
665 g_settings_get (base, "test-byte", "y", &byte);
666 g_assert_cmpuint (byte, ==, 36);
668 g_object_unref (child);
669 g_object_unref (settings);
670 g_object_unref (base);
674 keys_changed_cb (GSettings *settings,
680 g_assert_cmpint (n_keys, ==, 2);
682 g_assert_true ((keys[0] == g_quark_from_static_string ("greeting") &&
683 keys[1] == g_quark_from_static_string ("farewell")) ||
684 (keys[1] == g_quark_from_static_string ("greeting") &&
685 keys[0] == g_quark_from_static_string ("farewell")));
687 g_settings_get (settings, "greeting", "s", &str);
688 g_assert_cmpstr (str, ==, "greetings from test_atomic");
692 g_settings_get (settings, "farewell", "s", &str);
693 g_assert_cmpstr (str, ==, "atomic bye-bye");
698 /* Check that delay-applied changes appear atomically.
699 * More specifically, verify that all changed keys appear
700 * with their new value while handling the change-event signal.
706 GSettings *settings2;
709 settings = g_settings_new ("org.gtk.test");
710 settings2 = g_settings_new ("org.gtk.test");
712 g_settings_set (settings2, "greeting", "s", "top o' the morning");
714 changed_cb_called = FALSE;
715 changed_cb_called2 = FALSE;
717 g_signal_connect (settings2, "change-event",
718 G_CALLBACK (keys_changed_cb), NULL);
720 g_settings_delay (settings);
722 g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
723 g_settings_set (settings, "farewell", "s", "atomic bye-bye");
725 g_settings_apply (settings);
727 g_settings_get (settings, "greeting", "s", &str);
728 g_assert_cmpstr (str, ==, "greetings from test_atomic");
732 g_settings_get (settings, "farewell", "s", &str);
733 g_assert_cmpstr (str, ==, "atomic bye-bye");
737 g_settings_get (settings2, "greeting", "s", &str);
738 g_assert_cmpstr (str, ==, "greetings from test_atomic");
742 g_settings_get (settings2, "farewell", "s", &str);
743 g_assert_cmpstr (str, ==, "atomic bye-bye");
747 g_object_unref (settings2);
748 g_object_unref (settings);
751 /* On Windows the interaction between the C library locale and libintl
752 * (from GNU gettext) is not like on POSIX, so just skip these tests
755 * There are several issues:
757 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
758 * in libintl (defined in its <libintl.h>).
760 * 2) The locale names that setlocale() accepts and returns aren't in
761 * the "de_DE" style, but like "German_Germany".
763 * 3) libintl looks at the Win32 thread locale and not the C library
764 * locale. (And even if libintl would use the C library's locale, as
765 * there are several alternative C library DLLs, libintl might be
766 * linked to a different one than the application code, so they
767 * wouldn't have the same C library locale anyway.)
770 /* Test that translations work for schema defaults.
772 * This test relies on the de.po file in the same directory
773 * to be compiled into ./de/LC_MESSAGES/test.mo
782 bindtextdomain ("test", locale_dir);
783 bind_textdomain_codeset ("test", "UTF-8");
785 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
787 settings = g_settings_new ("org.gtk.test.localized");
789 g_setenv ("LC_MESSAGES", "C", TRUE);
790 setlocale (LC_MESSAGES, "C");
791 str = g_settings_get_string (settings, "error-message");
792 g_setenv ("LC_MESSAGES", locale, TRUE);
793 setlocale (LC_MESSAGES, locale);
795 g_assert_cmpstr (str, ==, "Unnamed");
799 g_setenv ("LC_MESSAGES", "de_DE.UTF-8", TRUE);
800 setlocale (LC_MESSAGES, "de_DE.UTF-8");
801 /* Only do the test if translation is actually working... */
802 if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
804 str = g_settings_get_string (settings, "error-message");
806 g_assert_cmpstr (str, ==, "Unbenannt");
811 g_printerr ("warning: translation is not working... skipping test. ");
813 g_setenv ("LC_MESSAGES", locale, TRUE);
814 setlocale (LC_MESSAGES, locale);
816 g_object_unref (settings);
819 /* Test that message context works as expected with translated
820 * schema defaults. Also, verify that non-ASCII UTF-8 content
823 * This test relies on the de.po file in the same directory
824 * to be compiled into ./de/LC_MESSAGES/test.mo
827 test_l10n_context (void)
833 bindtextdomain ("test", locale_dir);
834 bind_textdomain_codeset ("test", "UTF-8");
836 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
838 settings = g_settings_new ("org.gtk.test.localized");
840 g_setenv ("LC_MESSAGES", "C", TRUE);
841 setlocale (LC_MESSAGES, "C");
842 g_settings_get (settings, "backspace", "s", &str);
843 g_setenv ("LC_MESSAGES", locale, TRUE);
844 setlocale (LC_MESSAGES, locale);
846 g_assert_cmpstr (str, ==, "BackSpace");
850 g_setenv ("LC_MESSAGES", "de_DE.UTF-8", TRUE);
851 setlocale (LC_MESSAGES, "de_DE.UTF-8");
852 /* Only do the test if translation is actually working... */
853 if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
855 g_settings_get (settings, "backspace", "s", &str);
857 g_assert_cmpstr (str, ==, "Löschen");
862 g_printerr ("warning: translation is not working... skipping test. ");
864 g_setenv ("LC_MESSAGES", locale, TRUE);
865 setlocale (LC_MESSAGES, locale);
867 g_object_unref (settings);
893 GObject parent_instance;
896 gboolean anti_bool_prop;
907 gchar *no_write_prop;
915 GObjectClass parent_class;
918 static GType test_object_get_type (void);
919 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
922 test_object_init (TestObject *object)
927 test_object_finalize (GObject *object)
929 TestObject *testo = (TestObject*)object;
930 g_strfreev (testo->strv_prop);
931 g_free (testo->string_prop);
932 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
936 test_object_get_property (GObject *object,
941 TestObject *test_object = (TestObject *)object;
946 g_value_set_boolean (value, test_object->bool_prop);
949 g_value_set_boolean (value, test_object->anti_bool_prop);
952 g_value_set_schar (value, test_object->byte_prop);
955 g_value_set_uint (value, test_object->uint16_prop);
958 g_value_set_int (value, test_object->int16_prop);
961 g_value_set_int (value, test_object->int_prop);
964 g_value_set_uint (value, test_object->uint_prop);
967 g_value_set_int64 (value, test_object->int64_prop);
970 g_value_set_uint64 (value, test_object->uint64_prop);
973 g_value_set_double (value, test_object->double_prop);
976 g_value_set_string (value, test_object->string_prop);
979 g_value_set_string (value, test_object->no_write_prop);
982 g_value_set_boxed (value, test_object->strv_prop);
985 g_value_set_enum (value, test_object->enum_prop);
988 g_value_set_flags (value, test_object->flags_prop);
991 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
997 test_object_set_property (GObject *object,
1002 TestObject *test_object = (TestObject *)object;
1007 test_object->bool_prop = g_value_get_boolean (value);
1009 case PROP_ANTI_BOOL:
1010 test_object->anti_bool_prop = g_value_get_boolean (value);
1013 test_object->byte_prop = g_value_get_schar (value);
1016 test_object->int16_prop = g_value_get_int (value);
1019 test_object->uint16_prop = g_value_get_uint (value);
1022 test_object->int_prop = g_value_get_int (value);
1025 test_object->uint_prop = g_value_get_uint (value);
1028 test_object->int64_prop = g_value_get_int64 (value);
1031 test_object->uint64_prop = g_value_get_uint64 (value);
1034 test_object->double_prop = g_value_get_double (value);
1037 g_free (test_object->string_prop);
1038 test_object->string_prop = g_value_dup_string (value);
1041 g_free (test_object->no_read_prop);
1042 test_object->no_read_prop = g_value_dup_string (value);
1045 g_strfreev (test_object->strv_prop);
1046 test_object->strv_prop = g_value_dup_boxed (value);
1049 test_object->enum_prop = g_value_get_enum (value);
1052 test_object->flags_prop = g_value_get_flags (value);
1055 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1061 test_enum_get_type (void)
1063 static gsize define_type_id = 0;
1065 if (g_once_init_enter (&define_type_id))
1067 static const GEnumValue values[] = {
1068 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
1069 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
1070 { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
1071 { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
1075 GType type_id = g_enum_register_static ("TestEnum", values);
1076 g_once_init_leave (&define_type_id, type_id);
1079 return define_type_id;
1083 test_flags_get_type (void)
1085 static gsize define_type_id = 0;
1087 if (g_once_init_enter (&define_type_id))
1089 static const GFlagsValue values[] = {
1090 { TEST_FLAGS_NONE, "TEST_FLAGS_NONE", "none" },
1091 { TEST_FLAGS_MOURNING, "TEST_FLAGS_MOURNING", "mourning" },
1092 { TEST_FLAGS_LAUGHING, "TEST_FLAGS_LAUGHING", "laughing" },
1093 { TEST_FLAGS_WALKING, "TEST_FLAGS_WALKING", "walking" },
1097 GType type_id = g_flags_register_static ("TestFlags", values);
1098 g_once_init_leave (&define_type_id, type_id);
1101 return define_type_id;
1105 test_object_class_init (TestObjectClass *class)
1107 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
1109 gobject_class->get_property = test_object_get_property;
1110 gobject_class->set_property = test_object_set_property;
1111 gobject_class->finalize = test_object_finalize;
1113 g_object_class_install_property (gobject_class, PROP_BOOL,
1114 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
1115 g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
1116 g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
1117 g_object_class_install_property (gobject_class, PROP_BYTE,
1118 g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
1119 g_object_class_install_property (gobject_class, PROP_INT16,
1120 g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
1121 g_object_class_install_property (gobject_class, PROP_UINT16,
1122 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
1123 g_object_class_install_property (gobject_class, PROP_INT,
1124 g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
1125 g_object_class_install_property (gobject_class, PROP_UINT,
1126 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
1127 g_object_class_install_property (gobject_class, PROP_INT64,
1128 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
1129 g_object_class_install_property (gobject_class, PROP_UINT64,
1130 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
1131 g_object_class_install_property (gobject_class, PROP_DOUBLE,
1132 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
1133 g_object_class_install_property (gobject_class, PROP_STRING,
1134 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
1135 g_object_class_install_property (gobject_class, PROP_NO_WRITE,
1136 g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
1137 g_object_class_install_property (gobject_class, PROP_NO_READ,
1138 g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
1139 g_object_class_install_property (gobject_class, PROP_STRV,
1140 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
1141 g_object_class_install_property (gobject_class, PROP_ENUM,
1142 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
1143 g_object_class_install_property (gobject_class, PROP_FLAGS,
1144 g_param_spec_flags ("flags", "", "", test_flags_get_type (), TEST_FLAGS_NONE, G_PARAM_READWRITE));
1148 test_object_new (void)
1150 return (TestObject*)g_object_new (test_object_get_type (), NULL);
1153 /* Test basic binding functionality for simple types.
1154 * Verify that with bidirectional bindings, changes on either side
1155 * are notified on the other end.
1158 test_simple_binding (void)
1161 GSettings *settings;
1177 settings = g_settings_new ("org.gtk.test.binding");
1178 obj = test_object_new ();
1180 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
1181 g_object_set (obj, "bool", TRUE, NULL);
1182 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1184 g_settings_set_boolean (settings, "bool", FALSE);
1186 g_object_get (obj, "bool", &b, NULL);
1187 g_assert_cmpint (b, ==, FALSE);
1189 g_settings_bind (settings, "anti-bool", obj, "anti-bool",
1190 G_SETTINGS_BIND_INVERT_BOOLEAN);
1191 g_object_set (obj, "anti-bool", FALSE, NULL);
1192 g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
1194 g_settings_set_boolean (settings, "anti-bool", FALSE);
1196 g_object_get (obj, "anti-bool", &b, NULL);
1197 g_assert_cmpint (b, ==, TRUE);
1199 g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
1201 g_object_set (obj, "byte", 123, NULL);
1203 g_settings_get (settings, "byte", "y", &y);
1204 g_assert_cmpint (y, ==, 123);
1206 g_settings_set (settings, "byte", "y", 54);
1208 g_object_get (obj, "byte", &y, NULL);
1209 g_assert_cmpint (y, ==, 54);
1211 g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
1213 g_object_set (obj, "int16", 1234, NULL);
1215 g_settings_get (settings, "int16", "n", &n);
1216 g_assert_cmpint (n, ==, 1234);
1218 g_settings_set (settings, "int16", "n", 4321);
1220 g_object_get (obj, "int16", &n2, NULL);
1221 g_assert_cmpint (n2, ==, 4321);
1223 g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
1225 g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1227 g_settings_get (settings, "uint16", "q", &q);
1228 g_assert_cmpuint (q, ==, G_MAXUINT16);
1230 g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1232 g_object_get (obj, "uint16", &q2, NULL);
1233 g_assert_cmpuint (q2, ==, (guint16) G_MAXINT16);
1235 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1237 g_object_set (obj, "int", 12345, NULL);
1238 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1240 g_settings_set_int (settings, "int", 54321);
1242 g_object_get (obj, "int", &i, NULL);
1243 g_assert_cmpint (i, ==, 54321);
1245 g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1247 g_object_set (obj, "uint", 12345, NULL);
1248 g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);
1250 g_settings_set_uint (settings, "uint", 54321);
1252 g_object_get (obj, "uint", &u, NULL);
1253 g_assert_cmpuint (u, ==, 54321);
1255 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1257 g_object_set (obj, "uint64", (guint64) 12345, NULL);
1258 g_assert_cmpuint (g_settings_get_uint64 (settings, "uint64"), ==, 12345);
1260 g_settings_set_uint64 (settings, "uint64", 54321);
1262 g_object_get (obj, "uint64", &u64, NULL);
1263 g_assert_cmpuint (u64, ==, 54321);
1265 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1267 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1269 g_settings_get (settings, "int64", "x", &i64);
1270 g_assert_cmpint (i64, ==, G_MAXINT64);
1272 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1274 g_object_get (obj, "int64", &i64, NULL);
1275 g_assert_cmpint (i64, ==, G_MININT64);
1277 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1279 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1281 g_settings_get (settings, "uint64", "t", &u64);
1282 g_assert_cmpuint (u64, ==, G_MAXUINT64);
1284 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1286 g_object_get (obj, "uint64", &u64, NULL);
1287 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1289 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1291 g_object_set (obj, "string", "bu ba", NULL);
1292 s = g_settings_get_string (settings, "string");
1293 g_assert_cmpstr (s, ==, "bu ba");
1296 g_settings_set_string (settings, "string", "bla bla");
1297 g_object_get (obj, "string", &s, NULL);
1298 g_assert_cmpstr (s, ==, "bla bla");
1301 g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1303 g_object_set (obj, "string", "non-unicode:\315", NULL);
1304 value = g_settings_get_value (settings, "chararray");
1305 g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1306 g_variant_unref (value);
1308 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1310 g_object_set (obj, "double", G_MAXFLOAT, NULL);
1311 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1313 g_settings_set_double (settings, "double", G_MINFLOAT);
1315 g_object_get (obj, "double", &d, NULL);
1316 g_assert_cmpfloat (d, ==, G_MINFLOAT);
1318 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1319 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1321 g_settings_set_double (settings, "double", -G_MINDOUBLE);
1323 g_object_get (obj, "double", &d, NULL);
1324 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1326 strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1327 g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1328 g_object_set (obj, "strv", strv, NULL);
1330 strv = g_settings_get_strv (settings, "strv");
1331 s = g_strjoinv (",", strv);
1332 g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1335 strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1336 g_settings_set_strv (settings, "strv", (const gchar **) strv);
1338 g_object_get (obj, "strv", &strv, NULL);
1339 s = g_strjoinv (",", strv);
1340 g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1343 g_settings_set_strv (settings, "strv", NULL);
1344 g_object_get (obj, "strv", &strv, NULL);
1345 g_assert_nonnull (strv);
1346 g_assert_cmpint (g_strv_length (strv), ==, 0);
1349 g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1350 g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1351 s = g_settings_get_string (settings, "enum");
1352 g_assert_cmpstr (s, ==, "baz");
1354 g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1356 g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1358 g_object_get (obj, "enum", &i, NULL);
1359 g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1361 g_settings_set_string (settings, "enum", "baz");
1363 g_object_get (obj, "enum", &i, NULL);
1364 g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1366 g_settings_bind (settings, "flags", obj, "flags", G_SETTINGS_BIND_DEFAULT);
1367 g_object_set (obj, "flags", TEST_FLAGS_MOURNING, NULL);
1368 strv = g_settings_get_strv (settings, "flags");
1369 g_assert_cmpint (g_strv_length (strv), ==, 1);
1370 g_assert_cmpstr (strv[0], ==, "mourning");
1373 g_assert_cmpint (g_settings_get_flags (settings, "flags"), ==, TEST_FLAGS_MOURNING);
1375 g_settings_set_flags (settings, "flags", TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1377 g_object_get (obj, "flags", &i, NULL);
1378 g_assert_cmpint (i, ==, TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1380 g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1382 g_object_set (obj, "uint", 12345, NULL);
1383 g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);
1385 g_settings_set_uint (settings, "uint", 54321);
1387 g_object_get (obj, "uint", &u, NULL);
1388 g_assert_cmpuint (u, ==, 54321);
1390 g_settings_bind (settings, "range", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1391 g_object_set (obj, "uint", 22, NULL);
1393 g_assert_cmpuint (g_settings_get_uint (settings, "range"), ==, 22);
1394 g_object_get (obj, "uint", &u, NULL);
1395 g_assert_cmpuint (u, ==, 22);
1397 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1398 "* is out of schema-specified range for*");
1399 g_object_set (obj, "uint", 45, NULL);
1400 g_test_assert_expected_messages ();
1402 g_object_get (obj, "uint", &u, NULL);
1403 g_assert_cmpuint (g_settings_get_uint (settings, "range"), ==, 22);
1404 /* The value of the object is currently not reset back to its initial value
1405 g_assert_cmpuint (u, ==, 22); */
1407 g_object_unref (obj);
1408 g_object_unref (settings);
1415 GSettings *settings;
1417 settings = g_settings_new ("org.gtk.test.binding");
1418 obj = test_object_new ();
1420 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1422 g_object_set (obj, "int", 12345, NULL);
1423 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1425 g_settings_unbind (obj, "int");
1427 g_object_set (obj, "int", 54321, NULL);
1428 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1430 g_object_unref (obj);
1431 g_object_unref (settings);
1435 test_bind_writable (void)
1438 GSettings *settings;
1441 settings = g_settings_new ("org.gtk.test.binding");
1442 obj = test_object_new ();
1444 g_object_set (obj, "bool", FALSE, NULL);
1446 g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1448 g_object_get (obj, "bool", &b, NULL);
1451 g_settings_unbind (obj, "bool");
1453 g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1455 g_object_get (obj, "bool", &b, NULL);
1458 g_object_unref (obj);
1459 g_object_unref (settings);
1462 /* Test one-way bindings.
1463 * Verify that changes on one side show up on the other,
1464 * but not vice versa
1467 test_directional_binding (void)
1470 GSettings *settings;
1474 settings = g_settings_new ("org.gtk.test.binding");
1475 obj = test_object_new ();
1477 g_object_set (obj, "bool", FALSE, NULL);
1478 g_settings_set_boolean (settings, "bool", FALSE);
1480 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1482 g_settings_set_boolean (settings, "bool", TRUE);
1483 g_object_get (obj, "bool", &b, NULL);
1484 g_assert_cmpint (b, ==, TRUE);
1486 g_object_set (obj, "bool", FALSE, NULL);
1487 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1489 g_object_set (obj, "int", 20, NULL);
1490 g_settings_set_int (settings, "int", 20);
1492 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1494 g_object_set (obj, "int", 32, NULL);
1495 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1497 g_settings_set_int (settings, "int", 20);
1498 g_object_get (obj, "int", &i, NULL);
1499 g_assert_cmpint (i, ==, 32);
1501 g_object_unref (obj);
1502 g_object_unref (settings);
1505 /* Test that type mismatch is caught when creating a binding */
1507 test_typesafe_binding (void)
1509 if (!g_test_undefined ())
1512 if (g_test_subprocess ())
1515 GSettings *settings;
1517 settings = g_settings_new ("org.gtk.test.binding");
1518 obj = test_object_new ();
1520 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1522 g_object_unref (obj);
1523 g_object_unref (settings);
1526 g_test_trap_subprocess (NULL, 0, 0);
1527 g_test_trap_assert_failed ();
1528 g_test_trap_assert_stderr ("*not compatible*");
1532 string_to_bool (GValue *value,
1538 s = g_variant_get_string (variant, NULL);
1539 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1545 bool_to_string (const GValue *value,
1546 const GVariantType *expected_type,
1549 if (g_value_get_boolean (value))
1550 return g_variant_new_string ("true");
1552 return g_variant_new_string ("false");
1556 bool_to_bool (const GValue *value,
1557 const GVariantType *expected_type,
1560 return g_variant_new_boolean (g_value_get_boolean (value));
1563 /* Test custom bindings.
1564 * Translate strings to booleans and back
1567 test_custom_binding (void)
1570 GSettings *settings;
1574 settings = g_settings_new ("org.gtk.test.binding");
1575 obj = test_object_new ();
1577 g_settings_set_string (settings, "string", "true");
1579 g_settings_bind_with_mapping (settings, "string",
1581 G_SETTINGS_BIND_DEFAULT,
1586 g_settings_set_string (settings, "string", "false");
1587 g_object_get (obj, "bool", &b, NULL);
1588 g_assert_cmpint (b, ==, FALSE);
1590 g_settings_set_string (settings, "string", "not true");
1591 g_object_get (obj, "bool", &b, NULL);
1592 g_assert_cmpint (b, ==, FALSE);
1594 g_object_set (obj, "bool", TRUE, NULL);
1595 s = g_settings_get_string (settings, "string");
1596 g_assert_cmpstr (s, ==, "true");
1599 g_settings_bind_with_mapping (settings, "string",
1601 G_SETTINGS_BIND_DEFAULT,
1602 string_to_bool, bool_to_bool,
1604 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1605 "*binding mapping function for key 'string' returned"
1606 " GVariant of type 'b' when type 's' was requested*");
1607 g_object_set (obj, "bool", FALSE, NULL);
1608 g_test_assert_expected_messages ();
1610 g_object_unref (obj);
1611 g_object_unref (settings);
1614 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1615 * initial settings value is transported to the object
1616 * side, but later settings changes do not affect the
1620 test_no_change_binding (void)
1623 GSettings *settings;
1626 settings = g_settings_new ("org.gtk.test.binding");
1627 obj = test_object_new ();
1629 g_object_set (obj, "bool", TRUE, NULL);
1630 g_settings_set_boolean (settings, "bool", FALSE);
1632 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1634 g_object_get (obj, "bool", &b, NULL);
1635 g_assert_cmpint (b, ==, FALSE);
1637 g_settings_set_boolean (settings, "bool", TRUE);
1638 g_object_get (obj, "bool", &b, NULL);
1639 g_assert_cmpint (b, ==, FALSE);
1641 g_settings_set_boolean (settings, "bool", FALSE);
1642 g_object_set (obj, "bool", TRUE, NULL);
1643 b = g_settings_get_boolean (settings, "bool");
1644 g_assert_cmpint (b, ==, TRUE);
1646 g_object_unref (obj);
1647 g_object_unref (settings);
1650 /* Test that binding a non-readable property only
1651 * works in 'GET' mode.
1654 test_no_read_binding_fail (void)
1657 GSettings *settings;
1659 settings = g_settings_new ("org.gtk.test.binding");
1660 obj = test_object_new ();
1662 g_settings_bind (settings, "string", obj, "no-read", 0);
1666 test_no_read_binding_pass (void)
1669 GSettings *settings;
1671 settings = g_settings_new ("org.gtk.test.binding");
1672 obj = test_object_new ();
1674 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1680 test_no_read_binding (void)
1682 if (g_test_undefined ())
1684 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/fail", 0, 0);
1685 g_test_trap_assert_failed ();
1686 g_test_trap_assert_stderr ("*property*is not readable*");
1689 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/pass", 0, 0);
1690 g_test_trap_assert_passed ();
1693 /* Test that binding a non-writable property only
1694 * works in 'SET' mode.
1697 test_no_write_binding_fail (void)
1700 GSettings *settings;
1702 settings = g_settings_new ("org.gtk.test.binding");
1703 obj = test_object_new ();
1705 g_settings_bind (settings, "string", obj, "no-write", 0);
1709 test_no_write_binding_pass (void)
1712 GSettings *settings;
1714 settings = g_settings_new ("org.gtk.test.binding");
1715 obj = test_object_new ();
1717 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1723 test_no_write_binding (void)
1725 if (g_test_undefined ())
1727 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/fail", 0, 0);
1728 g_test_trap_assert_failed ();
1729 g_test_trap_assert_stderr ("*property*is not writable*");
1732 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/pass", 0, 0);
1733 g_test_trap_assert_passed ();
1737 key_changed_cb (GSettings *settings, const gchar *key, gpointer data)
1746 const gchar *root_group;
1747 const gchar *keyfile_group;
1748 const gchar *root_path;
1752 * Test that using a keyfile works
1755 test_keyfile (Fixture *fixture,
1756 gconstpointer user_data)
1758 GSettingsBackend *kf_backend;
1759 GSettings *settings;
1763 GError *error = NULL;
1766 gboolean called = FALSE;
1767 gchar *keyfile_path = NULL, *store_path = NULL;
1769 keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
1770 store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
1771 kf_backend = g_keyfile_settings_backend_new (store_path, "/", "root");
1772 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1773 g_object_unref (kf_backend);
1775 g_settings_reset (settings, "greeting");
1776 str = g_settings_get_string (settings, "greeting");
1777 g_assert_cmpstr (str, ==, "Hello, earthlings");
1780 writable = g_settings_is_writable (settings, "greeting");
1781 g_assert_true (writable);
1782 g_settings_set (settings, "greeting", "s", "see if this works");
1784 str = g_settings_get_string (settings, "greeting");
1785 g_assert_cmpstr (str, ==, "see if this works");
1788 g_settings_delay (settings);
1789 g_settings_set (settings, "farewell", "s", "cheerio");
1790 g_settings_apply (settings);
1792 keyfile = g_key_file_new ();
1793 g_assert_true (g_key_file_load_from_file (keyfile, store_path, 0, NULL));
1795 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1796 g_assert_cmpstr (str, ==, "'see if this works'");
1799 str = g_key_file_get_string (keyfile, "tests", "farewell", NULL);
1800 g_assert_cmpstr (str, ==, "'cheerio'");
1802 g_key_file_free (keyfile);
1804 g_settings_reset (settings, "greeting");
1805 g_settings_apply (settings);
1806 keyfile = g_key_file_new ();
1807 g_assert_true (g_key_file_load_from_file (keyfile, store_path, 0, NULL));
1809 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1810 g_assert_null (str);
1813 g_signal_connect (settings, "changed::greeting", G_CALLBACK (key_changed_cb), &called);
1815 g_key_file_set_string (keyfile, "tests", "greeting", "'howdy'");
1816 data = g_key_file_to_data (keyfile, &len, NULL);
1817 g_file_set_contents (store_path, data, len, &error);
1818 g_assert_no_error (error);
1820 g_main_context_iteration (NULL, FALSE);
1821 g_signal_handlers_disconnect_by_func (settings, key_changed_cb, &called);
1823 str = g_settings_get_string (settings, "greeting");
1824 g_assert_cmpstr (str, ==, "howdy");
1827 /* Now check setting a string without quotes */
1829 g_signal_connect (settings, "changed::greeting", G_CALLBACK (key_changed_cb), &called);
1831 g_key_file_set_string (keyfile, "tests", "greeting", "he\"l🤗uń");
1833 data = g_key_file_to_data (keyfile, &len, NULL);
1834 g_file_set_contents (store_path, data, len, &error);
1835 g_assert_no_error (error);
1837 g_main_context_iteration (NULL, FALSE);
1838 g_signal_handlers_disconnect_by_func (settings, key_changed_cb, &called);
1840 str = g_settings_get_string (settings, "greeting");
1841 g_assert_cmpstr (str, ==, "he\"l🤗uń");
1844 g_settings_set (settings, "farewell", "s", "cheerio");
1846 /* Check that empty keys/groups are not allowed. */
1847 g_assert_false (g_settings_is_writable (settings, ""));
1848 g_assert_false (g_settings_is_writable (settings, "/"));
1850 /* When executing as root, changing the mode of the keyfile will have
1851 * no effect on the writability of the settings.
1853 if (geteuid () != 0)
1856 g_signal_connect (settings, "writable-changed::greeting",
1857 G_CALLBACK (key_changed_cb), &called);
1859 g_assert_no_errno (g_chmod (keyfile_path, 0500));
1861 g_main_context_iteration (NULL, FALSE);
1862 g_signal_handlers_disconnect_by_func (settings, key_changed_cb, &called);
1864 writable = g_settings_is_writable (settings, "greeting");
1865 g_assert_false (writable);
1868 g_key_file_free (keyfile);
1871 g_object_unref (settings);
1873 /* Clean up the temporary directory. */
1874 g_assert_no_errno (g_chmod (keyfile_path, 0777));
1875 g_assert_no_errno (g_remove (store_path));
1876 g_assert_no_errno (g_rmdir (keyfile_path));
1877 g_free (store_path);
1878 g_free (keyfile_path);
1882 * Test that using a keyfile works with a schema with no path set.
1885 test_keyfile_no_path (Fixture *fixture,
1886 gconstpointer user_data)
1888 const KeyfileTestData *test_data = user_data;
1889 GSettingsBackend *kf_backend;
1890 GSettings *settings;
1894 GError *error = NULL;
1895 gchar *keyfile_path = NULL, *store_path = NULL;
1897 keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
1898 store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
1899 kf_backend = g_keyfile_settings_backend_new (store_path, test_data->root_path, test_data->root_group);
1900 settings = g_settings_new_with_backend_and_path ("org.gtk.test.no-path", kf_backend, test_data->path);
1901 g_object_unref (kf_backend);
1903 g_settings_reset (settings, "test-boolean");
1904 g_assert_true (g_settings_get_boolean (settings, "test-boolean"));
1906 writable = g_settings_is_writable (settings, "test-boolean");
1907 g_assert_true (writable);
1908 g_settings_set (settings, "test-boolean", "b", FALSE);
1910 g_assert_false (g_settings_get_boolean (settings, "test-boolean"));
1912 g_settings_delay (settings);
1913 g_settings_set (settings, "test-boolean", "b", TRUE);
1914 g_settings_apply (settings);
1916 keyfile = g_key_file_new ();
1917 g_assert_true (g_key_file_load_from_file (keyfile, store_path, 0, NULL));
1919 g_assert_true (g_key_file_get_boolean (keyfile, test_data->keyfile_group, "test-boolean", NULL));
1921 g_key_file_free (keyfile);
1923 g_settings_reset (settings, "test-boolean");
1924 g_settings_apply (settings);
1925 keyfile = g_key_file_new ();
1926 g_assert_true (g_key_file_load_from_file (keyfile, store_path, 0, NULL));
1928 g_assert_false (g_key_file_get_string (keyfile, test_data->keyfile_group, "test-boolean", &error));
1929 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1930 g_clear_error (&error);
1932 /* Check that empty keys/groups are not allowed. */
1933 g_assert_false (g_settings_is_writable (settings, ""));
1934 g_assert_false (g_settings_is_writable (settings, "/"));
1936 /* Keys which ghost the root group name are not allowed. This can only be
1937 * tested when the path is `/` as otherwise it acts as a prefix and prevents
1939 if (g_str_equal (test_data->path, "/"))
1941 key = g_strdup_printf ("%s/%s", test_data->root_group, "");
1942 g_assert_false (g_settings_is_writable (settings, key));
1945 key = g_strdup_printf ("%s/%s", test_data->root_group, "/");
1946 g_assert_false (g_settings_is_writable (settings, key));
1949 key = g_strdup_printf ("%s/%s", test_data->root_group, "test-boolean");
1950 g_assert_false (g_settings_is_writable (settings, key));
1954 g_key_file_free (keyfile);
1955 g_object_unref (settings);
1957 /* Clean up the temporary directory. */
1958 g_assert_no_errno (g_chmod (keyfile_path, 0777));
1959 g_assert_no_errno (g_remove (store_path));
1960 g_assert_no_errno (g_rmdir (keyfile_path));
1961 g_free (store_path);
1962 g_free (keyfile_path);
1966 * Test that a keyfile rejects writes to keys outside its root path.
1969 test_keyfile_outside_root_path (Fixture *fixture,
1970 gconstpointer user_data)
1972 GSettingsBackend *kf_backend;
1973 GSettings *settings;
1974 gchar *keyfile_path = NULL, *store_path = NULL;
1976 keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
1977 store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
1978 kf_backend = g_keyfile_settings_backend_new (store_path, "/tests/basic-types/", "root");
1979 settings = g_settings_new_with_backend_and_path ("org.gtk.test.no-path", kf_backend, "/tests/");
1980 g_object_unref (kf_backend);
1982 g_assert_false (g_settings_is_writable (settings, "test-boolean"));
1984 g_object_unref (settings);
1986 /* Clean up the temporary directory. The keyfile probably doesn’t exist, so
1987 * don’t error on failure. */
1988 g_remove (store_path);
1989 g_assert_no_errno (g_rmdir (keyfile_path));
1990 g_free (store_path);
1991 g_free (keyfile_path);
1995 * Test that a keyfile rejects writes to keys in the root if no root group is set.
1998 test_keyfile_no_root_group (Fixture *fixture,
1999 gconstpointer user_data)
2001 GSettingsBackend *kf_backend;
2002 GSettings *settings;
2003 gchar *keyfile_path = NULL, *store_path = NULL;
2005 keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
2006 store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
2007 kf_backend = g_keyfile_settings_backend_new (store_path, "/", NULL);
2008 settings = g_settings_new_with_backend_and_path ("org.gtk.test.no-path", kf_backend, "/");
2009 g_object_unref (kf_backend);
2011 g_assert_false (g_settings_is_writable (settings, "test-boolean"));
2012 g_assert_true (g_settings_is_writable (settings, "child/test-boolean"));
2014 g_object_unref (settings);
2016 /* Clean up the temporary directory. The keyfile probably doesn’t exist, so
2017 * don’t error on failure. */
2018 g_remove (store_path);
2019 g_assert_no_errno (g_rmdir (keyfile_path));
2020 g_free (store_path);
2021 g_free (keyfile_path);
2024 /* Test that getting child schemas works
2027 test_child_schema (void)
2029 GSettings *settings;
2033 /* first establish some known conditions */
2034 settings = g_settings_new ("org.gtk.test.basic-types");
2035 g_settings_set (settings, "test-byte", "y", 36);
2037 g_settings_get (settings, "test-byte", "y", &byte);
2038 g_assert_cmpint (byte, ==, 36);
2040 g_object_unref (settings);
2042 settings = g_settings_new ("org.gtk.test");
2043 child = g_settings_get_child (settings, "basic-types");
2044 g_assert_nonnull (child);
2046 g_settings_get (child, "test-byte", "y", &byte);
2047 g_assert_cmpint (byte, ==, 36);
2049 g_object_unref (child);
2050 g_object_unref (settings);
2053 #include "../strinfo.c"
2058 /* "foo" has a value of 1
2059 * "bar" has a value of 2
2060 * "baz" is an alias for "bar"
2063 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
2064 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
2066 const guint32 *strinfo = (guint32 *) array;
2067 guint length = sizeof array / 4;
2071 /* build it and compare */
2074 builder = g_string_new (NULL);
2075 strinfo_builder_append_item (builder, "foo", 1);
2076 strinfo_builder_append_item (builder, "bar", 2);
2077 g_assert_true (strinfo_builder_append_alias (builder, "baz", "bar"));
2078 g_assert_cmpmem (builder->str, builder->len, strinfo, length * 4);
2079 g_string_free (builder, TRUE);
2082 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
2084 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
2086 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
2088 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
2091 g_assert_true (strinfo_enum_from_string (strinfo, length, "foo", &result));
2092 g_assert_cmpint (result, ==, 1);
2093 g_assert_true (strinfo_enum_from_string (strinfo, length, "bar", &result));
2094 g_assert_cmpint (result, ==, 2);
2095 g_assert_false (strinfo_enum_from_string (strinfo, length, "baz", &result));
2096 g_assert_false (strinfo_enum_from_string (strinfo, length, "quux", &result));
2098 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
2099 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
2100 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
2101 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
2103 g_assert_true (strinfo_is_string_valid (strinfo, length, "foo"));
2104 g_assert_true (strinfo_is_string_valid (strinfo, length, "bar"));
2105 g_assert_false (strinfo_is_string_valid (strinfo, length, "baz"));
2106 g_assert_false (strinfo_is_string_valid (strinfo, length, "quux"));
2110 test_enums_non_enum_key (void)
2114 direct = g_settings_new ("org.gtk.test.enums.direct");
2115 g_settings_get_enum (direct, "test");
2116 g_assert_not_reached ();
2120 test_enums_non_enum_value (void)
2122 GSettings *settings;
2124 settings = g_settings_new ("org.gtk.test.enums");
2125 g_settings_set_enum (settings, "test", 42);
2126 g_assert_not_reached ();
2130 test_enums_range (void)
2132 GSettings *settings;
2134 settings = g_settings_new ("org.gtk.test.enums");
2135 g_settings_set_string (settings, "test", "qux");
2136 g_assert_not_reached ();
2140 test_enums_non_flags (void)
2142 GSettings *settings;
2144 settings = g_settings_new ("org.gtk.test.enums");
2145 g_settings_get_flags (settings, "test");
2146 g_assert_not_reached ();
2152 GSettings *settings, *direct;
2155 settings = g_settings_new ("org.gtk.test.enums");
2156 direct = g_settings_new ("org.gtk.test.enums.direct");
2158 if (g_test_undefined () && !backend_set)
2160 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-key", 0, 0);
2161 g_test_trap_assert_failed ();
2162 g_test_trap_assert_stderr ("*not associated with an enum*");
2164 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-value", 0, 0);
2165 g_test_trap_assert_failed ();
2166 g_test_trap_assert_stderr ("*invalid enum value 42*");
2168 g_test_trap_subprocess ("/gsettings/enums/subprocess/range", 0, 0);
2169 g_test_trap_assert_failed ();
2170 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2172 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-flags", 0, 0);
2173 g_test_trap_assert_failed ();
2174 g_test_trap_assert_stderr ("*not associated with a flags*");
2177 str = g_settings_get_string (settings, "test");
2178 g_assert_cmpstr (str, ==, "bar");
2181 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
2183 str = g_settings_get_string (settings, "test");
2184 g_assert_cmpstr (str, ==, "foo");
2187 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
2189 g_settings_set_string (direct, "test", "qux");
2191 str = g_settings_get_string (direct, "test");
2192 g_assert_cmpstr (str, ==, "qux");
2195 str = g_settings_get_string (settings, "test");
2196 g_assert_cmpstr (str, ==, "quux");
2199 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
2201 g_object_unref (direct);
2202 g_object_unref (settings);
2206 test_flags_non_flags_key (void)
2210 direct = g_settings_new ("org.gtk.test.enums.direct");
2211 g_settings_get_flags (direct, "test");
2212 g_assert_not_reached ();
2216 test_flags_non_flags_value (void)
2218 GSettings *settings;
2220 settings = g_settings_new ("org.gtk.test.enums");
2221 g_settings_set_flags (settings, "f-test", 0x42);
2222 g_assert_not_reached ();
2226 test_flags_range (void)
2228 GSettings *settings;
2230 settings = g_settings_new ("org.gtk.test.enums");
2231 g_settings_set_strv (settings, "f-test",
2232 (const gchar **) g_strsplit ("rock", ",", 0));
2233 g_assert_not_reached ();
2237 test_flags_non_enum (void)
2239 GSettings *settings;
2241 settings = g_settings_new ("org.gtk.test.enums");
2242 g_settings_get_enum (settings, "f-test");
2243 g_assert_not_reached ();
2249 GSettings *settings, *direct;
2253 settings = g_settings_new ("org.gtk.test.enums");
2254 direct = g_settings_new ("org.gtk.test.enums.direct");
2256 if (g_test_undefined () && !backend_set)
2258 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-key", 0, 0);
2259 g_test_trap_assert_failed ();
2260 g_test_trap_assert_stderr ("*not associated with a flags*");
2262 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-value", 0, 0);
2263 g_test_trap_assert_failed ();
2264 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
2266 g_test_trap_subprocess ("/gsettings/flags/subprocess/range", 0, 0);
2267 g_test_trap_assert_failed ();
2268 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2270 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-enum", 0, 0);
2271 g_test_trap_assert_failed ();
2272 g_test_trap_assert_stderr ("*not associated with an enum*");
2275 strv = g_settings_get_strv (settings, "f-test");
2276 str = g_strjoinv (",", strv);
2277 g_assert_cmpstr (str, ==, "");
2281 g_settings_set_flags (settings, "f-test",
2282 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
2284 strv = g_settings_get_strv (settings, "f-test");
2285 str = g_strjoinv (",", strv);
2286 g_assert_cmpstr (str, ==, "talking,walking");
2290 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
2291 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
2293 strv = g_strsplit ("speaking,laughing", ",", 0);
2294 g_settings_set_strv (direct, "f-test", (const gchar **) strv);
2297 strv = g_settings_get_strv (direct, "f-test");
2298 str = g_strjoinv (",", strv);
2299 g_assert_cmpstr (str, ==, "speaking,laughing");
2303 strv = g_settings_get_strv (settings, "f-test");
2304 str = g_strjoinv (",", strv);
2305 g_assert_cmpstr (str, ==, "talking,laughing");
2309 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
2310 TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
2312 g_object_unref (direct);
2313 g_object_unref (settings);
2317 test_range_high (void)
2319 GSettings *settings;
2321 settings = g_settings_new ("org.gtk.test.range");
2322 g_settings_set_int (settings, "val", 45);
2323 g_assert_not_reached ();
2327 test_range_low (void)
2329 GSettings *settings;
2331 settings = g_settings_new ("org.gtk.test.range");
2332 g_settings_set_int (settings, "val", 1);
2333 g_assert_not_reached ();
2339 GSettings *settings, *direct;
2342 settings = g_settings_new ("org.gtk.test.range");
2343 direct = g_settings_new ("org.gtk.test.range.direct");
2345 if (g_test_undefined () && !backend_set)
2347 g_test_trap_subprocess ("/gsettings/range/subprocess/high", 0, 0);
2348 g_test_trap_assert_failed ();
2349 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2351 g_test_trap_subprocess ("/gsettings/range/subprocess/low", 0, 0);
2352 g_test_trap_assert_failed ();
2353 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2356 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2357 g_settings_set_int (direct, "val", 22);
2358 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
2359 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
2360 g_settings_set_int (direct, "val", 45);
2361 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
2362 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2363 g_settings_set_int (direct, "val", 1);
2364 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
2365 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2367 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2368 value = g_variant_new_int32 (1);
2369 g_assert_false (g_settings_range_check (settings, "val", value));
2370 g_variant_unref (value);
2371 value = g_variant_new_int32 (33);
2372 g_assert_true (g_settings_range_check (settings, "val", value));
2373 g_variant_unref (value);
2374 value = g_variant_new_int32 (45);
2375 g_assert_false (g_settings_range_check (settings, "val", value));
2376 g_variant_unref (value);
2377 G_GNUC_END_IGNORE_DEPRECATIONS
2379 g_object_unref (direct);
2380 g_object_unref (settings);
2384 strv_has_string (gchar **haystack,
2385 const gchar *needle)
2389 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
2391 if (g_strcmp0 (haystack[n], needle) == 0)
2398 strv_set_equal (gchar **strv, ...)
2407 va_start (list, strv);
2410 str = va_arg (list, const gchar *);
2413 if (!strv_has_string (strv, str))
2423 res = g_strv_length ((gchar**)strv) == count;
2429 test_list_items (void)
2431 GSettingsSchema *schema;
2432 GSettings *settings;
2436 settings = g_settings_new ("org.gtk.test");
2437 g_object_get (settings, "settings-schema", &schema, NULL);
2438 children = g_settings_list_children (settings);
2439 keys = g_settings_schema_list_keys (schema);
2441 g_assert_true (strv_set_equal (children, "basic-types", "complex-types", "localized", NULL));
2442 g_assert_true (strv_set_equal (keys, "greeting", "farewell", NULL));
2444 g_strfreev (children);
2447 g_settings_schema_unref (schema);
2448 g_object_unref (settings);
2452 test_list_schemas (void)
2454 const gchar * const *schemas;
2455 const gchar * const *relocs;
2457 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2458 relocs = g_settings_list_relocatable_schemas ();
2459 schemas = g_settings_list_schemas ();
2460 G_GNUC_END_IGNORE_DEPRECATIONS
2462 g_assert_true (strv_set_equal ((gchar **)relocs,
2463 "org.gtk.test.no-path",
2464 "org.gtk.test.extends.base",
2465 "org.gtk.test.extends.extended",
2468 g_assert_true (strv_set_equal ((gchar **)schemas,
2470 "org.gtk.test.basic-types",
2471 "org.gtk.test.complex-types",
2472 "org.gtk.test.localized",
2473 "org.gtk.test.binding",
2474 "org.gtk.test.enums",
2475 "org.gtk.test.enums.direct",
2476 "org.gtk.test.range",
2477 "org.gtk.test.range.direct",
2478 "org.gtk.test.mapped",
2479 "org.gtk.test.descriptions",
2480 "org.gtk.test.per-desktop",
2485 map_func (GVariant *value,
2489 gint *state = user_data;
2493 v = g_variant_get_int32 (value);
2499 g_assert_cmpint (v, ==, 1);
2503 else if (*state == 1)
2505 g_assert_cmpint (v, ==, 0);
2511 g_assert_null (value);
2512 *result = g_variant_new_int32 (5);
2518 test_get_mapped (void)
2520 GSettings *settings;
2525 settings = g_settings_new ("org.gtk.test.mapped");
2526 g_settings_set_int (settings, "val", 1);
2529 p = g_settings_get_mapped (settings, "val", map_func, &state);
2530 val = g_variant_get_int32 ((GVariant*)p);
2531 g_assert_cmpint (val, ==, 5);
2533 g_variant_unref (p);
2534 g_object_unref (settings);
2538 test_get_range (void)
2540 GSettings *settings;
2543 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2544 settings = g_settings_new ("org.gtk.test.range");
2545 range = g_settings_get_range (settings, "val");
2546 check_and_free (range, "('range', <(2, 44)>)");
2547 g_object_unref (settings);
2549 settings = g_settings_new ("org.gtk.test.enums");
2550 range = g_settings_get_range (settings, "test");
2551 check_and_free (range, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
2552 g_object_unref (settings);
2554 settings = g_settings_new ("org.gtk.test.enums");
2555 range = g_settings_get_range (settings, "f-test");
2556 check_and_free (range, "('flags', "
2557 "<['mourning', 'laughing', 'talking', 'walking']>)");
2558 g_object_unref (settings);
2560 settings = g_settings_new ("org.gtk.test");
2561 range = g_settings_get_range (settings, "greeting");
2562 check_and_free (range, "('type', <@as []>)");
2563 g_object_unref (settings);
2564 G_GNUC_END_IGNORE_DEPRECATIONS
2568 test_schema_source (void)
2570 GSettingsSchemaSource *parent;
2571 GSettingsSchemaSource *source;
2572 GSettingsBackend *backend;
2573 GSettingsSchema *schema;
2574 GError *error = NULL;
2575 GSettings *settings;
2578 backend = g_settings_backend_get_default ();
2580 /* make sure it fails properly */
2581 parent = g_settings_schema_source_get_default ();
2582 source = g_settings_schema_source_new_from_directory ("/path/that/does/not/exist", parent, TRUE, &error);
2583 g_assert_null (source);
2584 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
2585 g_clear_error (&error);
2587 /* Test error handling of corrupt compiled files. */
2588 source = g_settings_schema_source_new_from_directory ("schema-source-corrupt", parent, TRUE, &error);
2589 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
2590 g_assert_null (source);
2591 g_clear_error (&error);
2593 /* Test error handling of empty compiled files. */
2594 source = g_settings_schema_source_new_from_directory ("schema-source-empty", parent, TRUE, &error);
2595 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
2596 g_assert_null (source);
2597 g_clear_error (&error);
2599 /* create a source with the parent */
2600 source = g_settings_schema_source_new_from_directory ("schema-source", parent, TRUE, &error);
2601 g_assert_no_error (error);
2602 g_assert_nonnull (source);
2604 /* check recursive lookups are working */
2605 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2606 g_assert_nonnull (schema);
2607 g_settings_schema_unref (schema);
2609 /* check recursive lookups for non-existent schemas */
2610 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", TRUE);
2611 g_assert_null (schema);
2613 /* check non-recursive for schema that only exists in lower layers */
2614 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2615 g_assert_null (schema);
2617 /* check non-recursive lookup for non-existent */
2618 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", FALSE);
2619 g_assert_null (schema);
2621 /* check non-recursive for schema that exists in toplevel */
2622 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2623 g_assert_nonnull (schema);
2624 g_settings_schema_unref (schema);
2626 /* check recursive for schema that exists in toplevel */
2627 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2628 g_assert_nonnull (schema);
2630 /* try to use it for something */
2631 settings = g_settings_new_full (schema, backend, g_settings_schema_get_path (schema));
2632 g_settings_schema_unref (schema);
2634 g_settings_get (settings, "enabled", "b", &enabled);
2635 g_assert_true (enabled);
2636 g_object_unref (settings);
2638 g_settings_schema_source_unref (source);
2640 /* try again, but with no parent */
2641 source = g_settings_schema_source_new_from_directory ("schema-source", NULL, FALSE, NULL);
2642 g_assert_nonnull (source);
2644 /* should not find it this time, even if recursive... */
2645 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2646 g_assert_null (schema);
2647 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2648 g_assert_null (schema);
2650 /* should still find our own... */
2651 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2652 g_assert_nonnull (schema);
2653 g_settings_schema_unref (schema);
2654 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2655 g_assert_nonnull (schema);
2656 g_settings_schema_unref (schema);
2658 g_settings_schema_source_unref (source);
2659 g_object_unref (backend);
2663 test_schema_list_keys (void)
2666 GSettingsSchemaSource *src = g_settings_schema_source_get_default ();
2667 GSettingsSchema *schema = g_settings_schema_source_lookup (src, "org.gtk.test", TRUE);
2668 g_assert_nonnull (schema);
2670 keys = g_settings_schema_list_keys (schema);
2672 g_assert_true (strv_set_equal ((gchar **)keys,
2678 g_settings_schema_unref (schema);
2684 GAction *string, *toggle;
2685 gboolean c1, c2, c3;
2686 GSettings *settings;
2688 GVariantType *param_type;
2690 GVariantType *state_type;
2693 settings = g_settings_new ("org.gtk.test.basic-types");
2694 string = g_settings_create_action (settings, "test-string");
2695 toggle = g_settings_create_action (settings, "test-boolean");
2696 g_object_unref (settings); /* should be held by the actions */
2698 g_signal_connect (settings, "changed", G_CALLBACK (changed_cb2), &c1);
2699 g_signal_connect (string, "notify::state", G_CALLBACK (changed_cb2), &c2);
2700 g_signal_connect (toggle, "notify::state", G_CALLBACK (changed_cb2), &c3);
2702 c1 = c2 = c3 = FALSE;
2703 g_settings_set_string (settings, "test-string", "hello world");
2704 check_and_free (g_action_get_state (string), "'hello world'");
2705 g_assert_true (c1 && c2 && !c3);
2706 c1 = c2 = c3 = FALSE;
2708 g_action_activate (string, g_variant_new_string ("hihi"));
2709 check_and_free (g_settings_get_value (settings, "test-string"), "'hihi'");
2710 g_assert_true (c1 && c2 && !c3);
2711 c1 = c2 = c3 = FALSE;
2713 g_action_change_state (string, g_variant_new_string ("kthxbye"));
2714 check_and_free (g_settings_get_value (settings, "test-string"), "'kthxbye'");
2715 g_assert_true (c1 && c2 && !c3);
2716 c1 = c2 = c3 = FALSE;
2718 g_action_change_state (toggle, g_variant_new_boolean (TRUE));
2719 g_assert_true (g_settings_get_boolean (settings, "test-boolean"));
2720 g_assert_true (c1 && !c2 && c3);
2721 c1 = c2 = c3 = FALSE;
2723 g_action_activate (toggle, NULL);
2724 g_assert_false (g_settings_get_boolean (settings, "test-boolean"));
2725 g_assert_true (c1 && !c2 && c3);
2727 g_object_get (string,
2729 "parameter-type", ¶m_type,
2730 "enabled", &enabled,
2731 "state-type", &state_type,
2735 g_assert_cmpstr (name, ==, "test-string");
2736 g_assert_true (g_variant_type_equal (param_type, G_VARIANT_TYPE_STRING));
2737 g_assert_true (enabled);
2738 g_assert_true (g_variant_type_equal (state_type, G_VARIANT_TYPE_STRING));
2739 g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "kthxbye");
2742 g_variant_type_free (param_type);
2743 g_variant_type_free (state_type);
2744 g_variant_unref (state);
2746 g_object_unref (string);
2747 g_object_unref (toggle);
2751 test_null_backend (void)
2753 GSettingsBackend *backend;
2754 GSettings *settings;
2758 backend = g_null_settings_backend_new ();
2759 settings = g_settings_new_with_backend_and_path ("org.gtk.test", backend, "/tests/");
2761 g_object_get (settings, "schema-id", &str, NULL);
2762 g_assert_cmpstr (str, ==, "org.gtk.test");
2765 g_settings_get (settings, "greeting", "s", &str);
2766 g_assert_cmpstr (str, ==, "Hello, earthlings");
2769 g_settings_set (settings, "greeting", "s", "goodbye world");
2770 g_settings_get (settings, "greeting", "s", &str);
2771 g_assert_cmpstr (str, ==, "Hello, earthlings");
2774 writable = g_settings_is_writable (settings, "greeting");
2775 g_assert_false (writable);
2777 g_settings_reset (settings, "greeting");
2779 g_settings_delay (settings);
2780 g_settings_set (settings, "greeting", "s", "goodbye world");
2781 g_settings_apply (settings);
2782 g_settings_get (settings, "greeting", "s", &str);
2783 g_assert_cmpstr (str, ==, "Hello, earthlings");
2786 g_object_unref (settings);
2787 g_object_unref (backend);
2791 test_memory_backend (void)
2793 GSettingsBackend *backend;
2795 backend = g_memory_settings_backend_new ();
2796 g_assert_true (G_IS_SETTINGS_BACKEND (backend));
2797 g_object_unref (backend);
2801 test_read_descriptions (void)
2803 GSettingsSchema *schema;
2804 GSettingsSchemaKey *key;
2805 GSettings *settings;
2807 settings = g_settings_new ("org.gtk.test");
2808 g_object_get (settings, "settings-schema", &schema, NULL);
2809 key = g_settings_schema_get_key (schema, "greeting");
2811 g_assert_cmpstr (g_settings_schema_key_get_summary (key), ==, "A greeting");
2812 g_assert_cmpstr (g_settings_schema_key_get_description (key), ==, "Greeting of the invading martians");
2814 g_settings_schema_key_unref (key);
2815 g_settings_schema_unref (schema);
2817 g_object_unref (settings);
2819 settings = g_settings_new ("org.gtk.test.descriptions");
2820 g_object_get (settings, "settings-schema", &schema, NULL);
2821 key = g_settings_schema_get_key (schema, "a");
2823 g_assert_cmpstr (g_settings_schema_key_get_summary (key), ==,
2825 "with some whitespace.\n\n"
2826 "because not everyone has a great editor.\n\n"
2827 "lots of space is as one.");
2829 g_settings_schema_key_unref (key);
2830 g_settings_schema_unref (schema);
2832 g_object_unref (settings);
2836 test_default_value (void)
2838 GSettings *settings;
2839 GSettingsSchema *schema;
2840 GSettingsSchemaKey *key;
2845 settings = g_settings_new ("org.gtk.test");
2846 g_object_get (settings, "settings-schema", &schema, NULL);
2847 key = g_settings_schema_get_key (schema, "greeting");
2848 g_settings_schema_unref (schema);
2849 g_settings_schema_key_ref (key);
2851 g_assert_true (g_variant_type_equal (g_settings_schema_key_get_value_type (key), G_VARIANT_TYPE_STRING));
2853 v = g_settings_schema_key_get_default_value (key);
2854 str = g_variant_get_string (v, NULL);
2855 g_assert_cmpstr (str, ==, "Hello, earthlings");
2856 g_variant_unref (v);
2858 g_settings_schema_key_unref (key);
2859 g_settings_schema_key_unref (key);
2861 g_settings_set (settings, "greeting", "s", "goodbye world");
2863 v = g_settings_get_user_value (settings, "greeting");
2864 str = g_variant_get_string (v, NULL);
2865 g_assert_cmpstr (str, ==, "goodbye world");
2866 g_variant_unref (v);
2868 v = g_settings_get_default_value (settings, "greeting");
2869 str = g_variant_get_string (v, NULL);
2870 g_assert_cmpstr (str, ==, "Hello, earthlings");
2871 g_variant_unref (v);
2873 g_settings_reset (settings, "greeting");
2875 v = g_settings_get_user_value (settings, "greeting");
2878 s = g_settings_get_string (settings, "greeting");
2879 g_assert_cmpstr (s, ==, "Hello, earthlings");
2882 g_object_unref (settings);
2886 string_map_func (GVariant *value,
2892 str = g_variant_get_string (value, NULL);
2893 *result = g_variant_new_string (str);
2898 /* Test that per-desktop values from org.gtk.test.gschema.override
2899 * does not change default value if current desktop is not listed in
2900 * $XDG_CURRENT_DESKTOP.
2903 test_per_desktop (void)
2905 GSettings *settings;
2910 settings = g_settings_new ("org.gtk.test.per-desktop");
2911 obj = test_object_new ();
2913 if (!g_test_subprocess ())
2915 g_test_trap_subprocess ("/gsettings/per-desktop/subprocess", 0, 0);
2916 g_test_trap_assert_passed ();
2919 str = g_settings_get_string (settings, "desktop");
2920 g_assert_cmpstr (str, ==, "GNOME");
2923 p = g_settings_get_mapped (settings, "desktop", string_map_func, NULL);
2925 str = g_variant_dup_string (p, NULL);
2926 g_assert_cmpstr (str, ==, "GNOME");
2929 g_variant_unref (p);
2931 g_settings_bind (settings, "desktop", obj, "string", G_SETTINGS_BIND_DEFAULT);
2933 g_object_get (obj, "string", &str, NULL);
2934 g_assert_cmpstr (str, ==, "GNOME");
2937 g_object_unref (settings);
2938 g_object_unref (obj);
2941 /* Test that per-desktop values from org.gtk.test.gschema.override
2942 * are successfully loaded based on the value of $XDG_CURRENT_DESKTOP.
2945 test_per_desktop_subprocess (void)
2947 GSettings *settings;
2952 g_setenv ("XDG_CURRENT_DESKTOP", "GNOME-Classic:GNOME", TRUE);
2954 settings = g_settings_new ("org.gtk.test.per-desktop");
2955 obj = test_object_new ();
2957 str = g_settings_get_string (settings, "desktop");
2958 g_assert_cmpstr (str, ==, "GNOME Classic");
2961 p = g_settings_get_mapped (settings, "desktop", string_map_func, NULL);
2963 str = g_variant_dup_string (p, NULL);
2964 g_assert_cmpstr (str, ==, "GNOME Classic");
2967 g_variant_unref (p);
2969 g_settings_bind (settings, "desktop", obj, "string", G_SETTINGS_BIND_DEFAULT);
2971 g_object_get (obj, "string", &str, NULL);
2972 g_assert_cmpstr (str, ==, "GNOME Classic");
2975 g_object_unref (settings);
2976 g_object_unref (obj);
2980 test_extended_schema (void)
2982 GSettingsSchema *schema;
2983 GSettings *settings;
2986 settings = g_settings_new_with_path ("org.gtk.test.extends.extended", "/test/extendes/");
2987 g_object_get (settings, "settings-schema", &schema, NULL);
2988 keys = g_settings_schema_list_keys (schema);
2989 g_assert_true (strv_set_equal (keys, "int32", "string", "another-int32", NULL));
2991 g_object_unref (settings);
2992 g_settings_schema_unref (schema);
2996 main (int argc, char *argv[])
2999 gchar *override_text;
3002 const KeyfileTestData keyfile_test_data_explicit_path = { "/tests/", "root", "tests", "/" };
3003 const KeyfileTestData keyfile_test_data_empty_path = { "/", "root", "root", "/" };
3004 const KeyfileTestData keyfile_test_data_long_path = {
3005 "/tests/path/is/very/long/and/this/makes/some/comparisons/take/a/different/branch/",
3007 "tests/path/is/very/long/and/this/makes/some/comparisons/take/a/different/branch",
3011 /* Meson build sets this */
3012 #ifdef TEST_LOCALE_PATH
3013 if (g_str_has_suffix (TEST_LOCALE_PATH, "LC_MESSAGES"))
3015 locale_dir = TEST_LOCALE_PATH G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S "..";
3019 setlocale (LC_ALL, "");
3021 g_test_init (&argc, &argv, NULL);
3023 if (!g_test_subprocess ())
3025 GError *local_error = NULL;
3026 /* A GVDB header is 6 guint32s, and requires a magic number in the first
3027 * two guint32s. A set of zero bytes of a greater length is considered
3029 const guint8 gschemas_compiled_corrupt[sizeof (guint32) * 7] = { 0, };
3031 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
3033 g_setenv ("XDG_DATA_DIRS", ".", TRUE);
3034 g_setenv ("XDG_DATA_HOME", ".", TRUE);
3035 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
3036 g_setenv ("XDG_CURRENT_DESKTOP", "", TRUE);
3039 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
3041 g_remove ("org.gtk.test.enums.xml");
3042 /* #GLIB_MKENUMS is defined in meson.build */
3043 g_assert_true (g_spawn_command_line_sync (GLIB_MKENUMS " "
3044 "--template " SRCDIR "/enums.xml.template "
3045 SRCDIR "/testenum.h",
3046 &enums, NULL, &result, NULL));
3047 g_assert_cmpint (result, ==, 0);
3048 g_assert_true (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
3051 g_assert_true (g_file_get_contents (SRCDIR "/org.gtk.test.gschema.xml.orig", &schema_text, NULL, NULL));
3052 g_assert_true (g_file_set_contents ("org.gtk.test.gschema.xml", schema_text, -1, NULL));
3053 g_free (schema_text);
3055 g_assert_true (g_file_get_contents (SRCDIR "/org.gtk.test.gschema.override.orig", &override_text, NULL, NULL));
3056 g_assert_true (g_file_set_contents ("org.gtk.test.gschema.override", override_text, -1, NULL));
3057 g_free (override_text);
3059 g_remove ("gschemas.compiled");
3060 /* #GLIB_COMPILE_SCHEMAS is defined in meson.build */
3061 g_assert_true (g_spawn_command_line_sync (GLIB_COMPILE_SCHEMAS " --targetdir=. "
3062 "--schema-file=org.gtk.test.enums.xml "
3063 "--schema-file=org.gtk.test.gschema.xml "
3064 "--override-file=org.gtk.test.gschema.override",
3065 NULL, NULL, &result, NULL));
3066 g_assert_cmpint (result, ==, 0);
3068 g_remove ("schema-source/gschemas.compiled");
3069 g_mkdir ("schema-source", 0777);
3070 g_assert_true (g_spawn_command_line_sync (GLIB_COMPILE_SCHEMAS " --targetdir=schema-source "
3071 "--schema-file=" SRCDIR "/org.gtk.schemasourcecheck.gschema.xml",
3072 NULL, NULL, &result, NULL));
3073 g_assert_cmpint (result, ==, 0);
3075 g_remove ("schema-source-corrupt/gschemas.compiled");
3076 g_mkdir ("schema-source-corrupt", 0777);
3077 g_file_set_contents ("schema-source-corrupt/gschemas.compiled",
3078 (const gchar *) gschemas_compiled_corrupt,
3079 sizeof (gschemas_compiled_corrupt),
3081 g_assert_no_error (local_error);
3083 g_remove ("schema-source-empty/gschemas.compiled");
3084 g_mkdir ("schema-source-empty", 0777);
3085 g_file_set_contents ("schema-source-empty/gschemas.compiled",
3088 g_assert_no_error (local_error);
3091 g_test_add_func ("/gsettings/basic", test_basic);
3095 g_test_add_func ("/gsettings/no-schema", test_no_schema);
3096 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
3097 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
3098 g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
3099 g_test_add_func ("/gsettings/no-path", test_no_path);
3102 g_test_add_func ("/gsettings/basic-types", test_basic_types);
3103 g_test_add_func ("/gsettings/complex-types", test_complex_types);
3104 g_test_add_func ("/gsettings/changes", test_changes);
3106 g_test_add_func ("/gsettings/l10n", test_l10n);
3107 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
3109 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
3110 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
3111 g_test_add_func ("/gsettings/delay-child", test_delay_child);
3112 g_test_add_func ("/gsettings/atomic", test_atomic);
3114 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
3115 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
3116 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
3117 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
3118 g_test_add_func ("/gsettings/unbinding", test_unbind);
3119 g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
3123 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
3124 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
3125 g_test_add_func ("/gsettings/no-read-binding/subprocess/fail", test_no_read_binding_fail);
3126 g_test_add_func ("/gsettings/no-read-binding/subprocess/pass", test_no_read_binding_pass);
3127 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
3128 g_test_add_func ("/gsettings/no-write-binding/subprocess/fail", test_no_write_binding_fail);
3129 g_test_add_func ("/gsettings/no-write-binding/subprocess/pass", test_no_write_binding_pass);
3132 g_test_add ("/gsettings/keyfile", Fixture, NULL, setup, test_keyfile, teardown);
3133 g_test_add ("/gsettings/keyfile/explicit-path", Fixture, &keyfile_test_data_explicit_path, setup, test_keyfile_no_path, teardown);
3134 g_test_add ("/gsettings/keyfile/empty-path", Fixture, &keyfile_test_data_empty_path, setup, test_keyfile_no_path, teardown);
3135 g_test_add ("/gsettings/keyfile/long-path", Fixture, &keyfile_test_data_long_path, setup, test_keyfile_no_path, teardown);
3136 g_test_add ("/gsettings/keyfile/outside-root-path", Fixture, NULL, setup, test_keyfile_outside_root_path, teardown);
3137 g_test_add ("/gsettings/keyfile/no-root-group", Fixture, NULL, setup, test_keyfile_no_root_group, teardown);
3138 g_test_add_func ("/gsettings/child-schema", test_child_schema);
3139 g_test_add_func ("/gsettings/strinfo", test_strinfo);
3140 g_test_add_func ("/gsettings/enums", test_enums);
3141 g_test_add_func ("/gsettings/enums/subprocess/non-enum-key", test_enums_non_enum_key);
3142 g_test_add_func ("/gsettings/enums/subprocess/non-enum-value", test_enums_non_enum_value);
3143 g_test_add_func ("/gsettings/enums/subprocess/range", test_enums_range);
3144 g_test_add_func ("/gsettings/enums/subprocess/non-flags", test_enums_non_flags);
3145 g_test_add_func ("/gsettings/flags", test_flags);
3146 g_test_add_func ("/gsettings/flags/subprocess/non-flags-key", test_flags_non_flags_key);
3147 g_test_add_func ("/gsettings/flags/subprocess/non-flags-value", test_flags_non_flags_value);
3148 g_test_add_func ("/gsettings/flags/subprocess/range", test_flags_range);
3149 g_test_add_func ("/gsettings/flags/subprocess/non-enum", test_flags_non_enum);
3150 g_test_add_func ("/gsettings/range", test_range);
3151 g_test_add_func ("/gsettings/range/subprocess/high", test_range_high);
3152 g_test_add_func ("/gsettings/range/subprocess/low", test_range_low);
3153 g_test_add_func ("/gsettings/list-items", test_list_items);
3154 g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
3155 g_test_add_func ("/gsettings/mapped", test_get_mapped);
3156 g_test_add_func ("/gsettings/get-range", test_get_range);
3157 g_test_add_func ("/gsettings/schema-source", test_schema_source);
3158 g_test_add_func ("/gsettings/schema-list-keys", test_schema_list_keys);
3159 g_test_add_func ("/gsettings/actions", test_actions);
3160 g_test_add_func ("/gsettings/null-backend", test_null_backend);
3161 g_test_add_func ("/gsettings/memory-backend", test_memory_backend);
3162 g_test_add_func ("/gsettings/read-descriptions", test_read_descriptions);
3163 g_test_add_func ("/gsettings/test-extended-schema", test_extended_schema);
3164 g_test_add_func ("/gsettings/default-value", test_default_value);
3165 g_test_add_func ("/gsettings/per-desktop", test_per_desktop);
3166 g_test_add_func ("/gsettings/per-desktop/subprocess", test_per_desktop_subprocess);
3168 result = g_test_run ();
3172 /* FIXME: Due to the way #GSettings objects can be used without specifying a
3173 * backend, the default backend is leaked. In order to be able to run this
3174 * test under valgrind and get meaningful checking for real leaks, use this
3175 * hack to drop the final reference to the default #GSettingsBackend.
3177 * This should not be used in production code. */
3179 GSettingsBackend *backend;
3181 backend = g_settings_backend_get_default ();
3182 g_object_unref (backend); /* reference from the *_get_default() call */
3183 g_assert_finalize_object (backend); /* singleton reference owned by GLib */