6 #define G_SETTINGS_ENABLE_BACKEND
7 #include <gio/gsettingsbackend.h>
11 static gboolean backend_set
;
13 /* These tests rely on the schemas in org.gtk.test.gschema.xml
14 * to be compiled and installed in the same directory.
18 check_and_free (GVariant
*value
,
19 const gchar
*expected
)
23 printed
= g_variant_print (value
, TRUE
);
24 g_assert_cmpstr (printed
, ==, expected
);
27 g_variant_unref (value
);
31 /* Just to get warmed up: Read and set a string, and
32 * verify that can read the changed string back
40 settings
= g_settings_new ("org.gtk.test");
42 g_object_get (settings
, "schema", &str
, NULL
);
43 g_assert_cmpstr (str
, ==, "org.gtk.test");
46 g_settings_get (settings
, "greeting", "s", &str
);
47 g_assert_cmpstr (str
, ==, "Hello, earthlings");
50 g_settings_set (settings
, "greeting", "s", "goodbye world");
51 g_settings_get (settings
, "greeting", "s", &str
);
52 g_assert_cmpstr (str
, ==, "goodbye world");
56 if (!backend_set
&& g_test_undefined ())
58 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
60 settings
= g_settings_new ("org.gtk.test");
61 g_settings_set (settings
, "greeting", "i", 555);
64 g_test_trap_assert_failed ();
65 g_test_trap_assert_stderr ("*g_settings_set_value*expects type*");
68 g_settings_get (settings
, "greeting", "s", &str
);
69 g_assert_cmpstr (str
, ==, "goodbye world");
73 g_settings_reset (settings
, "greeting");
74 str
= g_settings_get_string (settings
, "greeting");
75 g_assert_cmpstr (str
, ==, "Hello, earthlings");
78 g_settings_set (settings
, "greeting", "s", "this is the end");
79 g_object_unref (settings
);
82 /* Check that we get an error when getting a key
83 * that is not in the schema
86 test_unknown_key (void)
88 if (!g_test_undefined ())
91 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
96 settings
= g_settings_new ("org.gtk.test");
97 value
= g_settings_get_value (settings
, "no_such_key");
99 g_assert (value
== NULL
);
101 g_object_unref (settings
);
103 g_test_trap_assert_failed ();
104 g_test_trap_assert_stderr ("*does not contain*");
107 /* Check that we get an error when the schema
108 * has not been installed
111 test_no_schema (void)
113 if (!g_test_undefined ())
116 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
120 settings
= g_settings_new ("no.such.schema");
122 g_assert (settings
== NULL
);
125 g_test_trap_assert_failed ();
126 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
129 /* Check that we get an error when passing a type string
130 * that does not match the schema
133 test_wrong_type (void)
135 if (!g_test_undefined ())
138 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
143 settings
= g_settings_new ("org.gtk.test");
145 g_settings_get (settings
, "greeting", "o", &str
);
147 g_assert (str
== NULL
);
149 g_test_trap_assert_failed ();
150 g_test_trap_assert_stderr ("*CRITICAL*");
152 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
156 settings
= g_settings_new ("org.gtk.test");
158 g_settings_set (settings
, "greeting", "o", "/a/path");
160 g_test_trap_assert_failed ();
161 g_test_trap_assert_stderr ("*CRITICAL*");
164 /* Check errors with explicit paths */
166 test_wrong_path (void)
168 if (!g_test_undefined ())
171 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
173 GSettings
*settings G_GNUC_UNUSED
;
175 settings
= g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
178 g_test_trap_assert_failed ();
179 g_test_trap_assert_stderr ("*but path * specified by schema*");
185 if (!g_test_undefined ())
188 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
190 GSettings
*settings G_GNUC_UNUSED
;
192 settings
= g_settings_new ("org.gtk.test.no-path");
195 g_test_trap_assert_failed ();
196 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
200 /* Check that we can successfully read and set the full
201 * range of all basic types
204 test_basic_types (void)
218 settings
= g_settings_new ("org.gtk.test.basic-types");
220 g_settings_get (settings
, "test-boolean", "b", &b
);
221 g_assert_cmpint (b
, ==, 1);
223 g_settings_set (settings
, "test-boolean", "b", 0);
224 g_settings_get (settings
, "test-boolean", "b", &b
);
225 g_assert_cmpint (b
, ==, 0);
227 g_settings_get (settings
, "test-byte", "y", &byte
);
228 g_assert_cmpint (byte
, ==, 25);
230 g_settings_set (settings
, "test-byte", "y", G_MAXUINT8
);
231 g_settings_get (settings
, "test-byte", "y", &byte
);
232 g_assert_cmpint (byte
, ==, G_MAXUINT8
);
234 g_settings_get (settings
, "test-int16", "n", &i16
);
235 g_assert_cmpint (i16
, ==, -1234);
237 g_settings_set (settings
, "test-int16", "n", G_MININT16
);
238 g_settings_get (settings
, "test-int16", "n", &i16
);
239 g_assert_cmpint (i16
, ==, G_MININT16
);
241 g_settings_set (settings
, "test-int16", "n", G_MAXINT16
);
242 g_settings_get (settings
, "test-int16", "n", &i16
);
243 g_assert_cmpint (i16
, ==, G_MAXINT16
);
245 g_settings_get (settings
, "test-uint16", "q", &u16
);
246 g_assert_cmpuint (u16
, ==, 1234);
248 g_settings_set (settings
, "test-uint16", "q", G_MAXUINT16
);
249 g_settings_get (settings
, "test-uint16", "q", &u16
);
250 g_assert_cmpuint (u16
, ==, G_MAXUINT16
);
252 g_settings_get (settings
, "test-int32", "i", &i32
);
253 g_assert_cmpint (i32
, ==, -123456);
255 g_settings_set (settings
, "test-int32", "i", G_MININT32
);
256 g_settings_get (settings
, "test-int32", "i", &i32
);
257 g_assert_cmpint (i32
, ==, G_MININT32
);
259 g_settings_set (settings
, "test-int32", "i", G_MAXINT32
);
260 g_settings_get (settings
, "test-int32", "i", &i32
);
261 g_assert_cmpint (i32
, ==, G_MAXINT32
);
263 g_settings_get (settings
, "test-uint32", "u", &u32
);
264 g_assert_cmpuint (u32
, ==, 123456);
266 g_settings_set (settings
, "test-uint32", "u", G_MAXUINT32
);
267 g_settings_get (settings
, "test-uint32", "u", &u32
);
268 g_assert_cmpuint (u32
, ==, G_MAXUINT32
);
270 g_settings_get (settings
, "test-int64", "x", &i64
);
271 g_assert_cmpuint (i64
, ==, -123456789);
273 g_settings_set (settings
, "test-int64", "x", G_MININT64
);
274 g_settings_get (settings
, "test-int64", "x", &i64
);
275 g_assert_cmpuint (i64
, ==, G_MININT64
);
277 g_settings_set (settings
, "test-int64", "x", G_MAXINT64
);
278 g_settings_get (settings
, "test-int64", "x", &i64
);
279 g_assert_cmpuint (i64
, ==, G_MAXINT64
);
281 g_settings_get (settings
, "test-uint64", "t", &u64
);
282 g_assert_cmpuint (u64
, ==, 123456789);
284 g_settings_set (settings
, "test-uint64", "t", G_MAXUINT64
);
285 g_settings_get (settings
, "test-uint64", "t", &u64
);
286 g_assert_cmpuint (u64
, ==, G_MAXUINT64
);
288 g_settings_get (settings
, "test-double", "d", &d
);
289 g_assert_cmpfloat (d
, ==, 123.456);
291 g_settings_set (settings
, "test-double", "d", G_MINDOUBLE
);
292 g_settings_get (settings
, "test-double", "d", &d
);
293 g_assert_cmpfloat (d
, ==, G_MINDOUBLE
);
295 g_settings_set (settings
, "test-double", "d", G_MAXDOUBLE
);
296 g_settings_get (settings
, "test-double", "d", &d
);
297 g_assert_cmpfloat (d
, ==, G_MAXDOUBLE
);
299 g_settings_get (settings
, "test-string", "s", &str
);
300 g_assert_cmpstr (str
, ==, "a string, it seems");
304 g_settings_get (settings
, "test-objectpath", "o", &str
);
305 g_assert_cmpstr (str
, ==, "/a/object/path");
306 g_object_unref (settings
);
311 /* Check that we can read an set complex types like
312 * tuples, arrays and dictionaries
315 test_complex_types (void)
320 GVariantIter
*iter
= NULL
;
322 settings
= g_settings_new ("org.gtk.test.complex-types");
324 g_settings_get (settings
, "test-tuple", "(s(ii))", &s
, &i1
, &i2
);
325 g_assert_cmpstr (s
, ==, "one");
326 g_assert_cmpint (i1
,==, 2);
327 g_assert_cmpint (i2
,==, 3);
331 g_settings_set (settings
, "test-tuple", "(s(ii))", "none", 0, 0);
332 g_settings_get (settings
, "test-tuple", "(s(ii))", &s
, &i1
, &i2
);
333 g_assert_cmpstr (s
, ==, "none");
334 g_assert_cmpint (i1
,==, 0);
335 g_assert_cmpint (i2
,==, 0);
339 g_settings_get (settings
, "test-array", "ai", &iter
);
340 g_assert_cmpint (g_variant_iter_n_children (iter
), ==, 6);
341 g_assert (g_variant_iter_next (iter
, "i", &i1
));
342 g_assert_cmpint (i1
, ==, 0);
343 g_assert (g_variant_iter_next (iter
, "i", &i1
));
344 g_assert_cmpint (i1
, ==, 1);
345 g_assert (g_variant_iter_next (iter
, "i", &i1
));
346 g_assert_cmpint (i1
, ==, 2);
347 g_assert (g_variant_iter_next (iter
, "i", &i1
));
348 g_assert_cmpint (i1
, ==, 3);
349 g_assert (g_variant_iter_next (iter
, "i", &i1
));
350 g_assert_cmpint (i1
, ==, 4);
351 g_assert (g_variant_iter_next (iter
, "i", &i1
));
352 g_assert_cmpint (i1
, ==, 5);
353 g_assert (!g_variant_iter_next (iter
, "i", &i1
));
354 g_variant_iter_free (iter
);
356 g_object_unref (settings
);
359 static gboolean changed_cb_called
;
362 changed_cb (GSettings
*settings
,
366 changed_cb_called
= TRUE
;
368 g_assert_cmpstr (key
, ==, data
);
371 /* Test that basic change notification with the changed signal works.
377 GSettings
*settings2
;
379 settings
= g_settings_new ("org.gtk.test");
381 g_signal_connect (settings
, "changed",
382 G_CALLBACK (changed_cb
), "greeting");
384 changed_cb_called
= FALSE
;
386 g_settings_set (settings
, "greeting", "s", "new greeting");
387 g_assert (changed_cb_called
);
389 settings2
= g_settings_new ("org.gtk.test");
391 changed_cb_called
= FALSE
;
393 g_settings_set (settings2
, "greeting", "s", "hi");
394 g_assert (changed_cb_called
);
396 g_object_unref (settings2
);
397 g_object_unref (settings
);
400 static gboolean changed_cb_called2
;
403 changed_cb2 (GSettings
*settings
,
412 /* Test that changes done to a delay-mode instance
413 * don't appear to the outside world until apply. Also
414 * check that we get change notification when they are
416 * Also test that the has-unapplied property is properly
420 test_delay_apply (void)
423 GSettings
*settings2
;
426 settings
= g_settings_new ("org.gtk.test");
427 settings2
= g_settings_new ("org.gtk.test");
429 g_settings_set (settings2
, "greeting", "s", "top o' the morning");
431 changed_cb_called
= FALSE
;
432 changed_cb_called2
= FALSE
;
434 g_signal_connect (settings
, "changed",
435 G_CALLBACK (changed_cb2
), &changed_cb_called
);
436 g_signal_connect (settings2
, "changed",
437 G_CALLBACK (changed_cb2
), &changed_cb_called2
);
439 g_settings_delay (settings
);
441 g_settings_set (settings
, "greeting", "s", "greetings from test_delay_apply");
443 g_assert (changed_cb_called
);
444 g_assert (!changed_cb_called2
);
446 g_settings_get (settings
, "greeting", "s", &str
);
447 g_assert_cmpstr (str
, ==, "greetings from test_delay_apply");
451 g_settings_get (settings2
, "greeting", "s", &str
);
452 g_assert_cmpstr (str
, ==, "top o' the morning");
456 g_assert (g_settings_get_has_unapplied (settings
));
457 g_assert (!g_settings_get_has_unapplied (settings2
));
459 changed_cb_called
= FALSE
;
460 changed_cb_called2
= FALSE
;
462 g_settings_apply (settings
);
464 g_assert (!changed_cb_called
);
465 g_assert (changed_cb_called2
);
467 g_settings_get (settings
, "greeting", "s", &str
);
468 g_assert_cmpstr (str
, ==, "greetings from test_delay_apply");
472 g_settings_get (settings2
, "greeting", "s", &str
);
473 g_assert_cmpstr (str
, ==, "greetings from test_delay_apply");
477 g_assert (!g_settings_get_has_unapplied (settings
));
478 g_assert (!g_settings_get_has_unapplied (settings2
));
480 g_object_unref (settings2
);
481 g_object_unref (settings
);
484 /* Test that reverting unapplied changes in a delay-apply
485 * settings instance works.
488 test_delay_revert (void)
491 GSettings
*settings2
;
494 settings
= g_settings_new ("org.gtk.test");
495 settings2
= g_settings_new ("org.gtk.test");
497 g_settings_set (settings2
, "greeting", "s", "top o' the morning");
499 g_settings_delay (settings
);
501 g_settings_set (settings
, "greeting", "s", "greetings from test_delay_revert");
503 g_settings_get (settings
, "greeting", "s", &str
);
504 g_assert_cmpstr (str
, ==, "greetings from test_delay_revert");
508 g_settings_get (settings2
, "greeting", "s", &str
);
509 g_assert_cmpstr (str
, ==, "top o' the morning");
513 g_assert (g_settings_get_has_unapplied (settings
));
515 g_settings_revert (settings
);
517 g_assert (!g_settings_get_has_unapplied (settings
));
519 g_settings_get (settings
, "greeting", "s", &str
);
520 g_assert_cmpstr (str
, ==, "top o' the morning");
524 g_settings_get (settings2
, "greeting", "s", &str
);
525 g_assert_cmpstr (str
, ==, "top o' the morning");
529 g_object_unref (settings2
);
530 g_object_unref (settings
);
534 keys_changed_cb (GSettings
*settings
,
540 g_assert_cmpint (n_keys
, ==, 2);
542 g_assert ((keys
[0] == g_quark_from_static_string ("greeting") &&
543 keys
[1] == g_quark_from_static_string ("farewell")) ||
544 (keys
[1] == g_quark_from_static_string ("greeting") &&
545 keys
[0] == g_quark_from_static_string ("farewell")));
547 g_settings_get (settings
, "greeting", "s", &str
);
548 g_assert_cmpstr (str
, ==, "greetings from test_atomic");
552 g_settings_get (settings
, "farewell", "s", &str
);
553 g_assert_cmpstr (str
, ==, "atomic bye-bye");
558 /* Check that delay-applied changes appear atomically.
559 * More specifically, verify that all changed keys appear
560 * with their new value while handling the change-event signal.
566 GSettings
*settings2
;
569 settings
= g_settings_new ("org.gtk.test");
570 settings2
= g_settings_new ("org.gtk.test");
572 g_settings_set (settings2
, "greeting", "s", "top o' the morning");
574 changed_cb_called
= FALSE
;
575 changed_cb_called2
= FALSE
;
577 g_signal_connect (settings2
, "change-event",
578 G_CALLBACK (keys_changed_cb
), NULL
);
580 g_settings_delay (settings
);
582 g_settings_set (settings
, "greeting", "s", "greetings from test_atomic");
583 g_settings_set (settings
, "farewell", "s", "atomic bye-bye");
585 g_settings_apply (settings
);
587 g_settings_get (settings
, "greeting", "s", &str
);
588 g_assert_cmpstr (str
, ==, "greetings from test_atomic");
592 g_settings_get (settings
, "farewell", "s", &str
);
593 g_assert_cmpstr (str
, ==, "atomic bye-bye");
597 g_settings_get (settings2
, "greeting", "s", &str
);
598 g_assert_cmpstr (str
, ==, "greetings from test_atomic");
602 g_settings_get (settings2
, "farewell", "s", &str
);
603 g_assert_cmpstr (str
, ==, "atomic bye-bye");
607 g_object_unref (settings2
);
608 g_object_unref (settings
);
611 /* On Windows the interaction between the C library locale and libintl
612 * (from GNU gettext) is not like on POSIX, so just skip these tests
615 * There are several issues:
617 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
618 * in libintl (defined in its <libintl.h>).
620 * 2) The locale names that setlocale() accepts and returns aren't in
621 * the "de_DE" style, but like "German_Germany".
623 * 3) libintl looks at the Win32 thread locale and not the C library
624 * locale. (And even if libintl would use the C library's locale, as
625 * there are several alternative C library DLLs, libintl might be
626 * linked to a different one than the application code, so they
627 * wouldn't have the same C library locale anyway.)
630 /* Test that translations work for schema defaults.
632 * This test relies on the de.po file in the same directory
633 * to be compiled into ./de/LC_MESSAGES/test.mo
642 bindtextdomain ("test", ".");
643 bind_textdomain_codeset ("test", "UTF-8");
645 locale
= g_strdup (setlocale (LC_MESSAGES
, NULL
));
647 settings
= g_settings_new ("org.gtk.test.localized");
649 setlocale (LC_MESSAGES
, "C");
650 str
= g_settings_get_string (settings
, "error-message");
651 setlocale (LC_MESSAGES
, locale
);
653 g_assert_cmpstr (str
, ==, "Unnamed");
657 setlocale (LC_MESSAGES
, "de_DE");
658 str
= g_settings_get_string (settings
, "error-message");
659 setlocale (LC_MESSAGES
, locale
);
661 g_assert_cmpstr (str
, ==, "Unbenannt");
662 g_object_unref (settings
);
669 /* Test that message context works as expected with translated
670 * schema defaults. Also, verify that non-ASCII UTF-8 content
673 * This test relies on the de.po file in the same directory
674 * to be compiled into ./de/LC_MESSAGES/test.mo
677 test_l10n_context (void)
683 bindtextdomain ("test", ".");
684 bind_textdomain_codeset ("test", "UTF-8");
686 locale
= g_strdup (setlocale (LC_MESSAGES
, NULL
));
688 settings
= g_settings_new ("org.gtk.test.localized");
690 setlocale (LC_MESSAGES
, "C");
691 g_settings_get (settings
, "backspace", "s", &str
);
692 setlocale (LC_MESSAGES
, locale
);
694 g_assert_cmpstr (str
, ==, "BackSpace");
698 setlocale (LC_MESSAGES
, "de_DE");
699 g_settings_get (settings
, "backspace", "s", &str
);
700 setlocale (LC_MESSAGES
, locale
);
702 g_assert_cmpstr (str
, ==, "Löschen");
703 g_object_unref (settings
);
732 GObject parent_instance
;
735 gboolean anti_bool_prop
;
746 gchar
*no_write_prop
;
753 GObjectClass parent_class
;
756 static GType
test_object_get_type (void);
757 G_DEFINE_TYPE (TestObject
, test_object
, G_TYPE_OBJECT
)
760 test_object_init (TestObject
*object
)
765 test_object_finalize (GObject
*object
)
767 TestObject
*testo
= (TestObject
*)object
;
768 g_strfreev (testo
->strv_prop
);
769 g_free (testo
->string_prop
);
770 G_OBJECT_CLASS (test_object_parent_class
)->finalize (object
);
774 test_object_get_property (GObject
*object
,
779 TestObject
*test_object
= (TestObject
*)object
;
784 g_value_set_boolean (value
, test_object
->bool_prop
);
787 g_value_set_boolean (value
, test_object
->anti_bool_prop
);
790 g_value_set_schar (value
, test_object
->byte_prop
);
793 g_value_set_uint (value
, test_object
->uint16_prop
);
796 g_value_set_int (value
, test_object
->int16_prop
);
799 g_value_set_int (value
, test_object
->int_prop
);
802 g_value_set_uint (value
, test_object
->uint_prop
);
805 g_value_set_int64 (value
, test_object
->int64_prop
);
808 g_value_set_uint64 (value
, test_object
->uint64_prop
);
811 g_value_set_double (value
, test_object
->double_prop
);
814 g_value_set_string (value
, test_object
->string_prop
);
817 g_value_set_string (value
, test_object
->no_write_prop
);
820 g_value_set_boxed (value
, test_object
->strv_prop
);
823 g_value_set_enum (value
, test_object
->enum_prop
);
826 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
832 test_object_set_property (GObject
*object
,
837 TestObject
*test_object
= (TestObject
*)object
;
842 test_object
->bool_prop
= g_value_get_boolean (value
);
845 test_object
->anti_bool_prop
= g_value_get_boolean (value
);
848 test_object
->byte_prop
= g_value_get_schar (value
);
851 test_object
->int16_prop
= g_value_get_int (value
);
854 test_object
->uint16_prop
= g_value_get_uint (value
);
857 test_object
->int_prop
= g_value_get_int (value
);
860 test_object
->uint_prop
= g_value_get_uint (value
);
863 test_object
->int64_prop
= g_value_get_int64 (value
);
866 test_object
->uint64_prop
= g_value_get_uint64 (value
);
869 test_object
->double_prop
= g_value_get_double (value
);
872 g_free (test_object
->string_prop
);
873 test_object
->string_prop
= g_value_dup_string (value
);
876 g_free (test_object
->no_read_prop
);
877 test_object
->no_read_prop
= g_value_dup_string (value
);
880 g_strfreev (test_object
->strv_prop
);
881 test_object
->strv_prop
= g_value_dup_boxed (value
);
884 test_object
->enum_prop
= g_value_get_enum (value
);
887 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
893 test_enum_get_type (void)
895 static volatile gsize define_type_id
= 0;
897 if (g_once_init_enter (&define_type_id
))
899 static const GEnumValue values
[] = {
900 { TEST_ENUM_FOO
, "TEST_ENUM_FOO", "foo" },
901 { TEST_ENUM_BAR
, "TEST_ENUM_BAR", "bar" },
902 { TEST_ENUM_BAZ
, "TEST_ENUM_BAZ", "baz" },
903 { TEST_ENUM_QUUX
, "TEST_ENUM_QUUX", "quux" },
907 GType type_id
= g_enum_register_static ("TestEnum", values
);
908 g_once_init_leave (&define_type_id
, type_id
);
911 return define_type_id
;
915 test_object_class_init (TestObjectClass
*class)
917 GObjectClass
*gobject_class
= G_OBJECT_CLASS (class);
919 gobject_class
->get_property
= test_object_get_property
;
920 gobject_class
->set_property
= test_object_set_property
;
921 gobject_class
->finalize
= test_object_finalize
;
923 g_object_class_install_property (gobject_class
, PROP_BOOL
,
924 g_param_spec_boolean ("bool", "", "", FALSE
, G_PARAM_READWRITE
));
925 g_object_class_install_property (gobject_class
, PROP_ANTI_BOOL
,
926 g_param_spec_boolean ("anti-bool", "", "", FALSE
, G_PARAM_READWRITE
));
927 g_object_class_install_property (gobject_class
, PROP_BYTE
,
928 g_param_spec_char ("byte", "", "", G_MININT8
, G_MAXINT8
, 0, G_PARAM_READWRITE
));
929 g_object_class_install_property (gobject_class
, PROP_INT16
,
930 g_param_spec_int ("int16", "", "", -G_MAXINT16
, G_MAXINT16
, 0, G_PARAM_READWRITE
));
931 g_object_class_install_property (gobject_class
, PROP_UINT16
,
932 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16
, 0, G_PARAM_READWRITE
));
933 g_object_class_install_property (gobject_class
, PROP_INT
,
934 g_param_spec_int ("int", "", "", G_MININT
, G_MAXINT
, 0, G_PARAM_READWRITE
));
935 g_object_class_install_property (gobject_class
, PROP_UINT
,
936 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT
, 0, G_PARAM_READWRITE
));
937 g_object_class_install_property (gobject_class
, PROP_INT64
,
938 g_param_spec_int64 ("int64", "", "", G_MININT64
, G_MAXINT64
, 0, G_PARAM_READWRITE
));
939 g_object_class_install_property (gobject_class
, PROP_UINT64
,
940 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64
, 0, G_PARAM_READWRITE
));
941 g_object_class_install_property (gobject_class
, PROP_DOUBLE
,
942 g_param_spec_double ("double", "", "", -G_MAXDOUBLE
, G_MAXDOUBLE
, 0.0, G_PARAM_READWRITE
));
943 g_object_class_install_property (gobject_class
, PROP_STRING
,
944 g_param_spec_string ("string", "", "", NULL
, G_PARAM_READWRITE
));
945 g_object_class_install_property (gobject_class
, PROP_NO_WRITE
,
946 g_param_spec_string ("no-write", "", "", NULL
, G_PARAM_READABLE
));
947 g_object_class_install_property (gobject_class
, PROP_NO_READ
,
948 g_param_spec_string ("no-read", "", "", NULL
, G_PARAM_WRITABLE
));
949 g_object_class_install_property (gobject_class
, PROP_STRV
,
950 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV
, G_PARAM_READWRITE
));
951 g_object_class_install_property (gobject_class
, PROP_ENUM
,
952 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO
, G_PARAM_READWRITE
));
956 test_object_new (void)
958 return (TestObject
*)g_object_new (test_object_get_type (), NULL
);
961 /* Test basic binding functionality for simple types.
962 * Verify that with bidirectional bindings, changes on either side
963 * are notified on the other end.
966 test_simple_binding (void)
984 settings
= g_settings_new ("org.gtk.test.binding");
985 obj
= test_object_new ();
987 g_settings_bind (settings
, "bool", obj
, "bool", G_SETTINGS_BIND_DEFAULT
);
988 g_object_set (obj
, "bool", TRUE
, NULL
);
989 g_assert_cmpint (g_settings_get_boolean (settings
, "bool"), ==, TRUE
);
991 g_settings_set_boolean (settings
, "bool", FALSE
);
993 g_object_get (obj
, "bool", &b
, NULL
);
994 g_assert_cmpint (b
, ==, FALSE
);
996 g_settings_bind (settings
, "anti-bool", obj
, "anti-bool",
997 G_SETTINGS_BIND_INVERT_BOOLEAN
);
998 g_object_set (obj
, "anti-bool", FALSE
, NULL
);
999 g_assert_cmpint (g_settings_get_boolean (settings
, "anti-bool"), ==, TRUE
);
1001 g_settings_set_boolean (settings
, "anti-bool", FALSE
);
1003 g_object_get (obj
, "anti-bool", &b
, NULL
);
1004 g_assert_cmpint (b
, ==, TRUE
);
1006 g_settings_bind (settings
, "byte", obj
, "byte", G_SETTINGS_BIND_DEFAULT
);
1008 g_object_set (obj
, "byte", 123, NULL
);
1010 g_settings_get (settings
, "byte", "y", &y
);
1011 g_assert_cmpint (y
, ==, 123);
1013 g_settings_set (settings
, "byte", "y", 54);
1015 g_object_get (obj
, "byte", &y
, NULL
);
1016 g_assert_cmpint (y
, ==, 54);
1018 g_settings_bind (settings
, "int16", obj
, "int16", G_SETTINGS_BIND_DEFAULT
);
1020 g_object_set (obj
, "int16", 1234, NULL
);
1022 g_settings_get (settings
, "int16", "n", &n
);
1023 g_assert_cmpint (n
, ==, 1234);
1025 g_settings_set (settings
, "int16", "n", 4321);
1027 g_object_get (obj
, "int16", &n2
, NULL
);
1028 g_assert_cmpint (n2
, ==, 4321);
1030 g_settings_bind (settings
, "uint16", obj
, "uint16", G_SETTINGS_BIND_DEFAULT
);
1032 g_object_set (obj
, "uint16", (guint16
) G_MAXUINT16
, NULL
);
1034 g_settings_get (settings
, "uint16", "q", &q
);
1035 g_assert_cmpuint (q
, ==, G_MAXUINT16
);
1037 g_settings_set (settings
, "uint16", "q", (guint16
) G_MAXINT16
);
1039 g_object_get (obj
, "uint16", &q2
, NULL
);
1040 g_assert_cmpuint (q2
, ==, (guint16
) G_MAXINT16
);
1042 g_settings_bind (settings
, "int", obj
, "int", G_SETTINGS_BIND_DEFAULT
);
1044 g_object_set (obj
, "int", 12345, NULL
);
1045 g_assert_cmpint (g_settings_get_int (settings
, "int"), ==, 12345);
1047 g_settings_set_int (settings
, "int", 54321);
1049 g_object_get (obj
, "int", &i
, NULL
);
1050 g_assert_cmpint (i
, ==, 54321);
1052 g_settings_bind (settings
, "int64", obj
, "int64", G_SETTINGS_BIND_DEFAULT
);
1054 g_object_set (obj
, "int64", (gint64
) G_MAXINT64
, NULL
);
1056 g_settings_get (settings
, "int64", "x", &i64
);
1057 g_assert_cmpint (i64
, ==, G_MAXINT64
);
1059 g_settings_set (settings
, "int64", "x", (gint64
) G_MININT64
);
1061 g_object_get (obj
, "int64", &i64
, NULL
);
1062 g_assert_cmpint (i64
, ==, G_MININT64
);
1064 g_settings_bind (settings
, "uint64", obj
, "uint64", G_SETTINGS_BIND_DEFAULT
);
1066 g_object_set (obj
, "uint64", (guint64
) G_MAXUINT64
, NULL
);
1068 g_settings_get (settings
, "uint64", "t", &u64
);
1069 g_assert_cmpuint (u64
, ==, G_MAXUINT64
);
1071 g_settings_set (settings
, "uint64", "t", (guint64
) G_MAXINT64
);
1073 g_object_get (obj
, "uint64", &u64
, NULL
);
1074 g_assert_cmpuint (u64
, ==, (guint64
) G_MAXINT64
);
1076 g_settings_bind (settings
, "string", obj
, "string", G_SETTINGS_BIND_DEFAULT
);
1078 g_object_set (obj
, "string", "bu ba", NULL
);
1079 s
= g_settings_get_string (settings
, "string");
1080 g_assert_cmpstr (s
, ==, "bu ba");
1083 g_settings_set_string (settings
, "string", "bla bla");
1084 g_object_get (obj
, "string", &s
, NULL
);
1085 g_assert_cmpstr (s
, ==, "bla bla");
1088 g_settings_bind (settings
, "chararray", obj
, "string", G_SETTINGS_BIND_DEFAULT
);
1090 g_object_set (obj
, "string", "non-unicode:\315", NULL
);
1091 value
= g_settings_get_value (settings
, "chararray");
1092 g_assert_cmpstr (g_variant_get_bytestring (value
), ==, "non-unicode:\315");
1093 g_variant_unref (value
);
1095 g_settings_bind (settings
, "double", obj
, "double", G_SETTINGS_BIND_DEFAULT
);
1097 g_object_set (obj
, "double", G_MAXFLOAT
, NULL
);
1098 g_assert_cmpfloat (g_settings_get_double (settings
, "double"), ==, G_MAXFLOAT
);
1100 g_settings_set_double (settings
, "double", G_MINFLOAT
);
1102 g_object_get (obj
, "double", &d
, NULL
);
1103 g_assert_cmpfloat (d
, ==, G_MINFLOAT
);
1105 g_object_set (obj
, "double", G_MAXDOUBLE
, NULL
);
1106 g_assert_cmpfloat (g_settings_get_double (settings
, "double"), ==, G_MAXDOUBLE
);
1108 g_settings_set_double (settings
, "double", -G_MINDOUBLE
);
1110 g_object_get (obj
, "double", &d
, NULL
);
1111 g_assert_cmpfloat (d
, ==, -G_MINDOUBLE
);
1113 strv
= g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1114 g_settings_bind (settings
, "strv", obj
, "strv", G_SETTINGS_BIND_DEFAULT
);
1115 g_object_set (obj
, "strv", strv
, NULL
);
1117 strv
= g_settings_get_strv (settings
, "strv");
1118 s
= g_strjoinv (",", strv
);
1119 g_assert_cmpstr (s
, ==, "plastic bag,middle class,polyethylene");
1122 strv
= g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1123 g_settings_set_strv (settings
, "strv", (const gchar
**) strv
);
1125 g_object_get (obj
, "strv", &strv
, NULL
);
1126 s
= g_strjoinv (",", strv
);
1127 g_assert_cmpstr (s
, ==, "decaffeinate,unleaded,keep all surfaces clean");
1131 g_settings_bind (settings
, "enum", obj
, "enum", G_SETTINGS_BIND_DEFAULT
);
1132 g_object_set (obj
, "enum", TEST_ENUM_BAZ
, NULL
);
1133 s
= g_settings_get_string (settings
, "enum");
1134 g_assert_cmpstr (s
, ==, "baz");
1136 g_assert_cmpint (g_settings_get_enum (settings
, "enum"), ==, TEST_ENUM_BAZ
);
1138 g_settings_set_enum (settings
, "enum", TEST_ENUM_QUUX
);
1140 g_object_get (obj
, "enum", &i
, NULL
);
1141 g_assert_cmpint (i
, ==, TEST_ENUM_QUUX
);
1143 g_settings_set_string (settings
, "enum", "baz");
1145 g_object_get (obj
, "enum", &i
, NULL
);
1146 g_assert_cmpint (i
, ==, TEST_ENUM_BAZ
);
1148 g_object_unref (obj
);
1149 g_object_unref (settings
);
1156 GSettings
*settings
;
1158 settings
= g_settings_new ("org.gtk.test.binding");
1159 obj
= test_object_new ();
1161 g_settings_bind (settings
, "int", obj
, "int", G_SETTINGS_BIND_DEFAULT
);
1163 g_object_set (obj
, "int", 12345, NULL
);
1164 g_assert_cmpint (g_settings_get_int (settings
, "int"), ==, 12345);
1166 g_settings_unbind (obj
, "int");
1168 g_object_set (obj
, "int", 54321, NULL
);
1169 g_assert_cmpint (g_settings_get_int (settings
, "int"), ==, 12345);
1171 g_object_unref (obj
);
1172 g_object_unref (settings
);
1176 test_bind_writable (void)
1179 GSettings
*settings
;
1182 settings
= g_settings_new ("org.gtk.test.binding");
1183 obj
= test_object_new ();
1185 g_object_set (obj
, "bool", FALSE
, NULL
);
1187 g_settings_bind_writable (settings
, "int", obj
, "bool", FALSE
);
1189 g_object_get (obj
, "bool", &b
, NULL
);
1192 g_settings_unbind (obj
, "bool");
1194 g_settings_bind_writable (settings
, "int", obj
, "bool", TRUE
);
1196 g_object_get (obj
, "bool", &b
, NULL
);
1199 g_object_unref (obj
);
1200 g_object_unref (settings
);
1203 /* Test one-way bindings.
1204 * Verify that changes on one side show up on the other,
1205 * but not vice versa
1208 test_directional_binding (void)
1211 GSettings
*settings
;
1215 settings
= g_settings_new ("org.gtk.test.binding");
1216 obj
= test_object_new ();
1218 g_object_set (obj
, "bool", FALSE
, NULL
);
1219 g_settings_set_boolean (settings
, "bool", FALSE
);
1221 g_settings_bind (settings
, "bool", obj
, "bool", G_SETTINGS_BIND_GET
);
1223 g_settings_set_boolean (settings
, "bool", TRUE
);
1224 g_object_get (obj
, "bool", &b
, NULL
);
1225 g_assert_cmpint (b
, ==, TRUE
);
1227 g_object_set (obj
, "bool", FALSE
, NULL
);
1228 g_assert_cmpint (g_settings_get_boolean (settings
, "bool"), ==, TRUE
);
1230 g_object_set (obj
, "int", 20, NULL
);
1231 g_settings_set_int (settings
, "int", 20);
1233 g_settings_bind (settings
, "int", obj
, "int", G_SETTINGS_BIND_SET
);
1235 g_object_set (obj
, "int", 32, NULL
);
1236 g_assert_cmpint (g_settings_get_int (settings
, "int"), ==, 32);
1238 g_settings_set_int (settings
, "int", 20);
1239 g_object_get (obj
, "int", &i
, NULL
);
1240 g_assert_cmpint (i
, ==, 32);
1242 g_object_unref (obj
);
1243 g_object_unref (settings
);
1246 /* Test that type mismatch is caught when creating a binding
1249 test_typesafe_binding (void)
1251 if (!g_test_undefined ())
1254 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
1257 GSettings
*settings
;
1259 settings
= g_settings_new ("org.gtk.test.binding");
1260 obj
= test_object_new ();
1262 g_settings_bind (settings
, "string", obj
, "int", G_SETTINGS_BIND_DEFAULT
);
1264 g_object_unref (obj
);
1265 g_object_unref (settings
);
1267 g_test_trap_assert_failed ();
1268 g_test_trap_assert_stderr ("*not compatible*");
1272 string_to_bool (GValue
*value
,
1278 s
= g_variant_get_string (variant
, NULL
);
1279 g_value_set_boolean (value
, g_strcmp0 (s
, "true") == 0);
1285 bool_to_string (const GValue
*value
,
1286 const GVariantType
*expected_type
,
1289 if (g_value_get_boolean (value
))
1290 return g_variant_new_string ("true");
1292 return g_variant_new_string ("false");
1295 /* Test custom bindings.
1296 * Translate strings to booleans and back
1299 test_custom_binding (void)
1302 GSettings
*settings
;
1306 settings
= g_settings_new ("org.gtk.test.binding");
1307 obj
= test_object_new ();
1309 g_settings_set_string (settings
, "string", "true");
1311 g_settings_bind_with_mapping (settings
, "string",
1313 G_SETTINGS_BIND_DEFAULT
,
1318 g_settings_set_string (settings
, "string", "false");
1319 g_object_get (obj
, "bool", &b
, NULL
);
1320 g_assert_cmpint (b
, ==, FALSE
);
1322 g_settings_set_string (settings
, "string", "not true");
1323 g_object_get (obj
, "bool", &b
, NULL
);
1324 g_assert_cmpint (b
, ==, FALSE
);
1326 g_object_set (obj
, "bool", TRUE
, NULL
);
1327 s
= g_settings_get_string (settings
, "string");
1328 g_assert_cmpstr (s
, ==, "true");
1331 g_object_unref (obj
);
1332 g_object_unref (settings
);
1335 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1336 * initial settings value is transported to the object
1337 * side, but later settings changes do not affect the
1341 test_no_change_binding (void)
1344 GSettings
*settings
;
1347 settings
= g_settings_new ("org.gtk.test.binding");
1348 obj
= test_object_new ();
1350 g_object_set (obj
, "bool", TRUE
, NULL
);
1351 g_settings_set_boolean (settings
, "bool", FALSE
);
1353 g_settings_bind (settings
, "bool", obj
, "bool", G_SETTINGS_BIND_GET_NO_CHANGES
);
1355 g_object_get (obj
, "bool", &b
, NULL
);
1356 g_assert_cmpint (b
, ==, FALSE
);
1358 g_settings_set_boolean (settings
, "bool", TRUE
);
1359 g_object_get (obj
, "bool", &b
, NULL
);
1360 g_assert_cmpint (b
, ==, FALSE
);
1362 g_settings_set_boolean (settings
, "bool", FALSE
);
1363 g_object_set (obj
, "bool", TRUE
, NULL
);
1364 b
= g_settings_get_boolean (settings
, "bool");
1365 g_assert_cmpint (b
, ==, TRUE
);
1367 g_object_unref (obj
);
1368 g_object_unref (settings
);
1371 /* Test that binding a non-readable property only
1372 * works in 'GET' mode.
1375 test_no_read_binding (void)
1377 if (g_test_undefined ())
1379 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
1382 GSettings
*settings
;
1384 settings
= g_settings_new ("org.gtk.test.binding");
1385 obj
= test_object_new ();
1387 g_settings_bind (settings
, "string", obj
, "no-read", 0);
1389 g_test_trap_assert_failed ();
1390 g_test_trap_assert_stderr ("*property*is not readable*");
1393 if (g_test_trap_fork (0, 0))
1396 GSettings
*settings
;
1398 settings
= g_settings_new ("org.gtk.test.binding");
1399 obj
= test_object_new ();
1401 g_settings_bind (settings
, "string", obj
, "no-read", G_SETTINGS_BIND_GET
);
1405 g_test_trap_assert_passed ();
1408 /* Test that binding a non-writable property only
1409 * works in 'SET' mode.
1412 test_no_write_binding (void)
1414 if (g_test_undefined ())
1416 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
1419 GSettings
*settings
;
1421 settings
= g_settings_new ("org.gtk.test.binding");
1422 obj
= test_object_new ();
1424 g_settings_bind (settings
, "string", obj
, "no-write", 0);
1426 g_test_trap_assert_failed ();
1427 g_test_trap_assert_stderr ("*property*is not writable*");
1430 if (g_test_trap_fork (0, 0))
1433 GSettings
*settings
;
1435 settings
= g_settings_new ("org.gtk.test.binding");
1436 obj
= test_object_new ();
1438 g_settings_bind (settings
, "string", obj
, "no-write", G_SETTINGS_BIND_SET
);
1442 g_test_trap_assert_passed ();
1446 * Test that using a keyfile works
1451 GSettingsBackend
*kf_backend
;
1452 GSettings
*settings
;
1456 g_remove ("gsettings.store");
1458 kf_backend
= g_keyfile_settings_backend_new ("gsettings.store", "/", "root");
1459 settings
= g_settings_new_with_backend ("org.gtk.test", kf_backend
);
1460 g_object_unref (kf_backend
);
1462 g_settings_set (settings
, "greeting", "s", "see if this works");
1464 keyfile
= g_key_file_new ();
1465 g_assert (g_key_file_load_from_file (keyfile
, "gsettings.store", 0, NULL
));
1467 str
= g_key_file_get_string (keyfile
, "tests", "greeting", NULL
);
1468 g_assert_cmpstr (str
, ==, "'see if this works'");
1471 g_key_file_free (keyfile
);
1472 g_object_unref (settings
);
1475 /* Test that getting child schemas works
1478 test_child_schema (void)
1480 GSettings
*settings
;
1484 /* first establish some known conditions */
1485 settings
= g_settings_new ("org.gtk.test.basic-types");
1486 g_settings_set (settings
, "test-byte", "y", 36);
1488 g_settings_get (settings
, "test-byte", "y", &byte
);
1489 g_assert_cmpint (byte
, ==, 36);
1491 g_object_unref (settings
);
1493 settings
= g_settings_new ("org.gtk.test");
1494 child
= g_settings_get_child (settings
, "basic-types");
1495 g_assert (child
!= NULL
);
1497 g_settings_get (child
, "test-byte", "y", &byte
);
1498 g_assert_cmpint (byte
, ==, 36);
1500 g_object_unref (child
);
1501 g_object_unref (settings
);
1505 glib_translations_work (void)
1509 gchar
*orig
= "Unnamed";
1511 locale
= g_strdup (setlocale (LC_MESSAGES
, NULL
));
1512 if (!setlocale (LC_MESSAGES
, "de"))
1515 works
= dgettext ("glib20", orig
) != orig
;
1516 setlocale (LC_MESSAGES
, locale
);
1522 #include "../strinfo.c"
1527 /* "foo" has a value of 1
1528 * "bar" has a value of 2
1529 * "baz" is an alias for "bar"
1532 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1533 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1535 const guint32
*strinfo
= (guint32
*) array
;
1536 guint length
= sizeof array
/ 4;
1540 /* build it and compare */
1543 builder
= g_string_new (NULL
);
1544 strinfo_builder_append_item (builder
, "foo", 1);
1545 strinfo_builder_append_item (builder
, "bar", 2);
1546 g_assert (strinfo_builder_append_alias (builder
, "baz", "bar"));
1547 g_assert_cmpint (builder
->len
% 4, ==, 0);
1548 g_assert_cmpint (builder
->len
/ 4, ==, length
);
1549 g_assert (memcmp (builder
->str
, strinfo
, length
* 4) == 0);
1550 g_string_free (builder
, TRUE
);
1553 g_assert_cmpstr (strinfo_string_from_alias (strinfo
, length
, "foo"),
1555 g_assert_cmpstr (strinfo_string_from_alias (strinfo
, length
, "bar"),
1557 g_assert_cmpstr (strinfo_string_from_alias (strinfo
, length
, "baz"),
1559 g_assert_cmpstr (strinfo_string_from_alias (strinfo
, length
, "quux"),
1562 g_assert (strinfo_enum_from_string (strinfo
, length
, "foo", &result
));
1563 g_assert_cmpint (result
, ==, 1);
1564 g_assert (strinfo_enum_from_string (strinfo
, length
, "bar", &result
));
1565 g_assert_cmpint (result
, ==, 2);
1566 g_assert (!strinfo_enum_from_string (strinfo
, length
, "baz", &result
));
1567 g_assert (!strinfo_enum_from_string (strinfo
, length
, "quux", &result
));
1569 g_assert_cmpstr (strinfo_string_from_enum (strinfo
, length
, 0), ==, NULL
);
1570 g_assert_cmpstr (strinfo_string_from_enum (strinfo
, length
, 1), ==, "foo");
1571 g_assert_cmpstr (strinfo_string_from_enum (strinfo
, length
, 2), ==, "bar");
1572 g_assert_cmpstr (strinfo_string_from_enum (strinfo
, length
, 3), ==, NULL
);
1574 g_assert (strinfo_is_string_valid (strinfo
, length
, "foo"));
1575 g_assert (strinfo_is_string_valid (strinfo
, length
, "bar"));
1576 g_assert (!strinfo_is_string_valid (strinfo
, length
, "baz"));
1577 g_assert (!strinfo_is_string_valid (strinfo
, length
, "quux"));
1583 GSettings
*settings
, *direct
;
1586 settings
= g_settings_new ("org.gtk.test.enums");
1587 direct
= g_settings_new ("org.gtk.test.enums.direct");
1589 if (g_test_undefined () && !backend_set
)
1591 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
1592 g_settings_get_enum (direct
, "test");
1593 g_test_trap_assert_failed ();
1594 g_test_trap_assert_stderr ("*not associated with an enum*");
1596 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
1597 g_settings_set_enum (settings
, "test", 42);
1598 g_test_trap_assert_failed ();
1599 g_test_trap_assert_stderr ("*invalid enum value 42*");
1601 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
1602 g_settings_set_string (settings
, "test", "qux");
1603 g_test_trap_assert_failed ();
1604 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1606 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
1607 g_settings_get_flags (settings
, "test");
1608 g_test_trap_assert_failed ();
1609 g_test_trap_assert_stderr ("*not associated with a flags*");
1612 str
= g_settings_get_string (settings
, "test");
1613 g_assert_cmpstr (str
, ==, "bar");
1616 g_settings_set_enum (settings
, "test", TEST_ENUM_FOO
);
1618 str
= g_settings_get_string (settings
, "test");
1619 g_assert_cmpstr (str
, ==, "foo");
1622 g_assert_cmpint (g_settings_get_enum (settings
, "test"), ==, TEST_ENUM_FOO
);
1624 g_settings_set_string (direct
, "test", "qux");
1626 str
= g_settings_get_string (direct
, "test");
1627 g_assert_cmpstr (str
, ==, "qux");
1630 str
= g_settings_get_string (settings
, "test");
1631 g_assert_cmpstr (str
, ==, "quux");
1634 g_assert_cmpint (g_settings_get_enum (settings
, "test"), ==, TEST_ENUM_QUUX
);
1640 GSettings
*settings
, *direct
;
1644 settings
= g_settings_new ("org.gtk.test.enums");
1645 direct
= g_settings_new ("org.gtk.test.enums.direct");
1647 if (g_test_undefined () && !backend_set
)
1649 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
1650 g_settings_get_flags (direct
, "test");
1651 g_test_trap_assert_failed ();
1652 g_test_trap_assert_stderr ("*not associated with a flags*");
1654 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
1655 g_settings_set_flags (settings
, "f-test", 0x42);
1656 g_test_trap_assert_failed ();
1657 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
1659 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
1660 g_settings_set_strv (settings
, "f-test",
1661 (const gchar
**) g_strsplit ("rock", ",", 0));
1662 g_test_trap_assert_failed ();
1663 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1665 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
1666 g_settings_get_enum (settings
, "f-test");
1667 g_test_trap_assert_failed ();
1668 g_test_trap_assert_stderr ("*not associated with an enum*");
1671 strv
= g_settings_get_strv (settings
, "f-test");
1672 str
= g_strjoinv (",", strv
);
1673 g_assert_cmpstr (str
, ==, "");
1677 g_settings_set_flags (settings
, "f-test",
1678 TEST_FLAGS_WALKING
| TEST_FLAGS_TALKING
);
1680 strv
= g_settings_get_strv (settings
, "f-test");
1681 str
= g_strjoinv (",", strv
);
1682 g_assert_cmpstr (str
, ==, "talking,walking");
1686 g_assert_cmpint (g_settings_get_flags (settings
, "f-test"), ==,
1687 TEST_FLAGS_WALKING
| TEST_FLAGS_TALKING
);
1689 strv
= g_strsplit ("speaking,laughing", ",", 0);
1690 g_settings_set_strv (direct
, "f-test", (const gchar
**) strv
);
1693 strv
= g_settings_get_strv (direct
, "f-test");
1694 str
= g_strjoinv (",", strv
);
1695 g_assert_cmpstr (str
, ==, "speaking,laughing");
1699 strv
= g_settings_get_strv (settings
, "f-test");
1700 str
= g_strjoinv (",", strv
);
1701 g_assert_cmpstr (str
, ==, "talking,laughing");
1705 g_assert_cmpint (g_settings_get_flags (settings
, "f-test"), ==,
1706 TEST_FLAGS_TALKING
| TEST_FLAGS_LAUGHING
);
1712 GSettings
*settings
, *direct
;
1714 settings
= g_settings_new ("org.gtk.test.range");
1715 direct
= g_settings_new ("org.gtk.test.range.direct");
1717 if (g_test_undefined () && !backend_set
)
1719 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
1720 g_settings_set_int (settings
, "val", 45);
1721 g_test_trap_assert_failed ();
1722 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1724 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
1725 g_settings_set_int (settings
, "val", 1);
1726 g_test_trap_assert_failed ();
1727 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1730 g_assert_cmpint (g_settings_get_int (settings
, "val"), ==, 33);
1731 g_settings_set_int (direct
, "val", 22);
1732 g_assert_cmpint (g_settings_get_int (direct
, "val"), ==, 22);
1733 g_assert_cmpint (g_settings_get_int (settings
, "val"), ==, 22);
1734 g_settings_set_int (direct
, "val", 45);
1735 g_assert_cmpint (g_settings_get_int (direct
, "val"), ==, 45);
1736 g_assert_cmpint (g_settings_get_int (settings
, "val"), ==, 33);
1737 g_settings_set_int (direct
, "val", 1);
1738 g_assert_cmpint (g_settings_get_int (direct
, "val"), ==, 1);
1739 g_assert_cmpint (g_settings_get_int (settings
, "val"), ==, 33);
1743 strv_has_string (gchar
**haystack
,
1744 const gchar
*needle
)
1748 for (n
= 0; haystack
!= NULL
&& haystack
[n
] != NULL
; n
++)
1750 if (g_strcmp0 (haystack
[n
], needle
) == 0)
1757 strv_set_equal (gchar
**strv
, ...)
1766 va_start (list
, strv
);
1769 str
= va_arg (list
, const gchar
*);
1772 if (!strv_has_string (strv
, str
))
1782 res
= g_strv_length ((gchar
**)strv
) == count
;
1788 test_list_items (void)
1790 GSettings
*settings
;
1794 settings
= g_settings_new ("org.gtk.test");
1795 children
= g_settings_list_children (settings
);
1796 keys
= g_settings_list_keys (settings
);
1798 g_assert (strv_set_equal (children
, "basic-types", "complex-types", "localized", NULL
));
1799 g_assert (strv_set_equal (keys
, "greeting", "farewell", NULL
));
1801 g_strfreev (children
);
1804 g_object_unref (settings
);
1808 test_list_schemas (void)
1810 const gchar
* const *schemas
;
1811 const gchar
* const *relocs
;
1813 relocs
= g_settings_list_relocatable_schemas ();
1814 schemas
= g_settings_list_schemas ();
1816 g_assert (strv_set_equal ((gchar
**)relocs
,
1817 "org.gtk.test.no-path",
1820 g_assert (strv_set_equal ((gchar
**)schemas
,
1822 "org.gtk.test.basic-types",
1823 "org.gtk.test.complex-types",
1824 "org.gtk.test.localized",
1825 "org.gtk.test.binding",
1826 "org.gtk.test.enums",
1827 "org.gtk.test.enums.direct",
1828 "org.gtk.test.range",
1829 "org.gtk.test.range.direct",
1830 "org.gtk.test.mapped",
1835 map_func (GVariant
*value
,
1839 gint
*state
= user_data
;
1843 v
= g_variant_get_int32 (value
);
1849 g_assert_cmpint (v
, ==, 1);
1853 else if (*state
== 1)
1855 g_assert_cmpint (v
, ==, 0);
1861 g_assert (value
== NULL
);
1862 *result
= g_variant_new_int32 (5);
1868 test_get_mapped (void)
1870 GSettings
*settings
;
1875 settings
= g_settings_new ("org.gtk.test.mapped");
1876 g_settings_set_int (settings
, "val", 1);
1879 p
= g_settings_get_mapped (settings
, "val", map_func
, &state
);
1880 val
= g_variant_get_int32 ((GVariant
*)p
);
1881 g_assert_cmpint (val
, ==, 5);
1883 g_variant_unref (p
);
1884 g_object_unref (settings
);
1888 test_get_range (void)
1890 GSettings
*settings
;
1893 settings
= g_settings_new ("org.gtk.test.range");
1894 range
= g_settings_get_range (settings
, "val");
1895 check_and_free (range
, "('range', <(2, 44)>)");
1896 g_object_unref (settings
);
1898 settings
= g_settings_new ("org.gtk.test.enums");
1899 range
= g_settings_get_range (settings
, "test");
1900 check_and_free (range
, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
1901 g_object_unref (settings
);
1903 settings
= g_settings_new ("org.gtk.test.enums");
1904 range
= g_settings_get_range (settings
, "f-test");
1905 check_and_free (range
, "('flags', "
1906 "<['mourning', 'laughing', 'talking', 'walking']>)");
1907 g_object_unref (settings
);
1909 settings
= g_settings_new ("org.gtk.test");
1910 range
= g_settings_get_range (settings
, "greeting");
1911 check_and_free (range
, "('type', <@as []>)");
1912 g_object_unref (settings
);
1916 test_schema_source (void)
1918 GSettingsSchemaSource
*parent
;
1919 GSettingsSchemaSource
*source
;
1920 GSettingsBackend
*backend
;
1921 GSettingsSchema
*schema
;
1922 GError
*error
= NULL
;
1923 GSettings
*settings
;
1926 backend
= g_settings_backend_get_default ();
1928 /* make sure it fails properly */
1929 parent
= g_settings_schema_source_get_default ();
1930 source
= g_settings_schema_source_new_from_directory ("/path/that/does/not/exist", parent
, TRUE
, &error
);
1931 g_assert (source
== NULL
);
1932 g_assert_error (error
, G_FILE_ERROR
, G_FILE_ERROR_NOENT
);
1933 g_clear_error (&error
);
1935 /* create a source with the parent */
1936 source
= g_settings_schema_source_new_from_directory ("schema-source", parent
, TRUE
, &error
);
1937 g_assert_no_error (error
);
1938 g_assert (source
!= NULL
);
1940 /* check recursive lookups are working */
1941 schema
= g_settings_schema_source_lookup (source
, "org.gtk.test", TRUE
);
1942 g_assert (schema
!= NULL
);
1943 g_settings_schema_unref (schema
);
1945 /* check recursive lookups for non-existent schemas */
1946 schema
= g_settings_schema_source_lookup (source
, "org.gtk.doesnotexist", TRUE
);
1947 g_assert (schema
== NULL
);
1949 /* check non-recursive for schema that only exists in lower layers */
1950 schema
= g_settings_schema_source_lookup (source
, "org.gtk.test", FALSE
);
1951 g_assert (schema
== NULL
);
1953 /* check non-recursive lookup for non-existent */
1954 schema
= g_settings_schema_source_lookup (source
, "org.gtk.doesnotexist", FALSE
);
1955 g_assert (schema
== NULL
);
1957 /* check non-recursive for schema that exists in toplevel */
1958 schema
= g_settings_schema_source_lookup (source
, "org.gtk.schemasourcecheck", FALSE
);
1959 g_assert (schema
!= NULL
);
1960 g_settings_schema_unref (schema
);
1962 /* check recursive for schema that exists in toplevel */
1963 schema
= g_settings_schema_source_lookup (source
, "org.gtk.schemasourcecheck", TRUE
);
1964 g_assert (schema
!= NULL
);
1966 /* try to use it for something */
1967 settings
= g_settings_new_full (schema
, backend
, g_settings_schema_get_path (schema
));
1968 g_settings_schema_unref (schema
);
1970 g_settings_get (settings
, "enabled", "b", &enabled
);
1972 g_object_unref (settings
);
1974 g_settings_schema_source_unref (source
);
1976 /* try again, but with no parent */
1977 source
= g_settings_schema_source_new_from_directory ("schema-source", NULL
, FALSE
, NULL
);
1978 g_assert (source
!= NULL
);
1980 /* should not find it this time, even if recursive... */
1981 schema
= g_settings_schema_source_lookup (source
, "org.gtk.test", FALSE
);
1982 g_assert (schema
== NULL
);
1983 schema
= g_settings_schema_source_lookup (source
, "org.gtk.test", TRUE
);
1984 g_assert (schema
== NULL
);
1986 /* should still find our own... */
1987 schema
= g_settings_schema_source_lookup (source
, "org.gtk.schemasourcecheck", TRUE
);
1988 g_assert (schema
!= NULL
);
1989 g_settings_schema_unref (schema
);
1990 schema
= g_settings_schema_source_lookup (source
, "org.gtk.schemasourcecheck", FALSE
);
1991 g_assert (schema
!= NULL
);
1992 g_settings_schema_unref (schema
);
1994 g_settings_schema_source_unref (source
);
1998 main (int argc
, char *argv
[])
2003 setlocale (LC_ALL
, "");
2005 backend_set
= g_getenv ("GSETTINGS_BACKEND") != NULL
;
2007 g_setenv ("XDG_DATA_DIRS", ".", TRUE
);
2008 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE
);
2011 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE
);
2014 g_test_init (&argc
, &argv
, NULL
);
2016 g_remove ("org.gtk.test.enums.xml");
2017 g_assert (g_spawn_command_line_sync ("../../gobject/glib-mkenums "
2018 "--template " SRCDIR
"/enums.xml.template "
2019 SRCDIR
"/testenum.h",
2020 &enums
, NULL
, &result
, NULL
));
2021 g_assert (result
== 0);
2022 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums
, -1, NULL
));
2025 g_remove ("gschemas.compiled");
2026 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=. "
2027 "--schema-file=org.gtk.test.enums.xml "
2028 "--schema-file=" SRCDIR
"/org.gtk.test.gschema.xml",
2029 NULL
, NULL
, &result
, NULL
));
2030 g_assert (result
== 0);
2032 g_remove ("schema-source/gschemas.compiled");
2033 g_mkdir ("schema-source", 0777);
2034 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=schema-source "
2035 "--schema-file=" SRCDIR
"/org.gtk.schemasourcecheck.gschema.xml",
2036 NULL
, NULL
, &result
, NULL
));
2037 g_assert (result
== 0);
2039 g_test_add_func ("/gsettings/basic", test_basic
);
2043 g_test_add_func ("/gsettings/no-schema", test_no_schema
);
2044 g_test_add_func ("/gsettings/unknown-key", test_unknown_key
);
2045 g_test_add_func ("/gsettings/wrong-type", test_wrong_type
);
2046 g_test_add_func ("/gsettings/wrong-path", test_wrong_path
);
2047 g_test_add_func ("/gsettings/no-path", test_no_path
);
2050 g_test_add_func ("/gsettings/basic-types", test_basic_types
);
2051 g_test_add_func ("/gsettings/complex-types", test_complex_types
);
2052 g_test_add_func ("/gsettings/changes", test_changes
);
2054 if (glib_translations_work ())
2056 g_test_add_func ("/gsettings/l10n", test_l10n
);
2057 g_test_add_func ("/gsettings/l10n-context", test_l10n_context
);
2060 g_test_add_func ("/gsettings/delay-apply", test_delay_apply
);
2061 g_test_add_func ("/gsettings/delay-revert", test_delay_revert
);
2062 g_test_add_func ("/gsettings/atomic", test_atomic
);
2064 g_test_add_func ("/gsettings/simple-binding", test_simple_binding
);
2065 g_test_add_func ("/gsettings/directional-binding", test_directional_binding
);
2066 g_test_add_func ("/gsettings/custom-binding", test_custom_binding
);
2067 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding
);
2068 g_test_add_func ("/gsettings/unbinding", test_unbind
);
2069 g_test_add_func ("/gsettings/writable-binding", test_bind_writable
);
2073 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding
);
2074 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding
);
2075 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding
);
2078 g_test_add_func ("/gsettings/keyfile", test_keyfile
);
2079 g_test_add_func ("/gsettings/child-schema", test_child_schema
);
2080 g_test_add_func ("/gsettings/strinfo", test_strinfo
);
2081 g_test_add_func ("/gsettings/enums", test_enums
);
2082 g_test_add_func ("/gsettings/flags", test_flags
);
2083 g_test_add_func ("/gsettings/range", test_range
);
2084 g_test_add_func ("/gsettings/list-items", test_list_items
);
2085 g_test_add_func ("/gsettings/list-schemas", test_list_schemas
);
2086 g_test_add_func ("/gsettings/mapped", test_get_mapped
);
2087 g_test_add_func ("/gsettings/get-range", test_get_range
);
2088 g_test_add_func ("/gsettings/schema-source", test_schema_source
);
2090 result
= g_test_run ();