Update German translation
[glib.git] / gio / tests / gsettings.c
blobb1bbd4ed17c67e1d722b1601d5710d7e610fb7c0
1 #include <stdlib.h>
2 #include <locale.h>
3 #include <libintl.h>
4 #include <gio/gio.h>
5 #include <gstdio.h>
6 #define G_SETTINGS_ENABLE_BACKEND
7 #include <gio/gsettingsbackend.h>
9 #include "testenum.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.
17 static void
18 check_and_free (GVariant *value,
19 const gchar *expected)
21 gchar *printed;
23 printed = g_variant_print (value, TRUE);
24 g_assert_cmpstr (printed, ==, expected);
25 g_free (printed);
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
34 static void
35 test_basic (void)
37 gchar *str = NULL;
38 GObject *b;
39 gchar *path;
40 gboolean has_unapplied;
41 gboolean delay_apply;
42 GSettings *settings;
44 settings = g_settings_new ("org.gtk.test");
46 g_object_get (settings,
47 "schema-id", &str,
48 "backend", &b,
49 "path", &path,
50 "has-unapplied", &has_unapplied,
51 "delay-apply", &delay_apply,
52 NULL);
53 g_assert_cmpstr (str, ==, "org.gtk.test");
54 g_assert (b != NULL);
55 g_assert_cmpstr (path, ==, "/tests/");
56 g_assert (!has_unapplied);
57 g_assert (!delay_apply);
58 g_free (str);
59 g_object_unref (b);
60 g_free (path);
62 g_settings_get (settings, "greeting", "s", &str);
63 g_assert_cmpstr (str, ==, "Hello, earthlings");
64 g_free (str);
66 g_settings_set (settings, "greeting", "s", "goodbye world");
67 g_settings_get (settings, "greeting", "s", &str);
68 g_assert_cmpstr (str, ==, "goodbye world");
69 g_free (str);
70 str = NULL;
72 if (!backend_set && g_test_undefined ())
74 GSettings *tmp_settings = g_settings_new ("org.gtk.test");
76 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
77 "*g_settings_set_value*expects type*");
78 g_settings_set (tmp_settings, "greeting", "i", 555);
79 g_test_assert_expected_messages ();
81 g_object_unref (tmp_settings);
84 g_settings_get (settings, "greeting", "s", &str);
85 g_assert_cmpstr (str, ==, "goodbye world");
86 g_free (str);
87 str = NULL;
89 g_settings_reset (settings, "greeting");
90 str = g_settings_get_string (settings, "greeting");
91 g_assert_cmpstr (str, ==, "Hello, earthlings");
92 g_free (str);
94 g_settings_set (settings, "greeting", "s", "this is the end");
95 g_object_unref (settings);
98 /* Check that we get an error when getting a key
99 * that is not in the schema
101 static void
102 test_unknown_key (void)
104 if (!g_test_undefined ())
105 return;
107 if (g_test_subprocess ())
109 GSettings *settings;
110 GVariant *value;
112 settings = g_settings_new ("org.gtk.test");
113 value = g_settings_get_value (settings, "no_such_key");
115 g_assert (value == NULL);
117 g_object_unref (settings);
118 return;
120 g_test_trap_subprocess (NULL, 0, 0);
121 g_test_trap_assert_failed ();
122 g_test_trap_assert_stderr ("*does not contain*");
125 /* Check that we get an error when the schema
126 * has not been installed
128 static void
129 test_no_schema (void)
131 if (!g_test_undefined ())
132 return;
134 if (g_test_subprocess ())
136 GSettings *settings;
138 settings = g_settings_new ("no.such.schema");
140 g_assert (settings == NULL);
141 return;
143 g_test_trap_subprocess (NULL, 0, 0);
144 g_test_trap_assert_failed ();
145 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
148 /* Check that we get an error when passing a type string
149 * that does not match the schema
151 static void
152 test_wrong_type (void)
154 GSettings *settings;
155 gchar *str = NULL;
157 if (!g_test_undefined ())
158 return;
160 settings = g_settings_new ("org.gtk.test");
162 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
163 "*given value has a type of*");
164 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
165 "*valid_format_string*");
166 g_settings_get (settings, "greeting", "o", &str);
167 g_test_assert_expected_messages ();
169 g_assert (str == NULL);
171 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
172 "*expects type 's'*");
173 g_settings_set (settings, "greeting", "o", "/a/path");
174 g_test_assert_expected_messages ();
176 g_object_unref (settings);
179 /* Check errors with explicit paths */
180 static void
181 test_wrong_path (void)
183 if (!g_test_undefined ())
184 return;
186 if (g_test_subprocess ())
188 GSettings *settings G_GNUC_UNUSED;
190 settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
191 return;
193 g_test_trap_subprocess (NULL, 0, 0);
194 g_test_trap_assert_failed ();
195 g_test_trap_assert_stderr ("*but path * specified by schema*");
198 static void
199 test_no_path (void)
201 if (!g_test_undefined ())
202 return;
204 if (g_test_subprocess ())
206 GSettings *settings G_GNUC_UNUSED;
208 settings = g_settings_new ("org.gtk.test.no-path");
209 return;
211 g_test_trap_subprocess (NULL, 0, 0);
212 g_test_trap_assert_failed ();
213 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
217 /* Check that we can successfully read and set the full
218 * range of all basic types
220 static void
221 test_basic_types (void)
223 GSettings *settings;
224 gboolean b;
225 guint8 byte;
226 gint16 i16;
227 guint16 u16;
228 gint32 i32;
229 guint32 u32;
230 gint64 i64;
231 guint64 u64;
232 gdouble d;
233 gchar *str;
235 settings = g_settings_new ("org.gtk.test.basic-types");
237 g_settings_get (settings, "test-boolean", "b", &b);
238 g_assert_cmpint (b, ==, 1);
240 g_settings_set (settings, "test-boolean", "b", 0);
241 g_settings_get (settings, "test-boolean", "b", &b);
242 g_assert_cmpint (b, ==, 0);
244 g_settings_get (settings, "test-byte", "y", &byte);
245 g_assert_cmpint (byte, ==, 25);
247 g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
248 g_settings_get (settings, "test-byte", "y", &byte);
249 g_assert_cmpint (byte, ==, G_MAXUINT8);
251 g_settings_get (settings, "test-int16", "n", &i16);
252 g_assert_cmpint (i16, ==, -1234);
254 g_settings_set (settings, "test-int16", "n", G_MININT16);
255 g_settings_get (settings, "test-int16", "n", &i16);
256 g_assert_cmpint (i16, ==, G_MININT16);
258 g_settings_set (settings, "test-int16", "n", G_MAXINT16);
259 g_settings_get (settings, "test-int16", "n", &i16);
260 g_assert_cmpint (i16, ==, G_MAXINT16);
262 g_settings_get (settings, "test-uint16", "q", &u16);
263 g_assert_cmpuint (u16, ==, 1234);
265 g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
266 g_settings_get (settings, "test-uint16", "q", &u16);
267 g_assert_cmpuint (u16, ==, G_MAXUINT16);
269 g_settings_get (settings, "test-int32", "i", &i32);
270 g_assert_cmpint (i32, ==, -123456);
272 g_settings_set (settings, "test-int32", "i", G_MININT32);
273 g_settings_get (settings, "test-int32", "i", &i32);
274 g_assert_cmpint (i32, ==, G_MININT32);
276 g_settings_set (settings, "test-int32", "i", G_MAXINT32);
277 g_settings_get (settings, "test-int32", "i", &i32);
278 g_assert_cmpint (i32, ==, G_MAXINT32);
280 g_settings_get (settings, "test-uint32", "u", &u32);
281 g_assert_cmpuint (u32, ==, 123456);
283 g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
284 g_settings_get (settings, "test-uint32", "u", &u32);
285 g_assert_cmpuint (u32, ==, G_MAXUINT32);
287 g_settings_get (settings, "test-int64", "x", &i64);
288 g_assert_cmpuint (i64, ==, -123456789);
290 g_settings_set (settings, "test-int64", "x", G_MININT64);
291 g_settings_get (settings, "test-int64", "x", &i64);
292 g_assert_cmpuint (i64, ==, G_MININT64);
294 g_settings_set (settings, "test-int64", "x", G_MAXINT64);
295 g_settings_get (settings, "test-int64", "x", &i64);
296 g_assert_cmpuint (i64, ==, G_MAXINT64);
298 g_settings_get (settings, "test-uint64", "t", &u64);
299 g_assert_cmpuint (u64, ==, 123456789);
301 g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
302 g_settings_get (settings, "test-uint64", "t", &u64);
303 g_assert_cmpuint (u64, ==, G_MAXUINT64);
305 g_settings_get (settings, "test-double", "d", &d);
306 g_assert_cmpfloat (d, ==, 123.456);
308 g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
309 g_settings_get (settings, "test-double", "d", &d);
310 g_assert_cmpfloat (d, ==, G_MINDOUBLE);
312 g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
313 g_settings_get (settings, "test-double", "d", &d);
314 g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
316 g_settings_get (settings, "test-string", "s", &str);
317 g_assert_cmpstr (str, ==, "a string, it seems");
318 g_free (str);
319 str = NULL;
321 g_settings_get (settings, "test-objectpath", "o", &str);
322 g_assert_cmpstr (str, ==, "/a/object/path");
323 g_object_unref (settings);
324 g_free (str);
325 str = NULL;
328 /* Check that we can read an set complex types like
329 * tuples, arrays and dictionaries
331 static void
332 test_complex_types (void)
334 GSettings *settings;
335 gchar *s;
336 gint i1, i2;
337 GVariantIter *iter = NULL;
338 GVariant *v = NULL;
340 settings = g_settings_new ("org.gtk.test.complex-types");
342 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
343 g_assert_cmpstr (s, ==, "one");
344 g_assert_cmpint (i1,==, 2);
345 g_assert_cmpint (i2,==, 3);
346 g_free (s) ;
347 s = NULL;
349 g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
350 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
351 g_assert_cmpstr (s, ==, "none");
352 g_assert_cmpint (i1,==, 0);
353 g_assert_cmpint (i2,==, 0);
354 g_free (s);
355 s = NULL;
357 g_settings_get (settings, "test-array", "ai", &iter);
358 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
359 g_assert (g_variant_iter_next (iter, "i", &i1));
360 g_assert_cmpint (i1, ==, 0);
361 g_assert (g_variant_iter_next (iter, "i", &i1));
362 g_assert_cmpint (i1, ==, 1);
363 g_assert (g_variant_iter_next (iter, "i", &i1));
364 g_assert_cmpint (i1, ==, 2);
365 g_assert (g_variant_iter_next (iter, "i", &i1));
366 g_assert_cmpint (i1, ==, 3);
367 g_assert (g_variant_iter_next (iter, "i", &i1));
368 g_assert_cmpint (i1, ==, 4);
369 g_assert (g_variant_iter_next (iter, "i", &i1));
370 g_assert_cmpint (i1, ==, 5);
371 g_assert (!g_variant_iter_next (iter, "i", &i1));
372 g_variant_iter_free (iter);
374 g_settings_get (settings, "test-dict", "a{sau}", &iter);
375 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 2);
376 g_assert (g_variant_iter_next (iter, "{&s@au}", &s, &v));
377 g_assert_cmpstr (s, ==, "AC");
378 g_assert_cmpstr ((char *)g_variant_get_type (v), ==, "au");
379 g_variant_unref (v);
380 g_assert (g_variant_iter_next (iter, "{&s@au}", &s, &v));
381 g_assert_cmpstr (s, ==, "IV");
382 g_assert_cmpstr ((char *)g_variant_get_type (v), ==, "au");
383 g_variant_unref (v);
384 g_variant_iter_free (iter);
386 v = g_settings_get_value (settings, "test-dict");
387 g_assert_cmpstr ((char *)g_variant_get_type (v), ==, "a{sau}");
388 g_variant_unref (v);
390 g_object_unref (settings);
393 static gboolean changed_cb_called;
395 static void
396 changed_cb (GSettings *settings,
397 const gchar *key,
398 gpointer data)
400 changed_cb_called = TRUE;
402 g_assert_cmpstr (key, ==, data);
405 /* Test that basic change notification with the changed signal works.
407 static void
408 test_changes (void)
410 GSettings *settings;
411 GSettings *settings2;
413 settings = g_settings_new ("org.gtk.test");
415 g_signal_connect (settings, "changed",
416 G_CALLBACK (changed_cb), "greeting");
418 changed_cb_called = FALSE;
420 g_settings_set (settings, "greeting", "s", "new greeting");
421 g_assert (changed_cb_called);
423 settings2 = g_settings_new ("org.gtk.test");
425 changed_cb_called = FALSE;
427 g_settings_set (settings2, "greeting", "s", "hi");
428 g_assert (changed_cb_called);
430 g_object_unref (settings2);
431 g_object_unref (settings);
434 static gboolean changed_cb_called2;
436 static void
437 changed_cb2 (GSettings *settings,
438 const gchar *key,
439 gpointer data)
441 gboolean *p = data;
443 *p = TRUE;
446 /* Test that changes done to a delay-mode instance
447 * don't appear to the outside world until apply. Also
448 * check that we get change notification when they are
449 * applied.
450 * Also test that the has-unapplied property is properly
451 * maintained.
453 static void
454 test_delay_apply (void)
456 GSettings *settings;
457 GSettings *settings2;
458 gchar *str;
459 gboolean writable;
460 GVariant *v;
461 const gchar *s;
463 settings = g_settings_new ("org.gtk.test");
464 settings2 = g_settings_new ("org.gtk.test");
466 g_settings_set (settings2, "greeting", "s", "top o' the morning");
468 changed_cb_called = FALSE;
469 changed_cb_called2 = FALSE;
471 g_signal_connect (settings, "changed",
472 G_CALLBACK (changed_cb2), &changed_cb_called);
473 g_signal_connect (settings2, "changed",
474 G_CALLBACK (changed_cb2), &changed_cb_called2);
476 g_settings_delay (settings);
478 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
480 g_assert (changed_cb_called);
481 g_assert (!changed_cb_called2);
483 writable = g_settings_is_writable (settings, "greeting");
484 g_assert (writable);
486 g_settings_get (settings, "greeting", "s", &str);
487 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
488 g_free (str);
489 str = NULL;
491 v = g_settings_get_user_value (settings, "greeting");
492 s = g_variant_get_string (v, NULL);
493 g_assert_cmpstr (s, ==, "greetings from test_delay_apply");
494 g_variant_unref (v);
496 g_settings_get (settings2, "greeting", "s", &str);
497 g_assert_cmpstr (str, ==, "top o' the morning");
498 g_free (str);
499 str = NULL;
501 g_assert (g_settings_get_has_unapplied (settings));
502 g_assert (!g_settings_get_has_unapplied (settings2));
504 changed_cb_called = FALSE;
505 changed_cb_called2 = FALSE;
507 g_settings_apply (settings);
509 g_assert (!changed_cb_called);
510 g_assert (changed_cb_called2);
512 g_settings_get (settings, "greeting", "s", &str);
513 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
514 g_free (str);
515 str = NULL;
517 g_settings_get (settings2, "greeting", "s", &str);
518 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
519 g_free (str);
520 str = NULL;
522 g_assert (!g_settings_get_has_unapplied (settings));
523 g_assert (!g_settings_get_has_unapplied (settings2));
525 g_settings_reset (settings, "greeting");
526 g_settings_apply (settings);
528 g_settings_get (settings, "greeting", "s", &str);
529 g_assert_cmpstr (str, ==, "Hello, earthlings");
530 g_free (str);
532 g_object_unref (settings2);
533 g_object_unref (settings);
536 /* Test that reverting unapplied changes in a delay-apply
537 * settings instance works.
539 static void
540 test_delay_revert (void)
542 GSettings *settings;
543 GSettings *settings2;
544 gchar *str;
546 settings = g_settings_new ("org.gtk.test");
547 settings2 = g_settings_new ("org.gtk.test");
549 g_settings_set (settings2, "greeting", "s", "top o' the morning");
551 g_settings_get (settings, "greeting", "s", &str);
552 g_assert_cmpstr (str, ==, "top o' the morning");
553 g_free (str);
555 g_settings_delay (settings);
557 g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
559 g_settings_get (settings, "greeting", "s", &str);
560 g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
561 g_free (str);
562 str = NULL;
564 g_settings_get (settings2, "greeting", "s", &str);
565 g_assert_cmpstr (str, ==, "top o' the morning");
566 g_free (str);
567 str = NULL;
569 g_assert (g_settings_get_has_unapplied (settings));
571 g_settings_revert (settings);
573 g_assert (!g_settings_get_has_unapplied (settings));
575 g_settings_get (settings, "greeting", "s", &str);
576 g_assert_cmpstr (str, ==, "top o' the morning");
577 g_free (str);
578 str = NULL;
580 g_settings_get (settings2, "greeting", "s", &str);
581 g_assert_cmpstr (str, ==, "top o' the morning");
582 g_free (str);
583 str = NULL;
585 g_object_unref (settings2);
586 g_object_unref (settings);
589 static void
590 test_delay_child (void)
592 GSettings *base;
593 GSettings *settings;
594 GSettings *child;
595 guint8 byte;
596 gboolean delay;
598 base = g_settings_new ("org.gtk.test.basic-types");
599 g_settings_set (base, "test-byte", "y", 36);
601 settings = g_settings_new ("org.gtk.test");
602 g_settings_delay (settings);
603 g_object_get (settings, "delay-apply", &delay, NULL);
604 g_assert (delay);
606 child = g_settings_get_child (settings, "basic-types");
607 g_assert (child != NULL);
609 g_object_get (child, "delay-apply", &delay, NULL);
610 g_assert (!delay);
612 g_settings_get (child, "test-byte", "y", &byte);
613 g_assert_cmpuint (byte, ==, 36);
615 g_settings_set (child, "test-byte", "y", 42);
617 /* make sure the child was delayed too */
618 g_settings_get (base, "test-byte", "y", &byte);
619 g_assert_cmpuint (byte, ==, 36);
621 g_object_unref (child);
622 g_object_unref (settings);
623 g_object_unref (base);
626 static void
627 keys_changed_cb (GSettings *settings,
628 const GQuark *keys,
629 gint n_keys)
631 gchar *str;
633 g_assert_cmpint (n_keys, ==, 2);
635 g_assert ((keys[0] == g_quark_from_static_string ("greeting") &&
636 keys[1] == g_quark_from_static_string ("farewell")) ||
637 (keys[1] == g_quark_from_static_string ("greeting") &&
638 keys[0] == g_quark_from_static_string ("farewell")));
640 g_settings_get (settings, "greeting", "s", &str);
641 g_assert_cmpstr (str, ==, "greetings from test_atomic");
642 g_free (str);
643 str = NULL;
645 g_settings_get (settings, "farewell", "s", &str);
646 g_assert_cmpstr (str, ==, "atomic bye-bye");
647 g_free (str);
648 str = NULL;
651 /* Check that delay-applied changes appear atomically.
652 * More specifically, verify that all changed keys appear
653 * with their new value while handling the change-event signal.
655 static void
656 test_atomic (void)
658 GSettings *settings;
659 GSettings *settings2;
660 gchar *str;
662 settings = g_settings_new ("org.gtk.test");
663 settings2 = g_settings_new ("org.gtk.test");
665 g_settings_set (settings2, "greeting", "s", "top o' the morning");
667 changed_cb_called = FALSE;
668 changed_cb_called2 = FALSE;
670 g_signal_connect (settings2, "change-event",
671 G_CALLBACK (keys_changed_cb), NULL);
673 g_settings_delay (settings);
675 g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
676 g_settings_set (settings, "farewell", "s", "atomic bye-bye");
678 g_settings_apply (settings);
680 g_settings_get (settings, "greeting", "s", &str);
681 g_assert_cmpstr (str, ==, "greetings from test_atomic");
682 g_free (str);
683 str = NULL;
685 g_settings_get (settings, "farewell", "s", &str);
686 g_assert_cmpstr (str, ==, "atomic bye-bye");
687 g_free (str);
688 str = NULL;
690 g_settings_get (settings2, "greeting", "s", &str);
691 g_assert_cmpstr (str, ==, "greetings from test_atomic");
692 g_free (str);
693 str = NULL;
695 g_settings_get (settings2, "farewell", "s", &str);
696 g_assert_cmpstr (str, ==, "atomic bye-bye");
697 g_free (str);
698 str = NULL;
700 g_object_unref (settings2);
701 g_object_unref (settings);
704 /* On Windows the interaction between the C library locale and libintl
705 * (from GNU gettext) is not like on POSIX, so just skip these tests
706 * for now.
708 * There are several issues:
710 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
711 * in libintl (defined in its <libintl.h>).
713 * 2) The locale names that setlocale() accepts and returns aren't in
714 * the "de_DE" style, but like "German_Germany".
716 * 3) libintl looks at the Win32 thread locale and not the C library
717 * locale. (And even if libintl would use the C library's locale, as
718 * there are several alternative C library DLLs, libintl might be
719 * linked to a different one than the application code, so they
720 * wouldn't have the same C library locale anyway.)
723 /* Test that translations work for schema defaults.
725 * This test relies on the de.po file in the same directory
726 * to be compiled into ./de/LC_MESSAGES/test.mo
728 static void
729 test_l10n (void)
731 GSettings *settings;
732 gchar *str;
733 gchar *locale;
735 bindtextdomain ("test", ".");
736 bind_textdomain_codeset ("test", "UTF-8");
738 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
740 settings = g_settings_new ("org.gtk.test.localized");
742 setlocale (LC_MESSAGES, "C");
743 str = g_settings_get_string (settings, "error-message");
744 setlocale (LC_MESSAGES, locale);
746 g_assert_cmpstr (str, ==, "Unnamed");
747 g_free (str);
748 str = NULL;
750 setlocale (LC_MESSAGES, "de_DE");
751 /* Only do the test if translation is actually working... */
752 if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
754 str = g_settings_get_string (settings, "error-message");
756 g_assert_cmpstr (str, ==, "Unbenannt");
757 g_free (str);
758 str = NULL;
760 else
761 g_printerr ("warning: translation is not working... skipping test. ");
763 setlocale (LC_MESSAGES, locale);
764 g_free (locale);
765 g_object_unref (settings);
768 /* Test that message context works as expected with translated
769 * schema defaults. Also, verify that non-ASCII UTF-8 content
770 * works.
772 * This test relies on the de.po file in the same directory
773 * to be compiled into ./de/LC_MESSAGES/test.mo
775 static void
776 test_l10n_context (void)
778 GSettings *settings;
779 gchar *str;
780 gchar *locale;
782 bindtextdomain ("test", ".");
783 bind_textdomain_codeset ("test", "UTF-8");
785 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
787 settings = g_settings_new ("org.gtk.test.localized");
789 setlocale (LC_MESSAGES, "C");
790 g_settings_get (settings, "backspace", "s", &str);
791 setlocale (LC_MESSAGES, locale);
793 g_assert_cmpstr (str, ==, "BackSpace");
794 g_free (str);
795 str = NULL;
797 setlocale (LC_MESSAGES, "de_DE");
798 /* Only do the test if translation is actually working... */
799 if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
801 g_settings_get (settings, "backspace", "s", &str);
803 g_assert_cmpstr (str, ==, "Löschen");
804 g_free (str);
805 str = NULL;
807 else
808 g_printerr ("warning: translation is not working... skipping test. ");
810 setlocale (LC_MESSAGES, locale);
811 g_free (locale);
812 g_object_unref (settings);
815 enum
817 PROP_0,
818 PROP_BOOL,
819 PROP_ANTI_BOOL,
820 PROP_BYTE,
821 PROP_INT16,
822 PROP_UINT16,
823 PROP_INT,
824 PROP_UINT,
825 PROP_INT64,
826 PROP_UINT64,
827 PROP_DOUBLE,
828 PROP_STRING,
829 PROP_NO_READ,
830 PROP_NO_WRITE,
831 PROP_STRV,
832 PROP_ENUM,
833 PROP_FLAGS
836 typedef struct
838 GObject parent_instance;
840 gboolean bool_prop;
841 gboolean anti_bool_prop;
842 gint8 byte_prop;
843 gint int16_prop;
844 guint16 uint16_prop;
845 gint int_prop;
846 guint uint_prop;
847 gint64 int64_prop;
848 guint64 uint64_prop;
849 gdouble double_prop;
850 gchar *string_prop;
851 gchar *no_read_prop;
852 gchar *no_write_prop;
853 gchar **strv_prop;
854 guint enum_prop;
855 guint flags_prop;
856 } TestObject;
858 typedef struct
860 GObjectClass parent_class;
861 } TestObjectClass;
863 static GType test_object_get_type (void);
864 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
866 static void
867 test_object_init (TestObject *object)
871 static void
872 test_object_finalize (GObject *object)
874 TestObject *testo = (TestObject*)object;
875 g_strfreev (testo->strv_prop);
876 g_free (testo->string_prop);
877 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
880 static void
881 test_object_get_property (GObject *object,
882 guint prop_id,
883 GValue *value,
884 GParamSpec *pspec)
886 TestObject *test_object = (TestObject *)object;
888 switch (prop_id)
890 case PROP_BOOL:
891 g_value_set_boolean (value, test_object->bool_prop);
892 break;
893 case PROP_ANTI_BOOL:
894 g_value_set_boolean (value, test_object->anti_bool_prop);
895 break;
896 case PROP_BYTE:
897 g_value_set_schar (value, test_object->byte_prop);
898 break;
899 case PROP_UINT16:
900 g_value_set_uint (value, test_object->uint16_prop);
901 break;
902 case PROP_INT16:
903 g_value_set_int (value, test_object->int16_prop);
904 break;
905 case PROP_INT:
906 g_value_set_int (value, test_object->int_prop);
907 break;
908 case PROP_UINT:
909 g_value_set_uint (value, test_object->uint_prop);
910 break;
911 case PROP_INT64:
912 g_value_set_int64 (value, test_object->int64_prop);
913 break;
914 case PROP_UINT64:
915 g_value_set_uint64 (value, test_object->uint64_prop);
916 break;
917 case PROP_DOUBLE:
918 g_value_set_double (value, test_object->double_prop);
919 break;
920 case PROP_STRING:
921 g_value_set_string (value, test_object->string_prop);
922 break;
923 case PROP_NO_WRITE:
924 g_value_set_string (value, test_object->no_write_prop);
925 break;
926 case PROP_STRV:
927 g_value_set_boxed (value, test_object->strv_prop);
928 break;
929 case PROP_ENUM:
930 g_value_set_enum (value, test_object->enum_prop);
931 break;
932 case PROP_FLAGS:
933 g_value_set_flags (value, test_object->flags_prop);
934 break;
935 default:
936 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
937 break;
941 static void
942 test_object_set_property (GObject *object,
943 guint prop_id,
944 const GValue *value,
945 GParamSpec *pspec)
947 TestObject *test_object = (TestObject *)object;
949 switch (prop_id)
951 case PROP_BOOL:
952 test_object->bool_prop = g_value_get_boolean (value);
953 break;
954 case PROP_ANTI_BOOL:
955 test_object->anti_bool_prop = g_value_get_boolean (value);
956 break;
957 case PROP_BYTE:
958 test_object->byte_prop = g_value_get_schar (value);
959 break;
960 case PROP_INT16:
961 test_object->int16_prop = g_value_get_int (value);
962 break;
963 case PROP_UINT16:
964 test_object->uint16_prop = g_value_get_uint (value);
965 break;
966 case PROP_INT:
967 test_object->int_prop = g_value_get_int (value);
968 break;
969 case PROP_UINT:
970 test_object->uint_prop = g_value_get_uint (value);
971 break;
972 case PROP_INT64:
973 test_object->int64_prop = g_value_get_int64 (value);
974 break;
975 case PROP_UINT64:
976 test_object->uint64_prop = g_value_get_uint64 (value);
977 break;
978 case PROP_DOUBLE:
979 test_object->double_prop = g_value_get_double (value);
980 break;
981 case PROP_STRING:
982 g_free (test_object->string_prop);
983 test_object->string_prop = g_value_dup_string (value);
984 break;
985 case PROP_NO_READ:
986 g_free (test_object->no_read_prop);
987 test_object->no_read_prop = g_value_dup_string (value);
988 break;
989 case PROP_STRV:
990 g_strfreev (test_object->strv_prop);
991 test_object->strv_prop = g_value_dup_boxed (value);
992 break;
993 case PROP_ENUM:
994 test_object->enum_prop = g_value_get_enum (value);
995 break;
996 case PROP_FLAGS:
997 test_object->flags_prop = g_value_get_flags (value);
998 break;
999 default:
1000 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1001 break;
1005 static GType
1006 test_enum_get_type (void)
1008 static volatile gsize define_type_id = 0;
1010 if (g_once_init_enter (&define_type_id))
1012 static const GEnumValue values[] = {
1013 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
1014 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
1015 { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
1016 { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
1017 { 0, NULL, NULL }
1020 GType type_id = g_enum_register_static ("TestEnum", values);
1021 g_once_init_leave (&define_type_id, type_id);
1024 return define_type_id;
1027 static GType
1028 test_flags_get_type (void)
1030 static volatile gsize define_type_id = 0;
1032 if (g_once_init_enter (&define_type_id))
1034 static const GFlagsValue values[] = {
1035 { TEST_FLAGS_NONE, "TEST_FLAGS_NONE", "none" },
1036 { TEST_FLAGS_MOURNING, "TEST_FLAGS_MOURNING", "mourning" },
1037 { TEST_FLAGS_LAUGHING, "TEST_FLAGS_LAUGHING", "laughing" },
1038 { TEST_FLAGS_WALKING, "TEST_FLAGS_WALKING", "walking" },
1039 { 0, NULL, NULL }
1042 GType type_id = g_flags_register_static ("TestFlags", values);
1043 g_once_init_leave (&define_type_id, type_id);
1046 return define_type_id;
1049 static void
1050 test_object_class_init (TestObjectClass *class)
1052 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
1054 gobject_class->get_property = test_object_get_property;
1055 gobject_class->set_property = test_object_set_property;
1056 gobject_class->finalize = test_object_finalize;
1058 g_object_class_install_property (gobject_class, PROP_BOOL,
1059 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
1060 g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
1061 g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
1062 g_object_class_install_property (gobject_class, PROP_BYTE,
1063 g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
1064 g_object_class_install_property (gobject_class, PROP_INT16,
1065 g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
1066 g_object_class_install_property (gobject_class, PROP_UINT16,
1067 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
1068 g_object_class_install_property (gobject_class, PROP_INT,
1069 g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
1070 g_object_class_install_property (gobject_class, PROP_UINT,
1071 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
1072 g_object_class_install_property (gobject_class, PROP_INT64,
1073 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
1074 g_object_class_install_property (gobject_class, PROP_UINT64,
1075 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
1076 g_object_class_install_property (gobject_class, PROP_DOUBLE,
1077 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
1078 g_object_class_install_property (gobject_class, PROP_STRING,
1079 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
1080 g_object_class_install_property (gobject_class, PROP_NO_WRITE,
1081 g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
1082 g_object_class_install_property (gobject_class, PROP_NO_READ,
1083 g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
1084 g_object_class_install_property (gobject_class, PROP_STRV,
1085 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
1086 g_object_class_install_property (gobject_class, PROP_ENUM,
1087 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
1088 g_object_class_install_property (gobject_class, PROP_FLAGS,
1089 g_param_spec_flags ("flags", "", "", test_flags_get_type (), TEST_FLAGS_NONE, G_PARAM_READWRITE));
1092 static TestObject *
1093 test_object_new (void)
1095 return (TestObject*)g_object_new (test_object_get_type (), NULL);
1098 /* Test basic binding functionality for simple types.
1099 * Verify that with bidirectional bindings, changes on either side
1100 * are notified on the other end.
1102 static void
1103 test_simple_binding (void)
1105 TestObject *obj;
1106 GSettings *settings;
1107 gboolean b;
1108 gchar y;
1109 gint i;
1110 guint u;
1111 gint16 n;
1112 guint16 q;
1113 gint n2;
1114 guint q2;
1115 gint64 i64;
1116 guint64 u64;
1117 gdouble d;
1118 gchar *s;
1119 GVariant *value;
1120 gchar **strv;
1122 settings = g_settings_new ("org.gtk.test.binding");
1123 obj = test_object_new ();
1125 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
1126 g_object_set (obj, "bool", TRUE, NULL);
1127 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1129 g_settings_set_boolean (settings, "bool", FALSE);
1130 b = TRUE;
1131 g_object_get (obj, "bool", &b, NULL);
1132 g_assert_cmpint (b, ==, FALSE);
1134 g_settings_bind (settings, "anti-bool", obj, "anti-bool",
1135 G_SETTINGS_BIND_INVERT_BOOLEAN);
1136 g_object_set (obj, "anti-bool", FALSE, NULL);
1137 g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
1139 g_settings_set_boolean (settings, "anti-bool", FALSE);
1140 b = FALSE;
1141 g_object_get (obj, "anti-bool", &b, NULL);
1142 g_assert_cmpint (b, ==, TRUE);
1144 g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
1146 g_object_set (obj, "byte", 123, NULL);
1147 y = 'c';
1148 g_settings_get (settings, "byte", "y", &y);
1149 g_assert_cmpint (y, ==, 123);
1151 g_settings_set (settings, "byte", "y", 54);
1152 y = 'c';
1153 g_object_get (obj, "byte", &y, NULL);
1154 g_assert_cmpint (y, ==, 54);
1156 g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
1158 g_object_set (obj, "int16", 1234, NULL);
1159 n = 4321;
1160 g_settings_get (settings, "int16", "n", &n);
1161 g_assert_cmpint (n, ==, 1234);
1163 g_settings_set (settings, "int16", "n", 4321);
1164 n2 = 1111;
1165 g_object_get (obj, "int16", &n2, NULL);
1166 g_assert_cmpint (n2, ==, 4321);
1168 g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
1170 g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1171 q = 1111;
1172 g_settings_get (settings, "uint16", "q", &q);
1173 g_assert_cmpuint (q, ==, G_MAXUINT16);
1175 g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1176 q2 = 1111;
1177 g_object_get (obj, "uint16", &q2, NULL);
1178 g_assert_cmpuint (q2, ==, (guint16) G_MAXINT16);
1180 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1182 g_object_set (obj, "int", 12345, NULL);
1183 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1185 g_settings_set_int (settings, "int", 54321);
1186 i = 1111;
1187 g_object_get (obj, "int", &i, NULL);
1188 g_assert_cmpint (i, ==, 54321);
1190 g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1192 g_object_set (obj, "uint", 12345, NULL);
1193 g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);
1195 g_settings_set_uint (settings, "uint", 54321);
1196 u = 1111;
1197 g_object_get (obj, "uint", &u, NULL);
1198 g_assert_cmpuint (u, ==, 54321);
1200 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1202 g_object_set (obj, "uint64", (guint64) 12345, NULL);
1203 g_assert_cmpuint (g_settings_get_uint64 (settings, "uint64"), ==, 12345);
1205 g_settings_set_uint64 (settings, "uint64", 54321);
1206 u64 = 1111;
1207 g_object_get (obj, "uint64", &u64, NULL);
1208 g_assert_cmpuint (u64, ==, 54321);
1210 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1212 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1213 i64 = 1111;
1214 g_settings_get (settings, "int64", "x", &i64);
1215 g_assert_cmpint (i64, ==, G_MAXINT64);
1217 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1218 i64 = 1111;
1219 g_object_get (obj, "int64", &i64, NULL);
1220 g_assert_cmpint (i64, ==, G_MININT64);
1222 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1224 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1225 u64 = 1111;
1226 g_settings_get (settings, "uint64", "t", &u64);
1227 g_assert_cmpuint (u64, ==, G_MAXUINT64);
1229 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1230 u64 = 1111;
1231 g_object_get (obj, "uint64", &u64, NULL);
1232 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1234 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1236 g_object_set (obj, "string", "bu ba", NULL);
1237 s = g_settings_get_string (settings, "string");
1238 g_assert_cmpstr (s, ==, "bu ba");
1239 g_free (s);
1241 g_settings_set_string (settings, "string", "bla bla");
1242 g_object_get (obj, "string", &s, NULL);
1243 g_assert_cmpstr (s, ==, "bla bla");
1244 g_free (s);
1246 g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1248 g_object_set (obj, "string", "non-unicode:\315", NULL);
1249 value = g_settings_get_value (settings, "chararray");
1250 g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1251 g_variant_unref (value);
1253 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1255 g_object_set (obj, "double", G_MAXFLOAT, NULL);
1256 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1258 g_settings_set_double (settings, "double", G_MINFLOAT);
1259 d = 1.0;
1260 g_object_get (obj, "double", &d, NULL);
1261 g_assert_cmpfloat (d, ==, G_MINFLOAT);
1263 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1264 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1266 g_settings_set_double (settings, "double", -G_MINDOUBLE);
1267 d = 1.0;
1268 g_object_get (obj, "double", &d, NULL);
1269 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1271 strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1272 g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1273 g_object_set (obj, "strv", strv, NULL);
1274 g_strfreev (strv);
1275 strv = g_settings_get_strv (settings, "strv");
1276 s = g_strjoinv (",", strv);
1277 g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1278 g_strfreev (strv);
1279 g_free (s);
1280 strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1281 g_settings_set_strv (settings, "strv", (const gchar **) strv);
1282 g_strfreev (strv);
1283 g_object_get (obj, "strv", &strv, NULL);
1284 s = g_strjoinv (",", strv);
1285 g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1286 g_strfreev (strv);
1287 g_free (s);
1288 g_settings_set_strv (settings, "strv", NULL);
1289 g_object_get (obj, "strv", &strv, NULL);
1290 g_assert (strv != NULL);
1291 g_assert_cmpint (g_strv_length (strv), ==, 0);
1292 g_strfreev (strv);
1294 g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1295 g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1296 s = g_settings_get_string (settings, "enum");
1297 g_assert_cmpstr (s, ==, "baz");
1298 g_free (s);
1299 g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1301 g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1302 i = 230;
1303 g_object_get (obj, "enum", &i, NULL);
1304 g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1306 g_settings_set_string (settings, "enum", "baz");
1307 i = 230;
1308 g_object_get (obj, "enum", &i, NULL);
1309 g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1311 g_settings_bind (settings, "flags", obj, "flags", G_SETTINGS_BIND_DEFAULT);
1312 g_object_set (obj, "flags", TEST_FLAGS_MOURNING, NULL);
1313 strv = g_settings_get_strv (settings, "flags");
1314 g_assert_cmpint (g_strv_length (strv), ==, 1);
1315 g_assert_cmpstr (strv[0], ==, "mourning");
1316 g_strfreev (strv);
1318 g_assert_cmpint (g_settings_get_flags (settings, "flags"), ==, TEST_FLAGS_MOURNING);
1320 g_settings_set_flags (settings, "flags", TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1321 i = 230;
1322 g_object_get (obj, "flags", &i, NULL);
1323 g_assert_cmpint (i, ==, TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1325 g_object_unref (obj);
1326 g_object_unref (settings);
1329 static void
1330 test_unbind (void)
1332 TestObject *obj;
1333 GSettings *settings;
1335 settings = g_settings_new ("org.gtk.test.binding");
1336 obj = test_object_new ();
1338 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1340 g_object_set (obj, "int", 12345, NULL);
1341 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1343 g_settings_unbind (obj, "int");
1345 g_object_set (obj, "int", 54321, NULL);
1346 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1348 g_object_unref (obj);
1349 g_object_unref (settings);
1352 static void
1353 test_bind_writable (void)
1355 TestObject *obj;
1356 GSettings *settings;
1357 gboolean b;
1359 settings = g_settings_new ("org.gtk.test.binding");
1360 obj = test_object_new ();
1362 g_object_set (obj, "bool", FALSE, NULL);
1364 g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1366 g_object_get (obj, "bool", &b, NULL);
1367 g_assert (b);
1369 g_settings_unbind (obj, "bool");
1371 g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1373 g_object_get (obj, "bool", &b, NULL);
1374 g_assert (!b);
1376 g_object_unref (obj);
1377 g_object_unref (settings);
1380 /* Test one-way bindings.
1381 * Verify that changes on one side show up on the other,
1382 * but not vice versa
1384 static void
1385 test_directional_binding (void)
1387 TestObject *obj;
1388 GSettings *settings;
1389 gboolean b;
1390 gint i;
1392 settings = g_settings_new ("org.gtk.test.binding");
1393 obj = test_object_new ();
1395 g_object_set (obj, "bool", FALSE, NULL);
1396 g_settings_set_boolean (settings, "bool", FALSE);
1398 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1400 g_settings_set_boolean (settings, "bool", TRUE);
1401 g_object_get (obj, "bool", &b, NULL);
1402 g_assert_cmpint (b, ==, TRUE);
1404 g_object_set (obj, "bool", FALSE, NULL);
1405 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1407 g_object_set (obj, "int", 20, NULL);
1408 g_settings_set_int (settings, "int", 20);
1410 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1412 g_object_set (obj, "int", 32, NULL);
1413 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1415 g_settings_set_int (settings, "int", 20);
1416 g_object_get (obj, "int", &i, NULL);
1417 g_assert_cmpint (i, ==, 32);
1419 g_object_unref (obj);
1420 g_object_unref (settings);
1423 /* Test that type mismatch is caught when creating a binding */
1424 static void
1425 test_typesafe_binding (void)
1427 if (!g_test_undefined ())
1428 return;
1430 if (g_test_subprocess ())
1432 TestObject *obj;
1433 GSettings *settings;
1435 settings = g_settings_new ("org.gtk.test.binding");
1436 obj = test_object_new ();
1438 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1440 g_object_unref (obj);
1441 g_object_unref (settings);
1442 return;
1444 g_test_trap_subprocess (NULL, 0, 0);
1445 g_test_trap_assert_failed ();
1446 g_test_trap_assert_stderr ("*not compatible*");
1449 static gboolean
1450 string_to_bool (GValue *value,
1451 GVariant *variant,
1452 gpointer user_data)
1454 const gchar *s;
1456 s = g_variant_get_string (variant, NULL);
1457 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1459 return TRUE;
1462 static GVariant *
1463 bool_to_string (const GValue *value,
1464 const GVariantType *expected_type,
1465 gpointer user_data)
1467 if (g_value_get_boolean (value))
1468 return g_variant_new_string ("true");
1469 else
1470 return g_variant_new_string ("false");
1473 /* Test custom bindings.
1474 * Translate strings to booleans and back
1476 static void
1477 test_custom_binding (void)
1479 TestObject *obj;
1480 GSettings *settings;
1481 gchar *s;
1482 gboolean b;
1484 settings = g_settings_new ("org.gtk.test.binding");
1485 obj = test_object_new ();
1487 g_settings_set_string (settings, "string", "true");
1489 g_settings_bind_with_mapping (settings, "string",
1490 obj, "bool",
1491 G_SETTINGS_BIND_DEFAULT,
1492 string_to_bool,
1493 bool_to_string,
1494 NULL, NULL);
1496 g_settings_set_string (settings, "string", "false");
1497 g_object_get (obj, "bool", &b, NULL);
1498 g_assert_cmpint (b, ==, FALSE);
1500 g_settings_set_string (settings, "string", "not true");
1501 g_object_get (obj, "bool", &b, NULL);
1502 g_assert_cmpint (b, ==, FALSE);
1504 g_object_set (obj, "bool", TRUE, NULL);
1505 s = g_settings_get_string (settings, "string");
1506 g_assert_cmpstr (s, ==, "true");
1507 g_free (s);
1509 g_object_unref (obj);
1510 g_object_unref (settings);
1513 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1514 * initial settings value is transported to the object
1515 * side, but later settings changes do not affect the
1516 * object
1518 static void
1519 test_no_change_binding (void)
1521 TestObject *obj;
1522 GSettings *settings;
1523 gboolean b;
1525 settings = g_settings_new ("org.gtk.test.binding");
1526 obj = test_object_new ();
1528 g_object_set (obj, "bool", TRUE, NULL);
1529 g_settings_set_boolean (settings, "bool", FALSE);
1531 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1533 g_object_get (obj, "bool", &b, NULL);
1534 g_assert_cmpint (b, ==, FALSE);
1536 g_settings_set_boolean (settings, "bool", TRUE);
1537 g_object_get (obj, "bool", &b, NULL);
1538 g_assert_cmpint (b, ==, FALSE);
1540 g_settings_set_boolean (settings, "bool", FALSE);
1541 g_object_set (obj, "bool", TRUE, NULL);
1542 b = g_settings_get_boolean (settings, "bool");
1543 g_assert_cmpint (b, ==, TRUE);
1545 g_object_unref (obj);
1546 g_object_unref (settings);
1549 /* Test that binding a non-readable property only
1550 * works in 'GET' mode.
1552 static void
1553 test_no_read_binding_fail (void)
1555 TestObject *obj;
1556 GSettings *settings;
1558 settings = g_settings_new ("org.gtk.test.binding");
1559 obj = test_object_new ();
1561 g_settings_bind (settings, "string", obj, "no-read", 0);
1564 static void
1565 test_no_read_binding_pass (void)
1567 TestObject *obj;
1568 GSettings *settings;
1570 settings = g_settings_new ("org.gtk.test.binding");
1571 obj = test_object_new ();
1573 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1575 exit (0);
1578 static void
1579 test_no_read_binding (void)
1581 if (g_test_undefined ())
1583 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/fail", 0, 0);
1584 g_test_trap_assert_failed ();
1585 g_test_trap_assert_stderr ("*property*is not readable*");
1588 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/pass", 0, 0);
1589 g_test_trap_assert_passed ();
1592 /* Test that binding a non-writable property only
1593 * works in 'SET' mode.
1595 static void
1596 test_no_write_binding_fail (void)
1598 TestObject *obj;
1599 GSettings *settings;
1601 settings = g_settings_new ("org.gtk.test.binding");
1602 obj = test_object_new ();
1604 g_settings_bind (settings, "string", obj, "no-write", 0);
1607 static void
1608 test_no_write_binding_pass (void)
1610 TestObject *obj;
1611 GSettings *settings;
1613 settings = g_settings_new ("org.gtk.test.binding");
1614 obj = test_object_new ();
1616 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1618 exit (0);
1621 static void
1622 test_no_write_binding (void)
1624 if (g_test_undefined ())
1626 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/fail", 0, 0);
1627 g_test_trap_assert_failed ();
1628 g_test_trap_assert_stderr ("*property*is not writable*");
1631 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/pass", 0, 0);
1632 g_test_trap_assert_passed ();
1635 static void
1636 key_changed_cb (GSettings *settings, const gchar *key, gpointer data)
1638 gboolean *b = data;
1639 (*b) = TRUE;
1643 * Test that using a keyfile works
1645 static void
1646 test_keyfile (void)
1648 GSettingsBackend *kf_backend;
1649 GSettings *settings;
1650 GKeyFile *keyfile;
1651 gchar *str;
1652 gboolean writable;
1653 GError *error = NULL;
1654 gchar *data;
1655 gsize len;
1656 gboolean called = FALSE;
1658 g_remove ("keyfile/gsettings.store");
1659 g_rmdir ("keyfile");
1661 kf_backend = g_keyfile_settings_backend_new ("keyfile/gsettings.store", "/", "root");
1662 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1663 g_object_unref (kf_backend);
1665 g_settings_reset (settings, "greeting");
1666 str = g_settings_get_string (settings, "greeting");
1667 g_assert_cmpstr (str, ==, "Hello, earthlings");
1668 g_free (str);
1670 writable = g_settings_is_writable (settings, "greeting");
1671 g_assert (writable);
1672 g_settings_set (settings, "greeting", "s", "see if this works");
1674 str = g_settings_get_string (settings, "greeting");
1675 g_assert_cmpstr (str, ==, "see if this works");
1676 g_free (str);
1678 g_settings_delay (settings);
1679 g_settings_set (settings, "farewell", "s", "cheerio");
1680 g_settings_apply (settings);
1682 keyfile = g_key_file_new ();
1683 g_assert (g_key_file_load_from_file (keyfile, "keyfile/gsettings.store", 0, NULL));
1685 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1686 g_assert_cmpstr (str, ==, "'see if this works'");
1687 g_free (str);
1689 str = g_key_file_get_string (keyfile, "tests", "farewell", NULL);
1690 g_assert_cmpstr (str, ==, "'cheerio'");
1691 g_free (str);
1692 g_key_file_free (keyfile);
1694 g_settings_reset (settings, "greeting");
1695 g_settings_apply (settings);
1696 keyfile = g_key_file_new ();
1697 g_assert (g_key_file_load_from_file (keyfile, "keyfile/gsettings.store", 0, NULL));
1699 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1700 g_assert (str == NULL);
1702 called = FALSE;
1703 g_signal_connect (settings, "changed::greeting", G_CALLBACK (key_changed_cb), &called);
1705 g_key_file_set_string (keyfile, "tests", "greeting", "'howdy'");
1706 data = g_key_file_to_data (keyfile, &len, NULL);
1707 g_file_set_contents ("keyfile/gsettings.store", data, len, &error);
1708 g_assert_no_error (error);
1709 while (!called)
1710 g_main_context_iteration (NULL, FALSE);
1711 g_signal_handlers_disconnect_by_func (settings, key_changed_cb, &called);
1713 str = g_settings_get_string (settings, "greeting");
1714 g_assert_cmpstr (str, ==, "howdy");
1715 g_free (str);
1717 g_settings_set (settings, "farewell", "s", "cheerio");
1719 called = FALSE;
1720 g_signal_connect (settings, "writable-changed::greeting", G_CALLBACK (key_changed_cb), &called);
1722 g_chmod ("keyfile", 0500);
1723 while (!called)
1724 g_main_context_iteration (NULL, FALSE);
1725 g_signal_handlers_disconnect_by_func (settings, key_changed_cb, &called);
1727 writable = g_settings_is_writable (settings, "greeting");
1728 g_assert (!writable);
1730 g_key_file_free (keyfile);
1731 g_free (data);
1733 g_object_unref (settings);
1734 g_chmod ("keyfile", 0777);
1737 /* Test that getting child schemas works
1739 static void
1740 test_child_schema (void)
1742 GSettings *settings;
1743 GSettings *child;
1744 guint8 byte;
1746 /* first establish some known conditions */
1747 settings = g_settings_new ("org.gtk.test.basic-types");
1748 g_settings_set (settings, "test-byte", "y", 36);
1750 g_settings_get (settings, "test-byte", "y", &byte);
1751 g_assert_cmpint (byte, ==, 36);
1753 g_object_unref (settings);
1755 settings = g_settings_new ("org.gtk.test");
1756 child = g_settings_get_child (settings, "basic-types");
1757 g_assert (child != NULL);
1759 g_settings_get (child, "test-byte", "y", &byte);
1760 g_assert_cmpint (byte, ==, 36);
1762 g_object_unref (child);
1763 g_object_unref (settings);
1766 #include "../strinfo.c"
1768 static void
1769 test_strinfo (void)
1771 /* "foo" has a value of 1
1772 * "bar" has a value of 2
1773 * "baz" is an alias for "bar"
1775 gchar array[] =
1776 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1777 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1778 "\0\0\0\xff";
1779 const guint32 *strinfo = (guint32 *) array;
1780 guint length = sizeof array / 4;
1781 guint result;
1784 /* build it and compare */
1785 GString *builder;
1787 builder = g_string_new (NULL);
1788 strinfo_builder_append_item (builder, "foo", 1);
1789 strinfo_builder_append_item (builder, "bar", 2);
1790 g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
1791 g_assert_cmpmem (builder->str, builder->len, strinfo, length * 4);
1792 g_string_free (builder, TRUE);
1795 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
1796 ==, NULL);
1797 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
1798 ==, NULL);
1799 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
1800 ==, "bar");
1801 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
1802 ==, NULL);
1804 g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
1805 g_assert_cmpint (result, ==, 1);
1806 g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
1807 g_assert_cmpint (result, ==, 2);
1808 g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
1809 g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
1811 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
1812 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
1813 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
1814 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
1816 g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
1817 g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
1818 g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
1819 g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
1822 static void
1823 test_enums_non_enum_key (void)
1825 GSettings *direct;
1827 direct = g_settings_new ("org.gtk.test.enums.direct");
1828 g_settings_get_enum (direct, "test");
1829 g_assert_not_reached ();
1832 static void
1833 test_enums_non_enum_value (void)
1835 GSettings *settings;
1837 settings = g_settings_new ("org.gtk.test.enums");
1838 g_settings_set_enum (settings, "test", 42);
1839 g_assert_not_reached ();
1842 static void
1843 test_enums_range (void)
1845 GSettings *settings;
1847 settings = g_settings_new ("org.gtk.test.enums");
1848 g_settings_set_string (settings, "test", "qux");
1849 g_assert_not_reached ();
1852 static void
1853 test_enums_non_flags (void)
1855 GSettings *settings;
1857 settings = g_settings_new ("org.gtk.test.enums");
1858 g_settings_get_flags (settings, "test");
1859 g_assert_not_reached ();
1862 static void
1863 test_enums (void)
1865 GSettings *settings, *direct;
1866 gchar *str;
1868 settings = g_settings_new ("org.gtk.test.enums");
1869 direct = g_settings_new ("org.gtk.test.enums.direct");
1871 if (g_test_undefined () && !backend_set)
1873 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-key", 0, 0);
1874 g_test_trap_assert_failed ();
1875 g_test_trap_assert_stderr ("*not associated with an enum*");
1877 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-value", 0, 0);
1878 g_test_trap_assert_failed ();
1879 g_test_trap_assert_stderr ("*invalid enum value 42*");
1881 g_test_trap_subprocess ("/gsettings/enums/subprocess/range", 0, 0);
1882 g_test_trap_assert_failed ();
1883 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1885 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-flags", 0, 0);
1886 g_test_trap_assert_failed ();
1887 g_test_trap_assert_stderr ("*not associated with a flags*");
1890 str = g_settings_get_string (settings, "test");
1891 g_assert_cmpstr (str, ==, "bar");
1892 g_free (str);
1894 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
1896 str = g_settings_get_string (settings, "test");
1897 g_assert_cmpstr (str, ==, "foo");
1898 g_free (str);
1900 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
1902 g_settings_set_string (direct, "test", "qux");
1904 str = g_settings_get_string (direct, "test");
1905 g_assert_cmpstr (str, ==, "qux");
1906 g_free (str);
1908 str = g_settings_get_string (settings, "test");
1909 g_assert_cmpstr (str, ==, "quux");
1910 g_free (str);
1912 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
1914 g_object_unref (direct);
1915 g_object_unref (settings);
1918 static void
1919 test_flags_non_flags_key (void)
1921 GSettings *direct;
1923 direct = g_settings_new ("org.gtk.test.enums.direct");
1924 g_settings_get_flags (direct, "test");
1925 g_assert_not_reached ();
1928 static void
1929 test_flags_non_flags_value (void)
1931 GSettings *settings;
1933 settings = g_settings_new ("org.gtk.test.enums");
1934 g_settings_set_flags (settings, "f-test", 0x42);
1935 g_assert_not_reached ();
1938 static void
1939 test_flags_range (void)
1941 GSettings *settings;
1943 settings = g_settings_new ("org.gtk.test.enums");
1944 g_settings_set_strv (settings, "f-test",
1945 (const gchar **) g_strsplit ("rock", ",", 0));
1946 g_assert_not_reached ();
1949 static void
1950 test_flags_non_enum (void)
1952 GSettings *settings;
1954 settings = g_settings_new ("org.gtk.test.enums");
1955 g_settings_get_enum (settings, "f-test");
1956 g_assert_not_reached ();
1959 static void
1960 test_flags (void)
1962 GSettings *settings, *direct;
1963 gchar **strv;
1964 gchar *str;
1966 settings = g_settings_new ("org.gtk.test.enums");
1967 direct = g_settings_new ("org.gtk.test.enums.direct");
1969 if (g_test_undefined () && !backend_set)
1971 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-key", 0, 0);
1972 g_test_trap_assert_failed ();
1973 g_test_trap_assert_stderr ("*not associated with a flags*");
1975 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-value", 0, 0);
1976 g_test_trap_assert_failed ();
1977 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
1979 g_test_trap_subprocess ("/gsettings/flags/subprocess/range", 0, 0);
1980 g_test_trap_assert_failed ();
1981 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1983 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-enum", 0, 0);
1984 g_test_trap_assert_failed ();
1985 g_test_trap_assert_stderr ("*not associated with an enum*");
1988 strv = g_settings_get_strv (settings, "f-test");
1989 str = g_strjoinv (",", strv);
1990 g_assert_cmpstr (str, ==, "");
1991 g_strfreev (strv);
1992 g_free (str);
1994 g_settings_set_flags (settings, "f-test",
1995 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1997 strv = g_settings_get_strv (settings, "f-test");
1998 str = g_strjoinv (",", strv);
1999 g_assert_cmpstr (str, ==, "talking,walking");
2000 g_strfreev (strv);
2001 g_free (str);
2003 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
2004 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
2006 strv = g_strsplit ("speaking,laughing", ",", 0);
2007 g_settings_set_strv (direct, "f-test", (const gchar **) strv);
2008 g_strfreev (strv);
2010 strv = g_settings_get_strv (direct, "f-test");
2011 str = g_strjoinv (",", strv);
2012 g_assert_cmpstr (str, ==, "speaking,laughing");
2013 g_strfreev (strv);
2014 g_free (str);
2016 strv = g_settings_get_strv (settings, "f-test");
2017 str = g_strjoinv (",", strv);
2018 g_assert_cmpstr (str, ==, "talking,laughing");
2019 g_strfreev (strv);
2020 g_free (str);
2022 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
2023 TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
2025 g_object_unref (direct);
2026 g_object_unref (settings);
2029 static void
2030 test_range_high (void)
2032 GSettings *settings;
2034 settings = g_settings_new ("org.gtk.test.range");
2035 g_settings_set_int (settings, "val", 45);
2036 g_assert_not_reached ();
2039 static void
2040 test_range_low (void)
2042 GSettings *settings;
2044 settings = g_settings_new ("org.gtk.test.range");
2045 g_settings_set_int (settings, "val", 1);
2046 g_assert_not_reached ();
2049 static void
2050 test_range (void)
2052 GSettings *settings, *direct;
2053 GVariant *value;
2055 settings = g_settings_new ("org.gtk.test.range");
2056 direct = g_settings_new ("org.gtk.test.range.direct");
2058 if (g_test_undefined () && !backend_set)
2060 g_test_trap_subprocess ("/gsettings/range/subprocess/high", 0, 0);
2061 g_test_trap_assert_failed ();
2062 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2064 g_test_trap_subprocess ("/gsettings/range/subprocess/low", 0, 0);
2065 g_test_trap_assert_failed ();
2066 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2069 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2070 g_settings_set_int (direct, "val", 22);
2071 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
2072 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
2073 g_settings_set_int (direct, "val", 45);
2074 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
2075 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2076 g_settings_set_int (direct, "val", 1);
2077 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
2078 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2080 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2081 value = g_variant_new_int32 (1);
2082 g_assert (!g_settings_range_check (settings, "val", value));
2083 g_variant_unref (value);
2084 value = g_variant_new_int32 (33);
2085 g_assert (g_settings_range_check (settings, "val", value));
2086 g_variant_unref (value);
2087 value = g_variant_new_int32 (45);
2088 g_assert (!g_settings_range_check (settings, "val", value));
2089 g_variant_unref (value);
2090 G_GNUC_END_IGNORE_DEPRECATIONS
2092 g_object_unref (direct);
2093 g_object_unref (settings);
2096 static gboolean
2097 strv_has_string (gchar **haystack,
2098 const gchar *needle)
2100 guint n;
2102 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
2104 if (g_strcmp0 (haystack[n], needle) == 0)
2105 return TRUE;
2107 return FALSE;
2110 static gboolean
2111 strv_set_equal (gchar **strv, ...)
2113 gint count;
2114 va_list list;
2115 const gchar *str;
2116 gboolean res;
2118 res = TRUE;
2119 count = 0;
2120 va_start (list, strv);
2121 while (1)
2123 str = va_arg (list, const gchar *);
2124 if (str == NULL)
2125 break;
2126 if (!strv_has_string (strv, str))
2128 res = FALSE;
2129 break;
2131 count++;
2133 va_end (list);
2135 if (res)
2136 res = g_strv_length ((gchar**)strv) == count;
2138 return res;
2141 static void
2142 test_list_items (void)
2144 GSettingsSchema *schema;
2145 GSettings *settings;
2146 gchar **children;
2147 gchar **keys;
2149 settings = g_settings_new ("org.gtk.test");
2150 g_object_get (settings, "settings-schema", &schema, NULL);
2151 children = g_settings_list_children (settings);
2152 keys = g_settings_schema_list_keys (schema);
2154 g_assert (strv_set_equal (children, "basic-types", "complex-types", "localized", NULL));
2155 g_assert (strv_set_equal (keys, "greeting", "farewell", NULL));
2157 g_strfreev (children);
2158 g_strfreev (keys);
2160 g_settings_schema_unref (schema);
2161 g_object_unref (settings);
2164 static void
2165 test_list_schemas (void)
2167 const gchar * const *schemas;
2168 const gchar * const *relocs;
2170 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2171 relocs = g_settings_list_relocatable_schemas ();
2172 schemas = g_settings_list_schemas ();
2173 G_GNUC_END_IGNORE_DEPRECATIONS
2175 g_assert (strv_set_equal ((gchar **)relocs,
2176 "org.gtk.test.no-path",
2177 "org.gtk.test.extends.base",
2178 "org.gtk.test.extends.extended",
2179 NULL));
2181 g_assert (strv_set_equal ((gchar **)schemas,
2182 "org.gtk.test",
2183 "org.gtk.test.basic-types",
2184 "org.gtk.test.complex-types",
2185 "org.gtk.test.localized",
2186 "org.gtk.test.binding",
2187 "org.gtk.test.enums",
2188 "org.gtk.test.enums.direct",
2189 "org.gtk.test.range",
2190 "org.gtk.test.range.direct",
2191 "org.gtk.test.mapped",
2192 "org.gtk.test.descriptions",
2193 NULL));
2196 static gboolean
2197 map_func (GVariant *value,
2198 gpointer *result,
2199 gpointer user_data)
2201 gint *state = user_data;
2202 gint v;
2204 if (value)
2205 v = g_variant_get_int32 (value);
2206 else
2207 v = -1;
2209 if (*state == 0)
2211 g_assert_cmpint (v, ==, 1);
2212 (*state)++;
2213 return FALSE;
2215 else if (*state == 1)
2217 g_assert_cmpint (v, ==, 0);
2218 (*state)++;
2219 return FALSE;
2221 else
2223 g_assert (value == NULL);
2224 *result = g_variant_new_int32 (5);
2225 return TRUE;
2229 static void
2230 test_get_mapped (void)
2232 GSettings *settings;
2233 gint state;
2234 gpointer p;
2235 gint val;
2237 settings = g_settings_new ("org.gtk.test.mapped");
2238 g_settings_set_int (settings, "val", 1);
2240 state = 0;
2241 p = g_settings_get_mapped (settings, "val", map_func, &state);
2242 val = g_variant_get_int32 ((GVariant*)p);
2243 g_assert_cmpint (val, ==, 5);
2245 g_variant_unref (p);
2246 g_object_unref (settings);
2249 static void
2250 test_get_range (void)
2252 GSettings *settings;
2253 GVariant *range;
2255 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2256 settings = g_settings_new ("org.gtk.test.range");
2257 range = g_settings_get_range (settings, "val");
2258 check_and_free (range, "('range', <(2, 44)>)");
2259 g_object_unref (settings);
2261 settings = g_settings_new ("org.gtk.test.enums");
2262 range = g_settings_get_range (settings, "test");
2263 check_and_free (range, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
2264 g_object_unref (settings);
2266 settings = g_settings_new ("org.gtk.test.enums");
2267 range = g_settings_get_range (settings, "f-test");
2268 check_and_free (range, "('flags', "
2269 "<['mourning', 'laughing', 'talking', 'walking']>)");
2270 g_object_unref (settings);
2272 settings = g_settings_new ("org.gtk.test");
2273 range = g_settings_get_range (settings, "greeting");
2274 check_and_free (range, "('type', <@as []>)");
2275 g_object_unref (settings);
2276 G_GNUC_END_IGNORE_DEPRECATIONS
2279 static void
2280 test_schema_source (void)
2282 GSettingsSchemaSource *parent;
2283 GSettingsSchemaSource *source;
2284 GSettingsBackend *backend;
2285 GSettingsSchema *schema;
2286 GError *error = NULL;
2287 GSettings *settings;
2288 gboolean enabled;
2290 backend = g_settings_backend_get_default ();
2292 /* make sure it fails properly */
2293 parent = g_settings_schema_source_get_default ();
2294 source = g_settings_schema_source_new_from_directory ("/path/that/does/not/exist", parent, TRUE, &error);
2295 g_assert (source == NULL);
2296 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
2297 g_clear_error (&error);
2299 /* create a source with the parent */
2300 source = g_settings_schema_source_new_from_directory ("schema-source", parent, TRUE, &error);
2301 g_assert_no_error (error);
2302 g_assert (source != NULL);
2304 /* check recursive lookups are working */
2305 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2306 g_assert (schema != NULL);
2307 g_settings_schema_unref (schema);
2309 /* check recursive lookups for non-existent schemas */
2310 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", TRUE);
2311 g_assert (schema == NULL);
2313 /* check non-recursive for schema that only exists in lower layers */
2314 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2315 g_assert (schema == NULL);
2317 /* check non-recursive lookup for non-existent */
2318 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", FALSE);
2319 g_assert (schema == NULL);
2321 /* check non-recursive for schema that exists in toplevel */
2322 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2323 g_assert (schema != NULL);
2324 g_settings_schema_unref (schema);
2326 /* check recursive for schema that exists in toplevel */
2327 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2328 g_assert (schema != NULL);
2330 /* try to use it for something */
2331 settings = g_settings_new_full (schema, backend, g_settings_schema_get_path (schema));
2332 g_settings_schema_unref (schema);
2333 enabled = FALSE;
2334 g_settings_get (settings, "enabled", "b", &enabled);
2335 g_assert (enabled);
2336 g_object_unref (settings);
2338 g_settings_schema_source_unref (source);
2340 /* try again, but with no parent */
2341 source = g_settings_schema_source_new_from_directory ("schema-source", NULL, FALSE, NULL);
2342 g_assert (source != NULL);
2344 /* should not find it this time, even if recursive... */
2345 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2346 g_assert (schema == NULL);
2347 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2348 g_assert (schema == NULL);
2350 /* should still find our own... */
2351 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2352 g_assert (schema != NULL);
2353 g_settings_schema_unref (schema);
2354 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2355 g_assert (schema != NULL);
2356 g_settings_schema_unref (schema);
2358 g_settings_schema_source_unref (source);
2361 static void
2362 test_schema_list_keys (void)
2364 gchar **keys;
2365 GSettingsSchemaSource *src = g_settings_schema_source_get_default ();
2366 GSettingsSchema *schema = g_settings_schema_source_lookup (src, "org.gtk.test", TRUE);
2367 g_assert (schema != NULL);
2369 keys = g_settings_schema_list_keys (schema);
2371 g_assert (strv_set_equal ((gchar **)keys,
2372 "greeting",
2373 "farewell",
2374 NULL));
2376 g_strfreev (keys);
2377 g_settings_schema_unref (schema);
2380 static void
2381 test_actions (void)
2383 GAction *string, *toggle;
2384 gboolean c1, c2, c3;
2385 GSettings *settings;
2386 gchar *name;
2387 GVariantType *param_type;
2388 gboolean enabled;
2389 GVariantType *state_type;
2390 GVariant *state;
2392 settings = g_settings_new ("org.gtk.test.basic-types");
2393 string = g_settings_create_action (settings, "test-string");
2394 toggle = g_settings_create_action (settings, "test-boolean");
2395 g_object_unref (settings); /* should be held by the actions */
2397 g_signal_connect (settings, "changed", G_CALLBACK (changed_cb2), &c1);
2398 g_signal_connect (string, "notify::state", G_CALLBACK (changed_cb2), &c2);
2399 g_signal_connect (toggle, "notify::state", G_CALLBACK (changed_cb2), &c3);
2401 c1 = c2 = c3 = FALSE;
2402 g_settings_set_string (settings, "test-string", "hello world");
2403 check_and_free (g_action_get_state (string), "'hello world'");
2404 g_assert (c1 && c2 && !c3);
2405 c1 = c2 = c3 = FALSE;
2407 g_action_activate (string, g_variant_new_string ("hihi"));
2408 check_and_free (g_settings_get_value (settings, "test-string"), "'hihi'");
2409 g_assert (c1 && c2 && !c3);
2410 c1 = c2 = c3 = FALSE;
2412 g_action_change_state (string, g_variant_new_string ("kthxbye"));
2413 check_and_free (g_settings_get_value (settings, "test-string"), "'kthxbye'");
2414 g_assert (c1 && c2 && !c3);
2415 c1 = c2 = c3 = FALSE;
2417 g_action_change_state (toggle, g_variant_new_boolean (TRUE));
2418 g_assert (g_settings_get_boolean (settings, "test-boolean"));
2419 g_assert (c1 && !c2 && c3);
2420 c1 = c2 = c3 = FALSE;
2422 g_action_activate (toggle, NULL);
2423 g_assert (!g_settings_get_boolean (settings, "test-boolean"));
2424 g_assert (c1 && !c2 && c3);
2426 g_object_get (string,
2427 "name", &name,
2428 "parameter-type", &param_type,
2429 "enabled", &enabled,
2430 "state-type", &state_type,
2431 "state", &state,
2432 NULL);
2434 g_assert_cmpstr (name, ==, "test-string");
2435 g_assert (g_variant_type_equal (param_type, G_VARIANT_TYPE_STRING));
2436 g_assert (enabled);
2437 g_assert (g_variant_type_equal (state_type, G_VARIANT_TYPE_STRING));
2438 g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "kthxbye");
2440 g_free (name);
2441 g_variant_type_free (param_type);
2442 g_variant_type_free (state_type);
2443 g_variant_unref (state);
2445 g_object_unref (string);
2446 g_object_unref (toggle);
2449 static void
2450 test_null_backend (void)
2452 GSettingsBackend *backend;
2453 GSettings *settings;
2454 gchar *str;
2455 gboolean writable;
2457 backend = g_null_settings_backend_new ();
2458 settings = g_settings_new_with_backend_and_path ("org.gtk.test", backend, "/tests/");
2460 g_object_get (settings, "schema-id", &str, NULL);
2461 g_assert_cmpstr (str, ==, "org.gtk.test");
2462 g_free (str);
2464 g_settings_get (settings, "greeting", "s", &str);
2465 g_assert_cmpstr (str, ==, "Hello, earthlings");
2466 g_free (str);
2468 g_settings_set (settings, "greeting", "s", "goodbye world");
2469 g_settings_get (settings, "greeting", "s", &str);
2470 g_assert_cmpstr (str, ==, "Hello, earthlings");
2471 g_free (str);
2473 writable = g_settings_is_writable (settings, "greeting");
2474 g_assert (!writable);
2476 g_settings_reset (settings, "greeting");
2478 g_settings_delay (settings);
2479 g_settings_set (settings, "greeting", "s", "goodbye world");
2480 g_settings_apply (settings);
2481 g_settings_get (settings, "greeting", "s", &str);
2482 g_assert_cmpstr (str, ==, "Hello, earthlings");
2483 g_free (str);
2485 g_object_unref (settings);
2486 g_object_unref (backend);
2489 static void
2490 test_memory_backend (void)
2492 GSettingsBackend *backend;
2494 backend = g_memory_settings_backend_new ();
2495 g_assert (G_IS_SETTINGS_BACKEND (backend));
2496 g_object_unref (backend);
2499 static void
2500 test_read_descriptions (void)
2502 GSettingsSchema *schema;
2503 GSettingsSchemaKey *key;
2504 GSettings *settings;
2506 settings = g_settings_new ("org.gtk.test");
2507 g_object_get (settings, "settings-schema", &schema, NULL);
2508 key = g_settings_schema_get_key (schema, "greeting");
2510 g_assert_cmpstr (g_settings_schema_key_get_summary (key), ==, "A greeting");
2511 g_assert_cmpstr (g_settings_schema_key_get_description (key), ==, "Greeting of the invading martians");
2513 g_settings_schema_key_unref (key);
2514 g_settings_schema_unref (schema);
2516 g_object_unref (settings);
2518 settings = g_settings_new ("org.gtk.test.descriptions");
2519 g_object_get (settings, "settings-schema", &schema, NULL);
2520 key = g_settings_schema_get_key (schema, "a");
2522 g_assert_cmpstr (g_settings_schema_key_get_summary (key), ==,
2523 "a paragraph.\n\n"
2524 "with some whitespace.\n\n"
2525 "because not everyone has a great editor.\n\n"
2526 "lots of space is as one.");
2528 g_settings_schema_key_unref (key);
2529 g_settings_schema_unref (schema);
2531 g_object_unref (settings);
2534 static void
2535 test_default_value (void)
2537 GSettings *settings;
2538 GSettingsSchema *schema;
2539 GSettingsSchemaKey *key;
2540 GVariant *v;
2541 const gchar *str;
2542 gchar *s;
2544 settings = g_settings_new ("org.gtk.test");
2545 g_object_get (settings, "settings-schema", &schema, NULL);
2546 key = g_settings_schema_get_key (schema, "greeting");
2547 g_settings_schema_unref (schema);
2548 g_settings_schema_key_ref (key);
2550 g_assert (g_variant_type_equal (g_settings_schema_key_get_value_type (key), G_VARIANT_TYPE_STRING));
2552 v = g_settings_schema_key_get_default_value (key);
2553 str = g_variant_get_string (v, NULL);
2554 g_assert_cmpstr (str, ==, "Hello, earthlings");
2555 g_variant_unref (v);
2557 g_settings_schema_key_unref (key);
2558 g_settings_schema_key_unref (key);
2560 g_settings_set (settings, "greeting", "s", "goodbye world");
2562 v = g_settings_get_user_value (settings, "greeting");
2563 str = g_variant_get_string (v, NULL);
2564 g_assert_cmpstr (str, ==, "goodbye world");
2565 g_variant_unref (v);
2567 v = g_settings_get_default_value (settings, "greeting");
2568 str = g_variant_get_string (v, NULL);
2569 g_assert_cmpstr (str, ==, "Hello, earthlings");
2570 g_variant_unref (v);
2572 g_settings_reset (settings, "greeting");
2574 v = g_settings_get_user_value (settings, "greeting");
2575 g_assert_null (v);
2577 s = g_settings_get_string (settings, "greeting");
2578 g_assert_cmpstr (s, ==, "Hello, earthlings");
2579 g_free (s);
2581 g_object_unref (settings);
2584 static void
2585 test_extended_schema (void)
2587 GSettingsSchema *schema;
2588 GSettings *settings;
2589 gchar **keys;
2591 settings = g_settings_new_with_path ("org.gtk.test.extends.extended", "/test/extendes/");
2592 g_object_get (settings, "settings-schema", &schema, NULL);
2593 keys = g_settings_schema_list_keys (schema);
2594 g_assert (strv_set_equal (keys, "int32", "string", "another-int32", NULL));
2595 g_strfreev (keys);
2596 g_object_unref (settings);
2597 g_settings_schema_unref (schema);
2601 main (int argc, char *argv[])
2603 gchar *schema_text;
2604 gchar *enums;
2605 gint result;
2607 setlocale (LC_ALL, "");
2609 g_test_init (&argc, &argv, NULL);
2611 if (!g_test_subprocess ())
2613 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
2615 g_setenv ("XDG_DATA_DIRS", ".", TRUE);
2616 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
2618 if (!backend_set)
2619 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
2621 g_remove ("org.gtk.test.enums.xml");
2622 g_assert (g_spawn_command_line_sync ("../../gobject/glib-mkenums "
2623 "--template " SRCDIR "/enums.xml.template "
2624 SRCDIR "/testenum.h",
2625 &enums, NULL, &result, NULL));
2626 g_assert (result == 0);
2627 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
2628 g_free (enums);
2630 g_assert (g_file_get_contents (SRCDIR "/org.gtk.test.gschema.xml.orig", &schema_text, NULL, NULL));
2631 g_assert (g_file_set_contents ("org.gtk.test.gschema.xml", schema_text, -1, NULL));
2632 g_free (schema_text);
2634 g_remove ("gschemas.compiled");
2635 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=. "
2636 "--schema-file=org.gtk.test.enums.xml "
2637 "--schema-file=org.gtk.test.gschema.xml",
2638 NULL, NULL, &result, NULL));
2639 g_assert (result == 0);
2641 g_remove ("schema-source/gschemas.compiled");
2642 g_mkdir ("schema-source", 0777);
2643 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=schema-source "
2644 "--schema-file=" SRCDIR "/org.gtk.schemasourcecheck.gschema.xml",
2645 NULL, NULL, &result, NULL));
2646 g_assert (result == 0);
2649 g_test_add_func ("/gsettings/basic", test_basic);
2651 if (!backend_set)
2653 g_test_add_func ("/gsettings/no-schema", test_no_schema);
2654 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
2655 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
2656 g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
2657 g_test_add_func ("/gsettings/no-path", test_no_path);
2660 g_test_add_func ("/gsettings/basic-types", test_basic_types);
2661 g_test_add_func ("/gsettings/complex-types", test_complex_types);
2662 g_test_add_func ("/gsettings/changes", test_changes);
2664 g_test_add_func ("/gsettings/l10n", test_l10n);
2665 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
2667 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
2668 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
2669 g_test_add_func ("/gsettings/delay-child", test_delay_child);
2670 g_test_add_func ("/gsettings/atomic", test_atomic);
2672 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
2673 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
2674 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
2675 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
2676 g_test_add_func ("/gsettings/unbinding", test_unbind);
2677 g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
2679 if (!backend_set)
2681 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
2682 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
2683 g_test_add_func ("/gsettings/no-read-binding/subprocess/fail", test_no_read_binding_fail);
2684 g_test_add_func ("/gsettings/no-read-binding/subprocess/pass", test_no_read_binding_pass);
2685 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
2686 g_test_add_func ("/gsettings/no-write-binding/subprocess/fail", test_no_write_binding_fail);
2687 g_test_add_func ("/gsettings/no-write-binding/subprocess/pass", test_no_write_binding_pass);
2690 g_test_add_func ("/gsettings/keyfile", test_keyfile);
2691 g_test_add_func ("/gsettings/child-schema", test_child_schema);
2692 g_test_add_func ("/gsettings/strinfo", test_strinfo);
2693 g_test_add_func ("/gsettings/enums", test_enums);
2694 g_test_add_func ("/gsettings/enums/subprocess/non-enum-key", test_enums_non_enum_key);
2695 g_test_add_func ("/gsettings/enums/subprocess/non-enum-value", test_enums_non_enum_value);
2696 g_test_add_func ("/gsettings/enums/subprocess/range", test_enums_range);
2697 g_test_add_func ("/gsettings/enums/subprocess/non-flags", test_enums_non_flags);
2698 g_test_add_func ("/gsettings/flags", test_flags);
2699 g_test_add_func ("/gsettings/flags/subprocess/non-flags-key", test_flags_non_flags_key);
2700 g_test_add_func ("/gsettings/flags/subprocess/non-flags-value", test_flags_non_flags_value);
2701 g_test_add_func ("/gsettings/flags/subprocess/range", test_flags_range);
2702 g_test_add_func ("/gsettings/flags/subprocess/non-enum", test_flags_non_enum);
2703 g_test_add_func ("/gsettings/range", test_range);
2704 g_test_add_func ("/gsettings/range/subprocess/high", test_range_high);
2705 g_test_add_func ("/gsettings/range/subprocess/low", test_range_low);
2706 g_test_add_func ("/gsettings/list-items", test_list_items);
2707 g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
2708 g_test_add_func ("/gsettings/mapped", test_get_mapped);
2709 g_test_add_func ("/gsettings/get-range", test_get_range);
2710 g_test_add_func ("/gsettings/schema-source", test_schema_source);
2711 g_test_add_func ("/gsettings/schema-list-keys", test_schema_list_keys);
2712 g_test_add_func ("/gsettings/actions", test_actions);
2713 g_test_add_func ("/gsettings/null-backend", test_null_backend);
2714 g_test_add_func ("/gsettings/memory-backend", test_memory_backend);
2715 g_test_add_func ("/gsettings/read-descriptions", test_read_descriptions);
2716 g_test_add_func ("/gsettings/test-extended-schema", test_extended_schema);
2717 g_test_add_func ("/gsettings/default-value", test_default_value);
2719 result = g_test_run ();
2721 g_settings_sync ();
2723 return result;