6 #define G_SETTINGS_ENABLE_BACKEND
7 #include <gio/gsettingsbackend.h>
11 static const gchar
*locale_dir
= ".";
13 static gboolean backend_set
;
15 /* These tests rely on the schemas in org.gtk.test.gschema.xml
16 * to be compiled and installed in the same directory.
20 check_and_free (GVariant
*value
,
21 const gchar
*expected
)
25 printed
= g_variant_print (value
, TRUE
);
26 g_assert_cmpstr (printed
, ==, expected
);
29 g_variant_unref (value
);
33 /* Just to get warmed up: Read and set a string, and
34 * verify that can read the changed string back
42 gboolean has_unapplied
;
46 settings
= g_settings_new ("org.gtk.test");
48 g_object_get (settings
,
52 "has-unapplied", &has_unapplied
,
53 "delay-apply", &delay_apply
,
55 g_assert_cmpstr (str
, ==, "org.gtk.test");
57 g_assert_cmpstr (path
, ==, "/tests/");
58 g_assert (!has_unapplied
);
59 g_assert (!delay_apply
);
64 g_settings_get (settings
, "greeting", "s", &str
);
65 g_assert_cmpstr (str
, ==, "Hello, earthlings");
68 g_settings_set (settings
, "greeting", "s", "goodbye world");
69 g_settings_get (settings
, "greeting", "s", &str
);
70 g_assert_cmpstr (str
, ==, "goodbye world");
74 if (!backend_set
&& g_test_undefined ())
76 GSettings
*tmp_settings
= g_settings_new ("org.gtk.test");
78 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
,
79 "*g_settings_set_value*expects type*");
80 g_settings_set (tmp_settings
, "greeting", "i", 555);
81 g_test_assert_expected_messages ();
83 g_object_unref (tmp_settings
);
86 g_settings_get (settings
, "greeting", "s", &str
);
87 g_assert_cmpstr (str
, ==, "goodbye world");
91 g_settings_reset (settings
, "greeting");
92 str
= g_settings_get_string (settings
, "greeting");
93 g_assert_cmpstr (str
, ==, "Hello, earthlings");
96 g_settings_set (settings
, "greeting", "s", "this is the end");
97 g_object_unref (settings
);
100 /* Check that we get an error when getting a key
101 * that is not in the schema
104 test_unknown_key (void)
106 if (!g_test_undefined ())
109 if (g_test_subprocess ())
114 settings
= g_settings_new ("org.gtk.test");
115 value
= g_settings_get_value (settings
, "no_such_key");
117 g_assert (value
== NULL
);
119 g_object_unref (settings
);
122 g_test_trap_subprocess (NULL
, 0, 0);
123 g_test_trap_assert_failed ();
124 g_test_trap_assert_stderr ("*does not contain*");
127 /* Check that we get an error when the schema
128 * has not been installed
131 test_no_schema (void)
133 if (!g_test_undefined ())
136 if (g_test_subprocess ())
140 settings
= g_settings_new ("no.such.schema");
142 g_assert (settings
== NULL
);
145 g_test_trap_subprocess (NULL
, 0, 0);
146 g_test_trap_assert_failed ();
147 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
150 /* Check that we get an error when passing a type string
151 * that does not match the schema
154 test_wrong_type (void)
159 if (!g_test_undefined ())
162 settings
= g_settings_new ("org.gtk.test");
164 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL
,
165 "*given value has a type of*");
166 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL
,
167 "*valid_format_string*");
168 g_settings_get (settings
, "greeting", "o", &str
);
169 g_test_assert_expected_messages ();
171 g_assert (str
== NULL
);
173 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
,
174 "*expects type 's'*");
175 g_settings_set (settings
, "greeting", "o", "/a/path");
176 g_test_assert_expected_messages ();
178 g_object_unref (settings
);
181 /* Check errors with explicit paths */
183 test_wrong_path (void)
185 if (!g_test_undefined ())
188 if (g_test_subprocess ())
190 GSettings
*settings G_GNUC_UNUSED
;
192 settings
= g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
195 g_test_trap_subprocess (NULL
, 0, 0);
196 g_test_trap_assert_failed ();
197 g_test_trap_assert_stderr ("*but path * specified by schema*");
203 if (!g_test_undefined ())
206 if (g_test_subprocess ())
208 GSettings
*settings G_GNUC_UNUSED
;
210 settings
= g_settings_new ("org.gtk.test.no-path");
213 g_test_trap_subprocess (NULL
, 0, 0);
214 g_test_trap_assert_failed ();
215 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
219 /* Check that we can successfully read and set the full
220 * range of all basic types
223 test_basic_types (void)
237 settings
= g_settings_new ("org.gtk.test.basic-types");
239 g_settings_get (settings
, "test-boolean", "b", &b
);
240 g_assert_cmpint (b
, ==, 1);
242 g_settings_set (settings
, "test-boolean", "b", 0);
243 g_settings_get (settings
, "test-boolean", "b", &b
);
244 g_assert_cmpint (b
, ==, 0);
246 g_settings_get (settings
, "test-byte", "y", &byte
);
247 g_assert_cmpint (byte
, ==, 25);
249 g_settings_set (settings
, "test-byte", "y", G_MAXUINT8
);
250 g_settings_get (settings
, "test-byte", "y", &byte
);
251 g_assert_cmpint (byte
, ==, G_MAXUINT8
);
253 g_settings_get (settings
, "test-int16", "n", &i16
);
254 g_assert_cmpint (i16
, ==, -1234);
256 g_settings_set (settings
, "test-int16", "n", G_MININT16
);
257 g_settings_get (settings
, "test-int16", "n", &i16
);
258 g_assert_cmpint (i16
, ==, G_MININT16
);
260 g_settings_set (settings
, "test-int16", "n", G_MAXINT16
);
261 g_settings_get (settings
, "test-int16", "n", &i16
);
262 g_assert_cmpint (i16
, ==, G_MAXINT16
);
264 g_settings_get (settings
, "test-uint16", "q", &u16
);
265 g_assert_cmpuint (u16
, ==, 1234);
267 g_settings_set (settings
, "test-uint16", "q", G_MAXUINT16
);
268 g_settings_get (settings
, "test-uint16", "q", &u16
);
269 g_assert_cmpuint (u16
, ==, G_MAXUINT16
);
271 g_settings_get (settings
, "test-int32", "i", &i32
);
272 g_assert_cmpint (i32
, ==, -123456);
274 g_settings_set (settings
, "test-int32", "i", G_MININT32
);
275 g_settings_get (settings
, "test-int32", "i", &i32
);
276 g_assert_cmpint (i32
, ==, G_MININT32
);
278 g_settings_set (settings
, "test-int32", "i", G_MAXINT32
);
279 g_settings_get (settings
, "test-int32", "i", &i32
);
280 g_assert_cmpint (i32
, ==, G_MAXINT32
);
282 g_settings_get (settings
, "test-uint32", "u", &u32
);
283 g_assert_cmpuint (u32
, ==, 123456);
285 g_settings_set (settings
, "test-uint32", "u", G_MAXUINT32
);
286 g_settings_get (settings
, "test-uint32", "u", &u32
);
287 g_assert_cmpuint (u32
, ==, G_MAXUINT32
);
289 g_settings_get (settings
, "test-int64", "x", &i64
);
290 g_assert_cmpuint (i64
, ==, -123456789);
292 g_settings_set (settings
, "test-int64", "x", G_MININT64
);
293 g_settings_get (settings
, "test-int64", "x", &i64
);
294 g_assert_cmpuint (i64
, ==, G_MININT64
);
296 g_settings_set (settings
, "test-int64", "x", G_MAXINT64
);
297 g_settings_get (settings
, "test-int64", "x", &i64
);
298 g_assert_cmpuint (i64
, ==, G_MAXINT64
);
300 g_settings_get (settings
, "test-uint64", "t", &u64
);
301 g_assert_cmpuint (u64
, ==, 123456789);
303 g_settings_set (settings
, "test-uint64", "t", G_MAXUINT64
);
304 g_settings_get (settings
, "test-uint64", "t", &u64
);
305 g_assert_cmpuint (u64
, ==, G_MAXUINT64
);
307 g_settings_get (settings
, "test-double", "d", &d
);
308 g_assert_cmpfloat (d
, ==, 123.456);
310 g_settings_set (settings
, "test-double", "d", G_MINDOUBLE
);
311 g_settings_get (settings
, "test-double", "d", &d
);
312 g_assert_cmpfloat (d
, ==, G_MINDOUBLE
);
314 g_settings_set (settings
, "test-double", "d", G_MAXDOUBLE
);
315 g_settings_get (settings
, "test-double", "d", &d
);
316 g_assert_cmpfloat (d
, ==, G_MAXDOUBLE
);
318 g_settings_get (settings
, "test-string", "s", &str
);
319 g_assert_cmpstr (str
, ==, "a string, it seems");
323 g_settings_get (settings
, "test-objectpath", "o", &str
);
324 g_assert_cmpstr (str
, ==, "/a/object/path");
325 g_object_unref (settings
);
330 /* Check that we can read an set complex types like
331 * tuples, arrays and dictionaries
334 test_complex_types (void)
339 GVariantIter
*iter
= NULL
;
342 settings
= g_settings_new ("org.gtk.test.complex-types");
344 g_settings_get (settings
, "test-tuple", "(s(ii))", &s
, &i1
, &i2
);
345 g_assert_cmpstr (s
, ==, "one");
346 g_assert_cmpint (i1
,==, 2);
347 g_assert_cmpint (i2
,==, 3);
351 g_settings_set (settings
, "test-tuple", "(s(ii))", "none", 0, 0);
352 g_settings_get (settings
, "test-tuple", "(s(ii))", &s
, &i1
, &i2
);
353 g_assert_cmpstr (s
, ==, "none");
354 g_assert_cmpint (i1
,==, 0);
355 g_assert_cmpint (i2
,==, 0);
359 g_settings_get (settings
, "test-array", "ai", &iter
);
360 g_assert_cmpint (g_variant_iter_n_children (iter
), ==, 6);
361 g_assert (g_variant_iter_next (iter
, "i", &i1
));
362 g_assert_cmpint (i1
, ==, 0);
363 g_assert (g_variant_iter_next (iter
, "i", &i1
));
364 g_assert_cmpint (i1
, ==, 1);
365 g_assert (g_variant_iter_next (iter
, "i", &i1
));
366 g_assert_cmpint (i1
, ==, 2);
367 g_assert (g_variant_iter_next (iter
, "i", &i1
));
368 g_assert_cmpint (i1
, ==, 3);
369 g_assert (g_variant_iter_next (iter
, "i", &i1
));
370 g_assert_cmpint (i1
, ==, 4);
371 g_assert (g_variant_iter_next (iter
, "i", &i1
));
372 g_assert_cmpint (i1
, ==, 5);
373 g_assert (!g_variant_iter_next (iter
, "i", &i1
));
374 g_variant_iter_free (iter
);
376 g_settings_get (settings
, "test-dict", "a{sau}", &iter
);
377 g_assert_cmpint (g_variant_iter_n_children (iter
), ==, 2);
378 g_assert (g_variant_iter_next (iter
, "{&s@au}", &s
, &v
));
379 g_assert_cmpstr (s
, ==, "AC");
380 g_assert_cmpstr ((char *)g_variant_get_type (v
), ==, "au");
382 g_assert (g_variant_iter_next (iter
, "{&s@au}", &s
, &v
));
383 g_assert_cmpstr (s
, ==, "IV");
384 g_assert_cmpstr ((char *)g_variant_get_type (v
), ==, "au");
386 g_variant_iter_free (iter
);
388 v
= g_settings_get_value (settings
, "test-dict");
389 g_assert_cmpstr ((char *)g_variant_get_type (v
), ==, "a{sau}");
392 g_object_unref (settings
);
395 static gboolean changed_cb_called
;
398 changed_cb (GSettings
*settings
,
402 changed_cb_called
= TRUE
;
404 g_assert_cmpstr (key
, ==, data
);
407 /* Test that basic change notification with the changed signal works.
413 GSettings
*settings2
;
415 settings
= g_settings_new ("org.gtk.test");
417 g_signal_connect (settings
, "changed",
418 G_CALLBACK (changed_cb
), "greeting");
420 changed_cb_called
= FALSE
;
422 g_settings_set (settings
, "greeting", "s", "new greeting");
423 g_assert (changed_cb_called
);
425 settings2
= g_settings_new ("org.gtk.test");
427 changed_cb_called
= FALSE
;
429 g_settings_set (settings2
, "greeting", "s", "hi");
430 g_assert (changed_cb_called
);
432 g_object_unref (settings2
);
433 g_object_unref (settings
);
436 static gboolean changed_cb_called2
;
439 changed_cb2 (GSettings
*settings
,
448 /* Test that changes done to a delay-mode instance
449 * don't appear to the outside world until apply. Also
450 * check that we get change notification when they are
452 * Also test that the has-unapplied property is properly
456 test_delay_apply (void)
459 GSettings
*settings2
;
465 settings
= g_settings_new ("org.gtk.test");
466 settings2
= g_settings_new ("org.gtk.test");
468 g_settings_set (settings2
, "greeting", "s", "top o' the morning");
470 changed_cb_called
= FALSE
;
471 changed_cb_called2
= FALSE
;
473 g_signal_connect (settings
, "changed",
474 G_CALLBACK (changed_cb2
), &changed_cb_called
);
475 g_signal_connect (settings2
, "changed",
476 G_CALLBACK (changed_cb2
), &changed_cb_called2
);
478 g_settings_delay (settings
);
480 g_settings_set (settings
, "greeting", "s", "greetings from test_delay_apply");
482 g_assert (changed_cb_called
);
483 g_assert (!changed_cb_called2
);
485 writable
= g_settings_is_writable (settings
, "greeting");
488 g_settings_get (settings
, "greeting", "s", &str
);
489 g_assert_cmpstr (str
, ==, "greetings from test_delay_apply");
493 v
= g_settings_get_user_value (settings
, "greeting");
494 s
= g_variant_get_string (v
, NULL
);
495 g_assert_cmpstr (s
, ==, "greetings from test_delay_apply");
498 g_settings_get (settings2
, "greeting", "s", &str
);
499 g_assert_cmpstr (str
, ==, "top o' the morning");
503 g_assert (g_settings_get_has_unapplied (settings
));
504 g_assert (!g_settings_get_has_unapplied (settings2
));
506 changed_cb_called
= FALSE
;
507 changed_cb_called2
= FALSE
;
509 g_settings_apply (settings
);
511 g_assert (!changed_cb_called
);
512 g_assert (changed_cb_called2
);
514 g_settings_get (settings
, "greeting", "s", &str
);
515 g_assert_cmpstr (str
, ==, "greetings from test_delay_apply");
519 g_settings_get (settings2
, "greeting", "s", &str
);
520 g_assert_cmpstr (str
, ==, "greetings from test_delay_apply");
524 g_assert (!g_settings_get_has_unapplied (settings
));
525 g_assert (!g_settings_get_has_unapplied (settings2
));
527 g_settings_reset (settings
, "greeting");
528 g_settings_apply (settings
);
530 g_settings_get (settings
, "greeting", "s", &str
);
531 g_assert_cmpstr (str
, ==, "Hello, earthlings");
534 g_object_unref (settings2
);
535 g_object_unref (settings
);
538 /* Test that reverting unapplied changes in a delay-apply
539 * settings instance works.
542 test_delay_revert (void)
545 GSettings
*settings2
;
548 settings
= g_settings_new ("org.gtk.test");
549 settings2
= g_settings_new ("org.gtk.test");
551 g_settings_set (settings2
, "greeting", "s", "top o' the morning");
553 g_settings_get (settings
, "greeting", "s", &str
);
554 g_assert_cmpstr (str
, ==, "top o' the morning");
557 g_settings_delay (settings
);
559 g_settings_set (settings
, "greeting", "s", "greetings from test_delay_revert");
561 g_settings_get (settings
, "greeting", "s", &str
);
562 g_assert_cmpstr (str
, ==, "greetings from test_delay_revert");
566 g_settings_get (settings2
, "greeting", "s", &str
);
567 g_assert_cmpstr (str
, ==, "top o' the morning");
571 g_assert (g_settings_get_has_unapplied (settings
));
573 g_settings_revert (settings
);
575 g_assert (!g_settings_get_has_unapplied (settings
));
577 g_settings_get (settings
, "greeting", "s", &str
);
578 g_assert_cmpstr (str
, ==, "top o' the morning");
582 g_settings_get (settings2
, "greeting", "s", &str
);
583 g_assert_cmpstr (str
, ==, "top o' the morning");
587 g_object_unref (settings2
);
588 g_object_unref (settings
);
592 test_delay_child (void)
600 base
= g_settings_new ("org.gtk.test.basic-types");
601 g_settings_set (base
, "test-byte", "y", 36);
603 settings
= g_settings_new ("org.gtk.test");
604 g_settings_delay (settings
);
605 g_object_get (settings
, "delay-apply", &delay
, NULL
);
608 child
= g_settings_get_child (settings
, "basic-types");
609 g_assert (child
!= NULL
);
611 g_object_get (child
, "delay-apply", &delay
, NULL
);
614 g_settings_get (child
, "test-byte", "y", &byte
);
615 g_assert_cmpuint (byte
, ==, 36);
617 g_settings_set (child
, "test-byte", "y", 42);
619 /* make sure the child was delayed too */
620 g_settings_get (base
, "test-byte", "y", &byte
);
621 g_assert_cmpuint (byte
, ==, 36);
623 g_object_unref (child
);
624 g_object_unref (settings
);
625 g_object_unref (base
);
629 keys_changed_cb (GSettings
*settings
,
635 g_assert_cmpint (n_keys
, ==, 2);
637 g_assert ((keys
[0] == g_quark_from_static_string ("greeting") &&
638 keys
[1] == g_quark_from_static_string ("farewell")) ||
639 (keys
[1] == g_quark_from_static_string ("greeting") &&
640 keys
[0] == g_quark_from_static_string ("farewell")));
642 g_settings_get (settings
, "greeting", "s", &str
);
643 g_assert_cmpstr (str
, ==, "greetings from test_atomic");
647 g_settings_get (settings
, "farewell", "s", &str
);
648 g_assert_cmpstr (str
, ==, "atomic bye-bye");
653 /* Check that delay-applied changes appear atomically.
654 * More specifically, verify that all changed keys appear
655 * with their new value while handling the change-event signal.
661 GSettings
*settings2
;
664 settings
= g_settings_new ("org.gtk.test");
665 settings2
= g_settings_new ("org.gtk.test");
667 g_settings_set (settings2
, "greeting", "s", "top o' the morning");
669 changed_cb_called
= FALSE
;
670 changed_cb_called2
= FALSE
;
672 g_signal_connect (settings2
, "change-event",
673 G_CALLBACK (keys_changed_cb
), NULL
);
675 g_settings_delay (settings
);
677 g_settings_set (settings
, "greeting", "s", "greetings from test_atomic");
678 g_settings_set (settings
, "farewell", "s", "atomic bye-bye");
680 g_settings_apply (settings
);
682 g_settings_get (settings
, "greeting", "s", &str
);
683 g_assert_cmpstr (str
, ==, "greetings from test_atomic");
687 g_settings_get (settings
, "farewell", "s", &str
);
688 g_assert_cmpstr (str
, ==, "atomic bye-bye");
692 g_settings_get (settings2
, "greeting", "s", &str
);
693 g_assert_cmpstr (str
, ==, "greetings from test_atomic");
697 g_settings_get (settings2
, "farewell", "s", &str
);
698 g_assert_cmpstr (str
, ==, "atomic bye-bye");
702 g_object_unref (settings2
);
703 g_object_unref (settings
);
706 /* On Windows the interaction between the C library locale and libintl
707 * (from GNU gettext) is not like on POSIX, so just skip these tests
710 * There are several issues:
712 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
713 * in libintl (defined in its <libintl.h>).
715 * 2) The locale names that setlocale() accepts and returns aren't in
716 * the "de_DE" style, but like "German_Germany".
718 * 3) libintl looks at the Win32 thread locale and not the C library
719 * locale. (And even if libintl would use the C library's locale, as
720 * there are several alternative C library DLLs, libintl might be
721 * linked to a different one than the application code, so they
722 * wouldn't have the same C library locale anyway.)
725 /* Test that translations work for schema defaults.
727 * This test relies on the de.po file in the same directory
728 * to be compiled into ./de/LC_MESSAGES/test.mo
737 bindtextdomain ("test", locale_dir
);
738 bind_textdomain_codeset ("test", "UTF-8");
740 locale
= g_strdup (setlocale (LC_MESSAGES
, NULL
));
742 settings
= g_settings_new ("org.gtk.test.localized");
744 setlocale (LC_MESSAGES
, "C");
745 str
= g_settings_get_string (settings
, "error-message");
746 setlocale (LC_MESSAGES
, locale
);
748 g_assert_cmpstr (str
, ==, "Unnamed");
752 setlocale (LC_MESSAGES
, "de_DE");
753 /* Only do the test if translation is actually working... */
754 if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
756 str
= g_settings_get_string (settings
, "error-message");
758 g_assert_cmpstr (str
, ==, "Unbenannt");
763 g_printerr ("warning: translation is not working... skipping test. ");
765 setlocale (LC_MESSAGES
, locale
);
767 g_object_unref (settings
);
770 /* Test that message context works as expected with translated
771 * schema defaults. Also, verify that non-ASCII UTF-8 content
774 * This test relies on the de.po file in the same directory
775 * to be compiled into ./de/LC_MESSAGES/test.mo
778 test_l10n_context (void)
784 bindtextdomain ("test", locale_dir
);
785 bind_textdomain_codeset ("test", "UTF-8");
787 locale
= g_strdup (setlocale (LC_MESSAGES
, NULL
));
789 settings
= g_settings_new ("org.gtk.test.localized");
791 setlocale (LC_MESSAGES
, "C");
792 g_settings_get (settings
, "backspace", "s", &str
);
793 setlocale (LC_MESSAGES
, locale
);
795 g_assert_cmpstr (str
, ==, "BackSpace");
799 setlocale (LC_MESSAGES
, "de_DE");
800 /* Only do the test if translation is actually working... */
801 if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
803 g_settings_get (settings
, "backspace", "s", &str
);
805 g_assert_cmpstr (str
, ==, "Löschen");
810 g_printerr ("warning: translation is not working... skipping test. ");
812 setlocale (LC_MESSAGES
, locale
);
814 g_object_unref (settings
);
840 GObject parent_instance
;
843 gboolean anti_bool_prop
;
854 gchar
*no_write_prop
;
862 GObjectClass parent_class
;
865 static GType
test_object_get_type (void);
866 G_DEFINE_TYPE (TestObject
, test_object
, G_TYPE_OBJECT
)
869 test_object_init (TestObject
*object
)
874 test_object_finalize (GObject
*object
)
876 TestObject
*testo
= (TestObject
*)object
;
877 g_strfreev (testo
->strv_prop
);
878 g_free (testo
->string_prop
);
879 G_OBJECT_CLASS (test_object_parent_class
)->finalize (object
);
883 test_object_get_property (GObject
*object
,
888 TestObject
*test_object
= (TestObject
*)object
;
893 g_value_set_boolean (value
, test_object
->bool_prop
);
896 g_value_set_boolean (value
, test_object
->anti_bool_prop
);
899 g_value_set_schar (value
, test_object
->byte_prop
);
902 g_value_set_uint (value
, test_object
->uint16_prop
);
905 g_value_set_int (value
, test_object
->int16_prop
);
908 g_value_set_int (value
, test_object
->int_prop
);
911 g_value_set_uint (value
, test_object
->uint_prop
);
914 g_value_set_int64 (value
, test_object
->int64_prop
);
917 g_value_set_uint64 (value
, test_object
->uint64_prop
);
920 g_value_set_double (value
, test_object
->double_prop
);
923 g_value_set_string (value
, test_object
->string_prop
);
926 g_value_set_string (value
, test_object
->no_write_prop
);
929 g_value_set_boxed (value
, test_object
->strv_prop
);
932 g_value_set_enum (value
, test_object
->enum_prop
);
935 g_value_set_flags (value
, test_object
->flags_prop
);
938 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
944 test_object_set_property (GObject
*object
,
949 TestObject
*test_object
= (TestObject
*)object
;
954 test_object
->bool_prop
= g_value_get_boolean (value
);
957 test_object
->anti_bool_prop
= g_value_get_boolean (value
);
960 test_object
->byte_prop
= g_value_get_schar (value
);
963 test_object
->int16_prop
= g_value_get_int (value
);
966 test_object
->uint16_prop
= g_value_get_uint (value
);
969 test_object
->int_prop
= g_value_get_int (value
);
972 test_object
->uint_prop
= g_value_get_uint (value
);
975 test_object
->int64_prop
= g_value_get_int64 (value
);
978 test_object
->uint64_prop
= g_value_get_uint64 (value
);
981 test_object
->double_prop
= g_value_get_double (value
);
984 g_free (test_object
->string_prop
);
985 test_object
->string_prop
= g_value_dup_string (value
);
988 g_free (test_object
->no_read_prop
);
989 test_object
->no_read_prop
= g_value_dup_string (value
);
992 g_strfreev (test_object
->strv_prop
);
993 test_object
->strv_prop
= g_value_dup_boxed (value
);
996 test_object
->enum_prop
= g_value_get_enum (value
);
999 test_object
->flags_prop
= g_value_get_flags (value
);
1002 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
1008 test_enum_get_type (void)
1010 static volatile gsize define_type_id
= 0;
1012 if (g_once_init_enter (&define_type_id
))
1014 static const GEnumValue values
[] = {
1015 { TEST_ENUM_FOO
, "TEST_ENUM_FOO", "foo" },
1016 { TEST_ENUM_BAR
, "TEST_ENUM_BAR", "bar" },
1017 { TEST_ENUM_BAZ
, "TEST_ENUM_BAZ", "baz" },
1018 { TEST_ENUM_QUUX
, "TEST_ENUM_QUUX", "quux" },
1022 GType type_id
= g_enum_register_static ("TestEnum", values
);
1023 g_once_init_leave (&define_type_id
, type_id
);
1026 return define_type_id
;
1030 test_flags_get_type (void)
1032 static volatile gsize define_type_id
= 0;
1034 if (g_once_init_enter (&define_type_id
))
1036 static const GFlagsValue values
[] = {
1037 { TEST_FLAGS_NONE
, "TEST_FLAGS_NONE", "none" },
1038 { TEST_FLAGS_MOURNING
, "TEST_FLAGS_MOURNING", "mourning" },
1039 { TEST_FLAGS_LAUGHING
, "TEST_FLAGS_LAUGHING", "laughing" },
1040 { TEST_FLAGS_WALKING
, "TEST_FLAGS_WALKING", "walking" },
1044 GType type_id
= g_flags_register_static ("TestFlags", values
);
1045 g_once_init_leave (&define_type_id
, type_id
);
1048 return define_type_id
;
1052 test_object_class_init (TestObjectClass
*class)
1054 GObjectClass
*gobject_class
= G_OBJECT_CLASS (class);
1056 gobject_class
->get_property
= test_object_get_property
;
1057 gobject_class
->set_property
= test_object_set_property
;
1058 gobject_class
->finalize
= test_object_finalize
;
1060 g_object_class_install_property (gobject_class
, PROP_BOOL
,
1061 g_param_spec_boolean ("bool", "", "", FALSE
, G_PARAM_READWRITE
));
1062 g_object_class_install_property (gobject_class
, PROP_ANTI_BOOL
,
1063 g_param_spec_boolean ("anti-bool", "", "", FALSE
, G_PARAM_READWRITE
));
1064 g_object_class_install_property (gobject_class
, PROP_BYTE
,
1065 g_param_spec_char ("byte", "", "", G_MININT8
, G_MAXINT8
, 0, G_PARAM_READWRITE
));
1066 g_object_class_install_property (gobject_class
, PROP_INT16
,
1067 g_param_spec_int ("int16", "", "", -G_MAXINT16
, G_MAXINT16
, 0, G_PARAM_READWRITE
));
1068 g_object_class_install_property (gobject_class
, PROP_UINT16
,
1069 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16
, 0, G_PARAM_READWRITE
));
1070 g_object_class_install_property (gobject_class
, PROP_INT
,
1071 g_param_spec_int ("int", "", "", G_MININT
, G_MAXINT
, 0, G_PARAM_READWRITE
));
1072 g_object_class_install_property (gobject_class
, PROP_UINT
,
1073 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT
, 0, G_PARAM_READWRITE
));
1074 g_object_class_install_property (gobject_class
, PROP_INT64
,
1075 g_param_spec_int64 ("int64", "", "", G_MININT64
, G_MAXINT64
, 0, G_PARAM_READWRITE
));
1076 g_object_class_install_property (gobject_class
, PROP_UINT64
,
1077 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64
, 0, G_PARAM_READWRITE
));
1078 g_object_class_install_property (gobject_class
, PROP_DOUBLE
,
1079 g_param_spec_double ("double", "", "", -G_MAXDOUBLE
, G_MAXDOUBLE
, 0.0, G_PARAM_READWRITE
));
1080 g_object_class_install_property (gobject_class
, PROP_STRING
,
1081 g_param_spec_string ("string", "", "", NULL
, G_PARAM_READWRITE
));
1082 g_object_class_install_property (gobject_class
, PROP_NO_WRITE
,
1083 g_param_spec_string ("no-write", "", "", NULL
, G_PARAM_READABLE
));
1084 g_object_class_install_property (gobject_class
, PROP_NO_READ
,
1085 g_param_spec_string ("no-read", "", "", NULL
, G_PARAM_WRITABLE
));
1086 g_object_class_install_property (gobject_class
, PROP_STRV
,
1087 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV
, G_PARAM_READWRITE
));
1088 g_object_class_install_property (gobject_class
, PROP_ENUM
,
1089 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO
, G_PARAM_READWRITE
));
1090 g_object_class_install_property (gobject_class
, PROP_FLAGS
,
1091 g_param_spec_flags ("flags", "", "", test_flags_get_type (), TEST_FLAGS_NONE
, G_PARAM_READWRITE
));
1095 test_object_new (void)
1097 return (TestObject
*)g_object_new (test_object_get_type (), NULL
);
1100 /* Test basic binding functionality for simple types.
1101 * Verify that with bidirectional bindings, changes on either side
1102 * are notified on the other end.
1105 test_simple_binding (void)
1108 GSettings
*settings
;
1124 settings
= g_settings_new ("org.gtk.test.binding");
1125 obj
= test_object_new ();
1127 g_settings_bind (settings
, "bool", obj
, "bool", G_SETTINGS_BIND_DEFAULT
);
1128 g_object_set (obj
, "bool", TRUE
, NULL
);
1129 g_assert_cmpint (g_settings_get_boolean (settings
, "bool"), ==, TRUE
);
1131 g_settings_set_boolean (settings
, "bool", FALSE
);
1133 g_object_get (obj
, "bool", &b
, NULL
);
1134 g_assert_cmpint (b
, ==, FALSE
);
1136 g_settings_bind (settings
, "anti-bool", obj
, "anti-bool",
1137 G_SETTINGS_BIND_INVERT_BOOLEAN
);
1138 g_object_set (obj
, "anti-bool", FALSE
, NULL
);
1139 g_assert_cmpint (g_settings_get_boolean (settings
, "anti-bool"), ==, TRUE
);
1141 g_settings_set_boolean (settings
, "anti-bool", FALSE
);
1143 g_object_get (obj
, "anti-bool", &b
, NULL
);
1144 g_assert_cmpint (b
, ==, TRUE
);
1146 g_settings_bind (settings
, "byte", obj
, "byte", G_SETTINGS_BIND_DEFAULT
);
1148 g_object_set (obj
, "byte", 123, NULL
);
1150 g_settings_get (settings
, "byte", "y", &y
);
1151 g_assert_cmpint (y
, ==, 123);
1153 g_settings_set (settings
, "byte", "y", 54);
1155 g_object_get (obj
, "byte", &y
, NULL
);
1156 g_assert_cmpint (y
, ==, 54);
1158 g_settings_bind (settings
, "int16", obj
, "int16", G_SETTINGS_BIND_DEFAULT
);
1160 g_object_set (obj
, "int16", 1234, NULL
);
1162 g_settings_get (settings
, "int16", "n", &n
);
1163 g_assert_cmpint (n
, ==, 1234);
1165 g_settings_set (settings
, "int16", "n", 4321);
1167 g_object_get (obj
, "int16", &n2
, NULL
);
1168 g_assert_cmpint (n2
, ==, 4321);
1170 g_settings_bind (settings
, "uint16", obj
, "uint16", G_SETTINGS_BIND_DEFAULT
);
1172 g_object_set (obj
, "uint16", (guint16
) G_MAXUINT16
, NULL
);
1174 g_settings_get (settings
, "uint16", "q", &q
);
1175 g_assert_cmpuint (q
, ==, G_MAXUINT16
);
1177 g_settings_set (settings
, "uint16", "q", (guint16
) G_MAXINT16
);
1179 g_object_get (obj
, "uint16", &q2
, NULL
);
1180 g_assert_cmpuint (q2
, ==, (guint16
) G_MAXINT16
);
1182 g_settings_bind (settings
, "int", obj
, "int", G_SETTINGS_BIND_DEFAULT
);
1184 g_object_set (obj
, "int", 12345, NULL
);
1185 g_assert_cmpint (g_settings_get_int (settings
, "int"), ==, 12345);
1187 g_settings_set_int (settings
, "int", 54321);
1189 g_object_get (obj
, "int", &i
, NULL
);
1190 g_assert_cmpint (i
, ==, 54321);
1192 g_settings_bind (settings
, "uint", obj
, "uint", G_SETTINGS_BIND_DEFAULT
);
1194 g_object_set (obj
, "uint", 12345, NULL
);
1195 g_assert_cmpuint (g_settings_get_uint (settings
, "uint"), ==, 12345);
1197 g_settings_set_uint (settings
, "uint", 54321);
1199 g_object_get (obj
, "uint", &u
, NULL
);
1200 g_assert_cmpuint (u
, ==, 54321);
1202 g_settings_bind (settings
, "uint64", obj
, "uint64", G_SETTINGS_BIND_DEFAULT
);
1204 g_object_set (obj
, "uint64", (guint64
) 12345, NULL
);
1205 g_assert_cmpuint (g_settings_get_uint64 (settings
, "uint64"), ==, 12345);
1207 g_settings_set_uint64 (settings
, "uint64", 54321);
1209 g_object_get (obj
, "uint64", &u64
, NULL
);
1210 g_assert_cmpuint (u64
, ==, 54321);
1212 g_settings_bind (settings
, "int64", obj
, "int64", G_SETTINGS_BIND_DEFAULT
);
1214 g_object_set (obj
, "int64", (gint64
) G_MAXINT64
, NULL
);
1216 g_settings_get (settings
, "int64", "x", &i64
);
1217 g_assert_cmpint (i64
, ==, G_MAXINT64
);
1219 g_settings_set (settings
, "int64", "x", (gint64
) G_MININT64
);
1221 g_object_get (obj
, "int64", &i64
, NULL
);
1222 g_assert_cmpint (i64
, ==, G_MININT64
);
1224 g_settings_bind (settings
, "uint64", obj
, "uint64", G_SETTINGS_BIND_DEFAULT
);
1226 g_object_set (obj
, "uint64", (guint64
) G_MAXUINT64
, NULL
);
1228 g_settings_get (settings
, "uint64", "t", &u64
);
1229 g_assert_cmpuint (u64
, ==, G_MAXUINT64
);
1231 g_settings_set (settings
, "uint64", "t", (guint64
) G_MAXINT64
);
1233 g_object_get (obj
, "uint64", &u64
, NULL
);
1234 g_assert_cmpuint (u64
, ==, (guint64
) G_MAXINT64
);
1236 g_settings_bind (settings
, "string", obj
, "string", G_SETTINGS_BIND_DEFAULT
);
1238 g_object_set (obj
, "string", "bu ba", NULL
);
1239 s
= g_settings_get_string (settings
, "string");
1240 g_assert_cmpstr (s
, ==, "bu ba");
1243 g_settings_set_string (settings
, "string", "bla bla");
1244 g_object_get (obj
, "string", &s
, NULL
);
1245 g_assert_cmpstr (s
, ==, "bla bla");
1248 g_settings_bind (settings
, "chararray", obj
, "string", G_SETTINGS_BIND_DEFAULT
);
1250 g_object_set (obj
, "string", "non-unicode:\315", NULL
);
1251 value
= g_settings_get_value (settings
, "chararray");
1252 g_assert_cmpstr (g_variant_get_bytestring (value
), ==, "non-unicode:\315");
1253 g_variant_unref (value
);
1255 g_settings_bind (settings
, "double", obj
, "double", G_SETTINGS_BIND_DEFAULT
);
1257 g_object_set (obj
, "double", G_MAXFLOAT
, NULL
);
1258 g_assert_cmpfloat (g_settings_get_double (settings
, "double"), ==, G_MAXFLOAT
);
1260 g_settings_set_double (settings
, "double", G_MINFLOAT
);
1262 g_object_get (obj
, "double", &d
, NULL
);
1263 g_assert_cmpfloat (d
, ==, G_MINFLOAT
);
1265 g_object_set (obj
, "double", G_MAXDOUBLE
, NULL
);
1266 g_assert_cmpfloat (g_settings_get_double (settings
, "double"), ==, G_MAXDOUBLE
);
1268 g_settings_set_double (settings
, "double", -G_MINDOUBLE
);
1270 g_object_get (obj
, "double", &d
, NULL
);
1271 g_assert_cmpfloat (d
, ==, -G_MINDOUBLE
);
1273 strv
= g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1274 g_settings_bind (settings
, "strv", obj
, "strv", G_SETTINGS_BIND_DEFAULT
);
1275 g_object_set (obj
, "strv", strv
, NULL
);
1277 strv
= g_settings_get_strv (settings
, "strv");
1278 s
= g_strjoinv (",", strv
);
1279 g_assert_cmpstr (s
, ==, "plastic bag,middle class,polyethylene");
1282 strv
= g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1283 g_settings_set_strv (settings
, "strv", (const gchar
**) strv
);
1285 g_object_get (obj
, "strv", &strv
, NULL
);
1286 s
= g_strjoinv (",", strv
);
1287 g_assert_cmpstr (s
, ==, "decaffeinate,unleaded,keep all surfaces clean");
1290 g_settings_set_strv (settings
, "strv", NULL
);
1291 g_object_get (obj
, "strv", &strv
, NULL
);
1292 g_assert (strv
!= NULL
);
1293 g_assert_cmpint (g_strv_length (strv
), ==, 0);
1296 g_settings_bind (settings
, "enum", obj
, "enum", G_SETTINGS_BIND_DEFAULT
);
1297 g_object_set (obj
, "enum", TEST_ENUM_BAZ
, NULL
);
1298 s
= g_settings_get_string (settings
, "enum");
1299 g_assert_cmpstr (s
, ==, "baz");
1301 g_assert_cmpint (g_settings_get_enum (settings
, "enum"), ==, TEST_ENUM_BAZ
);
1303 g_settings_set_enum (settings
, "enum", TEST_ENUM_QUUX
);
1305 g_object_get (obj
, "enum", &i
, NULL
);
1306 g_assert_cmpint (i
, ==, TEST_ENUM_QUUX
);
1308 g_settings_set_string (settings
, "enum", "baz");
1310 g_object_get (obj
, "enum", &i
, NULL
);
1311 g_assert_cmpint (i
, ==, TEST_ENUM_BAZ
);
1313 g_settings_bind (settings
, "flags", obj
, "flags", G_SETTINGS_BIND_DEFAULT
);
1314 g_object_set (obj
, "flags", TEST_FLAGS_MOURNING
, NULL
);
1315 strv
= g_settings_get_strv (settings
, "flags");
1316 g_assert_cmpint (g_strv_length (strv
), ==, 1);
1317 g_assert_cmpstr (strv
[0], ==, "mourning");
1320 g_assert_cmpint (g_settings_get_flags (settings
, "flags"), ==, TEST_FLAGS_MOURNING
);
1322 g_settings_set_flags (settings
, "flags", TEST_FLAGS_MOURNING
| TEST_FLAGS_WALKING
);
1324 g_object_get (obj
, "flags", &i
, NULL
);
1325 g_assert_cmpint (i
, ==, TEST_FLAGS_MOURNING
| TEST_FLAGS_WALKING
);
1327 g_object_unref (obj
);
1328 g_object_unref (settings
);
1335 GSettings
*settings
;
1337 settings
= g_settings_new ("org.gtk.test.binding");
1338 obj
= test_object_new ();
1340 g_settings_bind (settings
, "int", obj
, "int", G_SETTINGS_BIND_DEFAULT
);
1342 g_object_set (obj
, "int", 12345, NULL
);
1343 g_assert_cmpint (g_settings_get_int (settings
, "int"), ==, 12345);
1345 g_settings_unbind (obj
, "int");
1347 g_object_set (obj
, "int", 54321, NULL
);
1348 g_assert_cmpint (g_settings_get_int (settings
, "int"), ==, 12345);
1350 g_object_unref (obj
);
1351 g_object_unref (settings
);
1355 test_bind_writable (void)
1358 GSettings
*settings
;
1361 settings
= g_settings_new ("org.gtk.test.binding");
1362 obj
= test_object_new ();
1364 g_object_set (obj
, "bool", FALSE
, NULL
);
1366 g_settings_bind_writable (settings
, "int", obj
, "bool", FALSE
);
1368 g_object_get (obj
, "bool", &b
, NULL
);
1371 g_settings_unbind (obj
, "bool");
1373 g_settings_bind_writable (settings
, "int", obj
, "bool", TRUE
);
1375 g_object_get (obj
, "bool", &b
, NULL
);
1378 g_object_unref (obj
);
1379 g_object_unref (settings
);
1382 /* Test one-way bindings.
1383 * Verify that changes on one side show up on the other,
1384 * but not vice versa
1387 test_directional_binding (void)
1390 GSettings
*settings
;
1394 settings
= g_settings_new ("org.gtk.test.binding");
1395 obj
= test_object_new ();
1397 g_object_set (obj
, "bool", FALSE
, NULL
);
1398 g_settings_set_boolean (settings
, "bool", FALSE
);
1400 g_settings_bind (settings
, "bool", obj
, "bool", G_SETTINGS_BIND_GET
);
1402 g_settings_set_boolean (settings
, "bool", TRUE
);
1403 g_object_get (obj
, "bool", &b
, NULL
);
1404 g_assert_cmpint (b
, ==, TRUE
);
1406 g_object_set (obj
, "bool", FALSE
, NULL
);
1407 g_assert_cmpint (g_settings_get_boolean (settings
, "bool"), ==, TRUE
);
1409 g_object_set (obj
, "int", 20, NULL
);
1410 g_settings_set_int (settings
, "int", 20);
1412 g_settings_bind (settings
, "int", obj
, "int", G_SETTINGS_BIND_SET
);
1414 g_object_set (obj
, "int", 32, NULL
);
1415 g_assert_cmpint (g_settings_get_int (settings
, "int"), ==, 32);
1417 g_settings_set_int (settings
, "int", 20);
1418 g_object_get (obj
, "int", &i
, NULL
);
1419 g_assert_cmpint (i
, ==, 32);
1421 g_object_unref (obj
);
1422 g_object_unref (settings
);
1425 /* Test that type mismatch is caught when creating a binding */
1427 test_typesafe_binding (void)
1429 if (!g_test_undefined ())
1432 if (g_test_subprocess ())
1435 GSettings
*settings
;
1437 settings
= g_settings_new ("org.gtk.test.binding");
1438 obj
= test_object_new ();
1440 g_settings_bind (settings
, "string", obj
, "int", G_SETTINGS_BIND_DEFAULT
);
1442 g_object_unref (obj
);
1443 g_object_unref (settings
);
1446 g_test_trap_subprocess (NULL
, 0, 0);
1447 g_test_trap_assert_failed ();
1448 g_test_trap_assert_stderr ("*not compatible*");
1452 string_to_bool (GValue
*value
,
1458 s
= g_variant_get_string (variant
, NULL
);
1459 g_value_set_boolean (value
, g_strcmp0 (s
, "true") == 0);
1465 bool_to_string (const GValue
*value
,
1466 const GVariantType
*expected_type
,
1469 if (g_value_get_boolean (value
))
1470 return g_variant_new_string ("true");
1472 return g_variant_new_string ("false");
1475 /* Test custom bindings.
1476 * Translate strings to booleans and back
1479 test_custom_binding (void)
1482 GSettings
*settings
;
1486 settings
= g_settings_new ("org.gtk.test.binding");
1487 obj
= test_object_new ();
1489 g_settings_set_string (settings
, "string", "true");
1491 g_settings_bind_with_mapping (settings
, "string",
1493 G_SETTINGS_BIND_DEFAULT
,
1498 g_settings_set_string (settings
, "string", "false");
1499 g_object_get (obj
, "bool", &b
, NULL
);
1500 g_assert_cmpint (b
, ==, FALSE
);
1502 g_settings_set_string (settings
, "string", "not true");
1503 g_object_get (obj
, "bool", &b
, NULL
);
1504 g_assert_cmpint (b
, ==, FALSE
);
1506 g_object_set (obj
, "bool", TRUE
, NULL
);
1507 s
= g_settings_get_string (settings
, "string");
1508 g_assert_cmpstr (s
, ==, "true");
1511 g_object_unref (obj
);
1512 g_object_unref (settings
);
1515 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1516 * initial settings value is transported to the object
1517 * side, but later settings changes do not affect the
1521 test_no_change_binding (void)
1524 GSettings
*settings
;
1527 settings
= g_settings_new ("org.gtk.test.binding");
1528 obj
= test_object_new ();
1530 g_object_set (obj
, "bool", TRUE
, NULL
);
1531 g_settings_set_boolean (settings
, "bool", FALSE
);
1533 g_settings_bind (settings
, "bool", obj
, "bool", G_SETTINGS_BIND_GET_NO_CHANGES
);
1535 g_object_get (obj
, "bool", &b
, NULL
);
1536 g_assert_cmpint (b
, ==, FALSE
);
1538 g_settings_set_boolean (settings
, "bool", TRUE
);
1539 g_object_get (obj
, "bool", &b
, NULL
);
1540 g_assert_cmpint (b
, ==, FALSE
);
1542 g_settings_set_boolean (settings
, "bool", FALSE
);
1543 g_object_set (obj
, "bool", TRUE
, NULL
);
1544 b
= g_settings_get_boolean (settings
, "bool");
1545 g_assert_cmpint (b
, ==, TRUE
);
1547 g_object_unref (obj
);
1548 g_object_unref (settings
);
1551 /* Test that binding a non-readable property only
1552 * works in 'GET' mode.
1555 test_no_read_binding_fail (void)
1558 GSettings
*settings
;
1560 settings
= g_settings_new ("org.gtk.test.binding");
1561 obj
= test_object_new ();
1563 g_settings_bind (settings
, "string", obj
, "no-read", 0);
1567 test_no_read_binding_pass (void)
1570 GSettings
*settings
;
1572 settings
= g_settings_new ("org.gtk.test.binding");
1573 obj
= test_object_new ();
1575 g_settings_bind (settings
, "string", obj
, "no-read", G_SETTINGS_BIND_GET
);
1581 test_no_read_binding (void)
1583 if (g_test_undefined ())
1585 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/fail", 0, 0);
1586 g_test_trap_assert_failed ();
1587 g_test_trap_assert_stderr ("*property*is not readable*");
1590 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/pass", 0, 0);
1591 g_test_trap_assert_passed ();
1594 /* Test that binding a non-writable property only
1595 * works in 'SET' mode.
1598 test_no_write_binding_fail (void)
1601 GSettings
*settings
;
1603 settings
= g_settings_new ("org.gtk.test.binding");
1604 obj
= test_object_new ();
1606 g_settings_bind (settings
, "string", obj
, "no-write", 0);
1610 test_no_write_binding_pass (void)
1613 GSettings
*settings
;
1615 settings
= g_settings_new ("org.gtk.test.binding");
1616 obj
= test_object_new ();
1618 g_settings_bind (settings
, "string", obj
, "no-write", G_SETTINGS_BIND_SET
);
1624 test_no_write_binding (void)
1626 if (g_test_undefined ())
1628 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/fail", 0, 0);
1629 g_test_trap_assert_failed ();
1630 g_test_trap_assert_stderr ("*property*is not writable*");
1633 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/pass", 0, 0);
1634 g_test_trap_assert_passed ();
1638 key_changed_cb (GSettings
*settings
, const gchar
*key
, gpointer data
)
1645 * Test that using a keyfile works
1650 GSettingsBackend
*kf_backend
;
1651 GSettings
*settings
;
1655 GError
*error
= NULL
;
1658 gboolean called
= FALSE
;
1660 g_remove ("keyfile/gsettings.store");
1661 g_rmdir ("keyfile");
1663 kf_backend
= g_keyfile_settings_backend_new ("keyfile/gsettings.store", "/", "root");
1664 settings
= g_settings_new_with_backend ("org.gtk.test", kf_backend
);
1665 g_object_unref (kf_backend
);
1667 g_settings_reset (settings
, "greeting");
1668 str
= g_settings_get_string (settings
, "greeting");
1669 g_assert_cmpstr (str
, ==, "Hello, earthlings");
1672 writable
= g_settings_is_writable (settings
, "greeting");
1673 g_assert (writable
);
1674 g_settings_set (settings
, "greeting", "s", "see if this works");
1676 str
= g_settings_get_string (settings
, "greeting");
1677 g_assert_cmpstr (str
, ==, "see if this works");
1680 g_settings_delay (settings
);
1681 g_settings_set (settings
, "farewell", "s", "cheerio");
1682 g_settings_apply (settings
);
1684 keyfile
= g_key_file_new ();
1685 g_assert (g_key_file_load_from_file (keyfile
, "keyfile/gsettings.store", 0, NULL
));
1687 str
= g_key_file_get_string (keyfile
, "tests", "greeting", NULL
);
1688 g_assert_cmpstr (str
, ==, "'see if this works'");
1691 str
= g_key_file_get_string (keyfile
, "tests", "farewell", NULL
);
1692 g_assert_cmpstr (str
, ==, "'cheerio'");
1694 g_key_file_free (keyfile
);
1696 g_settings_reset (settings
, "greeting");
1697 g_settings_apply (settings
);
1698 keyfile
= g_key_file_new ();
1699 g_assert (g_key_file_load_from_file (keyfile
, "keyfile/gsettings.store", 0, NULL
));
1701 str
= g_key_file_get_string (keyfile
, "tests", "greeting", NULL
);
1702 g_assert (str
== NULL
);
1705 g_signal_connect (settings
, "changed::greeting", G_CALLBACK (key_changed_cb
), &called
);
1707 g_key_file_set_string (keyfile
, "tests", "greeting", "'howdy'");
1708 data
= g_key_file_to_data (keyfile
, &len
, NULL
);
1709 g_file_set_contents ("keyfile/gsettings.store", data
, len
, &error
);
1710 g_assert_no_error (error
);
1712 g_main_context_iteration (NULL
, FALSE
);
1713 g_signal_handlers_disconnect_by_func (settings
, key_changed_cb
, &called
);
1715 str
= g_settings_get_string (settings
, "greeting");
1716 g_assert_cmpstr (str
, ==, "howdy");
1719 g_settings_set (settings
, "farewell", "s", "cheerio");
1722 g_signal_connect (settings
, "writable-changed::greeting", G_CALLBACK (key_changed_cb
), &called
);
1724 g_chmod ("keyfile", 0500);
1726 g_main_context_iteration (NULL
, FALSE
);
1727 g_signal_handlers_disconnect_by_func (settings
, key_changed_cb
, &called
);
1729 writable
= g_settings_is_writable (settings
, "greeting");
1730 g_assert (!writable
);
1732 g_key_file_free (keyfile
);
1735 g_object_unref (settings
);
1736 g_chmod ("keyfile", 0777);
1739 /* Test that getting child schemas works
1742 test_child_schema (void)
1744 GSettings
*settings
;
1748 /* first establish some known conditions */
1749 settings
= g_settings_new ("org.gtk.test.basic-types");
1750 g_settings_set (settings
, "test-byte", "y", 36);
1752 g_settings_get (settings
, "test-byte", "y", &byte
);
1753 g_assert_cmpint (byte
, ==, 36);
1755 g_object_unref (settings
);
1757 settings
= g_settings_new ("org.gtk.test");
1758 child
= g_settings_get_child (settings
, "basic-types");
1759 g_assert (child
!= NULL
);
1761 g_settings_get (child
, "test-byte", "y", &byte
);
1762 g_assert_cmpint (byte
, ==, 36);
1764 g_object_unref (child
);
1765 g_object_unref (settings
);
1768 #include "../strinfo.c"
1773 /* "foo" has a value of 1
1774 * "bar" has a value of 2
1775 * "baz" is an alias for "bar"
1778 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1779 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1781 const guint32
*strinfo
= (guint32
*) array
;
1782 guint length
= sizeof array
/ 4;
1786 /* build it and compare */
1789 builder
= g_string_new (NULL
);
1790 strinfo_builder_append_item (builder
, "foo", 1);
1791 strinfo_builder_append_item (builder
, "bar", 2);
1792 g_assert (strinfo_builder_append_alias (builder
, "baz", "bar"));
1793 g_assert_cmpmem (builder
->str
, builder
->len
, strinfo
, length
* 4);
1794 g_string_free (builder
, TRUE
);
1797 g_assert_cmpstr (strinfo_string_from_alias (strinfo
, length
, "foo"),
1799 g_assert_cmpstr (strinfo_string_from_alias (strinfo
, length
, "bar"),
1801 g_assert_cmpstr (strinfo_string_from_alias (strinfo
, length
, "baz"),
1803 g_assert_cmpstr (strinfo_string_from_alias (strinfo
, length
, "quux"),
1806 g_assert (strinfo_enum_from_string (strinfo
, length
, "foo", &result
));
1807 g_assert_cmpint (result
, ==, 1);
1808 g_assert (strinfo_enum_from_string (strinfo
, length
, "bar", &result
));
1809 g_assert_cmpint (result
, ==, 2);
1810 g_assert (!strinfo_enum_from_string (strinfo
, length
, "baz", &result
));
1811 g_assert (!strinfo_enum_from_string (strinfo
, length
, "quux", &result
));
1813 g_assert_cmpstr (strinfo_string_from_enum (strinfo
, length
, 0), ==, NULL
);
1814 g_assert_cmpstr (strinfo_string_from_enum (strinfo
, length
, 1), ==, "foo");
1815 g_assert_cmpstr (strinfo_string_from_enum (strinfo
, length
, 2), ==, "bar");
1816 g_assert_cmpstr (strinfo_string_from_enum (strinfo
, length
, 3), ==, NULL
);
1818 g_assert (strinfo_is_string_valid (strinfo
, length
, "foo"));
1819 g_assert (strinfo_is_string_valid (strinfo
, length
, "bar"));
1820 g_assert (!strinfo_is_string_valid (strinfo
, length
, "baz"));
1821 g_assert (!strinfo_is_string_valid (strinfo
, length
, "quux"));
1825 test_enums_non_enum_key (void)
1829 direct
= g_settings_new ("org.gtk.test.enums.direct");
1830 g_settings_get_enum (direct
, "test");
1831 g_assert_not_reached ();
1835 test_enums_non_enum_value (void)
1837 GSettings
*settings
;
1839 settings
= g_settings_new ("org.gtk.test.enums");
1840 g_settings_set_enum (settings
, "test", 42);
1841 g_assert_not_reached ();
1845 test_enums_range (void)
1847 GSettings
*settings
;
1849 settings
= g_settings_new ("org.gtk.test.enums");
1850 g_settings_set_string (settings
, "test", "qux");
1851 g_assert_not_reached ();
1855 test_enums_non_flags (void)
1857 GSettings
*settings
;
1859 settings
= g_settings_new ("org.gtk.test.enums");
1860 g_settings_get_flags (settings
, "test");
1861 g_assert_not_reached ();
1867 GSettings
*settings
, *direct
;
1870 settings
= g_settings_new ("org.gtk.test.enums");
1871 direct
= g_settings_new ("org.gtk.test.enums.direct");
1873 if (g_test_undefined () && !backend_set
)
1875 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-key", 0, 0);
1876 g_test_trap_assert_failed ();
1877 g_test_trap_assert_stderr ("*not associated with an enum*");
1879 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-value", 0, 0);
1880 g_test_trap_assert_failed ();
1881 g_test_trap_assert_stderr ("*invalid enum value 42*");
1883 g_test_trap_subprocess ("/gsettings/enums/subprocess/range", 0, 0);
1884 g_test_trap_assert_failed ();
1885 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1887 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-flags", 0, 0);
1888 g_test_trap_assert_failed ();
1889 g_test_trap_assert_stderr ("*not associated with a flags*");
1892 str
= g_settings_get_string (settings
, "test");
1893 g_assert_cmpstr (str
, ==, "bar");
1896 g_settings_set_enum (settings
, "test", TEST_ENUM_FOO
);
1898 str
= g_settings_get_string (settings
, "test");
1899 g_assert_cmpstr (str
, ==, "foo");
1902 g_assert_cmpint (g_settings_get_enum (settings
, "test"), ==, TEST_ENUM_FOO
);
1904 g_settings_set_string (direct
, "test", "qux");
1906 str
= g_settings_get_string (direct
, "test");
1907 g_assert_cmpstr (str
, ==, "qux");
1910 str
= g_settings_get_string (settings
, "test");
1911 g_assert_cmpstr (str
, ==, "quux");
1914 g_assert_cmpint (g_settings_get_enum (settings
, "test"), ==, TEST_ENUM_QUUX
);
1916 g_object_unref (direct
);
1917 g_object_unref (settings
);
1921 test_flags_non_flags_key (void)
1925 direct
= g_settings_new ("org.gtk.test.enums.direct");
1926 g_settings_get_flags (direct
, "test");
1927 g_assert_not_reached ();
1931 test_flags_non_flags_value (void)
1933 GSettings
*settings
;
1935 settings
= g_settings_new ("org.gtk.test.enums");
1936 g_settings_set_flags (settings
, "f-test", 0x42);
1937 g_assert_not_reached ();
1941 test_flags_range (void)
1943 GSettings
*settings
;
1945 settings
= g_settings_new ("org.gtk.test.enums");
1946 g_settings_set_strv (settings
, "f-test",
1947 (const gchar
**) g_strsplit ("rock", ",", 0));
1948 g_assert_not_reached ();
1952 test_flags_non_enum (void)
1954 GSettings
*settings
;
1956 settings
= g_settings_new ("org.gtk.test.enums");
1957 g_settings_get_enum (settings
, "f-test");
1958 g_assert_not_reached ();
1964 GSettings
*settings
, *direct
;
1968 settings
= g_settings_new ("org.gtk.test.enums");
1969 direct
= g_settings_new ("org.gtk.test.enums.direct");
1971 if (g_test_undefined () && !backend_set
)
1973 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-key", 0, 0);
1974 g_test_trap_assert_failed ();
1975 g_test_trap_assert_stderr ("*not associated with a flags*");
1977 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-value", 0, 0);
1978 g_test_trap_assert_failed ();
1979 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
1981 g_test_trap_subprocess ("/gsettings/flags/subprocess/range", 0, 0);
1982 g_test_trap_assert_failed ();
1983 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1985 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-enum", 0, 0);
1986 g_test_trap_assert_failed ();
1987 g_test_trap_assert_stderr ("*not associated with an enum*");
1990 strv
= g_settings_get_strv (settings
, "f-test");
1991 str
= g_strjoinv (",", strv
);
1992 g_assert_cmpstr (str
, ==, "");
1996 g_settings_set_flags (settings
, "f-test",
1997 TEST_FLAGS_WALKING
| TEST_FLAGS_TALKING
);
1999 strv
= g_settings_get_strv (settings
, "f-test");
2000 str
= g_strjoinv (",", strv
);
2001 g_assert_cmpstr (str
, ==, "talking,walking");
2005 g_assert_cmpint (g_settings_get_flags (settings
, "f-test"), ==,
2006 TEST_FLAGS_WALKING
| TEST_FLAGS_TALKING
);
2008 strv
= g_strsplit ("speaking,laughing", ",", 0);
2009 g_settings_set_strv (direct
, "f-test", (const gchar
**) strv
);
2012 strv
= g_settings_get_strv (direct
, "f-test");
2013 str
= g_strjoinv (",", strv
);
2014 g_assert_cmpstr (str
, ==, "speaking,laughing");
2018 strv
= g_settings_get_strv (settings
, "f-test");
2019 str
= g_strjoinv (",", strv
);
2020 g_assert_cmpstr (str
, ==, "talking,laughing");
2024 g_assert_cmpint (g_settings_get_flags (settings
, "f-test"), ==,
2025 TEST_FLAGS_TALKING
| TEST_FLAGS_LAUGHING
);
2027 g_object_unref (direct
);
2028 g_object_unref (settings
);
2032 test_range_high (void)
2034 GSettings
*settings
;
2036 settings
= g_settings_new ("org.gtk.test.range");
2037 g_settings_set_int (settings
, "val", 45);
2038 g_assert_not_reached ();
2042 test_range_low (void)
2044 GSettings
*settings
;
2046 settings
= g_settings_new ("org.gtk.test.range");
2047 g_settings_set_int (settings
, "val", 1);
2048 g_assert_not_reached ();
2054 GSettings
*settings
, *direct
;
2057 settings
= g_settings_new ("org.gtk.test.range");
2058 direct
= g_settings_new ("org.gtk.test.range.direct");
2060 if (g_test_undefined () && !backend_set
)
2062 g_test_trap_subprocess ("/gsettings/range/subprocess/high", 0, 0);
2063 g_test_trap_assert_failed ();
2064 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2066 g_test_trap_subprocess ("/gsettings/range/subprocess/low", 0, 0);
2067 g_test_trap_assert_failed ();
2068 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2071 g_assert_cmpint (g_settings_get_int (settings
, "val"), ==, 33);
2072 g_settings_set_int (direct
, "val", 22);
2073 g_assert_cmpint (g_settings_get_int (direct
, "val"), ==, 22);
2074 g_assert_cmpint (g_settings_get_int (settings
, "val"), ==, 22);
2075 g_settings_set_int (direct
, "val", 45);
2076 g_assert_cmpint (g_settings_get_int (direct
, "val"), ==, 45);
2077 g_assert_cmpint (g_settings_get_int (settings
, "val"), ==, 33);
2078 g_settings_set_int (direct
, "val", 1);
2079 g_assert_cmpint (g_settings_get_int (direct
, "val"), ==, 1);
2080 g_assert_cmpint (g_settings_get_int (settings
, "val"), ==, 33);
2082 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2083 value
= g_variant_new_int32 (1);
2084 g_assert (!g_settings_range_check (settings
, "val", value
));
2085 g_variant_unref (value
);
2086 value
= g_variant_new_int32 (33);
2087 g_assert (g_settings_range_check (settings
, "val", value
));
2088 g_variant_unref (value
);
2089 value
= g_variant_new_int32 (45);
2090 g_assert (!g_settings_range_check (settings
, "val", value
));
2091 g_variant_unref (value
);
2092 G_GNUC_END_IGNORE_DEPRECATIONS
2094 g_object_unref (direct
);
2095 g_object_unref (settings
);
2099 strv_has_string (gchar
**haystack
,
2100 const gchar
*needle
)
2104 for (n
= 0; haystack
!= NULL
&& haystack
[n
] != NULL
; n
++)
2106 if (g_strcmp0 (haystack
[n
], needle
) == 0)
2113 strv_set_equal (gchar
**strv
, ...)
2122 va_start (list
, strv
);
2125 str
= va_arg (list
, const gchar
*);
2128 if (!strv_has_string (strv
, str
))
2138 res
= g_strv_length ((gchar
**)strv
) == count
;
2144 test_list_items (void)
2146 GSettingsSchema
*schema
;
2147 GSettings
*settings
;
2151 settings
= g_settings_new ("org.gtk.test");
2152 g_object_get (settings
, "settings-schema", &schema
, NULL
);
2153 children
= g_settings_list_children (settings
);
2154 keys
= g_settings_schema_list_keys (schema
);
2156 g_assert (strv_set_equal (children
, "basic-types", "complex-types", "localized", NULL
));
2157 g_assert (strv_set_equal (keys
, "greeting", "farewell", NULL
));
2159 g_strfreev (children
);
2162 g_settings_schema_unref (schema
);
2163 g_object_unref (settings
);
2167 test_list_schemas (void)
2169 const gchar
* const *schemas
;
2170 const gchar
* const *relocs
;
2172 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2173 relocs
= g_settings_list_relocatable_schemas ();
2174 schemas
= g_settings_list_schemas ();
2175 G_GNUC_END_IGNORE_DEPRECATIONS
2177 g_assert (strv_set_equal ((gchar
**)relocs
,
2178 "org.gtk.test.no-path",
2179 "org.gtk.test.extends.base",
2180 "org.gtk.test.extends.extended",
2183 g_assert (strv_set_equal ((gchar
**)schemas
,
2185 "org.gtk.test.basic-types",
2186 "org.gtk.test.complex-types",
2187 "org.gtk.test.localized",
2188 "org.gtk.test.binding",
2189 "org.gtk.test.enums",
2190 "org.gtk.test.enums.direct",
2191 "org.gtk.test.range",
2192 "org.gtk.test.range.direct",
2193 "org.gtk.test.mapped",
2194 "org.gtk.test.descriptions",
2199 map_func (GVariant
*value
,
2203 gint
*state
= user_data
;
2207 v
= g_variant_get_int32 (value
);
2213 g_assert_cmpint (v
, ==, 1);
2217 else if (*state
== 1)
2219 g_assert_cmpint (v
, ==, 0);
2225 g_assert (value
== NULL
);
2226 *result
= g_variant_new_int32 (5);
2232 test_get_mapped (void)
2234 GSettings
*settings
;
2239 settings
= g_settings_new ("org.gtk.test.mapped");
2240 g_settings_set_int (settings
, "val", 1);
2243 p
= g_settings_get_mapped (settings
, "val", map_func
, &state
);
2244 val
= g_variant_get_int32 ((GVariant
*)p
);
2245 g_assert_cmpint (val
, ==, 5);
2247 g_variant_unref (p
);
2248 g_object_unref (settings
);
2252 test_get_range (void)
2254 GSettings
*settings
;
2257 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2258 settings
= g_settings_new ("org.gtk.test.range");
2259 range
= g_settings_get_range (settings
, "val");
2260 check_and_free (range
, "('range', <(2, 44)>)");
2261 g_object_unref (settings
);
2263 settings
= g_settings_new ("org.gtk.test.enums");
2264 range
= g_settings_get_range (settings
, "test");
2265 check_and_free (range
, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
2266 g_object_unref (settings
);
2268 settings
= g_settings_new ("org.gtk.test.enums");
2269 range
= g_settings_get_range (settings
, "f-test");
2270 check_and_free (range
, "('flags', "
2271 "<['mourning', 'laughing', 'talking', 'walking']>)");
2272 g_object_unref (settings
);
2274 settings
= g_settings_new ("org.gtk.test");
2275 range
= g_settings_get_range (settings
, "greeting");
2276 check_and_free (range
, "('type', <@as []>)");
2277 g_object_unref (settings
);
2278 G_GNUC_END_IGNORE_DEPRECATIONS
2282 test_schema_source (void)
2284 GSettingsSchemaSource
*parent
;
2285 GSettingsSchemaSource
*source
;
2286 GSettingsBackend
*backend
;
2287 GSettingsSchema
*schema
;
2288 GError
*error
= NULL
;
2289 GSettings
*settings
;
2292 backend
= g_settings_backend_get_default ();
2294 /* make sure it fails properly */
2295 parent
= g_settings_schema_source_get_default ();
2296 source
= g_settings_schema_source_new_from_directory ("/path/that/does/not/exist", parent
, TRUE
, &error
);
2297 g_assert (source
== NULL
);
2298 g_assert_error (error
, G_FILE_ERROR
, G_FILE_ERROR_NOENT
);
2299 g_clear_error (&error
);
2301 /* create a source with the parent */
2302 source
= g_settings_schema_source_new_from_directory ("schema-source", parent
, TRUE
, &error
);
2303 g_assert_no_error (error
);
2304 g_assert (source
!= NULL
);
2306 /* check recursive lookups are working */
2307 schema
= g_settings_schema_source_lookup (source
, "org.gtk.test", TRUE
);
2308 g_assert (schema
!= NULL
);
2309 g_settings_schema_unref (schema
);
2311 /* check recursive lookups for non-existent schemas */
2312 schema
= g_settings_schema_source_lookup (source
, "org.gtk.doesnotexist", TRUE
);
2313 g_assert (schema
== NULL
);
2315 /* check non-recursive for schema that only exists in lower layers */
2316 schema
= g_settings_schema_source_lookup (source
, "org.gtk.test", FALSE
);
2317 g_assert (schema
== NULL
);
2319 /* check non-recursive lookup for non-existent */
2320 schema
= g_settings_schema_source_lookup (source
, "org.gtk.doesnotexist", FALSE
);
2321 g_assert (schema
== NULL
);
2323 /* check non-recursive for schema that exists in toplevel */
2324 schema
= g_settings_schema_source_lookup (source
, "org.gtk.schemasourcecheck", FALSE
);
2325 g_assert (schema
!= NULL
);
2326 g_settings_schema_unref (schema
);
2328 /* check recursive for schema that exists in toplevel */
2329 schema
= g_settings_schema_source_lookup (source
, "org.gtk.schemasourcecheck", TRUE
);
2330 g_assert (schema
!= NULL
);
2332 /* try to use it for something */
2333 settings
= g_settings_new_full (schema
, backend
, g_settings_schema_get_path (schema
));
2334 g_settings_schema_unref (schema
);
2336 g_settings_get (settings
, "enabled", "b", &enabled
);
2338 g_object_unref (settings
);
2340 g_settings_schema_source_unref (source
);
2342 /* try again, but with no parent */
2343 source
= g_settings_schema_source_new_from_directory ("schema-source", NULL
, FALSE
, NULL
);
2344 g_assert (source
!= NULL
);
2346 /* should not find it this time, even if recursive... */
2347 schema
= g_settings_schema_source_lookup (source
, "org.gtk.test", FALSE
);
2348 g_assert (schema
== NULL
);
2349 schema
= g_settings_schema_source_lookup (source
, "org.gtk.test", TRUE
);
2350 g_assert (schema
== NULL
);
2352 /* should still find our own... */
2353 schema
= g_settings_schema_source_lookup (source
, "org.gtk.schemasourcecheck", TRUE
);
2354 g_assert (schema
!= NULL
);
2355 g_settings_schema_unref (schema
);
2356 schema
= g_settings_schema_source_lookup (source
, "org.gtk.schemasourcecheck", FALSE
);
2357 g_assert (schema
!= NULL
);
2358 g_settings_schema_unref (schema
);
2360 g_settings_schema_source_unref (source
);
2364 test_schema_list_keys (void)
2367 GSettingsSchemaSource
*src
= g_settings_schema_source_get_default ();
2368 GSettingsSchema
*schema
= g_settings_schema_source_lookup (src
, "org.gtk.test", TRUE
);
2369 g_assert (schema
!= NULL
);
2371 keys
= g_settings_schema_list_keys (schema
);
2373 g_assert (strv_set_equal ((gchar
**)keys
,
2379 g_settings_schema_unref (schema
);
2385 GAction
*string
, *toggle
;
2386 gboolean c1
, c2
, c3
;
2387 GSettings
*settings
;
2389 GVariantType
*param_type
;
2391 GVariantType
*state_type
;
2394 settings
= g_settings_new ("org.gtk.test.basic-types");
2395 string
= g_settings_create_action (settings
, "test-string");
2396 toggle
= g_settings_create_action (settings
, "test-boolean");
2397 g_object_unref (settings
); /* should be held by the actions */
2399 g_signal_connect (settings
, "changed", G_CALLBACK (changed_cb2
), &c1
);
2400 g_signal_connect (string
, "notify::state", G_CALLBACK (changed_cb2
), &c2
);
2401 g_signal_connect (toggle
, "notify::state", G_CALLBACK (changed_cb2
), &c3
);
2403 c1
= c2
= c3
= FALSE
;
2404 g_settings_set_string (settings
, "test-string", "hello world");
2405 check_and_free (g_action_get_state (string
), "'hello world'");
2406 g_assert (c1
&& c2
&& !c3
);
2407 c1
= c2
= c3
= FALSE
;
2409 g_action_activate (string
, g_variant_new_string ("hihi"));
2410 check_and_free (g_settings_get_value (settings
, "test-string"), "'hihi'");
2411 g_assert (c1
&& c2
&& !c3
);
2412 c1
= c2
= c3
= FALSE
;
2414 g_action_change_state (string
, g_variant_new_string ("kthxbye"));
2415 check_and_free (g_settings_get_value (settings
, "test-string"), "'kthxbye'");
2416 g_assert (c1
&& c2
&& !c3
);
2417 c1
= c2
= c3
= FALSE
;
2419 g_action_change_state (toggle
, g_variant_new_boolean (TRUE
));
2420 g_assert (g_settings_get_boolean (settings
, "test-boolean"));
2421 g_assert (c1
&& !c2
&& c3
);
2422 c1
= c2
= c3
= FALSE
;
2424 g_action_activate (toggle
, NULL
);
2425 g_assert (!g_settings_get_boolean (settings
, "test-boolean"));
2426 g_assert (c1
&& !c2
&& c3
);
2428 g_object_get (string
,
2430 "parameter-type", ¶m_type
,
2431 "enabled", &enabled
,
2432 "state-type", &state_type
,
2436 g_assert_cmpstr (name
, ==, "test-string");
2437 g_assert (g_variant_type_equal (param_type
, G_VARIANT_TYPE_STRING
));
2439 g_assert (g_variant_type_equal (state_type
, G_VARIANT_TYPE_STRING
));
2440 g_assert_cmpstr (g_variant_get_string (state
, NULL
), ==, "kthxbye");
2443 g_variant_type_free (param_type
);
2444 g_variant_type_free (state_type
);
2445 g_variant_unref (state
);
2447 g_object_unref (string
);
2448 g_object_unref (toggle
);
2452 test_null_backend (void)
2454 GSettingsBackend
*backend
;
2455 GSettings
*settings
;
2459 backend
= g_null_settings_backend_new ();
2460 settings
= g_settings_new_with_backend_and_path ("org.gtk.test", backend
, "/tests/");
2462 g_object_get (settings
, "schema-id", &str
, NULL
);
2463 g_assert_cmpstr (str
, ==, "org.gtk.test");
2466 g_settings_get (settings
, "greeting", "s", &str
);
2467 g_assert_cmpstr (str
, ==, "Hello, earthlings");
2470 g_settings_set (settings
, "greeting", "s", "goodbye world");
2471 g_settings_get (settings
, "greeting", "s", &str
);
2472 g_assert_cmpstr (str
, ==, "Hello, earthlings");
2475 writable
= g_settings_is_writable (settings
, "greeting");
2476 g_assert (!writable
);
2478 g_settings_reset (settings
, "greeting");
2480 g_settings_delay (settings
);
2481 g_settings_set (settings
, "greeting", "s", "goodbye world");
2482 g_settings_apply (settings
);
2483 g_settings_get (settings
, "greeting", "s", &str
);
2484 g_assert_cmpstr (str
, ==, "Hello, earthlings");
2487 g_object_unref (settings
);
2488 g_object_unref (backend
);
2492 test_memory_backend (void)
2494 GSettingsBackend
*backend
;
2496 backend
= g_memory_settings_backend_new ();
2497 g_assert (G_IS_SETTINGS_BACKEND (backend
));
2498 g_object_unref (backend
);
2502 test_read_descriptions (void)
2504 GSettingsSchema
*schema
;
2505 GSettingsSchemaKey
*key
;
2506 GSettings
*settings
;
2508 settings
= g_settings_new ("org.gtk.test");
2509 g_object_get (settings
, "settings-schema", &schema
, NULL
);
2510 key
= g_settings_schema_get_key (schema
, "greeting");
2512 g_assert_cmpstr (g_settings_schema_key_get_summary (key
), ==, "A greeting");
2513 g_assert_cmpstr (g_settings_schema_key_get_description (key
), ==, "Greeting of the invading martians");
2515 g_settings_schema_key_unref (key
);
2516 g_settings_schema_unref (schema
);
2518 g_object_unref (settings
);
2520 settings
= g_settings_new ("org.gtk.test.descriptions");
2521 g_object_get (settings
, "settings-schema", &schema
, NULL
);
2522 key
= g_settings_schema_get_key (schema
, "a");
2524 g_assert_cmpstr (g_settings_schema_key_get_summary (key
), ==,
2526 "with some whitespace.\n\n"
2527 "because not everyone has a great editor.\n\n"
2528 "lots of space is as one.");
2530 g_settings_schema_key_unref (key
);
2531 g_settings_schema_unref (schema
);
2533 g_object_unref (settings
);
2537 test_default_value (void)
2539 GSettings
*settings
;
2540 GSettingsSchema
*schema
;
2541 GSettingsSchemaKey
*key
;
2546 settings
= g_settings_new ("org.gtk.test");
2547 g_object_get (settings
, "settings-schema", &schema
, NULL
);
2548 key
= g_settings_schema_get_key (schema
, "greeting");
2549 g_settings_schema_unref (schema
);
2550 g_settings_schema_key_ref (key
);
2552 g_assert (g_variant_type_equal (g_settings_schema_key_get_value_type (key
), G_VARIANT_TYPE_STRING
));
2554 v
= g_settings_schema_key_get_default_value (key
);
2555 str
= g_variant_get_string (v
, NULL
);
2556 g_assert_cmpstr (str
, ==, "Hello, earthlings");
2557 g_variant_unref (v
);
2559 g_settings_schema_key_unref (key
);
2560 g_settings_schema_key_unref (key
);
2562 g_settings_set (settings
, "greeting", "s", "goodbye world");
2564 v
= g_settings_get_user_value (settings
, "greeting");
2565 str
= g_variant_get_string (v
, NULL
);
2566 g_assert_cmpstr (str
, ==, "goodbye world");
2567 g_variant_unref (v
);
2569 v
= g_settings_get_default_value (settings
, "greeting");
2570 str
= g_variant_get_string (v
, NULL
);
2571 g_assert_cmpstr (str
, ==, "Hello, earthlings");
2572 g_variant_unref (v
);
2574 g_settings_reset (settings
, "greeting");
2576 v
= g_settings_get_user_value (settings
, "greeting");
2579 s
= g_settings_get_string (settings
, "greeting");
2580 g_assert_cmpstr (s
, ==, "Hello, earthlings");
2583 g_object_unref (settings
);
2587 test_extended_schema (void)
2589 GSettingsSchema
*schema
;
2590 GSettings
*settings
;
2593 settings
= g_settings_new_with_path ("org.gtk.test.extends.extended", "/test/extendes/");
2594 g_object_get (settings
, "settings-schema", &schema
, NULL
);
2595 keys
= g_settings_schema_list_keys (schema
);
2596 g_assert (strv_set_equal (keys
, "int32", "string", "another-int32", NULL
));
2598 g_object_unref (settings
);
2599 g_settings_schema_unref (schema
);
2603 main (int argc
, char *argv
[])
2609 /* Meson build sets this */
2610 #ifdef TEST_LOCALE_PATH
2611 if (g_str_has_suffix (TEST_LOCALE_PATH
, "LC_MESSAGES"))
2613 locale_dir
= TEST_LOCALE_PATH G_DIR_SEPARATOR_S
".." G_DIR_SEPARATOR_S
"..";
2617 setlocale (LC_ALL
, "");
2619 g_test_init (&argc
, &argv
, NULL
);
2621 if (!g_test_subprocess ())
2623 backend_set
= g_getenv ("GSETTINGS_BACKEND") != NULL
;
2625 g_setenv ("XDG_DATA_DIRS", ".", TRUE
);
2626 g_setenv ("XDG_DATA_HOME", ".", TRUE
);
2627 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE
);
2630 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE
);
2632 /* Meson build defines this, autotools build does not */
2633 #ifndef GLIB_MKENUMS
2634 #define GLIB_MKENUMS "../../gobject/glib-mkenums"
2637 g_remove ("org.gtk.test.enums.xml");
2638 g_assert (g_spawn_command_line_sync (GLIB_MKENUMS
" "
2639 "--template " SRCDIR
"/enums.xml.template "
2640 SRCDIR
"/testenum.h",
2641 &enums
, NULL
, &result
, NULL
));
2642 g_assert (result
== 0);
2643 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums
, -1, NULL
));
2646 g_assert (g_file_get_contents (SRCDIR
"/org.gtk.test.gschema.xml.orig", &schema_text
, NULL
, NULL
));
2647 g_assert (g_file_set_contents ("org.gtk.test.gschema.xml", schema_text
, -1, NULL
));
2648 g_free (schema_text
);
2650 /* Meson build defines this, autotools build does not */
2651 #ifndef GLIB_COMPILE_SCHEMAS
2652 #define GLIB_COMPILE_SCHEMAS "../glib-compile-schemas"
2655 g_remove ("gschemas.compiled");
2656 g_assert (g_spawn_command_line_sync (GLIB_COMPILE_SCHEMAS
" --targetdir=. "
2657 "--schema-file=org.gtk.test.enums.xml "
2658 "--schema-file=org.gtk.test.gschema.xml",
2659 NULL
, NULL
, &result
, NULL
));
2660 g_assert (result
== 0);
2662 g_remove ("schema-source/gschemas.compiled");
2663 g_mkdir ("schema-source", 0777);
2664 g_assert (g_spawn_command_line_sync (GLIB_COMPILE_SCHEMAS
" --targetdir=schema-source "
2665 "--schema-file=" SRCDIR
"/org.gtk.schemasourcecheck.gschema.xml",
2666 NULL
, NULL
, &result
, NULL
));
2667 g_assert (result
== 0);
2670 g_test_add_func ("/gsettings/basic", test_basic
);
2674 g_test_add_func ("/gsettings/no-schema", test_no_schema
);
2675 g_test_add_func ("/gsettings/unknown-key", test_unknown_key
);
2676 g_test_add_func ("/gsettings/wrong-type", test_wrong_type
);
2677 g_test_add_func ("/gsettings/wrong-path", test_wrong_path
);
2678 g_test_add_func ("/gsettings/no-path", test_no_path
);
2681 g_test_add_func ("/gsettings/basic-types", test_basic_types
);
2682 g_test_add_func ("/gsettings/complex-types", test_complex_types
);
2683 g_test_add_func ("/gsettings/changes", test_changes
);
2685 g_test_add_func ("/gsettings/l10n", test_l10n
);
2686 g_test_add_func ("/gsettings/l10n-context", test_l10n_context
);
2688 g_test_add_func ("/gsettings/delay-apply", test_delay_apply
);
2689 g_test_add_func ("/gsettings/delay-revert", test_delay_revert
);
2690 g_test_add_func ("/gsettings/delay-child", test_delay_child
);
2691 g_test_add_func ("/gsettings/atomic", test_atomic
);
2693 g_test_add_func ("/gsettings/simple-binding", test_simple_binding
);
2694 g_test_add_func ("/gsettings/directional-binding", test_directional_binding
);
2695 g_test_add_func ("/gsettings/custom-binding", test_custom_binding
);
2696 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding
);
2697 g_test_add_func ("/gsettings/unbinding", test_unbind
);
2698 g_test_add_func ("/gsettings/writable-binding", test_bind_writable
);
2702 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding
);
2703 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding
);
2704 g_test_add_func ("/gsettings/no-read-binding/subprocess/fail", test_no_read_binding_fail
);
2705 g_test_add_func ("/gsettings/no-read-binding/subprocess/pass", test_no_read_binding_pass
);
2706 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding
);
2707 g_test_add_func ("/gsettings/no-write-binding/subprocess/fail", test_no_write_binding_fail
);
2708 g_test_add_func ("/gsettings/no-write-binding/subprocess/pass", test_no_write_binding_pass
);
2711 g_test_add_func ("/gsettings/keyfile", test_keyfile
);
2712 g_test_add_func ("/gsettings/child-schema", test_child_schema
);
2713 g_test_add_func ("/gsettings/strinfo", test_strinfo
);
2714 g_test_add_func ("/gsettings/enums", test_enums
);
2715 g_test_add_func ("/gsettings/enums/subprocess/non-enum-key", test_enums_non_enum_key
);
2716 g_test_add_func ("/gsettings/enums/subprocess/non-enum-value", test_enums_non_enum_value
);
2717 g_test_add_func ("/gsettings/enums/subprocess/range", test_enums_range
);
2718 g_test_add_func ("/gsettings/enums/subprocess/non-flags", test_enums_non_flags
);
2719 g_test_add_func ("/gsettings/flags", test_flags
);
2720 g_test_add_func ("/gsettings/flags/subprocess/non-flags-key", test_flags_non_flags_key
);
2721 g_test_add_func ("/gsettings/flags/subprocess/non-flags-value", test_flags_non_flags_value
);
2722 g_test_add_func ("/gsettings/flags/subprocess/range", test_flags_range
);
2723 g_test_add_func ("/gsettings/flags/subprocess/non-enum", test_flags_non_enum
);
2724 g_test_add_func ("/gsettings/range", test_range
);
2725 g_test_add_func ("/gsettings/range/subprocess/high", test_range_high
);
2726 g_test_add_func ("/gsettings/range/subprocess/low", test_range_low
);
2727 g_test_add_func ("/gsettings/list-items", test_list_items
);
2728 g_test_add_func ("/gsettings/list-schemas", test_list_schemas
);
2729 g_test_add_func ("/gsettings/mapped", test_get_mapped
);
2730 g_test_add_func ("/gsettings/get-range", test_get_range
);
2731 g_test_add_func ("/gsettings/schema-source", test_schema_source
);
2732 g_test_add_func ("/gsettings/schema-list-keys", test_schema_list_keys
);
2733 g_test_add_func ("/gsettings/actions", test_actions
);
2734 g_test_add_func ("/gsettings/null-backend", test_null_backend
);
2735 g_test_add_func ("/gsettings/memory-backend", test_memory_backend
);
2736 g_test_add_func ("/gsettings/read-descriptions", test_read_descriptions
);
2737 g_test_add_func ("/gsettings/test-extended-schema", test_extended_schema
);
2738 g_test_add_func ("/gsettings/default-value", test_default_value
);
2740 result
= g_test_run ();