10 #define G_SETTINGS_ENABLE_BACKEND
11 #include <gio/gsettingsbackend.h>
16 /* Needed on macOS and FreeBSD for uselocale() */
20 static const gchar *locale_dir = ".";
22 static gboolean backend_set;
24 /* These tests rely on the schemas in org.gtk.test.gschema.xml
25 * to be compiled and installed in the same directory.
34 setup (Fixture *fixture,
35 gconstpointer user_data)
39 fixture->tmp_dir = g_dir_make_tmp ("gio-test-gsettings_XXXXXX", &error);
40 g_assert_no_error (error);
42 g_test_message ("Using temporary directory: %s", fixture->tmp_dir);
46 teardown (Fixture *fixture,
47 gconstpointer user_data)
49 g_assert_no_errno (g_rmdir (fixture->tmp_dir));
50 g_clear_pointer (&fixture->tmp_dir, g_free);
54 check_and_free (GVariant *value,
55 const gchar *expected)
59 printed = g_variant_print (value, TRUE);
60 g_assert_cmpstr (printed, ==, expected);
63 g_variant_unref (value);
66 /* Wrapper around g_assert_cmpstr() which gets a setting from a #GSettings
67 * using g_settings_get(). */
68 #define settings_assert_cmpstr(settings, key, op, expected_value) G_STMT_START { \
70 g_settings_get ((settings), (key), "s", &__str); \
71 g_assert_cmpstr (__str, op, (expected_value)); \
76 /* Just to get warmed up: Read and set a string, and
77 * verify that can read the changed string back
85 gboolean has_unapplied;
89 settings = g_settings_new ("org.gtk.test");
91 g_object_get (settings,
95 "has-unapplied", &has_unapplied,
96 "delay-apply", &delay_apply,
98 g_assert_cmpstr (str, ==, "org.gtk.test");
100 g_assert_cmpstr (path, ==, "/tests/");
101 g_assert_false (has_unapplied);
102 g_assert_false (delay_apply);
107 settings_assert_cmpstr (settings, "greeting", ==, "Hello, earthlings");
109 g_settings_set (settings, "greeting", "s", "goodbye world");
110 settings_assert_cmpstr (settings, "greeting", ==, "goodbye world");
112 if (!backend_set && g_test_undefined ())
114 GSettings *tmp_settings = g_settings_new ("org.gtk.test");
116 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
117 "*g_settings_set_value*expects type*");
118 g_settings_set (tmp_settings, "greeting", "i", 555);
119 g_test_assert_expected_messages ();
121 g_object_unref (tmp_settings);
124 settings_assert_cmpstr (settings, "greeting", ==, "goodbye world");
126 g_settings_reset (settings, "greeting");
127 str = g_settings_get_string (settings, "greeting");
128 g_assert_cmpstr (str, ==, "Hello, earthlings");
131 g_settings_set (settings, "greeting", "s", "this is the end");
132 g_object_unref (settings);
135 /* Check that we get an error when getting a key
136 * that is not in the schema
139 test_unknown_key (void)
141 if (!g_test_undefined ())
144 if (g_test_subprocess ())
149 settings = g_settings_new ("org.gtk.test");
150 value = g_settings_get_value (settings, "no_such_key");
152 g_assert_null (value);
154 g_object_unref (settings);
157 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
158 g_test_trap_assert_failed ();
159 g_test_trap_assert_stderr ("*does not contain*");
162 /* Check that we get an error when the schema
163 * has not been installed
166 test_no_schema (void)
168 if (!g_test_undefined ())
171 if (g_test_subprocess ())
175 settings = g_settings_new ("no.such.schema");
177 g_assert_null (settings);
180 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
181 g_test_trap_assert_failed ();
182 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
185 /* Check that we get an error when passing a type string
186 * that does not match the schema
189 test_wrong_type (void)
194 if (!g_test_undefined ())
197 settings = g_settings_new ("org.gtk.test");
199 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
200 "*given value has a type of*");
201 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
202 "*valid_format_string*");
203 g_settings_get (settings, "greeting", "o", &str);
204 g_test_assert_expected_messages ();
208 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
209 "*expects type 's'*");
210 g_settings_set (settings, "greeting", "o", "/a/path");
211 g_test_assert_expected_messages ();
213 g_object_unref (settings);
216 /* Check errors with explicit paths */
218 test_wrong_path (void)
220 if (!g_test_undefined ())
223 if (g_test_subprocess ())
225 GSettings *settings G_GNUC_UNUSED;
227 settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
230 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
231 g_test_trap_assert_failed ();
232 g_test_trap_assert_stderr ("*but path * specified by schema*");
238 if (!g_test_undefined ())
241 if (g_test_subprocess ())
243 GSettings *settings G_GNUC_UNUSED;
245 settings = g_settings_new ("org.gtk.test.no-path");
248 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
249 g_test_trap_assert_failed ();
250 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
254 /* Check that we can successfully read and set the full
255 * range of all basic types
258 test_basic_types (void)
272 settings = g_settings_new ("org.gtk.test.basic-types");
274 g_settings_get (settings, "test-boolean", "b", &b);
275 g_assert_cmpint (b, ==, 1);
277 g_settings_set (settings, "test-boolean", "b", 0);
278 g_settings_get (settings, "test-boolean", "b", &b);
279 g_assert_cmpint (b, ==, 0);
281 g_settings_get (settings, "test-byte", "y", &byte);
282 g_assert_cmpint (byte, ==, 25);
284 g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
285 g_settings_get (settings, "test-byte", "y", &byte);
286 g_assert_cmpint (byte, ==, G_MAXUINT8);
288 g_settings_get (settings, "test-int16", "n", &i16);
289 g_assert_cmpint (i16, ==, -1234);
291 g_settings_set (settings, "test-int16", "n", G_MININT16);
292 g_settings_get (settings, "test-int16", "n", &i16);
293 g_assert_cmpint (i16, ==, G_MININT16);
295 g_settings_set (settings, "test-int16", "n", G_MAXINT16);
296 g_settings_get (settings, "test-int16", "n", &i16);
297 g_assert_cmpint (i16, ==, G_MAXINT16);
299 g_settings_get (settings, "test-uint16", "q", &u16);
300 g_assert_cmpuint (u16, ==, 1234);
302 g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
303 g_settings_get (settings, "test-uint16", "q", &u16);
304 g_assert_cmpuint (u16, ==, G_MAXUINT16);
306 g_settings_get (settings, "test-int32", "i", &i32);
307 g_assert_cmpint (i32, ==, -123456);
309 g_settings_set (settings, "test-int32", "i", G_MININT32);
310 g_settings_get (settings, "test-int32", "i", &i32);
311 g_assert_cmpint (i32, ==, G_MININT32);
313 g_settings_set (settings, "test-int32", "i", G_MAXINT32);
314 g_settings_get (settings, "test-int32", "i", &i32);
315 g_assert_cmpint (i32, ==, G_MAXINT32);
317 g_settings_get (settings, "test-uint32", "u", &u32);
318 g_assert_cmpuint (u32, ==, 123456);
320 g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
321 g_settings_get (settings, "test-uint32", "u", &u32);
322 g_assert_cmpuint (u32, ==, G_MAXUINT32);
324 g_settings_get (settings, "test-int64", "x", &i64);
325 g_assert_cmpuint (i64, ==, -123456789);
327 g_settings_set (settings, "test-int64", "x", G_MININT64);
328 g_settings_get (settings, "test-int64", "x", &i64);
329 g_assert_cmpuint (i64, ==, G_MININT64);
331 g_settings_set (settings, "test-int64", "x", G_MAXINT64);
332 g_settings_get (settings, "test-int64", "x", &i64);
333 g_assert_cmpuint (i64, ==, G_MAXINT64);
335 g_settings_get (settings, "test-uint64", "t", &u64);
336 g_assert_cmpuint (u64, ==, 123456789);
338 g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
339 g_settings_get (settings, "test-uint64", "t", &u64);
340 g_assert_cmpuint (u64, ==, G_MAXUINT64);
342 g_settings_get (settings, "test-double", "d", &d);
343 g_assert_cmpfloat (d, ==, 123.456);
345 g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
346 g_settings_get (settings, "test-double", "d", &d);
347 g_assert_cmpfloat (d, ==, G_MINDOUBLE);
349 g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
350 g_settings_get (settings, "test-double", "d", &d);
351 g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
353 settings_assert_cmpstr (settings, "test-string", ==, "a string, it seems");
355 g_settings_get (settings, "test-objectpath", "o", &str);
356 g_assert_cmpstr (str, ==, "/a/object/path");
357 g_object_unref (settings);
362 /* Check that we can read an set complex types like
363 * tuples, arrays and dictionaries
366 test_complex_types (void)
371 GVariantIter *iter = NULL;
374 settings = g_settings_new ("org.gtk.test.complex-types");
376 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
377 g_assert_cmpstr (s, ==, "one");
378 g_assert_cmpint (i1,==, 2);
379 g_assert_cmpint (i2,==, 3);
383 g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
384 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
385 g_assert_cmpstr (s, ==, "none");
386 g_assert_cmpint (i1,==, 0);
387 g_assert_cmpint (i2,==, 0);
391 g_settings_get (settings, "test-array", "ai", &iter);
392 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
393 g_assert_true (g_variant_iter_next (iter, "i", &i1));
394 g_assert_cmpint (i1, ==, 0);
395 g_assert_true (g_variant_iter_next (iter, "i", &i1));
396 g_assert_cmpint (i1, ==, 1);
397 g_assert_true (g_variant_iter_next (iter, "i", &i1));
398 g_assert_cmpint (i1, ==, 2);
399 g_assert_true (g_variant_iter_next (iter, "i", &i1));
400 g_assert_cmpint (i1, ==, 3);
401 g_assert_true (g_variant_iter_next (iter, "i", &i1));
402 g_assert_cmpint (i1, ==, 4);
403 g_assert_true (g_variant_iter_next (iter, "i", &i1));
404 g_assert_cmpint (i1, ==, 5);
405 g_assert_false (g_variant_iter_next (iter, "i", &i1));
406 g_variant_iter_free (iter);
408 g_settings_get (settings, "test-dict", "a{sau}", &iter);
409 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 2);
410 g_assert_true (g_variant_iter_next (iter, "{&s@au}", &s, &v));
411 g_assert_cmpstr (s, ==, "AC");
412 g_assert_cmpstr ((char *)g_variant_get_type (v), ==, "au");
414 g_assert_true (g_variant_iter_next (iter, "{&s@au}", &s, &v));
415 g_assert_cmpstr (s, ==, "IV");
416 g_assert_cmpstr ((char *)g_variant_get_type (v), ==, "au");
418 g_variant_iter_free (iter);
420 v = g_settings_get_value (settings, "test-dict");
421 g_assert_cmpstr ((char *)g_variant_get_type (v), ==, "a{sau}");
424 g_object_unref (settings);
427 static gboolean changed_cb_called;
430 changed_cb (GSettings *settings,
434 changed_cb_called = TRUE;
436 g_assert_cmpstr (key, ==, data);
439 /* Test that basic change notification with the changed signal works.
445 GSettings *settings2;
447 settings = g_settings_new ("org.gtk.test");
449 g_signal_connect (settings, "changed",
450 G_CALLBACK (changed_cb), "greeting");
452 changed_cb_called = FALSE;
454 g_settings_set (settings, "greeting", "s", "new greeting");
455 g_assert_true (changed_cb_called);
457 settings2 = g_settings_new ("org.gtk.test");
459 changed_cb_called = FALSE;
461 g_settings_set (settings2, "greeting", "s", "hi");
462 g_assert_true (changed_cb_called);
464 g_object_unref (settings2);
465 g_object_unref (settings);
468 static gboolean changed_cb_called2;
471 changed_cb2 (GSettings *settings,
480 /* Test that changes done to a delay-mode instance
481 * don't appear to the outside world until apply. Also
482 * check that we get change notification when they are
484 * Also test that the has-unapplied property is properly
488 test_delay_apply (void)
491 GSettings *settings2;
496 settings = g_settings_new ("org.gtk.test");
497 settings2 = g_settings_new ("org.gtk.test");
499 g_settings_set (settings2, "greeting", "s", "top o' the morning");
501 changed_cb_called = FALSE;
502 changed_cb_called2 = FALSE;
504 g_signal_connect (settings, "changed",
505 G_CALLBACK (changed_cb2), &changed_cb_called);
506 g_signal_connect (settings2, "changed",
507 G_CALLBACK (changed_cb2), &changed_cb_called2);
509 g_settings_delay (settings);
511 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
513 g_assert_true (changed_cb_called);
514 g_assert_false (changed_cb_called2);
516 /* Try resetting the key and ensure a notification is emitted on the delayed #GSettings object. */
517 changed_cb_called = FALSE;
518 changed_cb_called2 = FALSE;
520 g_settings_reset (settings, "greeting");
522 g_assert_true (changed_cb_called);
523 g_assert_false (changed_cb_called2);
525 /* Locally change the greeting again. */
526 changed_cb_called = FALSE;
527 changed_cb_called2 = FALSE;
529 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
531 g_assert_true (changed_cb_called);
532 g_assert_false (changed_cb_called2);
534 writable = g_settings_is_writable (settings, "greeting");
535 g_assert_true (writable);
537 settings_assert_cmpstr (settings, "greeting", ==, "greetings from test_delay_apply");
539 v = g_settings_get_user_value (settings, "greeting");
540 s = g_variant_get_string (v, NULL);
541 g_assert_cmpstr (s, ==, "greetings from test_delay_apply");
544 settings_assert_cmpstr (settings2, "greeting", ==, "top o' the morning");
546 g_assert_true (g_settings_get_has_unapplied (settings));
547 g_assert_false (g_settings_get_has_unapplied (settings2));
549 changed_cb_called = FALSE;
550 changed_cb_called2 = FALSE;
552 g_settings_apply (settings);
554 g_assert_false (changed_cb_called);
555 g_assert_true (changed_cb_called2);
557 settings_assert_cmpstr (settings, "greeting", ==, "greetings from test_delay_apply");
558 settings_assert_cmpstr (settings2, "greeting", ==, "greetings from test_delay_apply");
560 g_assert_false (g_settings_get_has_unapplied (settings));
561 g_assert_false (g_settings_get_has_unapplied (settings2));
563 g_settings_reset (settings, "greeting");
564 g_settings_apply (settings);
566 settings_assert_cmpstr (settings, "greeting", ==, "Hello, earthlings");
568 g_object_unref (settings2);
569 g_object_unref (settings);
572 /* Test that reverting unapplied changes in a delay-apply
573 * settings instance works.
576 test_delay_revert (void)
579 GSettings *settings2;
581 settings = g_settings_new ("org.gtk.test");
582 settings2 = g_settings_new ("org.gtk.test");
584 g_settings_set (settings2, "greeting", "s", "top o' the morning");
586 settings_assert_cmpstr (settings, "greeting", ==, "top o' the morning");
588 g_settings_delay (settings);
590 g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
592 settings_assert_cmpstr (settings, "greeting", ==, "greetings from test_delay_revert");
593 settings_assert_cmpstr (settings2, "greeting", ==, "top o' the morning");
595 g_assert_true (g_settings_get_has_unapplied (settings));
597 g_settings_revert (settings);
599 g_assert_false (g_settings_get_has_unapplied (settings));
601 settings_assert_cmpstr (settings, "greeting", ==, "top o' the morning");
602 settings_assert_cmpstr (settings2, "greeting", ==, "top o' the morning");
604 g_object_unref (settings2);
605 g_object_unref (settings);
609 test_delay_child (void)
617 base = g_settings_new ("org.gtk.test.basic-types");
618 g_settings_set (base, "test-byte", "y", 36);
620 settings = g_settings_new ("org.gtk.test");
621 g_settings_delay (settings);
622 g_object_get (settings, "delay-apply", &delay, NULL);
623 g_assert_true (delay);
625 child = g_settings_get_child (settings, "basic-types");
626 g_assert_nonnull (child);
628 g_object_get (child, "delay-apply", &delay, NULL);
629 g_assert_true (delay);
631 g_settings_get (child, "test-byte", "y", &byte);
632 g_assert_cmpuint (byte, ==, 36);
634 g_settings_set (child, "test-byte", "y", 42);
636 /* make sure the child was delayed too */
637 g_settings_get (base, "test-byte", "y", &byte);
638 g_assert_cmpuint (byte, ==, 36);
640 /* apply the child and the changes should be saved */
641 g_settings_apply (child);
642 g_settings_get (base, "test-byte", "y", &byte);
643 g_assert_cmpuint (byte, ==, 42);
645 g_object_unref (child);
646 g_object_unref (settings);
647 g_object_unref (base);
651 test_delay_reset_key (void)
653 GSettings *direct_settings = NULL, *delayed_settings = NULL;
655 g_test_summary ("Test that resetting a key on a delayed settings instance works");
657 delayed_settings = g_settings_new ("org.gtk.test");
658 direct_settings = g_settings_new ("org.gtk.test");
660 g_settings_set (direct_settings, "greeting", "s", "ey up");
662 settings_assert_cmpstr (delayed_settings, "greeting", ==, "ey up");
664 /* Set up a delayed settings backend. */
665 g_settings_delay (delayed_settings);
667 g_settings_set (delayed_settings, "greeting", "s", "how do");
669 settings_assert_cmpstr (delayed_settings, "greeting", ==, "how do");
670 settings_assert_cmpstr (direct_settings, "greeting", ==, "ey up");
672 g_assert_true (g_settings_get_has_unapplied (delayed_settings));
674 g_settings_reset (delayed_settings, "greeting");
676 /* There are still unapplied settings, because the reset is resetting to the
677 * value from the schema, not the value from @direct_settings. */
678 g_assert_true (g_settings_get_has_unapplied (delayed_settings));
680 settings_assert_cmpstr (delayed_settings, "greeting", ==, "Hello, earthlings");
681 settings_assert_cmpstr (direct_settings, "greeting", ==, "ey up");
683 /* Apply the settings changes (i.e. the reset). */
684 g_settings_apply (delayed_settings);
686 g_assert_false (g_settings_get_has_unapplied (delayed_settings));
688 settings_assert_cmpstr (delayed_settings, "greeting", ==, "Hello, earthlings");
689 settings_assert_cmpstr (direct_settings, "greeting", ==, "Hello, earthlings");
691 g_object_unref (direct_settings);
692 g_object_unref (delayed_settings);
696 keys_changed_cb (GSettings *settings,
700 g_assert_cmpint (n_keys, ==, 2);
702 g_assert_true ((keys[0] == g_quark_from_static_string ("greeting") &&
703 keys[1] == g_quark_from_static_string ("farewell")) ||
704 (keys[1] == g_quark_from_static_string ("greeting") &&
705 keys[0] == g_quark_from_static_string ("farewell")));
707 settings_assert_cmpstr (settings, "greeting", ==, "greetings from test_atomic");
708 settings_assert_cmpstr (settings, "farewell", ==, "atomic bye-bye");
711 /* Check that delay-applied changes appear atomically.
712 * More specifically, verify that all changed keys appear
713 * with their new value while handling the change-event signal.
719 GSettings *settings2;
721 settings = g_settings_new ("org.gtk.test");
722 settings2 = g_settings_new ("org.gtk.test");
724 g_settings_set (settings2, "greeting", "s", "top o' the morning");
726 changed_cb_called = FALSE;
727 changed_cb_called2 = FALSE;
729 g_signal_connect (settings2, "change-event",
730 G_CALLBACK (keys_changed_cb), NULL);
732 g_settings_delay (settings);
734 g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
735 g_settings_set (settings, "farewell", "s", "atomic bye-bye");
737 g_settings_apply (settings);
739 settings_assert_cmpstr (settings, "greeting", ==, "greetings from test_atomic");
740 settings_assert_cmpstr (settings, "farewell", ==, "atomic bye-bye");
741 settings_assert_cmpstr (settings2, "greeting", ==, "greetings from test_atomic");
742 settings_assert_cmpstr (settings2, "farewell", ==, "atomic bye-bye");
744 g_object_unref (settings2);
745 g_object_unref (settings);
748 /* On Windows the interaction between the C library locale and libintl
749 * (from GNU gettext) is not like on POSIX, so just skip these tests
752 * There are several issues:
754 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
755 * in libintl (defined in its <libintl.h>).
757 * 2) The locale names that uselocale() accepts and returns aren't in
758 * the "de_DE" style, but like "German_Germany".
760 * 3) libintl looks at the Win32 thread locale and not the C library
761 * locale. (And even if libintl would use the C library's locale, as
762 * there are several alternative C library DLLs, libintl might be
763 * linked to a different one than the application code, so they
764 * wouldn't have the same C library locale anyway.)
767 /* Test that translations work for schema defaults.
769 * This test relies on the de.po file in the same directory
770 * to be compiled into ./de/LC_MESSAGES/test.mo
775 #ifndef HAVE_USELOCALE
776 g_test_skip ("Unsafe to change locale because platform does not support uselocale()");
780 locale_t original_locale;
784 bindtextdomain ("test", locale_dir);
785 bind_textdomain_codeset ("test", "UTF-8");
787 original_locale = uselocale ((locale_t) 0);
788 g_assert_true (original_locale != (locale_t) 0);
789 new_locale = newlocale (LC_MESSAGES_MASK, "C", (locale_t) 0);
790 g_assert_true (new_locale != (locale_t) 0);
791 result = uselocale (new_locale);
792 g_assert_true (result == original_locale);
794 settings = g_settings_new ("org.gtk.test.localized");
795 str = g_settings_get_string (settings, "error-message");
797 result = uselocale (original_locale);
798 g_assert_true (result == new_locale);
799 freelocale (new_locale);
801 g_assert_cmpstr (str, ==, "Unnamed");
805 new_locale = newlocale (LC_MESSAGES_MASK, "de_DE.UTF-8", (locale_t) 0);
806 if (new_locale == (locale_t) 0)
808 g_test_skip ("Cannot run test becaues de_DE.UTF-8 locale is not available");
809 g_object_unref (settings);
812 result = uselocale (new_locale);
813 g_assert_true (result == original_locale);
815 /* Only do the test if translation is actually working... */
816 if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
818 str = g_settings_get_string (settings, "error-message");
820 g_assert_cmpstr (str, ==, "Unbenannt");
826 g_test_skip ("translation is not working");
829 result = uselocale (original_locale);
830 g_assert_true (result == new_locale);
831 freelocale (new_locale);
833 g_object_unref (settings);
837 /* Test that message context works as expected with translated
838 * schema defaults. Also, verify that non-ASCII UTF-8 content
841 * This test relies on the de.po file in the same directory
842 * to be compiled into ./de/LC_MESSAGES/test.mo
845 test_l10n_context (void)
847 #ifndef HAVE_USELOCALE
848 g_test_skip ("Unsafe to change locale because platform does not support uselocale()");
852 locale_t original_locale;
856 bindtextdomain ("test", locale_dir);
857 bind_textdomain_codeset ("test", "UTF-8");
859 settings = g_settings_new ("org.gtk.test.localized");
861 original_locale = uselocale ((locale_t) 0);
862 g_assert_true (original_locale != (locale_t) 0);
863 new_locale = newlocale (LC_MESSAGES_MASK, "C", (locale_t) 0);
864 g_assert_true (new_locale != (locale_t) 0);
865 result = uselocale (new_locale);
866 g_assert_true (result == original_locale);
868 g_settings_get (settings, "backspace", "s", &str);
870 result = uselocale (original_locale);
871 g_assert_true (result == new_locale);
872 freelocale (new_locale);
874 g_assert_cmpstr (str, ==, "BackSpace");
878 new_locale = newlocale (LC_MESSAGES_MASK, "de_DE.UTF-8", (locale_t) 0);
879 if (new_locale == (locale_t) 0)
881 g_test_skip ("Cannot run test becaues de_DE.UTF-8 locale is not available");
882 g_object_unref (settings);
885 result = uselocale (new_locale);
886 g_assert_true (result == original_locale);
888 /* Only do the test if translation is actually working... */
889 if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
890 settings_assert_cmpstr (settings, "backspace", ==, "Löschen");
892 g_test_skip ("translation is not working");
894 result = uselocale (original_locale);
895 g_assert_true (result == new_locale);
896 freelocale (new_locale);
898 g_object_unref (settings);
902 /* Test use of l10n="time" and LC_TIME. */
904 test_l10n_time (void)
906 #ifndef HAVE_USELOCALE
907 g_test_skip ("Unsafe to change locale because platform does not support uselocale()");
911 locale_t original_locale;
915 g_test_summary ("Test that l10n='time' attribute uses the correct category for translations");
916 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2575");
918 bindtextdomain ("test", locale_dir);
919 bind_textdomain_codeset ("test", "UTF-8");
921 settings = g_settings_new ("org.gtk.test.localized");
923 original_locale = uselocale ((locale_t) 0);
924 g_assert_true (original_locale != (locale_t) 0);
925 new_locale = duplocale (original_locale);
926 g_assert_true (new_locale != (locale_t) 0);
927 new_locale = newlocale (LC_TIME_MASK, "C", new_locale);
928 g_assert_true (new_locale != (locale_t) 0);
929 result = uselocale (new_locale);
930 g_assert_true (result == original_locale);
932 str = g_settings_get_string (settings, "midnight");
934 result = uselocale (original_locale);
935 g_assert_true (result == new_locale);
937 g_assert_cmpstr (str, ==, "12:00 AM");
941 new_locale = newlocale (LC_TIME_MASK, "de_DE.UTF-8", new_locale);
942 if (new_locale == (locale_t) 0)
944 g_test_skip ("Cannot run test becaues de_DE.UTF-8 locale is not available");
945 g_object_unref (settings);
948 result = uselocale (new_locale);
949 g_assert_true (result != (locale_t) 0);
951 /* Only do the test if translation is actually working... */
952 if (g_str_equal (dgettext ("test", "\"12:00 AM\""), "\"00:00\""))
954 str = g_settings_get_string (settings, "midnight");
956 g_assert_cmpstr (str, ==, "00:00");
962 g_test_skip ("translation is not working");
965 result = uselocale (original_locale);
966 g_assert_true (result == new_locale);
967 freelocale (new_locale);
969 g_object_unref (settings);
996 GObject parent_instance;
999 gboolean anti_bool_prop;
1002 guint16 uint16_prop;
1006 guint64 uint64_prop;
1007 gdouble double_prop;
1009 gchar *no_read_prop;
1010 gchar *no_write_prop;
1018 GObjectClass parent_class;
1021 static GType test_object_get_type (void);
1022 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
1025 test_object_init (TestObject *object)
1030 test_object_finalize (GObject *object)
1032 TestObject *testo = (TestObject*)object;
1033 g_strfreev (testo->strv_prop);
1034 g_free (testo->string_prop);
1035 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
1039 test_object_get_property (GObject *object,
1044 TestObject *test_object = (TestObject *)object;
1049 g_value_set_boolean (value, test_object->bool_prop);
1051 case PROP_ANTI_BOOL:
1052 g_value_set_boolean (value, test_object->anti_bool_prop);
1055 g_value_set_schar (value, test_object->byte_prop);
1058 g_value_set_uint (value, test_object->uint16_prop);
1061 g_value_set_int (value, test_object->int16_prop);
1064 g_value_set_int (value, test_object->int_prop);
1067 g_value_set_uint (value, test_object->uint_prop);
1070 g_value_set_int64 (value, test_object->int64_prop);
1073 g_value_set_uint64 (value, test_object->uint64_prop);
1076 g_value_set_double (value, test_object->double_prop);
1079 g_value_set_string (value, test_object->string_prop);
1082 g_value_set_string (value, test_object->no_write_prop);
1085 g_value_set_boxed (value, test_object->strv_prop);
1088 g_value_set_enum (value, test_object->enum_prop);
1091 g_value_set_flags (value, test_object->flags_prop);
1094 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1100 test_object_set_property (GObject *object,
1102 const GValue *value,
1105 TestObject *test_object = (TestObject *)object;
1110 test_object->bool_prop = g_value_get_boolean (value);
1112 case PROP_ANTI_BOOL:
1113 test_object->anti_bool_prop = g_value_get_boolean (value);
1116 test_object->byte_prop = g_value_get_schar (value);
1119 test_object->int16_prop = g_value_get_int (value);
1122 test_object->uint16_prop = g_value_get_uint (value);
1125 test_object->int_prop = g_value_get_int (value);
1128 test_object->uint_prop = g_value_get_uint (value);
1131 test_object->int64_prop = g_value_get_int64 (value);
1134 test_object->uint64_prop = g_value_get_uint64 (value);
1137 test_object->double_prop = g_value_get_double (value);
1140 g_free (test_object->string_prop);
1141 test_object->string_prop = g_value_dup_string (value);
1144 g_free (test_object->no_read_prop);
1145 test_object->no_read_prop = g_value_dup_string (value);
1148 g_strfreev (test_object->strv_prop);
1149 test_object->strv_prop = g_value_dup_boxed (value);
1152 test_object->enum_prop = g_value_get_enum (value);
1155 test_object->flags_prop = g_value_get_flags (value);
1158 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1164 test_enum_get_type (void)
1166 static gsize define_type_id = 0;
1168 if (g_once_init_enter (&define_type_id))
1170 static const GEnumValue values[] = {
1171 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
1172 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
1173 { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
1174 { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
1178 GType type_id = g_enum_register_static ("TestEnum", values);
1179 g_once_init_leave (&define_type_id, type_id);
1182 return define_type_id;
1186 test_flags_get_type (void)
1188 static gsize define_type_id = 0;
1190 if (g_once_init_enter (&define_type_id))
1192 static const GFlagsValue values[] = {
1193 { TEST_FLAGS_NONE, "TEST_FLAGS_NONE", "none" },
1194 { TEST_FLAGS_MOURNING, "TEST_FLAGS_MOURNING", "mourning" },
1195 { TEST_FLAGS_LAUGHING, "TEST_FLAGS_LAUGHING", "laughing" },
1196 { TEST_FLAGS_WALKING, "TEST_FLAGS_WALKING", "walking" },
1200 GType type_id = g_flags_register_static ("TestFlags", values);
1201 g_once_init_leave (&define_type_id, type_id);
1204 return define_type_id;
1208 test_object_class_init (TestObjectClass *class)
1210 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
1212 gobject_class->get_property = test_object_get_property;
1213 gobject_class->set_property = test_object_set_property;
1214 gobject_class->finalize = test_object_finalize;
1216 g_object_class_install_property (gobject_class, PROP_BOOL,
1217 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
1218 g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
1219 g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
1220 g_object_class_install_property (gobject_class, PROP_BYTE,
1221 g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
1222 g_object_class_install_property (gobject_class, PROP_INT16,
1223 g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
1224 g_object_class_install_property (gobject_class, PROP_UINT16,
1225 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
1226 g_object_class_install_property (gobject_class, PROP_INT,
1227 g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
1228 g_object_class_install_property (gobject_class, PROP_UINT,
1229 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
1230 g_object_class_install_property (gobject_class, PROP_INT64,
1231 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
1232 g_object_class_install_property (gobject_class, PROP_UINT64,
1233 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
1234 g_object_class_install_property (gobject_class, PROP_DOUBLE,
1235 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
1236 g_object_class_install_property (gobject_class, PROP_STRING,
1237 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
1238 g_object_class_install_property (gobject_class, PROP_NO_WRITE,
1239 g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
1240 g_object_class_install_property (gobject_class, PROP_NO_READ,
1241 g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
1242 g_object_class_install_property (gobject_class, PROP_STRV,
1243 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
1244 g_object_class_install_property (gobject_class, PROP_ENUM,
1245 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
1246 g_object_class_install_property (gobject_class, PROP_FLAGS,
1247 g_param_spec_flags ("flags", "", "", test_flags_get_type (), TEST_FLAGS_NONE, G_PARAM_READWRITE));
1251 test_object_new (void)
1253 return (TestObject*)g_object_new (test_object_get_type (), NULL);
1256 /* Test basic binding functionality for simple types.
1257 * Verify that with bidirectional bindings, changes on either side
1258 * are notified on the other end.
1261 test_simple_binding (void)
1264 GSettings *settings;
1280 settings = g_settings_new ("org.gtk.test.binding");
1281 obj = test_object_new ();
1283 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
1284 g_object_set (obj, "bool", TRUE, NULL);
1285 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1287 g_settings_set_boolean (settings, "bool", FALSE);
1289 g_object_get (obj, "bool", &b, NULL);
1290 g_assert_cmpint (b, ==, FALSE);
1292 g_settings_bind (settings, "anti-bool", obj, "anti-bool",
1293 G_SETTINGS_BIND_INVERT_BOOLEAN);
1294 g_object_set (obj, "anti-bool", FALSE, NULL);
1295 g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
1297 g_settings_set_boolean (settings, "anti-bool", FALSE);
1299 g_object_get (obj, "anti-bool", &b, NULL);
1300 g_assert_cmpint (b, ==, TRUE);
1302 g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
1304 g_object_set (obj, "byte", 123, NULL);
1306 g_settings_get (settings, "byte", "y", &y);
1307 g_assert_cmpint (y, ==, 123);
1309 g_settings_set (settings, "byte", "y", 54);
1311 g_object_get (obj, "byte", &y, NULL);
1312 g_assert_cmpint (y, ==, 54);
1314 g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
1316 g_object_set (obj, "int16", 1234, NULL);
1318 g_settings_get (settings, "int16", "n", &n);
1319 g_assert_cmpint (n, ==, 1234);
1321 g_settings_set (settings, "int16", "n", 4321);
1323 g_object_get (obj, "int16", &n2, NULL);
1324 g_assert_cmpint (n2, ==, 4321);
1326 g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
1328 g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1330 g_settings_get (settings, "uint16", "q", &q);
1331 g_assert_cmpuint (q, ==, G_MAXUINT16);
1333 g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1335 g_object_get (obj, "uint16", &q2, NULL);
1336 g_assert_cmpuint (q2, ==, (guint16) G_MAXINT16);
1338 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1340 g_object_set (obj, "int", 12345, NULL);
1341 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1343 g_settings_set_int (settings, "int", 54321);
1345 g_object_get (obj, "int", &i, NULL);
1346 g_assert_cmpint (i, ==, 54321);
1348 g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1350 g_object_set (obj, "uint", 12345, NULL);
1351 g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);
1353 g_settings_set_uint (settings, "uint", 54321);
1355 g_object_get (obj, "uint", &u, NULL);
1356 g_assert_cmpuint (u, ==, 54321);
1358 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1360 g_object_set (obj, "uint64", (guint64) 12345, NULL);
1361 g_assert_cmpuint (g_settings_get_uint64 (settings, "uint64"), ==, 12345);
1363 g_settings_set_uint64 (settings, "uint64", 54321);
1365 g_object_get (obj, "uint64", &u64, NULL);
1366 g_assert_cmpuint (u64, ==, 54321);
1368 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1370 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1372 g_settings_get (settings, "int64", "x", &i64);
1373 g_assert_cmpint (i64, ==, G_MAXINT64);
1375 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1377 g_object_get (obj, "int64", &i64, NULL);
1378 g_assert_cmpint (i64, ==, G_MININT64);
1380 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1382 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1384 g_settings_get (settings, "uint64", "t", &u64);
1385 g_assert_cmpuint (u64, ==, G_MAXUINT64);
1387 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1389 g_object_get (obj, "uint64", &u64, NULL);
1390 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1392 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1394 g_object_set (obj, "string", "bu ba", NULL);
1395 s = g_settings_get_string (settings, "string");
1396 g_assert_cmpstr (s, ==, "bu ba");
1399 g_settings_set_string (settings, "string", "bla bla");
1400 g_object_get (obj, "string", &s, NULL);
1401 g_assert_cmpstr (s, ==, "bla bla");
1404 g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1406 g_object_set (obj, "string", "non-unicode:\315", NULL);
1407 value = g_settings_get_value (settings, "chararray");
1408 g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1409 g_variant_unref (value);
1411 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1413 g_object_set (obj, "double", G_MAXFLOAT, NULL);
1414 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1416 g_settings_set_double (settings, "double", G_MINFLOAT);
1418 g_object_get (obj, "double", &d, NULL);
1419 g_assert_cmpfloat (d, ==, G_MINFLOAT);
1421 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1422 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1424 g_settings_set_double (settings, "double", -G_MINDOUBLE);
1426 g_object_get (obj, "double", &d, NULL);
1427 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1429 strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1430 g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1431 g_object_set (obj, "strv", strv, NULL);
1433 strv = g_settings_get_strv (settings, "strv");
1434 s = g_strjoinv (",", strv);
1435 g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1438 strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1439 g_settings_set_strv (settings, "strv", (const gchar **) strv);
1441 g_object_get (obj, "strv", &strv, NULL);
1442 s = g_strjoinv (",", strv);
1443 g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1446 g_settings_set_strv (settings, "strv", NULL);
1447 g_object_get (obj, "strv", &strv, NULL);
1448 g_assert_nonnull (strv);
1449 g_assert_cmpint (g_strv_length (strv), ==, 0);
1452 g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1453 g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1454 s = g_settings_get_string (settings, "enum");
1455 g_assert_cmpstr (s, ==, "baz");
1457 g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1459 g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1461 g_object_get (obj, "enum", &i, NULL);
1462 g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1464 g_settings_set_string (settings, "enum", "baz");
1466 g_object_get (obj, "enum", &i, NULL);
1467 g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1469 g_settings_bind (settings, "flags", obj, "flags", G_SETTINGS_BIND_DEFAULT);
1470 g_object_set (obj, "flags", TEST_FLAGS_MOURNING, NULL);
1471 strv = g_settings_get_strv (settings, "flags");
1472 g_assert_cmpint (g_strv_length (strv), ==, 1);
1473 g_assert_cmpstr (strv[0], ==, "mourning");
1476 g_assert_cmpint (g_settings_get_flags (settings, "flags"), ==, TEST_FLAGS_MOURNING);
1478 g_settings_set_flags (settings, "flags", TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1480 g_object_get (obj, "flags", &i, NULL);
1481 g_assert_cmpint (i, ==, TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1483 g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1485 g_object_set (obj, "uint", 12345, NULL);
1486 g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);
1488 g_settings_set_uint (settings, "uint", 54321);
1490 g_object_get (obj, "uint", &u, NULL);
1491 g_assert_cmpuint (u, ==, 54321);
1493 g_settings_bind (settings, "range", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1494 g_object_set (obj, "uint", 22, NULL);
1496 g_assert_cmpuint (g_settings_get_uint (settings, "range"), ==, 22);
1497 g_object_get (obj, "uint", &u, NULL);
1498 g_assert_cmpuint (u, ==, 22);
1500 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1501 "* is out of schema-specified range for*");
1502 g_object_set (obj, "uint", 45, NULL);
1503 g_test_assert_expected_messages ();
1505 g_object_get (obj, "uint", &u, NULL);
1506 g_assert_cmpuint (g_settings_get_uint (settings, "range"), ==, 22);
1507 /* The value of the object is currently not reset back to its initial value
1508 g_assert_cmpuint (u, ==, 22); */
1510 g_object_unref (obj);
1511 g_object_unref (settings);
1518 GSettings *settings;
1520 settings = g_settings_new ("org.gtk.test.binding");
1521 obj = test_object_new ();
1523 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1525 g_object_set (obj, "int", 12345, NULL);
1526 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1528 g_settings_unbind (obj, "int");
1530 g_object_set (obj, "int", 54321, NULL);
1531 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1533 g_object_unref (obj);
1534 g_object_unref (settings);
1538 test_bind_writable (void)
1541 GSettings *settings;
1544 settings = g_settings_new ("org.gtk.test.binding");
1545 obj = test_object_new ();
1547 g_object_set (obj, "bool", FALSE, NULL);
1549 g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1551 g_object_get (obj, "bool", &b, NULL);
1554 g_settings_unbind (obj, "bool");
1556 g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1558 g_object_get (obj, "bool", &b, NULL);
1561 g_object_unref (obj);
1562 g_object_unref (settings);
1565 /* Test one-way bindings.
1566 * Verify that changes on one side show up on the other,
1567 * but not vice versa
1570 test_directional_binding (void)
1573 GSettings *settings;
1577 settings = g_settings_new ("org.gtk.test.binding");
1578 obj = test_object_new ();
1580 g_object_set (obj, "bool", FALSE, NULL);
1581 g_settings_set_boolean (settings, "bool", FALSE);
1583 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1585 g_settings_set_boolean (settings, "bool", TRUE);
1586 g_object_get (obj, "bool", &b, NULL);
1587 g_assert_cmpint (b, ==, TRUE);
1589 g_object_set (obj, "bool", FALSE, NULL);
1590 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1592 g_object_set (obj, "int", 20, NULL);
1593 g_settings_set_int (settings, "int", 20);
1595 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1597 g_object_set (obj, "int", 32, NULL);
1598 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1600 g_settings_set_int (settings, "int", 20);
1601 g_object_get (obj, "int", &i, NULL);
1602 g_assert_cmpint (i, ==, 32);
1604 g_object_unref (obj);
1605 g_object_unref (settings);
1608 /* Test that type mismatch is caught when creating a binding */
1610 test_typesafe_binding (void)
1612 if (!g_test_undefined ())
1615 if (g_test_subprocess ())
1618 GSettings *settings;
1620 settings = g_settings_new ("org.gtk.test.binding");
1621 obj = test_object_new ();
1623 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1625 g_object_unref (obj);
1626 g_object_unref (settings);
1629 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
1630 g_test_trap_assert_failed ();
1631 g_test_trap_assert_stderr ("*not compatible*");
1635 string_to_bool (GValue *value,
1641 s = g_variant_get_string (variant, NULL);
1642 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1648 bool_to_string (const GValue *value,
1649 const GVariantType *expected_type,
1652 if (g_value_get_boolean (value))
1653 return g_variant_new_string ("true");
1655 return g_variant_new_string ("false");
1659 bool_to_bool (const GValue *value,
1660 const GVariantType *expected_type,
1663 return g_variant_new_boolean (g_value_get_boolean (value));
1666 /* Test custom bindings.
1667 * Translate strings to booleans and back
1670 test_custom_binding (void)
1673 GSettings *settings;
1677 settings = g_settings_new ("org.gtk.test.binding");
1678 obj = test_object_new ();
1680 g_settings_set_string (settings, "string", "true");
1682 g_settings_bind_with_mapping (settings, "string",
1684 G_SETTINGS_BIND_DEFAULT,
1689 g_settings_set_string (settings, "string", "false");
1690 g_object_get (obj, "bool", &b, NULL);
1691 g_assert_cmpint (b, ==, FALSE);
1693 g_settings_set_string (settings, "string", "not true");
1694 g_object_get (obj, "bool", &b, NULL);
1695 g_assert_cmpint (b, ==, FALSE);
1697 g_object_set (obj, "bool", TRUE, NULL);
1698 s = g_settings_get_string (settings, "string");
1699 g_assert_cmpstr (s, ==, "true");
1702 g_settings_bind_with_mapping (settings, "string",
1704 G_SETTINGS_BIND_DEFAULT,
1705 string_to_bool, bool_to_bool,
1707 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1708 "*binding mapping function for key 'string' returned"
1709 " GVariant of type 'b' when type 's' was requested*");
1710 g_object_set (obj, "bool", FALSE, NULL);
1711 g_test_assert_expected_messages ();
1713 g_object_unref (obj);
1714 g_object_unref (settings);
1717 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1718 * initial settings value is transported to the object
1719 * side, but later settings changes do not affect the
1723 test_no_change_binding (void)
1726 GSettings *settings;
1729 settings = g_settings_new ("org.gtk.test.binding");
1730 obj = test_object_new ();
1732 g_object_set (obj, "bool", TRUE, NULL);
1733 g_settings_set_boolean (settings, "bool", FALSE);
1735 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1737 g_object_get (obj, "bool", &b, NULL);
1738 g_assert_cmpint (b, ==, FALSE);
1740 g_settings_set_boolean (settings, "bool", TRUE);
1741 g_object_get (obj, "bool", &b, NULL);
1742 g_assert_cmpint (b, ==, FALSE);
1744 g_settings_set_boolean (settings, "bool", FALSE);
1745 g_object_set (obj, "bool", TRUE, NULL);
1746 b = g_settings_get_boolean (settings, "bool");
1747 g_assert_cmpint (b, ==, TRUE);
1749 g_object_unref (obj);
1750 g_object_unref (settings);
1753 /* Test that binding a non-readable property only
1754 * works in 'GET' mode.
1757 test_no_read_binding_fail (void)
1760 GSettings *settings;
1762 settings = g_settings_new ("org.gtk.test.binding");
1763 obj = test_object_new ();
1765 g_settings_bind (settings, "string", obj, "no-read", 0);
1769 test_no_read_binding_pass (void)
1772 GSettings *settings;
1774 settings = g_settings_new ("org.gtk.test.binding");
1775 obj = test_object_new ();
1777 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1783 test_no_read_binding (void)
1785 if (g_test_undefined ())
1787 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/fail", 0,
1788 G_TEST_SUBPROCESS_DEFAULT);
1789 g_test_trap_assert_failed ();
1790 g_test_trap_assert_stderr ("*property*is not readable*");
1793 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/pass", 0,
1794 G_TEST_SUBPROCESS_DEFAULT);
1795 g_test_trap_assert_passed ();
1798 /* Test that binding a non-writable property only
1799 * works in 'SET' mode.
1802 test_no_write_binding_fail (void)
1805 GSettings *settings;
1807 settings = g_settings_new ("org.gtk.test.binding");
1808 obj = test_object_new ();
1810 g_settings_bind (settings, "string", obj, "no-write", 0);
1814 test_no_write_binding_pass (void)
1817 GSettings *settings;
1819 settings = g_settings_new ("org.gtk.test.binding");
1820 obj = test_object_new ();
1822 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1828 test_no_write_binding (void)
1830 if (g_test_undefined ())
1832 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/fail", 0,
1833 G_TEST_SUBPROCESS_DEFAULT);
1834 g_test_trap_assert_failed ();
1835 g_test_trap_assert_stderr ("*property*is not writable*");
1838 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/pass", 0,
1839 G_TEST_SUBPROCESS_DEFAULT);
1840 g_test_trap_assert_passed ();
1844 key_changed_cb (GSettings *settings, const gchar *key, gpointer data)
1853 const gchar *root_group;
1854 const gchar *keyfile_group;
1855 const gchar *root_path;
1859 * Test that using a keyfile works
1862 test_keyfile (Fixture *fixture,
1863 gconstpointer user_data)
1865 GSettingsBackend *kf_backend;
1866 GSettings *settings;
1870 GError *error = NULL;
1873 gboolean called = FALSE;
1874 gchar *keyfile_path = NULL, *store_path = NULL;
1876 keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
1877 store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
1878 kf_backend = g_keyfile_settings_backend_new (store_path, "/", "root");
1879 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1880 g_object_unref (kf_backend);
1882 g_settings_reset (settings, "greeting");
1883 str = g_settings_get_string (settings, "greeting");
1884 g_assert_cmpstr (str, ==, "Hello, earthlings");
1887 writable = g_settings_is_writable (settings, "greeting");
1888 g_assert_true (writable);
1889 g_settings_set (settings, "greeting", "s", "see if this works");
1891 str = g_settings_get_string (settings, "greeting");
1892 g_assert_cmpstr (str, ==, "see if this works");
1895 g_settings_delay (settings);
1896 g_settings_set (settings, "farewell", "s", "cheerio");
1897 g_settings_apply (settings);
1899 keyfile = g_key_file_new ();
1900 g_assert_true (g_key_file_load_from_file (keyfile, store_path, 0, NULL));
1902 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1903 g_assert_cmpstr (str, ==, "'see if this works'");
1906 str = g_key_file_get_string (keyfile, "tests", "farewell", NULL);
1907 g_assert_cmpstr (str, ==, "'cheerio'");
1909 g_key_file_free (keyfile);
1911 g_settings_reset (settings, "greeting");
1912 g_settings_apply (settings);
1913 keyfile = g_key_file_new ();
1914 g_assert_true (g_key_file_load_from_file (keyfile, store_path, 0, NULL));
1916 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1917 g_assert_null (str);
1920 g_signal_connect (settings, "changed::greeting", G_CALLBACK (key_changed_cb), &called);
1922 g_key_file_set_string (keyfile, "tests", "greeting", "'howdy'");
1923 data = g_key_file_to_data (keyfile, &len, NULL);
1924 g_file_set_contents (store_path, data, len, &error);
1925 g_assert_no_error (error);
1927 g_main_context_iteration (NULL, FALSE);
1928 g_signal_handlers_disconnect_by_func (settings, key_changed_cb, &called);
1930 str = g_settings_get_string (settings, "greeting");
1931 g_assert_cmpstr (str, ==, "howdy");
1934 /* Now check setting a string without quotes */
1936 g_signal_connect (settings, "changed::greeting", G_CALLBACK (key_changed_cb), &called);
1938 g_key_file_set_string (keyfile, "tests", "greeting", "he\"l🤗uń");
1940 data = g_key_file_to_data (keyfile, &len, NULL);
1941 g_file_set_contents (store_path, data, len, &error);
1942 g_assert_no_error (error);
1944 g_main_context_iteration (NULL, FALSE);
1945 g_signal_handlers_disconnect_by_func (settings, key_changed_cb, &called);
1947 str = g_settings_get_string (settings, "greeting");
1948 g_assert_cmpstr (str, ==, "he\"l🤗uń");
1951 g_settings_set (settings, "farewell", "s", "cheerio");
1953 /* Check that empty keys/groups are not allowed. */
1954 g_assert_false (g_settings_is_writable (settings, ""));
1955 g_assert_false (g_settings_is_writable (settings, "/"));
1957 /* When executing as root, changing the mode of the keyfile will have
1958 * no effect on the writability of the settings.
1960 if (geteuid () != 0)
1963 g_signal_connect (settings, "writable-changed::greeting",
1964 G_CALLBACK (key_changed_cb), &called);
1966 g_assert_no_errno (g_chmod (keyfile_path, 0500));
1968 g_main_context_iteration (NULL, FALSE);
1969 g_signal_handlers_disconnect_by_func (settings, key_changed_cb, &called);
1971 writable = g_settings_is_writable (settings, "greeting");
1972 g_assert_false (writable);
1975 g_key_file_free (keyfile);
1978 g_object_unref (settings);
1980 /* Clean up the temporary directory. */
1981 g_assert_no_errno (g_chmod (keyfile_path, 0777));
1982 g_assert_no_errno (g_remove (store_path));
1983 g_assert_no_errno (g_rmdir (keyfile_path));
1984 g_free (store_path);
1985 g_free (keyfile_path);
1989 * Test that using a keyfile works with a schema with no path set.
1992 test_keyfile_no_path (Fixture *fixture,
1993 gconstpointer user_data)
1995 const KeyfileTestData *test_data = user_data;
1996 GSettingsBackend *kf_backend;
1997 GSettings *settings;
2001 GError *error = NULL;
2002 gchar *keyfile_path = NULL, *store_path = NULL;
2004 keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
2005 store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
2006 kf_backend = g_keyfile_settings_backend_new (store_path, test_data->root_path, test_data->root_group);
2007 settings = g_settings_new_with_backend_and_path ("org.gtk.test.no-path", kf_backend, test_data->path);
2008 g_object_unref (kf_backend);
2010 g_settings_reset (settings, "test-boolean");
2011 g_assert_true (g_settings_get_boolean (settings, "test-boolean"));
2013 writable = g_settings_is_writable (settings, "test-boolean");
2014 g_assert_true (writable);
2015 g_settings_set (settings, "test-boolean", "b", FALSE);
2017 g_assert_false (g_settings_get_boolean (settings, "test-boolean"));
2019 g_settings_delay (settings);
2020 g_settings_set (settings, "test-boolean", "b", TRUE);
2021 g_settings_apply (settings);
2023 keyfile = g_key_file_new ();
2024 g_assert_true (g_key_file_load_from_file (keyfile, store_path, 0, NULL));
2026 g_assert_true (g_key_file_get_boolean (keyfile, test_data->keyfile_group, "test-boolean", NULL));
2028 g_key_file_free (keyfile);
2030 g_settings_reset (settings, "test-boolean");
2031 g_settings_apply (settings);
2032 keyfile = g_key_file_new ();
2033 g_assert_true (g_key_file_load_from_file (keyfile, store_path, 0, NULL));
2035 g_assert_false (g_key_file_get_string (keyfile, test_data->keyfile_group, "test-boolean", &error));
2036 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
2037 g_clear_error (&error);
2039 /* Check that empty keys/groups are not allowed. */
2040 g_assert_false (g_settings_is_writable (settings, ""));
2041 g_assert_false (g_settings_is_writable (settings, "/"));
2043 /* Keys which ghost the root group name are not allowed. This can only be
2044 * tested when the path is `/` as otherwise it acts as a prefix and prevents
2046 if (g_str_equal (test_data->path, "/"))
2048 key = g_strdup_printf ("%s/%s", test_data->root_group, "");
2049 g_assert_false (g_settings_is_writable (settings, key));
2052 key = g_strdup_printf ("%s/%s", test_data->root_group, "/");
2053 g_assert_false (g_settings_is_writable (settings, key));
2056 key = g_strdup_printf ("%s/%s", test_data->root_group, "test-boolean");
2057 g_assert_false (g_settings_is_writable (settings, key));
2061 g_key_file_free (keyfile);
2062 g_object_unref (settings);
2064 /* Clean up the temporary directory. */
2065 g_assert_no_errno (g_chmod (keyfile_path, 0777));
2066 g_assert_no_errno (g_remove (store_path));
2067 g_assert_no_errno (g_rmdir (keyfile_path));
2068 g_free (store_path);
2069 g_free (keyfile_path);
2073 * Test that a keyfile rejects writes to keys outside its root path.
2076 test_keyfile_outside_root_path (Fixture *fixture,
2077 gconstpointer user_data)
2079 GSettingsBackend *kf_backend;
2080 GSettings *settings;
2081 gchar *keyfile_path = NULL, *store_path = NULL;
2083 keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
2084 store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
2085 kf_backend = g_keyfile_settings_backend_new (store_path, "/tests/basic-types/", "root");
2086 settings = g_settings_new_with_backend_and_path ("org.gtk.test.no-path", kf_backend, "/tests/");
2087 g_object_unref (kf_backend);
2089 g_assert_false (g_settings_is_writable (settings, "test-boolean"));
2091 g_object_unref (settings);
2093 /* Clean up the temporary directory. The keyfile probably doesn’t exist, so
2094 * don’t error on failure. */
2095 g_remove (store_path);
2096 g_assert_no_errno (g_rmdir (keyfile_path));
2097 g_free (store_path);
2098 g_free (keyfile_path);
2102 * Test that a keyfile rejects writes to keys in the root if no root group is set.
2105 test_keyfile_no_root_group (Fixture *fixture,
2106 gconstpointer user_data)
2108 GSettingsBackend *kf_backend;
2109 GSettings *settings;
2110 gchar *keyfile_path = NULL, *store_path = NULL;
2112 keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
2113 store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
2114 kf_backend = g_keyfile_settings_backend_new (store_path, "/", NULL);
2115 settings = g_settings_new_with_backend_and_path ("org.gtk.test.no-path", kf_backend, "/");
2116 g_object_unref (kf_backend);
2118 g_assert_false (g_settings_is_writable (settings, "test-boolean"));
2119 g_assert_true (g_settings_is_writable (settings, "child/test-boolean"));
2121 g_object_unref (settings);
2123 /* Clean up the temporary directory. The keyfile probably doesn’t exist, so
2124 * don’t error on failure. */
2125 g_remove (store_path);
2126 g_assert_no_errno (g_rmdir (keyfile_path));
2127 g_free (store_path);
2128 g_free (keyfile_path);
2131 /* Test that getting child schemas works
2134 test_child_schema (void)
2136 GSettings *settings;
2140 /* first establish some known conditions */
2141 settings = g_settings_new ("org.gtk.test.basic-types");
2142 g_settings_set (settings, "test-byte", "y", 36);
2144 g_settings_get (settings, "test-byte", "y", &byte);
2145 g_assert_cmpint (byte, ==, 36);
2147 g_object_unref (settings);
2149 settings = g_settings_new ("org.gtk.test");
2150 child = g_settings_get_child (settings, "basic-types");
2151 g_assert_nonnull (child);
2153 g_settings_get (child, "test-byte", "y", &byte);
2154 g_assert_cmpint (byte, ==, 36);
2156 g_object_unref (child);
2157 g_object_unref (settings);
2160 #include "../strinfo.c"
2165 /* "foo" has a value of 1
2166 * "bar" has a value of 2
2167 * "baz" is an alias for "bar"
2170 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
2171 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
2173 const guint32 *strinfo = (guint32 *) array;
2174 guint length = sizeof array / 4;
2178 /* build it and compare */
2181 builder = g_string_new (NULL);
2182 strinfo_builder_append_item (builder, "foo", 1);
2183 strinfo_builder_append_item (builder, "bar", 2);
2184 g_assert_true (strinfo_builder_append_alias (builder, "baz", "bar"));
2185 g_assert_cmpmem (builder->str, builder->len, strinfo, length * 4);
2186 g_string_free (builder, TRUE);
2189 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
2191 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
2193 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
2195 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
2198 g_assert_true (strinfo_enum_from_string (strinfo, length, "foo", &result));
2199 g_assert_cmpint (result, ==, 1);
2200 g_assert_true (strinfo_enum_from_string (strinfo, length, "bar", &result));
2201 g_assert_cmpint (result, ==, 2);
2202 g_assert_false (strinfo_enum_from_string (strinfo, length, "baz", &result));
2203 g_assert_false (strinfo_enum_from_string (strinfo, length, "quux", &result));
2205 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
2206 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
2207 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
2208 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
2210 g_assert_true (strinfo_is_string_valid (strinfo, length, "foo"));
2211 g_assert_true (strinfo_is_string_valid (strinfo, length, "bar"));
2212 g_assert_false (strinfo_is_string_valid (strinfo, length, "baz"));
2213 g_assert_false (strinfo_is_string_valid (strinfo, length, "quux"));
2217 test_enums_non_enum_key (void)
2221 direct = g_settings_new ("org.gtk.test.enums.direct");
2222 g_settings_get_enum (direct, "test");
2223 g_assert_not_reached ();
2227 test_enums_non_enum_value (void)
2229 GSettings *settings;
2231 settings = g_settings_new ("org.gtk.test.enums");
2232 g_settings_set_enum (settings, "test", 42);
2233 g_assert_not_reached ();
2237 test_enums_range (void)
2239 GSettings *settings;
2241 settings = g_settings_new ("org.gtk.test.enums");
2242 g_settings_set_string (settings, "test", "qux");
2243 g_assert_not_reached ();
2247 test_enums_non_flags (void)
2249 GSettings *settings;
2251 settings = g_settings_new ("org.gtk.test.enums");
2252 g_settings_get_flags (settings, "test");
2253 g_assert_not_reached ();
2259 GSettings *settings, *direct;
2262 settings = g_settings_new ("org.gtk.test.enums");
2263 direct = g_settings_new ("org.gtk.test.enums.direct");
2265 if (g_test_undefined () && !backend_set)
2267 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-key", 0,
2268 G_TEST_SUBPROCESS_DEFAULT);
2269 g_test_trap_assert_failed ();
2270 g_test_trap_assert_stderr ("*not associated with an enum*");
2272 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-value", 0,
2273 G_TEST_SUBPROCESS_DEFAULT);
2274 g_test_trap_assert_failed ();
2275 g_test_trap_assert_stderr ("*invalid enum value 42*");
2277 g_test_trap_subprocess ("/gsettings/enums/subprocess/range", 0,
2278 G_TEST_SUBPROCESS_DEFAULT);
2279 g_test_trap_assert_failed ();
2280 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2282 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-flags", 0,
2283 G_TEST_SUBPROCESS_DEFAULT);
2284 g_test_trap_assert_failed ();
2285 g_test_trap_assert_stderr ("*not associated with a flags*");
2288 str = g_settings_get_string (settings, "test");
2289 g_assert_cmpstr (str, ==, "bar");
2292 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
2294 str = g_settings_get_string (settings, "test");
2295 g_assert_cmpstr (str, ==, "foo");
2298 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
2300 g_settings_set_string (direct, "test", "qux");
2302 str = g_settings_get_string (direct, "test");
2303 g_assert_cmpstr (str, ==, "qux");
2306 str = g_settings_get_string (settings, "test");
2307 g_assert_cmpstr (str, ==, "quux");
2310 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
2312 g_object_unref (direct);
2313 g_object_unref (settings);
2317 test_flags_non_flags_key (void)
2321 direct = g_settings_new ("org.gtk.test.enums.direct");
2322 g_settings_get_flags (direct, "test");
2323 g_assert_not_reached ();
2327 test_flags_non_flags_value (void)
2329 GSettings *settings;
2331 settings = g_settings_new ("org.gtk.test.enums");
2332 g_settings_set_flags (settings, "f-test", 0x42);
2333 g_assert_not_reached ();
2337 test_flags_range (void)
2339 GSettings *settings;
2341 settings = g_settings_new ("org.gtk.test.enums");
2342 g_settings_set_strv (settings, "f-test",
2343 (const gchar **) g_strsplit ("rock", ",", 0));
2344 g_assert_not_reached ();
2348 test_flags_non_enum (void)
2350 GSettings *settings;
2352 settings = g_settings_new ("org.gtk.test.enums");
2353 g_settings_get_enum (settings, "f-test");
2354 g_assert_not_reached ();
2360 GSettings *settings, *direct;
2364 settings = g_settings_new ("org.gtk.test.enums");
2365 direct = g_settings_new ("org.gtk.test.enums.direct");
2367 if (g_test_undefined () && !backend_set)
2369 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-key", 0,
2370 G_TEST_SUBPROCESS_DEFAULT);
2371 g_test_trap_assert_failed ();
2372 g_test_trap_assert_stderr ("*not associated with a flags*");
2374 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-value", 0,
2375 G_TEST_SUBPROCESS_DEFAULT);
2376 g_test_trap_assert_failed ();
2377 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
2379 g_test_trap_subprocess ("/gsettings/flags/subprocess/range", 0,
2380 G_TEST_SUBPROCESS_DEFAULT);
2381 g_test_trap_assert_failed ();
2382 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2384 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-enum", 0,
2385 G_TEST_SUBPROCESS_DEFAULT);
2386 g_test_trap_assert_failed ();
2387 g_test_trap_assert_stderr ("*not associated with an enum*");
2390 strv = g_settings_get_strv (settings, "f-test");
2391 str = g_strjoinv (",", strv);
2392 g_assert_cmpstr (str, ==, "");
2396 g_settings_set_flags (settings, "f-test",
2397 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
2399 strv = g_settings_get_strv (settings, "f-test");
2400 str = g_strjoinv (",", strv);
2401 g_assert_cmpstr (str, ==, "talking,walking");
2405 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
2406 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
2408 strv = g_strsplit ("speaking,laughing", ",", 0);
2409 g_settings_set_strv (direct, "f-test", (const gchar **) strv);
2412 strv = g_settings_get_strv (direct, "f-test");
2413 str = g_strjoinv (",", strv);
2414 g_assert_cmpstr (str, ==, "speaking,laughing");
2418 strv = g_settings_get_strv (settings, "f-test");
2419 str = g_strjoinv (",", strv);
2420 g_assert_cmpstr (str, ==, "talking,laughing");
2424 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
2425 TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
2427 g_object_unref (direct);
2428 g_object_unref (settings);
2432 test_range_high (void)
2434 GSettings *settings;
2436 settings = g_settings_new ("org.gtk.test.range");
2437 g_settings_set_int (settings, "val", 45);
2438 g_assert_not_reached ();
2442 test_range_low (void)
2444 GSettings *settings;
2446 settings = g_settings_new ("org.gtk.test.range");
2447 g_settings_set_int (settings, "val", 1);
2448 g_assert_not_reached ();
2454 GSettings *settings, *direct;
2457 settings = g_settings_new ("org.gtk.test.range");
2458 direct = g_settings_new ("org.gtk.test.range.direct");
2460 if (g_test_undefined () && !backend_set)
2462 g_test_trap_subprocess ("/gsettings/range/subprocess/high", 0,
2463 G_TEST_SUBPROCESS_DEFAULT);
2464 g_test_trap_assert_failed ();
2465 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2467 g_test_trap_subprocess ("/gsettings/range/subprocess/low", 0,
2468 G_TEST_SUBPROCESS_DEFAULT);
2469 g_test_trap_assert_failed ();
2470 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2473 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2474 g_settings_set_int (direct, "val", 22);
2475 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
2476 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
2477 g_settings_set_int (direct, "val", 45);
2478 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
2479 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2480 g_settings_set_int (direct, "val", 1);
2481 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
2482 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2484 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2485 value = g_variant_new_int32 (1);
2486 g_assert_false (g_settings_range_check (settings, "val", value));
2487 g_variant_unref (value);
2488 value = g_variant_new_int32 (33);
2489 g_assert_true (g_settings_range_check (settings, "val", value));
2490 g_variant_unref (value);
2491 value = g_variant_new_int32 (45);
2492 g_assert_false (g_settings_range_check (settings, "val", value));
2493 g_variant_unref (value);
2494 G_GNUC_END_IGNORE_DEPRECATIONS
2496 g_object_unref (direct);
2497 g_object_unref (settings);
2501 strv_set_equal (const gchar * const *strv, ...)
2510 va_start (list, strv);
2513 str = va_arg (list, const gchar *);
2516 if (!g_strv_contains (strv, str))
2526 res = g_strv_length ((gchar**)strv) == count;
2532 test_list_items (void)
2534 GSettingsSchema *schema;
2535 GSettings *settings;
2539 settings = g_settings_new ("org.gtk.test");
2540 g_object_get (settings, "settings-schema", &schema, NULL);
2541 children = g_settings_list_children (settings);
2542 keys = g_settings_schema_list_keys (schema);
2544 g_assert_true (strv_set_equal ((const gchar * const *) children, "basic-types", "complex-types", "localized", NULL));
2545 g_assert_true (strv_set_equal ((const gchar * const *) keys, "greeting", "farewell", NULL));
2547 g_strfreev (children);
2550 g_settings_schema_unref (schema);
2551 g_object_unref (settings);
2555 test_list_schemas (void)
2557 const gchar * const *schemas;
2558 const gchar * const *relocs;
2560 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2561 relocs = g_settings_list_relocatable_schemas ();
2562 schemas = g_settings_list_schemas ();
2563 G_GNUC_END_IGNORE_DEPRECATIONS
2565 g_assert_true (strv_set_equal (relocs,
2566 "org.gtk.test.no-path",
2567 "org.gtk.test.extends.base",
2568 "org.gtk.test.extends.extended",
2571 g_assert_true (strv_set_equal (schemas,
2573 "org.gtk.test.basic-types",
2574 "org.gtk.test.complex-types",
2575 "org.gtk.test.localized",
2576 "org.gtk.test.binding",
2577 "org.gtk.test.enums",
2578 "org.gtk.test.enums.direct",
2579 "org.gtk.test.range",
2580 "org.gtk.test.range.direct",
2581 "org.gtk.test.mapped",
2582 "org.gtk.test.descriptions",
2583 "org.gtk.test.per-desktop",
2588 map_func (GVariant *value,
2592 gint *state = user_data;
2596 v = g_variant_get_int32 (value);
2602 g_assert_cmpint (v, ==, 1);
2606 else if (*state == 1)
2608 g_assert_cmpint (v, ==, 0);
2614 g_assert_null (value);
2615 *result = g_variant_new_int32 (5);
2621 test_get_mapped (void)
2623 GSettings *settings;
2628 settings = g_settings_new ("org.gtk.test.mapped");
2629 g_settings_set_int (settings, "val", 1);
2632 p = g_settings_get_mapped (settings, "val", map_func, &state);
2633 val = g_variant_get_int32 ((GVariant*)p);
2634 g_assert_cmpint (val, ==, 5);
2636 g_variant_unref (p);
2637 g_object_unref (settings);
2641 test_get_range (void)
2643 GSettings *settings;
2646 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2647 settings = g_settings_new ("org.gtk.test.range");
2648 range = g_settings_get_range (settings, "val");
2649 check_and_free (range, "('range', <(2, 44)>)");
2650 g_object_unref (settings);
2652 settings = g_settings_new ("org.gtk.test.enums");
2653 range = g_settings_get_range (settings, "test");
2654 check_and_free (range, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
2655 g_object_unref (settings);
2657 settings = g_settings_new ("org.gtk.test.enums");
2658 range = g_settings_get_range (settings, "f-test");
2659 check_and_free (range, "('flags', "
2660 "<['mourning', 'laughing', 'talking', 'walking']>)");
2661 g_object_unref (settings);
2663 settings = g_settings_new ("org.gtk.test");
2664 range = g_settings_get_range (settings, "greeting");
2665 check_and_free (range, "('type', <@as []>)");
2666 g_object_unref (settings);
2667 G_GNUC_END_IGNORE_DEPRECATIONS
2671 test_schema_source (void)
2673 GSettingsSchemaSource *parent;
2674 GSettingsSchemaSource *source;
2675 GSettingsBackend *backend;
2676 GSettingsSchema *schema;
2677 GError *error = NULL;
2678 GSettings *settings, *child;
2681 backend = g_settings_backend_get_default ();
2683 /* make sure it fails properly */
2684 parent = g_settings_schema_source_get_default ();
2685 source = g_settings_schema_source_new_from_directory ("/path/that/does/not/exist", parent, TRUE, &error);
2686 g_assert_null (source);
2687 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
2688 g_clear_error (&error);
2690 /* Test error handling of corrupt compiled files. */
2691 source = g_settings_schema_source_new_from_directory ("schema-source-corrupt", parent, TRUE, &error);
2692 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
2693 g_assert_null (source);
2694 g_clear_error (&error);
2696 /* Test error handling of empty compiled files. */
2697 source = g_settings_schema_source_new_from_directory ("schema-source-empty", parent, TRUE, &error);
2698 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
2699 g_assert_null (source);
2700 g_clear_error (&error);
2702 /* create a source with the parent */
2703 source = g_settings_schema_source_new_from_directory ("schema-source", parent, TRUE, &error);
2704 g_assert_no_error (error);
2705 g_assert_nonnull (source);
2707 /* check recursive lookups are working */
2708 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2709 g_assert_nonnull (schema);
2710 g_settings_schema_unref (schema);
2712 /* check recursive lookups for non-existent schemas */
2713 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", TRUE);
2714 g_assert_null (schema);
2716 /* check non-recursive for schema that only exists in lower layers */
2717 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2718 g_assert_null (schema);
2720 /* check non-recursive lookup for non-existent */
2721 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", FALSE);
2722 g_assert_null (schema);
2724 /* check non-recursive for schema that exists in toplevel */
2725 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2726 g_assert_nonnull (schema);
2727 g_settings_schema_unref (schema);
2729 /* check recursive for schema that exists in toplevel */
2730 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2731 g_assert_nonnull (schema);
2733 /* try to use it for something */
2734 settings = g_settings_new_full (schema, backend, "/test/");
2735 g_settings_schema_unref (schema);
2737 g_settings_get (settings, "enabled", "b", &enabled);
2738 g_assert_true (enabled);
2740 /* Check that child schemas are resolved from the correct schema source, see glib#1884 */
2741 child = g_settings_get_child (settings, "child");
2742 g_settings_get (settings, "enabled", "b", &enabled);
2744 g_object_unref (child);
2745 g_object_unref (settings);
2746 g_settings_schema_source_unref (source);
2748 /* try again, but with no parent */
2749 source = g_settings_schema_source_new_from_directory ("schema-source", NULL, FALSE, NULL);
2750 g_assert_nonnull (source);
2752 /* should not find it this time, even if recursive... */
2753 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2754 g_assert_null (schema);
2755 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2756 g_assert_null (schema);
2758 /* should still find our own... */
2759 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2760 g_assert_nonnull (schema);
2761 g_settings_schema_unref (schema);
2762 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2763 g_assert_nonnull (schema);
2764 g_settings_schema_unref (schema);
2766 g_settings_schema_source_unref (source);
2767 g_object_unref (backend);
2771 test_schema_list_keys (void)
2774 GSettingsSchemaSource *src = g_settings_schema_source_get_default ();
2775 GSettingsSchema *schema = g_settings_schema_source_lookup (src, "org.gtk.test", TRUE);
2776 g_assert_nonnull (schema);
2778 keys = g_settings_schema_list_keys (schema);
2780 g_assert_true (strv_set_equal ((const gchar * const *) keys,
2786 g_settings_schema_unref (schema);
2792 GAction *string, *toggle;
2793 gboolean c1, c2, c3;
2794 GSettings *settings;
2796 GVariantType *param_type;
2798 GVariantType *state_type;
2801 settings = g_settings_new ("org.gtk.test.basic-types");
2802 string = g_settings_create_action (settings, "test-string");
2803 toggle = g_settings_create_action (settings, "test-boolean");
2804 g_object_unref (settings); /* should be held by the actions */
2806 g_signal_connect (settings, "changed", G_CALLBACK (changed_cb2), &c1);
2807 g_signal_connect (string, "notify::state", G_CALLBACK (changed_cb2), &c2);
2808 g_signal_connect (toggle, "notify::state", G_CALLBACK (changed_cb2), &c3);
2810 c1 = c2 = c3 = FALSE;
2811 g_settings_set_string (settings, "test-string", "hello world");
2812 check_and_free (g_action_get_state (string), "'hello world'");
2813 g_assert_true (c1 && c2 && !c3);
2814 c1 = c2 = c3 = FALSE;
2816 g_action_activate (string, g_variant_new_string ("hihi"));
2817 check_and_free (g_settings_get_value (settings, "test-string"), "'hihi'");
2818 g_assert_true (c1 && c2 && !c3);
2819 c1 = c2 = c3 = FALSE;
2821 g_action_change_state (string, g_variant_new_string ("kthxbye"));
2822 check_and_free (g_settings_get_value (settings, "test-string"), "'kthxbye'");
2823 g_assert_true (c1 && c2 && !c3);
2824 c1 = c2 = c3 = FALSE;
2826 g_action_change_state (toggle, g_variant_new_boolean (TRUE));
2827 g_assert_true (g_settings_get_boolean (settings, "test-boolean"));
2828 g_assert_true (c1 && !c2 && c3);
2829 c1 = c2 = c3 = FALSE;
2831 g_action_activate (toggle, NULL);
2832 g_assert_false (g_settings_get_boolean (settings, "test-boolean"));
2833 g_assert_true (c1 && !c2 && c3);
2835 g_object_get (string,
2837 "parameter-type", ¶m_type,
2838 "enabled", &enabled,
2839 "state-type", &state_type,
2843 g_assert_cmpstr (name, ==, "test-string");
2844 g_assert_true (g_variant_type_equal (param_type, G_VARIANT_TYPE_STRING));
2845 g_assert_true (enabled);
2846 g_assert_true (g_variant_type_equal (state_type, G_VARIANT_TYPE_STRING));
2847 g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "kthxbye");
2850 g_variant_type_free (param_type);
2851 g_variant_type_free (state_type);
2852 g_variant_unref (state);
2854 g_object_unref (string);
2855 g_object_unref (toggle);
2859 test_null_backend (void)
2861 GSettingsBackend *backend;
2862 GSettings *settings;
2866 backend = g_null_settings_backend_new ();
2867 settings = g_settings_new_with_backend_and_path ("org.gtk.test", backend, "/tests/");
2869 g_object_get (settings, "schema-id", &str, NULL);
2870 g_assert_cmpstr (str, ==, "org.gtk.test");
2873 settings_assert_cmpstr (settings, "greeting", ==, "Hello, earthlings");
2875 g_settings_set (settings, "greeting", "s", "goodbye world");
2876 settings_assert_cmpstr (settings, "greeting", ==, "Hello, earthlings");
2878 writable = g_settings_is_writable (settings, "greeting");
2879 g_assert_false (writable);
2881 g_settings_reset (settings, "greeting");
2883 g_settings_delay (settings);
2884 g_settings_set (settings, "greeting", "s", "goodbye world");
2885 g_settings_apply (settings);
2886 settings_assert_cmpstr (settings, "greeting", ==, "Hello, earthlings");
2888 g_object_unref (settings);
2889 g_object_unref (backend);
2893 test_memory_backend (void)
2895 GSettingsBackend *backend;
2897 backend = g_memory_settings_backend_new ();
2898 g_assert_true (G_IS_SETTINGS_BACKEND (backend));
2899 g_object_unref (backend);
2903 test_read_descriptions (void)
2905 GSettingsSchema *schema;
2906 GSettingsSchemaKey *key;
2907 GSettings *settings;
2909 settings = g_settings_new ("org.gtk.test");
2910 g_object_get (settings, "settings-schema", &schema, NULL);
2911 key = g_settings_schema_get_key (schema, "greeting");
2913 g_assert_cmpstr (g_settings_schema_key_get_summary (key), ==, "A greeting");
2914 g_assert_cmpstr (g_settings_schema_key_get_description (key), ==, "Greeting of the invading martians");
2916 g_settings_schema_key_unref (key);
2917 g_settings_schema_unref (schema);
2919 g_object_unref (settings);
2921 settings = g_settings_new ("org.gtk.test.descriptions");
2922 g_object_get (settings, "settings-schema", &schema, NULL);
2923 key = g_settings_schema_get_key (schema, "a");
2925 g_assert_cmpstr (g_settings_schema_key_get_summary (key), ==,
2927 "with some whitespace.\n\n"
2928 "because not everyone has a great editor.\n\n"
2929 "lots of space is as one.");
2931 g_settings_schema_key_unref (key);
2932 g_settings_schema_unref (schema);
2934 g_object_unref (settings);
2938 test_default_value (void)
2940 GSettings *settings;
2941 GSettingsSchema *schema;
2942 GSettingsSchemaKey *key;
2947 settings = g_settings_new ("org.gtk.test");
2948 g_object_get (settings, "settings-schema", &schema, NULL);
2949 key = g_settings_schema_get_key (schema, "greeting");
2950 g_settings_schema_unref (schema);
2951 g_settings_schema_key_ref (key);
2953 g_assert_true (g_variant_type_equal (g_settings_schema_key_get_value_type (key), G_VARIANT_TYPE_STRING));
2955 v = g_settings_schema_key_get_default_value (key);
2956 str = g_variant_get_string (v, NULL);
2957 g_assert_cmpstr (str, ==, "Hello, earthlings");
2958 g_variant_unref (v);
2960 g_settings_schema_key_unref (key);
2961 g_settings_schema_key_unref (key);
2963 g_settings_set (settings, "greeting", "s", "goodbye world");
2965 v = g_settings_get_user_value (settings, "greeting");
2966 str = g_variant_get_string (v, NULL);
2967 g_assert_cmpstr (str, ==, "goodbye world");
2968 g_variant_unref (v);
2970 v = g_settings_get_default_value (settings, "greeting");
2971 str = g_variant_get_string (v, NULL);
2972 g_assert_cmpstr (str, ==, "Hello, earthlings");
2973 g_variant_unref (v);
2975 g_settings_reset (settings, "greeting");
2977 v = g_settings_get_user_value (settings, "greeting");
2980 s = g_settings_get_string (settings, "greeting");
2981 g_assert_cmpstr (s, ==, "Hello, earthlings");
2984 g_object_unref (settings);
2988 string_map_func (GVariant *value,
2994 str = g_variant_get_string (value, NULL);
2995 *result = g_variant_new_string (str);
3000 /* Test that per-desktop values from org.gtk.test.gschema.override
3001 * does not change default value if current desktop is not listed in
3002 * $XDG_CURRENT_DESKTOP.
3005 test_per_desktop (void)
3007 GSettings *settings;
3008 GAction *action_string;
3013 settings = g_settings_new ("org.gtk.test.per-desktop");
3014 obj = test_object_new ();
3016 if (!g_test_subprocess ())
3018 g_test_trap_subprocess ("/gsettings/per-desktop/subprocess", 0,
3019 G_TEST_SUBPROCESS_DEFAULT);
3020 g_test_trap_assert_passed ();
3023 str = g_settings_get_string (settings, "desktop");
3024 g_assert_cmpstr (str, ==, "GNOME");
3027 p = g_settings_get_mapped (settings, "desktop", string_map_func, NULL);
3029 str = g_variant_dup_string (p, NULL);
3030 g_assert_cmpstr (str, ==, "GNOME");
3033 g_variant_unref (p);
3035 g_settings_bind (settings, "desktop", obj, "string", G_SETTINGS_BIND_DEFAULT);
3037 g_object_get (obj, "string", &str, NULL);
3038 g_assert_cmpstr (str, ==, "GNOME");
3041 action_string = g_settings_create_action (settings, "desktop");
3042 check_and_free (g_action_get_state (action_string), "'GNOME'");
3044 g_clear_object (&action_string);
3045 g_object_unref (settings);
3046 g_object_unref (obj);
3049 /* Test that per-desktop values from org.gtk.test.gschema.override
3050 * are successfully loaded based on the value of $XDG_CURRENT_DESKTOP.
3053 test_per_desktop_subprocess (void)
3055 GSettings *settings;
3056 GAction *action_string;
3061 g_setenv ("XDG_CURRENT_DESKTOP", "GNOME-Classic:GNOME", TRUE);
3063 settings = g_settings_new ("org.gtk.test.per-desktop");
3064 obj = test_object_new ();
3066 str = g_settings_get_string (settings, "desktop");
3067 g_assert_cmpstr (str, ==, "GNOME Classic");
3070 p = g_settings_get_mapped (settings, "desktop", string_map_func, NULL);
3072 str = g_variant_dup_string (p, NULL);
3073 g_assert_cmpstr (str, ==, "GNOME Classic");
3076 g_variant_unref (p);
3078 g_settings_bind (settings, "desktop", obj, "string", G_SETTINGS_BIND_DEFAULT);
3080 g_object_get (obj, "string", &str, NULL);
3081 g_assert_cmpstr (str, ==, "GNOME Classic");
3084 action_string = g_settings_create_action (settings, "desktop");
3085 check_and_free (g_action_get_state (action_string), "'GNOME Classic'");
3087 g_clear_object (&action_string);
3088 g_object_unref (settings);
3089 g_object_unref (obj);
3093 test_extended_schema (void)
3095 GSettingsSchema *schema;
3096 GSettings *settings;
3099 settings = g_settings_new_with_path ("org.gtk.test.extends.extended", "/test/extendes/");
3100 g_object_get (settings, "settings-schema", &schema, NULL);
3101 keys = g_settings_schema_list_keys (schema);
3102 g_assert_true (strv_set_equal ((const gchar * const *) keys, "int32", "string", "another-int32", NULL));
3104 g_object_unref (settings);
3105 g_settings_schema_unref (schema);
3109 main (int argc, char *argv[])
3112 gchar *override_text;
3115 const KeyfileTestData keyfile_test_data_explicit_path = { "/tests/", "root", "tests", "/" };
3116 const KeyfileTestData keyfile_test_data_empty_path = { "/", "root", "root", "/" };
3117 const KeyfileTestData keyfile_test_data_long_path = {
3118 "/tests/path/is/very/long/and/this/makes/some/comparisons/take/a/different/branch/",
3120 "tests/path/is/very/long/and/this/makes/some/comparisons/take/a/different/branch",
3124 /* Meson build sets this */
3125 #ifdef TEST_LOCALE_PATH
3126 if (g_str_has_suffix (TEST_LOCALE_PATH, "LC_MESSAGES"))
3128 locale_dir = TEST_LOCALE_PATH G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S "..";
3132 setlocale (LC_ALL, "");
3134 g_test_init (&argc, &argv, NULL);
3136 if (!g_test_subprocess ())
3138 GError *local_error = NULL;
3139 char *subprocess_stdout = NULL;
3141 /* A GVDB header is 6 guint32s, and requires a magic number in the first
3142 * two guint32s. A set of zero bytes of a greater length is considered
3144 const guint8 gschemas_compiled_corrupt[sizeof (guint32) * 7] = { 0, };
3146 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
3148 g_setenv ("XDG_DATA_DIRS", ".", TRUE);
3149 g_setenv ("XDG_DATA_HOME", ".", TRUE);
3150 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
3151 g_setenv ("XDG_CURRENT_DESKTOP", "", TRUE);
3154 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
3156 g_remove ("org.gtk.test.enums.xml");
3157 /* #GLIB_MKENUMS is defined in meson.build */
3158 g_assert_true (g_spawn_command_line_sync (GLIB_MKENUMS " "
3159 "--template " SRCDIR "/enums.xml.template "
3160 SRCDIR "/testenum.h",
3161 &enums, NULL, &result, NULL));
3162 g_assert_cmpint (result, ==, 0);
3163 g_assert_true (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
3166 g_assert_true (g_file_get_contents (SRCDIR "/org.gtk.test.gschema.xml.orig", &schema_text, NULL, NULL));
3167 g_assert_true (g_file_set_contents ("org.gtk.test.gschema.xml", schema_text, -1, NULL));
3168 g_free (schema_text);
3170 g_assert_true (g_file_get_contents (SRCDIR "/org.gtk.test.gschema.override.orig", &override_text, NULL, NULL));
3171 g_assert_true (g_file_set_contents ("org.gtk.test.gschema.override", override_text, -1, NULL));
3172 g_free (override_text);
3174 g_remove ("gschemas.compiled");
3175 /* #GLIB_COMPILE_SCHEMAS is defined in meson.build */
3176 g_assert_true (g_spawn_command_line_sync (GLIB_COMPILE_SCHEMAS " --targetdir=. "
3177 "--schema-file=org.gtk.test.enums.xml "
3178 "--schema-file=org.gtk.test.gschema.xml "
3179 "--override-file=org.gtk.test.gschema.override",
3180 &subprocess_stdout, NULL, &result, NULL));
3181 if (subprocess_stdout && *g_strstrip (subprocess_stdout) != '\0')
3182 g_test_message ("%s", subprocess_stdout);
3183 g_clear_pointer (&subprocess_stdout, g_free);
3184 g_assert_cmpint (result, ==, 0);
3186 g_remove ("schema-source/gschemas.compiled");
3187 g_mkdir ("schema-source", 0777);
3188 g_assert_true (g_spawn_command_line_sync (GLIB_COMPILE_SCHEMAS " --targetdir=schema-source "
3189 "--schema-file=" SRCDIR "/org.gtk.schemasourcecheck.gschema.xml",
3190 &subprocess_stdout, NULL, &result, NULL));
3191 if (subprocess_stdout && *g_strstrip (subprocess_stdout) != '\0')
3192 g_test_message ("%s", subprocess_stdout);
3193 g_clear_pointer (&subprocess_stdout, g_free);
3194 g_assert_cmpint (result, ==, 0);
3196 g_remove ("schema-source-corrupt/gschemas.compiled");
3197 g_mkdir ("schema-source-corrupt", 0777);
3198 g_file_set_contents ("schema-source-corrupt/gschemas.compiled",
3199 (const gchar *) gschemas_compiled_corrupt,
3200 sizeof (gschemas_compiled_corrupt),
3202 g_assert_no_error (local_error);
3204 g_remove ("schema-source-empty/gschemas.compiled");
3205 g_mkdir ("schema-source-empty", 0777);
3206 g_file_set_contents ("schema-source-empty/gschemas.compiled",
3209 g_assert_no_error (local_error);
3212 g_test_add_func ("/gsettings/basic", test_basic);
3216 g_test_add_func ("/gsettings/no-schema", test_no_schema);
3217 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
3218 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
3219 g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
3220 g_test_add_func ("/gsettings/no-path", test_no_path);
3223 g_test_add_func ("/gsettings/basic-types", test_basic_types);
3224 g_test_add_func ("/gsettings/complex-types", test_complex_types);
3225 g_test_add_func ("/gsettings/changes", test_changes);
3227 g_test_add_func ("/gsettings/l10n", test_l10n);
3228 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
3229 g_test_add_func ("/gsettings/l10n-time", test_l10n_time);
3231 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
3232 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
3233 g_test_add_func ("/gsettings/delay-child", test_delay_child);
3234 g_test_add_func ("/gsettings/delay-reset-key", test_delay_reset_key);
3235 g_test_add_func ("/gsettings/atomic", test_atomic);
3237 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
3238 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
3239 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
3240 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
3241 g_test_add_func ("/gsettings/unbinding", test_unbind);
3242 g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
3246 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
3247 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
3248 g_test_add_func ("/gsettings/no-read-binding/subprocess/fail", test_no_read_binding_fail);
3249 g_test_add_func ("/gsettings/no-read-binding/subprocess/pass", test_no_read_binding_pass);
3250 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
3251 g_test_add_func ("/gsettings/no-write-binding/subprocess/fail", test_no_write_binding_fail);
3252 g_test_add_func ("/gsettings/no-write-binding/subprocess/pass", test_no_write_binding_pass);
3255 g_test_add ("/gsettings/keyfile", Fixture, NULL, setup, test_keyfile, teardown);
3256 g_test_add ("/gsettings/keyfile/explicit-path", Fixture, &keyfile_test_data_explicit_path, setup, test_keyfile_no_path, teardown);
3257 g_test_add ("/gsettings/keyfile/empty-path", Fixture, &keyfile_test_data_empty_path, setup, test_keyfile_no_path, teardown);
3258 g_test_add ("/gsettings/keyfile/long-path", Fixture, &keyfile_test_data_long_path, setup, test_keyfile_no_path, teardown);
3259 g_test_add ("/gsettings/keyfile/outside-root-path", Fixture, NULL, setup, test_keyfile_outside_root_path, teardown);
3260 g_test_add ("/gsettings/keyfile/no-root-group", Fixture, NULL, setup, test_keyfile_no_root_group, teardown);
3261 g_test_add_func ("/gsettings/child-schema", test_child_schema);
3262 g_test_add_func ("/gsettings/strinfo", test_strinfo);
3263 g_test_add_func ("/gsettings/enums", test_enums);
3264 g_test_add_func ("/gsettings/enums/subprocess/non-enum-key", test_enums_non_enum_key);
3265 g_test_add_func ("/gsettings/enums/subprocess/non-enum-value", test_enums_non_enum_value);
3266 g_test_add_func ("/gsettings/enums/subprocess/range", test_enums_range);
3267 g_test_add_func ("/gsettings/enums/subprocess/non-flags", test_enums_non_flags);
3268 g_test_add_func ("/gsettings/flags", test_flags);
3269 g_test_add_func ("/gsettings/flags/subprocess/non-flags-key", test_flags_non_flags_key);
3270 g_test_add_func ("/gsettings/flags/subprocess/non-flags-value", test_flags_non_flags_value);
3271 g_test_add_func ("/gsettings/flags/subprocess/range", test_flags_range);
3272 g_test_add_func ("/gsettings/flags/subprocess/non-enum", test_flags_non_enum);
3273 g_test_add_func ("/gsettings/range", test_range);
3274 g_test_add_func ("/gsettings/range/subprocess/high", test_range_high);
3275 g_test_add_func ("/gsettings/range/subprocess/low", test_range_low);
3276 g_test_add_func ("/gsettings/list-items", test_list_items);
3277 g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
3278 g_test_add_func ("/gsettings/mapped", test_get_mapped);
3279 g_test_add_func ("/gsettings/get-range", test_get_range);
3280 g_test_add_func ("/gsettings/schema-source", test_schema_source);
3281 g_test_add_func ("/gsettings/schema-list-keys", test_schema_list_keys);
3282 g_test_add_func ("/gsettings/actions", test_actions);
3283 g_test_add_func ("/gsettings/null-backend", test_null_backend);
3284 g_test_add_func ("/gsettings/memory-backend", test_memory_backend);
3285 g_test_add_func ("/gsettings/read-descriptions", test_read_descriptions);
3286 g_test_add_func ("/gsettings/test-extended-schema", test_extended_schema);
3287 g_test_add_func ("/gsettings/default-value", test_default_value);
3288 g_test_add_func ("/gsettings/per-desktop", test_per_desktop);
3289 g_test_add_func ("/gsettings/per-desktop/subprocess", test_per_desktop_subprocess);
3291 result = g_test_run ();
3295 /* FIXME: Due to the way #GSettings objects can be used without specifying a
3296 * backend, the default backend is leaked. In order to be able to run this
3297 * test under valgrind and get meaningful checking for real leaks, use this
3298 * hack to drop the final reference to the default #GSettingsBackend.
3300 * This should not be used in production code. */
3302 GSettingsBackend *backend;
3304 backend = g_settings_backend_get_default ();
3305 g_object_unref (backend); /* reference from the *_get_default() call */
3306 g_assert_finalize_object (backend); /* singleton reference owned by GLib */