Merge branch 'g-clear-pointer-no-side-effects' into 'master'
[glib.git] / gio / tests / gsettings.c
blob852a8b710bd23f36a91240cfab917e10817007d1
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 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.
19 static void
20 check_and_free (GVariant *value,
21 const gchar *expected)
23 gchar *printed;
25 printed = g_variant_print (value, TRUE);
26 g_assert_cmpstr (printed, ==, expected);
27 g_free (printed);
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
36 static void
37 test_basic (void)
39 gchar *str = NULL;
40 GObject *b;
41 gchar *path;
42 gboolean has_unapplied;
43 gboolean delay_apply;
44 GSettings *settings;
46 settings = g_settings_new ("org.gtk.test");
48 g_object_get (settings,
49 "schema-id", &str,
50 "backend", &b,
51 "path", &path,
52 "has-unapplied", &has_unapplied,
53 "delay-apply", &delay_apply,
54 NULL);
55 g_assert_cmpstr (str, ==, "org.gtk.test");
56 g_assert (b != NULL);
57 g_assert_cmpstr (path, ==, "/tests/");
58 g_assert (!has_unapplied);
59 g_assert (!delay_apply);
60 g_free (str);
61 g_object_unref (b);
62 g_free (path);
64 g_settings_get (settings, "greeting", "s", &str);
65 g_assert_cmpstr (str, ==, "Hello, earthlings");
66 g_free (str);
68 g_settings_set (settings, "greeting", "s", "goodbye world");
69 g_settings_get (settings, "greeting", "s", &str);
70 g_assert_cmpstr (str, ==, "goodbye world");
71 g_free (str);
72 str = NULL;
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");
88 g_free (str);
89 str = NULL;
91 g_settings_reset (settings, "greeting");
92 str = g_settings_get_string (settings, "greeting");
93 g_assert_cmpstr (str, ==, "Hello, earthlings");
94 g_free (str);
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
103 static void
104 test_unknown_key (void)
106 if (!g_test_undefined ())
107 return;
109 if (g_test_subprocess ())
111 GSettings *settings;
112 GVariant *value;
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);
120 return;
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
130 static void
131 test_no_schema (void)
133 if (!g_test_undefined ())
134 return;
136 if (g_test_subprocess ())
138 GSettings *settings;
140 settings = g_settings_new ("no.such.schema");
142 g_assert (settings == NULL);
143 return;
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
153 static void
154 test_wrong_type (void)
156 GSettings *settings;
157 gchar *str = NULL;
159 if (!g_test_undefined ())
160 return;
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 */
182 static void
183 test_wrong_path (void)
185 if (!g_test_undefined ())
186 return;
188 if (g_test_subprocess ())
190 GSettings *settings G_GNUC_UNUSED;
192 settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
193 return;
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*");
200 static void
201 test_no_path (void)
203 if (!g_test_undefined ())
204 return;
206 if (g_test_subprocess ())
208 GSettings *settings G_GNUC_UNUSED;
210 settings = g_settings_new ("org.gtk.test.no-path");
211 return;
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
222 static void
223 test_basic_types (void)
225 GSettings *settings;
226 gboolean b;
227 guint8 byte;
228 gint16 i16;
229 guint16 u16;
230 gint32 i32;
231 guint32 u32;
232 gint64 i64;
233 guint64 u64;
234 gdouble d;
235 gchar *str;
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");
320 g_free (str);
321 str = NULL;
323 g_settings_get (settings, "test-objectpath", "o", &str);
324 g_assert_cmpstr (str, ==, "/a/object/path");
325 g_object_unref (settings);
326 g_free (str);
327 str = NULL;
330 /* Check that we can read an set complex types like
331 * tuples, arrays and dictionaries
333 static void
334 test_complex_types (void)
336 GSettings *settings;
337 gchar *s;
338 gint i1, i2;
339 GVariantIter *iter = NULL;
340 GVariant *v = 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);
348 g_free (s) ;
349 s = NULL;
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);
356 g_free (s);
357 s = NULL;
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");
381 g_variant_unref (v);
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");
385 g_variant_unref (v);
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}");
390 g_variant_unref (v);
392 g_object_unref (settings);
395 static gboolean changed_cb_called;
397 static void
398 changed_cb (GSettings *settings,
399 const gchar *key,
400 gpointer data)
402 changed_cb_called = TRUE;
404 g_assert_cmpstr (key, ==, data);
407 /* Test that basic change notification with the changed signal works.
409 static void
410 test_changes (void)
412 GSettings *settings;
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;
438 static void
439 changed_cb2 (GSettings *settings,
440 const gchar *key,
441 gpointer data)
443 gboolean *p = data;
445 *p = TRUE;
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
451 * applied.
452 * Also test that the has-unapplied property is properly
453 * maintained.
455 static void
456 test_delay_apply (void)
458 GSettings *settings;
459 GSettings *settings2;
460 gchar *str;
461 gboolean writable;
462 GVariant *v;
463 const gchar *s;
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");
486 g_assert (writable);
488 g_settings_get (settings, "greeting", "s", &str);
489 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
490 g_free (str);
491 str = NULL;
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");
496 g_variant_unref (v);
498 g_settings_get (settings2, "greeting", "s", &str);
499 g_assert_cmpstr (str, ==, "top o' the morning");
500 g_free (str);
501 str = NULL;
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");
516 g_free (str);
517 str = NULL;
519 g_settings_get (settings2, "greeting", "s", &str);
520 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
521 g_free (str);
522 str = NULL;
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");
532 g_free (str);
534 g_object_unref (settings2);
535 g_object_unref (settings);
538 /* Test that reverting unapplied changes in a delay-apply
539 * settings instance works.
541 static void
542 test_delay_revert (void)
544 GSettings *settings;
545 GSettings *settings2;
546 gchar *str;
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");
555 g_free (str);
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");
563 g_free (str);
564 str = NULL;
566 g_settings_get (settings2, "greeting", "s", &str);
567 g_assert_cmpstr (str, ==, "top o' the morning");
568 g_free (str);
569 str = NULL;
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");
579 g_free (str);
580 str = NULL;
582 g_settings_get (settings2, "greeting", "s", &str);
583 g_assert_cmpstr (str, ==, "top o' the morning");
584 g_free (str);
585 str = NULL;
587 g_object_unref (settings2);
588 g_object_unref (settings);
591 static void
592 test_delay_child (void)
594 GSettings *base;
595 GSettings *settings;
596 GSettings *child;
597 guint8 byte;
598 gboolean delay;
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);
606 g_assert (delay);
608 child = g_settings_get_child (settings, "basic-types");
609 g_assert (child != NULL);
611 g_object_get (child, "delay-apply", &delay, NULL);
612 g_assert (!delay);
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);
628 static void
629 keys_changed_cb (GSettings *settings,
630 const GQuark *keys,
631 gint n_keys)
633 gchar *str;
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");
644 g_free (str);
645 str = NULL;
647 g_settings_get (settings, "farewell", "s", &str);
648 g_assert_cmpstr (str, ==, "atomic bye-bye");
649 g_free (str);
650 str = NULL;
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.
657 static void
658 test_atomic (void)
660 GSettings *settings;
661 GSettings *settings2;
662 gchar *str;
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");
684 g_free (str);
685 str = NULL;
687 g_settings_get (settings, "farewell", "s", &str);
688 g_assert_cmpstr (str, ==, "atomic bye-bye");
689 g_free (str);
690 str = NULL;
692 g_settings_get (settings2, "greeting", "s", &str);
693 g_assert_cmpstr (str, ==, "greetings from test_atomic");
694 g_free (str);
695 str = NULL;
697 g_settings_get (settings2, "farewell", "s", &str);
698 g_assert_cmpstr (str, ==, "atomic bye-bye");
699 g_free (str);
700 str = NULL;
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
708 * for now.
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
730 static void
731 test_l10n (void)
733 GSettings *settings;
734 gchar *str;
735 gchar *locale;
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 g_setenv ("LC_MESSAGES", "C", TRUE);
745 setlocale (LC_MESSAGES, "C");
746 str = g_settings_get_string (settings, "error-message");
747 g_setenv ("LC_MESSAGES", locale, TRUE);
748 setlocale (LC_MESSAGES, locale);
750 g_assert_cmpstr (str, ==, "Unnamed");
751 g_free (str);
752 str = NULL;
754 g_setenv ("LC_MESSAGES", "de_DE.UTF-8", TRUE);
755 setlocale (LC_MESSAGES, "de_DE.UTF-8");
756 /* Only do the test if translation is actually working... */
757 if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
759 str = g_settings_get_string (settings, "error-message");
761 g_assert_cmpstr (str, ==, "Unbenannt");
762 g_free (str);
763 str = NULL;
765 else
766 g_printerr ("warning: translation is not working... skipping test. ");
768 g_setenv ("LC_MESSAGES", locale, TRUE);
769 setlocale (LC_MESSAGES, locale);
770 g_free (locale);
771 g_object_unref (settings);
774 /* Test that message context works as expected with translated
775 * schema defaults. Also, verify that non-ASCII UTF-8 content
776 * works.
778 * This test relies on the de.po file in the same directory
779 * to be compiled into ./de/LC_MESSAGES/test.mo
781 static void
782 test_l10n_context (void)
784 GSettings *settings;
785 gchar *str;
786 gchar *locale;
788 bindtextdomain ("test", locale_dir);
789 bind_textdomain_codeset ("test", "UTF-8");
791 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
793 settings = g_settings_new ("org.gtk.test.localized");
795 g_setenv ("LC_MESSAGES", "C", TRUE);
796 setlocale (LC_MESSAGES, "C");
797 g_settings_get (settings, "backspace", "s", &str);
798 g_setenv ("LC_MESSAGES", locale, TRUE);
799 setlocale (LC_MESSAGES, locale);
801 g_assert_cmpstr (str, ==, "BackSpace");
802 g_free (str);
803 str = NULL;
805 g_setenv ("LC_MESSAGES", "de_DE.UTF-8", TRUE);
806 setlocale (LC_MESSAGES, "de_DE.UTF-8");
807 /* Only do the test if translation is actually working... */
808 if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
810 g_settings_get (settings, "backspace", "s", &str);
812 g_assert_cmpstr (str, ==, "Löschen");
813 g_free (str);
814 str = NULL;
816 else
817 g_printerr ("warning: translation is not working... skipping test. ");
819 g_setenv ("LC_MESSAGES", locale, TRUE);
820 setlocale (LC_MESSAGES, locale);
821 g_free (locale);
822 g_object_unref (settings);
825 enum
827 PROP_0,
828 PROP_BOOL,
829 PROP_ANTI_BOOL,
830 PROP_BYTE,
831 PROP_INT16,
832 PROP_UINT16,
833 PROP_INT,
834 PROP_UINT,
835 PROP_INT64,
836 PROP_UINT64,
837 PROP_DOUBLE,
838 PROP_STRING,
839 PROP_NO_READ,
840 PROP_NO_WRITE,
841 PROP_STRV,
842 PROP_ENUM,
843 PROP_FLAGS
846 typedef struct
848 GObject parent_instance;
850 gboolean bool_prop;
851 gboolean anti_bool_prop;
852 gint8 byte_prop;
853 gint int16_prop;
854 guint16 uint16_prop;
855 gint int_prop;
856 guint uint_prop;
857 gint64 int64_prop;
858 guint64 uint64_prop;
859 gdouble double_prop;
860 gchar *string_prop;
861 gchar *no_read_prop;
862 gchar *no_write_prop;
863 gchar **strv_prop;
864 guint enum_prop;
865 guint flags_prop;
866 } TestObject;
868 typedef struct
870 GObjectClass parent_class;
871 } TestObjectClass;
873 static GType test_object_get_type (void);
874 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
876 static void
877 test_object_init (TestObject *object)
881 static void
882 test_object_finalize (GObject *object)
884 TestObject *testo = (TestObject*)object;
885 g_strfreev (testo->strv_prop);
886 g_free (testo->string_prop);
887 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
890 static void
891 test_object_get_property (GObject *object,
892 guint prop_id,
893 GValue *value,
894 GParamSpec *pspec)
896 TestObject *test_object = (TestObject *)object;
898 switch (prop_id)
900 case PROP_BOOL:
901 g_value_set_boolean (value, test_object->bool_prop);
902 break;
903 case PROP_ANTI_BOOL:
904 g_value_set_boolean (value, test_object->anti_bool_prop);
905 break;
906 case PROP_BYTE:
907 g_value_set_schar (value, test_object->byte_prop);
908 break;
909 case PROP_UINT16:
910 g_value_set_uint (value, test_object->uint16_prop);
911 break;
912 case PROP_INT16:
913 g_value_set_int (value, test_object->int16_prop);
914 break;
915 case PROP_INT:
916 g_value_set_int (value, test_object->int_prop);
917 break;
918 case PROP_UINT:
919 g_value_set_uint (value, test_object->uint_prop);
920 break;
921 case PROP_INT64:
922 g_value_set_int64 (value, test_object->int64_prop);
923 break;
924 case PROP_UINT64:
925 g_value_set_uint64 (value, test_object->uint64_prop);
926 break;
927 case PROP_DOUBLE:
928 g_value_set_double (value, test_object->double_prop);
929 break;
930 case PROP_STRING:
931 g_value_set_string (value, test_object->string_prop);
932 break;
933 case PROP_NO_WRITE:
934 g_value_set_string (value, test_object->no_write_prop);
935 break;
936 case PROP_STRV:
937 g_value_set_boxed (value, test_object->strv_prop);
938 break;
939 case PROP_ENUM:
940 g_value_set_enum (value, test_object->enum_prop);
941 break;
942 case PROP_FLAGS:
943 g_value_set_flags (value, test_object->flags_prop);
944 break;
945 default:
946 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
947 break;
951 static void
952 test_object_set_property (GObject *object,
953 guint prop_id,
954 const GValue *value,
955 GParamSpec *pspec)
957 TestObject *test_object = (TestObject *)object;
959 switch (prop_id)
961 case PROP_BOOL:
962 test_object->bool_prop = g_value_get_boolean (value);
963 break;
964 case PROP_ANTI_BOOL:
965 test_object->anti_bool_prop = g_value_get_boolean (value);
966 break;
967 case PROP_BYTE:
968 test_object->byte_prop = g_value_get_schar (value);
969 break;
970 case PROP_INT16:
971 test_object->int16_prop = g_value_get_int (value);
972 break;
973 case PROP_UINT16:
974 test_object->uint16_prop = g_value_get_uint (value);
975 break;
976 case PROP_INT:
977 test_object->int_prop = g_value_get_int (value);
978 break;
979 case PROP_UINT:
980 test_object->uint_prop = g_value_get_uint (value);
981 break;
982 case PROP_INT64:
983 test_object->int64_prop = g_value_get_int64 (value);
984 break;
985 case PROP_UINT64:
986 test_object->uint64_prop = g_value_get_uint64 (value);
987 break;
988 case PROP_DOUBLE:
989 test_object->double_prop = g_value_get_double (value);
990 break;
991 case PROP_STRING:
992 g_free (test_object->string_prop);
993 test_object->string_prop = g_value_dup_string (value);
994 break;
995 case PROP_NO_READ:
996 g_free (test_object->no_read_prop);
997 test_object->no_read_prop = g_value_dup_string (value);
998 break;
999 case PROP_STRV:
1000 g_strfreev (test_object->strv_prop);
1001 test_object->strv_prop = g_value_dup_boxed (value);
1002 break;
1003 case PROP_ENUM:
1004 test_object->enum_prop = g_value_get_enum (value);
1005 break;
1006 case PROP_FLAGS:
1007 test_object->flags_prop = g_value_get_flags (value);
1008 break;
1009 default:
1010 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1011 break;
1015 static GType
1016 test_enum_get_type (void)
1018 static volatile gsize define_type_id = 0;
1020 if (g_once_init_enter (&define_type_id))
1022 static const GEnumValue values[] = {
1023 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
1024 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
1025 { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
1026 { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
1027 { 0, NULL, NULL }
1030 GType type_id = g_enum_register_static ("TestEnum", values);
1031 g_once_init_leave (&define_type_id, type_id);
1034 return define_type_id;
1037 static GType
1038 test_flags_get_type (void)
1040 static volatile gsize define_type_id = 0;
1042 if (g_once_init_enter (&define_type_id))
1044 static const GFlagsValue values[] = {
1045 { TEST_FLAGS_NONE, "TEST_FLAGS_NONE", "none" },
1046 { TEST_FLAGS_MOURNING, "TEST_FLAGS_MOURNING", "mourning" },
1047 { TEST_FLAGS_LAUGHING, "TEST_FLAGS_LAUGHING", "laughing" },
1048 { TEST_FLAGS_WALKING, "TEST_FLAGS_WALKING", "walking" },
1049 { 0, NULL, NULL }
1052 GType type_id = g_flags_register_static ("TestFlags", values);
1053 g_once_init_leave (&define_type_id, type_id);
1056 return define_type_id;
1059 static void
1060 test_object_class_init (TestObjectClass *class)
1062 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
1064 gobject_class->get_property = test_object_get_property;
1065 gobject_class->set_property = test_object_set_property;
1066 gobject_class->finalize = test_object_finalize;
1068 g_object_class_install_property (gobject_class, PROP_BOOL,
1069 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
1070 g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
1071 g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
1072 g_object_class_install_property (gobject_class, PROP_BYTE,
1073 g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
1074 g_object_class_install_property (gobject_class, PROP_INT16,
1075 g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
1076 g_object_class_install_property (gobject_class, PROP_UINT16,
1077 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
1078 g_object_class_install_property (gobject_class, PROP_INT,
1079 g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
1080 g_object_class_install_property (gobject_class, PROP_UINT,
1081 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
1082 g_object_class_install_property (gobject_class, PROP_INT64,
1083 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
1084 g_object_class_install_property (gobject_class, PROP_UINT64,
1085 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
1086 g_object_class_install_property (gobject_class, PROP_DOUBLE,
1087 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
1088 g_object_class_install_property (gobject_class, PROP_STRING,
1089 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
1090 g_object_class_install_property (gobject_class, PROP_NO_WRITE,
1091 g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
1092 g_object_class_install_property (gobject_class, PROP_NO_READ,
1093 g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
1094 g_object_class_install_property (gobject_class, PROP_STRV,
1095 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
1096 g_object_class_install_property (gobject_class, PROP_ENUM,
1097 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
1098 g_object_class_install_property (gobject_class, PROP_FLAGS,
1099 g_param_spec_flags ("flags", "", "", test_flags_get_type (), TEST_FLAGS_NONE, G_PARAM_READWRITE));
1102 static TestObject *
1103 test_object_new (void)
1105 return (TestObject*)g_object_new (test_object_get_type (), NULL);
1108 /* Test basic binding functionality for simple types.
1109 * Verify that with bidirectional bindings, changes on either side
1110 * are notified on the other end.
1112 static void
1113 test_simple_binding (void)
1115 TestObject *obj;
1116 GSettings *settings;
1117 gboolean b;
1118 gchar y;
1119 gint i;
1120 guint u;
1121 gint16 n;
1122 guint16 q;
1123 gint n2;
1124 guint q2;
1125 gint64 i64;
1126 guint64 u64;
1127 gdouble d;
1128 gchar *s;
1129 GVariant *value;
1130 gchar **strv;
1132 settings = g_settings_new ("org.gtk.test.binding");
1133 obj = test_object_new ();
1135 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
1136 g_object_set (obj, "bool", TRUE, NULL);
1137 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1139 g_settings_set_boolean (settings, "bool", FALSE);
1140 b = TRUE;
1141 g_object_get (obj, "bool", &b, NULL);
1142 g_assert_cmpint (b, ==, FALSE);
1144 g_settings_bind (settings, "anti-bool", obj, "anti-bool",
1145 G_SETTINGS_BIND_INVERT_BOOLEAN);
1146 g_object_set (obj, "anti-bool", FALSE, NULL);
1147 g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
1149 g_settings_set_boolean (settings, "anti-bool", FALSE);
1150 b = FALSE;
1151 g_object_get (obj, "anti-bool", &b, NULL);
1152 g_assert_cmpint (b, ==, TRUE);
1154 g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
1156 g_object_set (obj, "byte", 123, NULL);
1157 y = 'c';
1158 g_settings_get (settings, "byte", "y", &y);
1159 g_assert_cmpint (y, ==, 123);
1161 g_settings_set (settings, "byte", "y", 54);
1162 y = 'c';
1163 g_object_get (obj, "byte", &y, NULL);
1164 g_assert_cmpint (y, ==, 54);
1166 g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
1168 g_object_set (obj, "int16", 1234, NULL);
1169 n = 4321;
1170 g_settings_get (settings, "int16", "n", &n);
1171 g_assert_cmpint (n, ==, 1234);
1173 g_settings_set (settings, "int16", "n", 4321);
1174 n2 = 1111;
1175 g_object_get (obj, "int16", &n2, NULL);
1176 g_assert_cmpint (n2, ==, 4321);
1178 g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
1180 g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1181 q = 1111;
1182 g_settings_get (settings, "uint16", "q", &q);
1183 g_assert_cmpuint (q, ==, G_MAXUINT16);
1185 g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1186 q2 = 1111;
1187 g_object_get (obj, "uint16", &q2, NULL);
1188 g_assert_cmpuint (q2, ==, (guint16) G_MAXINT16);
1190 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1192 g_object_set (obj, "int", 12345, NULL);
1193 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1195 g_settings_set_int (settings, "int", 54321);
1196 i = 1111;
1197 g_object_get (obj, "int", &i, NULL);
1198 g_assert_cmpint (i, ==, 54321);
1200 g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1202 g_object_set (obj, "uint", 12345, NULL);
1203 g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);
1205 g_settings_set_uint (settings, "uint", 54321);
1206 u = 1111;
1207 g_object_get (obj, "uint", &u, NULL);
1208 g_assert_cmpuint (u, ==, 54321);
1210 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1212 g_object_set (obj, "uint64", (guint64) 12345, NULL);
1213 g_assert_cmpuint (g_settings_get_uint64 (settings, "uint64"), ==, 12345);
1215 g_settings_set_uint64 (settings, "uint64", 54321);
1216 u64 = 1111;
1217 g_object_get (obj, "uint64", &u64, NULL);
1218 g_assert_cmpuint (u64, ==, 54321);
1220 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1222 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1223 i64 = 1111;
1224 g_settings_get (settings, "int64", "x", &i64);
1225 g_assert_cmpint (i64, ==, G_MAXINT64);
1227 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1228 i64 = 1111;
1229 g_object_get (obj, "int64", &i64, NULL);
1230 g_assert_cmpint (i64, ==, G_MININT64);
1232 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1234 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1235 u64 = 1111;
1236 g_settings_get (settings, "uint64", "t", &u64);
1237 g_assert_cmpuint (u64, ==, G_MAXUINT64);
1239 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1240 u64 = 1111;
1241 g_object_get (obj, "uint64", &u64, NULL);
1242 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1244 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1246 g_object_set (obj, "string", "bu ba", NULL);
1247 s = g_settings_get_string (settings, "string");
1248 g_assert_cmpstr (s, ==, "bu ba");
1249 g_free (s);
1251 g_settings_set_string (settings, "string", "bla bla");
1252 g_object_get (obj, "string", &s, NULL);
1253 g_assert_cmpstr (s, ==, "bla bla");
1254 g_free (s);
1256 g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1258 g_object_set (obj, "string", "non-unicode:\315", NULL);
1259 value = g_settings_get_value (settings, "chararray");
1260 g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1261 g_variant_unref (value);
1263 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1265 g_object_set (obj, "double", G_MAXFLOAT, NULL);
1266 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1268 g_settings_set_double (settings, "double", G_MINFLOAT);
1269 d = 1.0;
1270 g_object_get (obj, "double", &d, NULL);
1271 g_assert_cmpfloat (d, ==, G_MINFLOAT);
1273 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1274 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1276 g_settings_set_double (settings, "double", -G_MINDOUBLE);
1277 d = 1.0;
1278 g_object_get (obj, "double", &d, NULL);
1279 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1281 strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1282 g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1283 g_object_set (obj, "strv", strv, NULL);
1284 g_strfreev (strv);
1285 strv = g_settings_get_strv (settings, "strv");
1286 s = g_strjoinv (",", strv);
1287 g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1288 g_strfreev (strv);
1289 g_free (s);
1290 strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1291 g_settings_set_strv (settings, "strv", (const gchar **) strv);
1292 g_strfreev (strv);
1293 g_object_get (obj, "strv", &strv, NULL);
1294 s = g_strjoinv (",", strv);
1295 g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1296 g_strfreev (strv);
1297 g_free (s);
1298 g_settings_set_strv (settings, "strv", NULL);
1299 g_object_get (obj, "strv", &strv, NULL);
1300 g_assert (strv != NULL);
1301 g_assert_cmpint (g_strv_length (strv), ==, 0);
1302 g_strfreev (strv);
1304 g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1305 g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1306 s = g_settings_get_string (settings, "enum");
1307 g_assert_cmpstr (s, ==, "baz");
1308 g_free (s);
1309 g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1311 g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1312 i = 230;
1313 g_object_get (obj, "enum", &i, NULL);
1314 g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1316 g_settings_set_string (settings, "enum", "baz");
1317 i = 230;
1318 g_object_get (obj, "enum", &i, NULL);
1319 g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1321 g_settings_bind (settings, "flags", obj, "flags", G_SETTINGS_BIND_DEFAULT);
1322 g_object_set (obj, "flags", TEST_FLAGS_MOURNING, NULL);
1323 strv = g_settings_get_strv (settings, "flags");
1324 g_assert_cmpint (g_strv_length (strv), ==, 1);
1325 g_assert_cmpstr (strv[0], ==, "mourning");
1326 g_strfreev (strv);
1328 g_assert_cmpint (g_settings_get_flags (settings, "flags"), ==, TEST_FLAGS_MOURNING);
1330 g_settings_set_flags (settings, "flags", TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1331 i = 230;
1332 g_object_get (obj, "flags", &i, NULL);
1333 g_assert_cmpint (i, ==, TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1335 g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1337 g_object_set (obj, "uint", 12345, NULL);
1338 g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);
1340 g_settings_set_uint (settings, "uint", 54321);
1341 u = 1111;
1342 g_object_get (obj, "uint", &u, NULL);
1343 g_assert_cmpuint (u, ==, 54321);
1345 g_settings_bind (settings, "range", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1346 g_object_set (obj, "uint", 22, NULL);
1347 u = 1111;
1348 g_assert_cmpuint (g_settings_get_uint (settings, "range"), ==, 22);
1349 g_object_get (obj, "uint", &u, NULL);
1350 g_assert_cmpuint (u, ==, 22);
1352 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1353 "* is out of schema-specified range for*");
1354 g_object_set (obj, "uint", 45, NULL);
1355 g_test_assert_expected_messages ();
1356 u = 1111;
1357 g_object_get (obj, "uint", &u, NULL);
1358 g_assert_cmpuint (g_settings_get_uint (settings, "range"), ==, 22);
1359 /* The value of the object is currently not reset back to its initial value
1360 g_assert_cmpuint (u, ==, 22); */
1362 g_object_unref (obj);
1363 g_object_unref (settings);
1366 static void
1367 test_unbind (void)
1369 TestObject *obj;
1370 GSettings *settings;
1372 settings = g_settings_new ("org.gtk.test.binding");
1373 obj = test_object_new ();
1375 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1377 g_object_set (obj, "int", 12345, NULL);
1378 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1380 g_settings_unbind (obj, "int");
1382 g_object_set (obj, "int", 54321, NULL);
1383 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1385 g_object_unref (obj);
1386 g_object_unref (settings);
1389 static void
1390 test_bind_writable (void)
1392 TestObject *obj;
1393 GSettings *settings;
1394 gboolean b;
1396 settings = g_settings_new ("org.gtk.test.binding");
1397 obj = test_object_new ();
1399 g_object_set (obj, "bool", FALSE, NULL);
1401 g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1403 g_object_get (obj, "bool", &b, NULL);
1404 g_assert (b);
1406 g_settings_unbind (obj, "bool");
1408 g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1410 g_object_get (obj, "bool", &b, NULL);
1411 g_assert (!b);
1413 g_object_unref (obj);
1414 g_object_unref (settings);
1417 /* Test one-way bindings.
1418 * Verify that changes on one side show up on the other,
1419 * but not vice versa
1421 static void
1422 test_directional_binding (void)
1424 TestObject *obj;
1425 GSettings *settings;
1426 gboolean b;
1427 gint i;
1429 settings = g_settings_new ("org.gtk.test.binding");
1430 obj = test_object_new ();
1432 g_object_set (obj, "bool", FALSE, NULL);
1433 g_settings_set_boolean (settings, "bool", FALSE);
1435 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1437 g_settings_set_boolean (settings, "bool", TRUE);
1438 g_object_get (obj, "bool", &b, NULL);
1439 g_assert_cmpint (b, ==, TRUE);
1441 g_object_set (obj, "bool", FALSE, NULL);
1442 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1444 g_object_set (obj, "int", 20, NULL);
1445 g_settings_set_int (settings, "int", 20);
1447 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1449 g_object_set (obj, "int", 32, NULL);
1450 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1452 g_settings_set_int (settings, "int", 20);
1453 g_object_get (obj, "int", &i, NULL);
1454 g_assert_cmpint (i, ==, 32);
1456 g_object_unref (obj);
1457 g_object_unref (settings);
1460 /* Test that type mismatch is caught when creating a binding */
1461 static void
1462 test_typesafe_binding (void)
1464 if (!g_test_undefined ())
1465 return;
1467 if (g_test_subprocess ())
1469 TestObject *obj;
1470 GSettings *settings;
1472 settings = g_settings_new ("org.gtk.test.binding");
1473 obj = test_object_new ();
1475 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1477 g_object_unref (obj);
1478 g_object_unref (settings);
1479 return;
1481 g_test_trap_subprocess (NULL, 0, 0);
1482 g_test_trap_assert_failed ();
1483 g_test_trap_assert_stderr ("*not compatible*");
1486 static gboolean
1487 string_to_bool (GValue *value,
1488 GVariant *variant,
1489 gpointer user_data)
1491 const gchar *s;
1493 s = g_variant_get_string (variant, NULL);
1494 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1496 return TRUE;
1499 static GVariant *
1500 bool_to_string (const GValue *value,
1501 const GVariantType *expected_type,
1502 gpointer user_data)
1504 if (g_value_get_boolean (value))
1505 return g_variant_new_string ("true");
1506 else
1507 return g_variant_new_string ("false");
1510 static GVariant *
1511 bool_to_bool (const GValue *value,
1512 const GVariantType *expected_type,
1513 gpointer user_data)
1515 return g_variant_new_boolean (g_value_get_boolean (value));
1518 /* Test custom bindings.
1519 * Translate strings to booleans and back
1521 static void
1522 test_custom_binding (void)
1524 TestObject *obj;
1525 GSettings *settings;
1526 gchar *s;
1527 gboolean b;
1529 settings = g_settings_new ("org.gtk.test.binding");
1530 obj = test_object_new ();
1532 g_settings_set_string (settings, "string", "true");
1534 g_settings_bind_with_mapping (settings, "string",
1535 obj, "bool",
1536 G_SETTINGS_BIND_DEFAULT,
1537 string_to_bool,
1538 bool_to_string,
1539 NULL, NULL);
1541 g_settings_set_string (settings, "string", "false");
1542 g_object_get (obj, "bool", &b, NULL);
1543 g_assert_cmpint (b, ==, FALSE);
1545 g_settings_set_string (settings, "string", "not true");
1546 g_object_get (obj, "bool", &b, NULL);
1547 g_assert_cmpint (b, ==, FALSE);
1549 g_object_set (obj, "bool", TRUE, NULL);
1550 s = g_settings_get_string (settings, "string");
1551 g_assert_cmpstr (s, ==, "true");
1552 g_free (s);
1554 g_settings_bind_with_mapping (settings, "string",
1555 obj, "bool",
1556 G_SETTINGS_BIND_DEFAULT,
1557 string_to_bool, bool_to_bool,
1558 NULL, NULL);
1559 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1560 "*binding mapping function for key 'string' returned"
1561 " GVariant of type 'b' when type 's' was requested*");
1562 g_object_set (obj, "bool", FALSE, NULL);
1563 g_test_assert_expected_messages ();
1565 g_object_unref (obj);
1566 g_object_unref (settings);
1569 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1570 * initial settings value is transported to the object
1571 * side, but later settings changes do not affect the
1572 * object
1574 static void
1575 test_no_change_binding (void)
1577 TestObject *obj;
1578 GSettings *settings;
1579 gboolean b;
1581 settings = g_settings_new ("org.gtk.test.binding");
1582 obj = test_object_new ();
1584 g_object_set (obj, "bool", TRUE, NULL);
1585 g_settings_set_boolean (settings, "bool", FALSE);
1587 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1589 g_object_get (obj, "bool", &b, NULL);
1590 g_assert_cmpint (b, ==, FALSE);
1592 g_settings_set_boolean (settings, "bool", TRUE);
1593 g_object_get (obj, "bool", &b, NULL);
1594 g_assert_cmpint (b, ==, FALSE);
1596 g_settings_set_boolean (settings, "bool", FALSE);
1597 g_object_set (obj, "bool", TRUE, NULL);
1598 b = g_settings_get_boolean (settings, "bool");
1599 g_assert_cmpint (b, ==, TRUE);
1601 g_object_unref (obj);
1602 g_object_unref (settings);
1605 /* Test that binding a non-readable property only
1606 * works in 'GET' mode.
1608 static void
1609 test_no_read_binding_fail (void)
1611 TestObject *obj;
1612 GSettings *settings;
1614 settings = g_settings_new ("org.gtk.test.binding");
1615 obj = test_object_new ();
1617 g_settings_bind (settings, "string", obj, "no-read", 0);
1620 static void
1621 test_no_read_binding_pass (void)
1623 TestObject *obj;
1624 GSettings *settings;
1626 settings = g_settings_new ("org.gtk.test.binding");
1627 obj = test_object_new ();
1629 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1631 exit (0);
1634 static void
1635 test_no_read_binding (void)
1637 if (g_test_undefined ())
1639 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/fail", 0, 0);
1640 g_test_trap_assert_failed ();
1641 g_test_trap_assert_stderr ("*property*is not readable*");
1644 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/pass", 0, 0);
1645 g_test_trap_assert_passed ();
1648 /* Test that binding a non-writable property only
1649 * works in 'SET' mode.
1651 static void
1652 test_no_write_binding_fail (void)
1654 TestObject *obj;
1655 GSettings *settings;
1657 settings = g_settings_new ("org.gtk.test.binding");
1658 obj = test_object_new ();
1660 g_settings_bind (settings, "string", obj, "no-write", 0);
1663 static void
1664 test_no_write_binding_pass (void)
1666 TestObject *obj;
1667 GSettings *settings;
1669 settings = g_settings_new ("org.gtk.test.binding");
1670 obj = test_object_new ();
1672 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1674 exit (0);
1677 static void
1678 test_no_write_binding (void)
1680 if (g_test_undefined ())
1682 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/fail", 0, 0);
1683 g_test_trap_assert_failed ();
1684 g_test_trap_assert_stderr ("*property*is not writable*");
1687 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/pass", 0, 0);
1688 g_test_trap_assert_passed ();
1691 static void
1692 key_changed_cb (GSettings *settings, const gchar *key, gpointer data)
1694 gboolean *b = data;
1695 (*b) = TRUE;
1699 * Test that using a keyfile works
1701 static void
1702 test_keyfile (void)
1704 GSettingsBackend *kf_backend;
1705 GSettings *settings;
1706 GKeyFile *keyfile;
1707 gchar *str;
1708 gboolean writable;
1709 GError *error = NULL;
1710 gchar *data;
1711 gsize len;
1712 gboolean called = FALSE;
1714 g_remove ("keyfile/gsettings.store");
1715 g_rmdir ("keyfile");
1717 kf_backend = g_keyfile_settings_backend_new ("keyfile/gsettings.store", "/", "root");
1718 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1719 g_object_unref (kf_backend);
1721 g_settings_reset (settings, "greeting");
1722 str = g_settings_get_string (settings, "greeting");
1723 g_assert_cmpstr (str, ==, "Hello, earthlings");
1724 g_free (str);
1726 writable = g_settings_is_writable (settings, "greeting");
1727 g_assert (writable);
1728 g_settings_set (settings, "greeting", "s", "see if this works");
1730 str = g_settings_get_string (settings, "greeting");
1731 g_assert_cmpstr (str, ==, "see if this works");
1732 g_free (str);
1734 g_settings_delay (settings);
1735 g_settings_set (settings, "farewell", "s", "cheerio");
1736 g_settings_apply (settings);
1738 keyfile = g_key_file_new ();
1739 g_assert (g_key_file_load_from_file (keyfile, "keyfile/gsettings.store", 0, NULL));
1741 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1742 g_assert_cmpstr (str, ==, "'see if this works'");
1743 g_free (str);
1745 str = g_key_file_get_string (keyfile, "tests", "farewell", NULL);
1746 g_assert_cmpstr (str, ==, "'cheerio'");
1747 g_free (str);
1748 g_key_file_free (keyfile);
1750 g_settings_reset (settings, "greeting");
1751 g_settings_apply (settings);
1752 keyfile = g_key_file_new ();
1753 g_assert (g_key_file_load_from_file (keyfile, "keyfile/gsettings.store", 0, NULL));
1755 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1756 g_assert (str == NULL);
1758 called = FALSE;
1759 g_signal_connect (settings, "changed::greeting", G_CALLBACK (key_changed_cb), &called);
1761 g_key_file_set_string (keyfile, "tests", "greeting", "'howdy'");
1762 data = g_key_file_to_data (keyfile, &len, NULL);
1763 g_file_set_contents ("keyfile/gsettings.store", data, len, &error);
1764 g_assert_no_error (error);
1765 while (!called)
1766 g_main_context_iteration (NULL, FALSE);
1767 g_signal_handlers_disconnect_by_func (settings, key_changed_cb, &called);
1769 str = g_settings_get_string (settings, "greeting");
1770 g_assert_cmpstr (str, ==, "howdy");
1771 g_free (str);
1773 g_settings_set (settings, "farewell", "s", "cheerio");
1775 called = FALSE;
1776 g_signal_connect (settings, "writable-changed::greeting", G_CALLBACK (key_changed_cb), &called);
1778 g_chmod ("keyfile", 0500);
1779 while (!called)
1780 g_main_context_iteration (NULL, FALSE);
1781 g_signal_handlers_disconnect_by_func (settings, key_changed_cb, &called);
1783 writable = g_settings_is_writable (settings, "greeting");
1784 g_assert (!writable);
1786 g_key_file_free (keyfile);
1787 g_free (data);
1789 g_object_unref (settings);
1790 g_chmod ("keyfile", 0777);
1793 /* Test that getting child schemas works
1795 static void
1796 test_child_schema (void)
1798 GSettings *settings;
1799 GSettings *child;
1800 guint8 byte;
1802 /* first establish some known conditions */
1803 settings = g_settings_new ("org.gtk.test.basic-types");
1804 g_settings_set (settings, "test-byte", "y", 36);
1806 g_settings_get (settings, "test-byte", "y", &byte);
1807 g_assert_cmpint (byte, ==, 36);
1809 g_object_unref (settings);
1811 settings = g_settings_new ("org.gtk.test");
1812 child = g_settings_get_child (settings, "basic-types");
1813 g_assert (child != NULL);
1815 g_settings_get (child, "test-byte", "y", &byte);
1816 g_assert_cmpint (byte, ==, 36);
1818 g_object_unref (child);
1819 g_object_unref (settings);
1822 #include "../strinfo.c"
1824 static void
1825 test_strinfo (void)
1827 /* "foo" has a value of 1
1828 * "bar" has a value of 2
1829 * "baz" is an alias for "bar"
1831 gchar array[] =
1832 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1833 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1834 "\0\0\0\xff";
1835 const guint32 *strinfo = (guint32 *) array;
1836 guint length = sizeof array / 4;
1837 guint result;
1840 /* build it and compare */
1841 GString *builder;
1843 builder = g_string_new (NULL);
1844 strinfo_builder_append_item (builder, "foo", 1);
1845 strinfo_builder_append_item (builder, "bar", 2);
1846 g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
1847 g_assert_cmpmem (builder->str, builder->len, strinfo, length * 4);
1848 g_string_free (builder, TRUE);
1851 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
1852 ==, NULL);
1853 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
1854 ==, NULL);
1855 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
1856 ==, "bar");
1857 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
1858 ==, NULL);
1860 g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
1861 g_assert_cmpint (result, ==, 1);
1862 g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
1863 g_assert_cmpint (result, ==, 2);
1864 g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
1865 g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
1867 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
1868 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
1869 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
1870 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
1872 g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
1873 g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
1874 g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
1875 g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
1878 static void
1879 test_enums_non_enum_key (void)
1881 GSettings *direct;
1883 direct = g_settings_new ("org.gtk.test.enums.direct");
1884 g_settings_get_enum (direct, "test");
1885 g_assert_not_reached ();
1888 static void
1889 test_enums_non_enum_value (void)
1891 GSettings *settings;
1893 settings = g_settings_new ("org.gtk.test.enums");
1894 g_settings_set_enum (settings, "test", 42);
1895 g_assert_not_reached ();
1898 static void
1899 test_enums_range (void)
1901 GSettings *settings;
1903 settings = g_settings_new ("org.gtk.test.enums");
1904 g_settings_set_string (settings, "test", "qux");
1905 g_assert_not_reached ();
1908 static void
1909 test_enums_non_flags (void)
1911 GSettings *settings;
1913 settings = g_settings_new ("org.gtk.test.enums");
1914 g_settings_get_flags (settings, "test");
1915 g_assert_not_reached ();
1918 static void
1919 test_enums (void)
1921 GSettings *settings, *direct;
1922 gchar *str;
1924 settings = g_settings_new ("org.gtk.test.enums");
1925 direct = g_settings_new ("org.gtk.test.enums.direct");
1927 if (g_test_undefined () && !backend_set)
1929 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-key", 0, 0);
1930 g_test_trap_assert_failed ();
1931 g_test_trap_assert_stderr ("*not associated with an enum*");
1933 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-value", 0, 0);
1934 g_test_trap_assert_failed ();
1935 g_test_trap_assert_stderr ("*invalid enum value 42*");
1937 g_test_trap_subprocess ("/gsettings/enums/subprocess/range", 0, 0);
1938 g_test_trap_assert_failed ();
1939 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1941 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-flags", 0, 0);
1942 g_test_trap_assert_failed ();
1943 g_test_trap_assert_stderr ("*not associated with a flags*");
1946 str = g_settings_get_string (settings, "test");
1947 g_assert_cmpstr (str, ==, "bar");
1948 g_free (str);
1950 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
1952 str = g_settings_get_string (settings, "test");
1953 g_assert_cmpstr (str, ==, "foo");
1954 g_free (str);
1956 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
1958 g_settings_set_string (direct, "test", "qux");
1960 str = g_settings_get_string (direct, "test");
1961 g_assert_cmpstr (str, ==, "qux");
1962 g_free (str);
1964 str = g_settings_get_string (settings, "test");
1965 g_assert_cmpstr (str, ==, "quux");
1966 g_free (str);
1968 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
1970 g_object_unref (direct);
1971 g_object_unref (settings);
1974 static void
1975 test_flags_non_flags_key (void)
1977 GSettings *direct;
1979 direct = g_settings_new ("org.gtk.test.enums.direct");
1980 g_settings_get_flags (direct, "test");
1981 g_assert_not_reached ();
1984 static void
1985 test_flags_non_flags_value (void)
1987 GSettings *settings;
1989 settings = g_settings_new ("org.gtk.test.enums");
1990 g_settings_set_flags (settings, "f-test", 0x42);
1991 g_assert_not_reached ();
1994 static void
1995 test_flags_range (void)
1997 GSettings *settings;
1999 settings = g_settings_new ("org.gtk.test.enums");
2000 g_settings_set_strv (settings, "f-test",
2001 (const gchar **) g_strsplit ("rock", ",", 0));
2002 g_assert_not_reached ();
2005 static void
2006 test_flags_non_enum (void)
2008 GSettings *settings;
2010 settings = g_settings_new ("org.gtk.test.enums");
2011 g_settings_get_enum (settings, "f-test");
2012 g_assert_not_reached ();
2015 static void
2016 test_flags (void)
2018 GSettings *settings, *direct;
2019 gchar **strv;
2020 gchar *str;
2022 settings = g_settings_new ("org.gtk.test.enums");
2023 direct = g_settings_new ("org.gtk.test.enums.direct");
2025 if (g_test_undefined () && !backend_set)
2027 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-key", 0, 0);
2028 g_test_trap_assert_failed ();
2029 g_test_trap_assert_stderr ("*not associated with a flags*");
2031 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-value", 0, 0);
2032 g_test_trap_assert_failed ();
2033 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
2035 g_test_trap_subprocess ("/gsettings/flags/subprocess/range", 0, 0);
2036 g_test_trap_assert_failed ();
2037 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2039 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-enum", 0, 0);
2040 g_test_trap_assert_failed ();
2041 g_test_trap_assert_stderr ("*not associated with an enum*");
2044 strv = g_settings_get_strv (settings, "f-test");
2045 str = g_strjoinv (",", strv);
2046 g_assert_cmpstr (str, ==, "");
2047 g_strfreev (strv);
2048 g_free (str);
2050 g_settings_set_flags (settings, "f-test",
2051 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
2053 strv = g_settings_get_strv (settings, "f-test");
2054 str = g_strjoinv (",", strv);
2055 g_assert_cmpstr (str, ==, "talking,walking");
2056 g_strfreev (strv);
2057 g_free (str);
2059 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
2060 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
2062 strv = g_strsplit ("speaking,laughing", ",", 0);
2063 g_settings_set_strv (direct, "f-test", (const gchar **) strv);
2064 g_strfreev (strv);
2066 strv = g_settings_get_strv (direct, "f-test");
2067 str = g_strjoinv (",", strv);
2068 g_assert_cmpstr (str, ==, "speaking,laughing");
2069 g_strfreev (strv);
2070 g_free (str);
2072 strv = g_settings_get_strv (settings, "f-test");
2073 str = g_strjoinv (",", strv);
2074 g_assert_cmpstr (str, ==, "talking,laughing");
2075 g_strfreev (strv);
2076 g_free (str);
2078 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
2079 TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
2081 g_object_unref (direct);
2082 g_object_unref (settings);
2085 static void
2086 test_range_high (void)
2088 GSettings *settings;
2090 settings = g_settings_new ("org.gtk.test.range");
2091 g_settings_set_int (settings, "val", 45);
2092 g_assert_not_reached ();
2095 static void
2096 test_range_low (void)
2098 GSettings *settings;
2100 settings = g_settings_new ("org.gtk.test.range");
2101 g_settings_set_int (settings, "val", 1);
2102 g_assert_not_reached ();
2105 static void
2106 test_range (void)
2108 GSettings *settings, *direct;
2109 GVariant *value;
2111 settings = g_settings_new ("org.gtk.test.range");
2112 direct = g_settings_new ("org.gtk.test.range.direct");
2114 if (g_test_undefined () && !backend_set)
2116 g_test_trap_subprocess ("/gsettings/range/subprocess/high", 0, 0);
2117 g_test_trap_assert_failed ();
2118 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2120 g_test_trap_subprocess ("/gsettings/range/subprocess/low", 0, 0);
2121 g_test_trap_assert_failed ();
2122 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2125 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2126 g_settings_set_int (direct, "val", 22);
2127 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
2128 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
2129 g_settings_set_int (direct, "val", 45);
2130 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
2131 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2132 g_settings_set_int (direct, "val", 1);
2133 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
2134 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2136 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2137 value = g_variant_new_int32 (1);
2138 g_assert (!g_settings_range_check (settings, "val", value));
2139 g_variant_unref (value);
2140 value = g_variant_new_int32 (33);
2141 g_assert (g_settings_range_check (settings, "val", value));
2142 g_variant_unref (value);
2143 value = g_variant_new_int32 (45);
2144 g_assert (!g_settings_range_check (settings, "val", value));
2145 g_variant_unref (value);
2146 G_GNUC_END_IGNORE_DEPRECATIONS
2148 g_object_unref (direct);
2149 g_object_unref (settings);
2152 static gboolean
2153 strv_has_string (gchar **haystack,
2154 const gchar *needle)
2156 guint n;
2158 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
2160 if (g_strcmp0 (haystack[n], needle) == 0)
2161 return TRUE;
2163 return FALSE;
2166 static gboolean
2167 strv_set_equal (gchar **strv, ...)
2169 gint count;
2170 va_list list;
2171 const gchar *str;
2172 gboolean res;
2174 res = TRUE;
2175 count = 0;
2176 va_start (list, strv);
2177 while (1)
2179 str = va_arg (list, const gchar *);
2180 if (str == NULL)
2181 break;
2182 if (!strv_has_string (strv, str))
2184 res = FALSE;
2185 break;
2187 count++;
2189 va_end (list);
2191 if (res)
2192 res = g_strv_length ((gchar**)strv) == count;
2194 return res;
2197 static void
2198 test_list_items (void)
2200 GSettingsSchema *schema;
2201 GSettings *settings;
2202 gchar **children;
2203 gchar **keys;
2205 settings = g_settings_new ("org.gtk.test");
2206 g_object_get (settings, "settings-schema", &schema, NULL);
2207 children = g_settings_list_children (settings);
2208 keys = g_settings_schema_list_keys (schema);
2210 g_assert (strv_set_equal (children, "basic-types", "complex-types", "localized", NULL));
2211 g_assert (strv_set_equal (keys, "greeting", "farewell", NULL));
2213 g_strfreev (children);
2214 g_strfreev (keys);
2216 g_settings_schema_unref (schema);
2217 g_object_unref (settings);
2220 static void
2221 test_list_schemas (void)
2223 const gchar * const *schemas;
2224 const gchar * const *relocs;
2226 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2227 relocs = g_settings_list_relocatable_schemas ();
2228 schemas = g_settings_list_schemas ();
2229 G_GNUC_END_IGNORE_DEPRECATIONS
2231 g_assert (strv_set_equal ((gchar **)relocs,
2232 "org.gtk.test.no-path",
2233 "org.gtk.test.extends.base",
2234 "org.gtk.test.extends.extended",
2235 NULL));
2237 g_assert (strv_set_equal ((gchar **)schemas,
2238 "org.gtk.test",
2239 "org.gtk.test.basic-types",
2240 "org.gtk.test.complex-types",
2241 "org.gtk.test.localized",
2242 "org.gtk.test.binding",
2243 "org.gtk.test.enums",
2244 "org.gtk.test.enums.direct",
2245 "org.gtk.test.range",
2246 "org.gtk.test.range.direct",
2247 "org.gtk.test.mapped",
2248 "org.gtk.test.descriptions",
2249 "org.gtk.test.per-desktop",
2250 NULL));
2253 static gboolean
2254 map_func (GVariant *value,
2255 gpointer *result,
2256 gpointer user_data)
2258 gint *state = user_data;
2259 gint v;
2261 if (value)
2262 v = g_variant_get_int32 (value);
2263 else
2264 v = -1;
2266 if (*state == 0)
2268 g_assert_cmpint (v, ==, 1);
2269 (*state)++;
2270 return FALSE;
2272 else if (*state == 1)
2274 g_assert_cmpint (v, ==, 0);
2275 (*state)++;
2276 return FALSE;
2278 else
2280 g_assert (value == NULL);
2281 *result = g_variant_new_int32 (5);
2282 return TRUE;
2286 static void
2287 test_get_mapped (void)
2289 GSettings *settings;
2290 gint state;
2291 gpointer p;
2292 gint val;
2294 settings = g_settings_new ("org.gtk.test.mapped");
2295 g_settings_set_int (settings, "val", 1);
2297 state = 0;
2298 p = g_settings_get_mapped (settings, "val", map_func, &state);
2299 val = g_variant_get_int32 ((GVariant*)p);
2300 g_assert_cmpint (val, ==, 5);
2302 g_variant_unref (p);
2303 g_object_unref (settings);
2306 static void
2307 test_get_range (void)
2309 GSettings *settings;
2310 GVariant *range;
2312 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2313 settings = g_settings_new ("org.gtk.test.range");
2314 range = g_settings_get_range (settings, "val");
2315 check_and_free (range, "('range', <(2, 44)>)");
2316 g_object_unref (settings);
2318 settings = g_settings_new ("org.gtk.test.enums");
2319 range = g_settings_get_range (settings, "test");
2320 check_and_free (range, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
2321 g_object_unref (settings);
2323 settings = g_settings_new ("org.gtk.test.enums");
2324 range = g_settings_get_range (settings, "f-test");
2325 check_and_free (range, "('flags', "
2326 "<['mourning', 'laughing', 'talking', 'walking']>)");
2327 g_object_unref (settings);
2329 settings = g_settings_new ("org.gtk.test");
2330 range = g_settings_get_range (settings, "greeting");
2331 check_and_free (range, "('type', <@as []>)");
2332 g_object_unref (settings);
2333 G_GNUC_END_IGNORE_DEPRECATIONS
2336 static void
2337 test_schema_source (void)
2339 GSettingsSchemaSource *parent;
2340 GSettingsSchemaSource *source;
2341 GSettingsBackend *backend;
2342 GSettingsSchema *schema;
2343 GError *error = NULL;
2344 GSettings *settings;
2345 gboolean enabled;
2347 backend = g_settings_backend_get_default ();
2349 /* make sure it fails properly */
2350 parent = g_settings_schema_source_get_default ();
2351 source = g_settings_schema_source_new_from_directory ("/path/that/does/not/exist", parent, TRUE, &error);
2352 g_assert (source == NULL);
2353 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
2354 g_clear_error (&error);
2356 /* Test error handling of corrupt compiled files. */
2357 source = g_settings_schema_source_new_from_directory ("schema-source-corrupt", parent, TRUE, &error);
2358 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
2359 g_assert_null (source);
2360 g_clear_error (&error);
2362 /* Test error handling of empty compiled files. */
2363 source = g_settings_schema_source_new_from_directory ("schema-source-empty", parent, TRUE, &error);
2364 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
2365 g_assert_null (source);
2366 g_clear_error (&error);
2368 /* create a source with the parent */
2369 source = g_settings_schema_source_new_from_directory ("schema-source", parent, TRUE, &error);
2370 g_assert_no_error (error);
2371 g_assert (source != NULL);
2373 /* check recursive lookups are working */
2374 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2375 g_assert (schema != NULL);
2376 g_settings_schema_unref (schema);
2378 /* check recursive lookups for non-existent schemas */
2379 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", TRUE);
2380 g_assert (schema == NULL);
2382 /* check non-recursive for schema that only exists in lower layers */
2383 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2384 g_assert (schema == NULL);
2386 /* check non-recursive lookup for non-existent */
2387 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", FALSE);
2388 g_assert (schema == NULL);
2390 /* check non-recursive for schema that exists in toplevel */
2391 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2392 g_assert (schema != NULL);
2393 g_settings_schema_unref (schema);
2395 /* check recursive for schema that exists in toplevel */
2396 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2397 g_assert (schema != NULL);
2399 /* try to use it for something */
2400 settings = g_settings_new_full (schema, backend, g_settings_schema_get_path (schema));
2401 g_settings_schema_unref (schema);
2402 enabled = FALSE;
2403 g_settings_get (settings, "enabled", "b", &enabled);
2404 g_assert (enabled);
2405 g_object_unref (settings);
2407 g_settings_schema_source_unref (source);
2409 /* try again, but with no parent */
2410 source = g_settings_schema_source_new_from_directory ("schema-source", NULL, FALSE, NULL);
2411 g_assert (source != NULL);
2413 /* should not find it this time, even if recursive... */
2414 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2415 g_assert (schema == NULL);
2416 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2417 g_assert (schema == NULL);
2419 /* should still find our own... */
2420 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2421 g_assert (schema != NULL);
2422 g_settings_schema_unref (schema);
2423 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2424 g_assert (schema != NULL);
2425 g_settings_schema_unref (schema);
2427 g_settings_schema_source_unref (source);
2430 static void
2431 test_schema_list_keys (void)
2433 gchar **keys;
2434 GSettingsSchemaSource *src = g_settings_schema_source_get_default ();
2435 GSettingsSchema *schema = g_settings_schema_source_lookup (src, "org.gtk.test", TRUE);
2436 g_assert (schema != NULL);
2438 keys = g_settings_schema_list_keys (schema);
2440 g_assert (strv_set_equal ((gchar **)keys,
2441 "greeting",
2442 "farewell",
2443 NULL));
2445 g_strfreev (keys);
2446 g_settings_schema_unref (schema);
2449 static void
2450 test_actions (void)
2452 GAction *string, *toggle;
2453 gboolean c1, c2, c3;
2454 GSettings *settings;
2455 gchar *name;
2456 GVariantType *param_type;
2457 gboolean enabled;
2458 GVariantType *state_type;
2459 GVariant *state;
2461 settings = g_settings_new ("org.gtk.test.basic-types");
2462 string = g_settings_create_action (settings, "test-string");
2463 toggle = g_settings_create_action (settings, "test-boolean");
2464 g_object_unref (settings); /* should be held by the actions */
2466 g_signal_connect (settings, "changed", G_CALLBACK (changed_cb2), &c1);
2467 g_signal_connect (string, "notify::state", G_CALLBACK (changed_cb2), &c2);
2468 g_signal_connect (toggle, "notify::state", G_CALLBACK (changed_cb2), &c3);
2470 c1 = c2 = c3 = FALSE;
2471 g_settings_set_string (settings, "test-string", "hello world");
2472 check_and_free (g_action_get_state (string), "'hello world'");
2473 g_assert (c1 && c2 && !c3);
2474 c1 = c2 = c3 = FALSE;
2476 g_action_activate (string, g_variant_new_string ("hihi"));
2477 check_and_free (g_settings_get_value (settings, "test-string"), "'hihi'");
2478 g_assert (c1 && c2 && !c3);
2479 c1 = c2 = c3 = FALSE;
2481 g_action_change_state (string, g_variant_new_string ("kthxbye"));
2482 check_and_free (g_settings_get_value (settings, "test-string"), "'kthxbye'");
2483 g_assert (c1 && c2 && !c3);
2484 c1 = c2 = c3 = FALSE;
2486 g_action_change_state (toggle, g_variant_new_boolean (TRUE));
2487 g_assert (g_settings_get_boolean (settings, "test-boolean"));
2488 g_assert (c1 && !c2 && c3);
2489 c1 = c2 = c3 = FALSE;
2491 g_action_activate (toggle, NULL);
2492 g_assert (!g_settings_get_boolean (settings, "test-boolean"));
2493 g_assert (c1 && !c2 && c3);
2495 g_object_get (string,
2496 "name", &name,
2497 "parameter-type", &param_type,
2498 "enabled", &enabled,
2499 "state-type", &state_type,
2500 "state", &state,
2501 NULL);
2503 g_assert_cmpstr (name, ==, "test-string");
2504 g_assert (g_variant_type_equal (param_type, G_VARIANT_TYPE_STRING));
2505 g_assert (enabled);
2506 g_assert (g_variant_type_equal (state_type, G_VARIANT_TYPE_STRING));
2507 g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "kthxbye");
2509 g_free (name);
2510 g_variant_type_free (param_type);
2511 g_variant_type_free (state_type);
2512 g_variant_unref (state);
2514 g_object_unref (string);
2515 g_object_unref (toggle);
2518 static void
2519 test_null_backend (void)
2521 GSettingsBackend *backend;
2522 GSettings *settings;
2523 gchar *str;
2524 gboolean writable;
2526 backend = g_null_settings_backend_new ();
2527 settings = g_settings_new_with_backend_and_path ("org.gtk.test", backend, "/tests/");
2529 g_object_get (settings, "schema-id", &str, NULL);
2530 g_assert_cmpstr (str, ==, "org.gtk.test");
2531 g_free (str);
2533 g_settings_get (settings, "greeting", "s", &str);
2534 g_assert_cmpstr (str, ==, "Hello, earthlings");
2535 g_free (str);
2537 g_settings_set (settings, "greeting", "s", "goodbye world");
2538 g_settings_get (settings, "greeting", "s", &str);
2539 g_assert_cmpstr (str, ==, "Hello, earthlings");
2540 g_free (str);
2542 writable = g_settings_is_writable (settings, "greeting");
2543 g_assert (!writable);
2545 g_settings_reset (settings, "greeting");
2547 g_settings_delay (settings);
2548 g_settings_set (settings, "greeting", "s", "goodbye world");
2549 g_settings_apply (settings);
2550 g_settings_get (settings, "greeting", "s", &str);
2551 g_assert_cmpstr (str, ==, "Hello, earthlings");
2552 g_free (str);
2554 g_object_unref (settings);
2555 g_object_unref (backend);
2558 static void
2559 test_memory_backend (void)
2561 GSettingsBackend *backend;
2563 backend = g_memory_settings_backend_new ();
2564 g_assert (G_IS_SETTINGS_BACKEND (backend));
2565 g_object_unref (backend);
2568 static void
2569 test_read_descriptions (void)
2571 GSettingsSchema *schema;
2572 GSettingsSchemaKey *key;
2573 GSettings *settings;
2575 settings = g_settings_new ("org.gtk.test");
2576 g_object_get (settings, "settings-schema", &schema, NULL);
2577 key = g_settings_schema_get_key (schema, "greeting");
2579 g_assert_cmpstr (g_settings_schema_key_get_summary (key), ==, "A greeting");
2580 g_assert_cmpstr (g_settings_schema_key_get_description (key), ==, "Greeting of the invading martians");
2582 g_settings_schema_key_unref (key);
2583 g_settings_schema_unref (schema);
2585 g_object_unref (settings);
2587 settings = g_settings_new ("org.gtk.test.descriptions");
2588 g_object_get (settings, "settings-schema", &schema, NULL);
2589 key = g_settings_schema_get_key (schema, "a");
2591 g_assert_cmpstr (g_settings_schema_key_get_summary (key), ==,
2592 "a paragraph.\n\n"
2593 "with some whitespace.\n\n"
2594 "because not everyone has a great editor.\n\n"
2595 "lots of space is as one.");
2597 g_settings_schema_key_unref (key);
2598 g_settings_schema_unref (schema);
2600 g_object_unref (settings);
2603 static void
2604 test_default_value (void)
2606 GSettings *settings;
2607 GSettingsSchema *schema;
2608 GSettingsSchemaKey *key;
2609 GVariant *v;
2610 const gchar *str;
2611 gchar *s;
2613 settings = g_settings_new ("org.gtk.test");
2614 g_object_get (settings, "settings-schema", &schema, NULL);
2615 key = g_settings_schema_get_key (schema, "greeting");
2616 g_settings_schema_unref (schema);
2617 g_settings_schema_key_ref (key);
2619 g_assert (g_variant_type_equal (g_settings_schema_key_get_value_type (key), G_VARIANT_TYPE_STRING));
2621 v = g_settings_schema_key_get_default_value (key);
2622 str = g_variant_get_string (v, NULL);
2623 g_assert_cmpstr (str, ==, "Hello, earthlings");
2624 g_variant_unref (v);
2626 g_settings_schema_key_unref (key);
2627 g_settings_schema_key_unref (key);
2629 g_settings_set (settings, "greeting", "s", "goodbye world");
2631 v = g_settings_get_user_value (settings, "greeting");
2632 str = g_variant_get_string (v, NULL);
2633 g_assert_cmpstr (str, ==, "goodbye world");
2634 g_variant_unref (v);
2636 v = g_settings_get_default_value (settings, "greeting");
2637 str = g_variant_get_string (v, NULL);
2638 g_assert_cmpstr (str, ==, "Hello, earthlings");
2639 g_variant_unref (v);
2641 g_settings_reset (settings, "greeting");
2643 v = g_settings_get_user_value (settings, "greeting");
2644 g_assert_null (v);
2646 s = g_settings_get_string (settings, "greeting");
2647 g_assert_cmpstr (s, ==, "Hello, earthlings");
2648 g_free (s);
2650 g_object_unref (settings);
2653 static gboolean
2654 string_map_func (GVariant *value,
2655 gpointer *result,
2656 gpointer user_data)
2658 const gchar *str;
2660 str = g_variant_get_string (value, NULL);
2661 *result = g_variant_new_string (str);
2663 return TRUE;
2666 /* Test that per-desktop values from org.gtk.test.gschema.override
2667 * does not change default value if current desktop is not listed in
2668 * $XDG_CURRENT_DESKTOP.
2670 static void
2671 test_per_desktop (void)
2673 GSettings *settings;
2674 TestObject *obj;
2675 gpointer p;
2676 gchar *str;
2678 settings = g_settings_new ("org.gtk.test.per-desktop");
2679 obj = test_object_new ();
2681 if (!g_test_subprocess ())
2683 g_test_trap_subprocess ("/gsettings/per-desktop/subprocess", 0, 0);
2684 g_test_trap_assert_passed ();
2687 str = g_settings_get_string (settings, "desktop");
2688 g_assert_cmpstr (str, ==, "GNOME");
2689 g_free (str);
2691 p = g_settings_get_mapped (settings, "desktop", string_map_func, NULL);
2693 str = g_variant_dup_string (p, NULL);
2694 g_assert_cmpstr (str, ==, "GNOME");
2695 g_free (str);
2697 g_variant_unref (p);
2699 g_settings_bind (settings, "desktop", obj, "string", G_SETTINGS_BIND_DEFAULT);
2701 g_object_get (obj, "string", &str, NULL);
2702 g_assert_cmpstr (str, ==, "GNOME");
2703 g_free (str);
2705 g_object_unref (settings);
2706 g_object_unref (obj);
2709 /* Test that per-desktop values from org.gtk.test.gschema.override
2710 * are successfully loaded based on the value of $XDG_CURRENT_DESKTOP.
2712 static void
2713 test_per_desktop_subprocess (void)
2715 GSettings *settings;
2716 TestObject *obj;
2717 gpointer p;
2718 gchar *str;
2720 g_setenv ("XDG_CURRENT_DESKTOP", "GNOME-Classic:GNOME", TRUE);
2722 settings = g_settings_new ("org.gtk.test.per-desktop");
2723 obj = test_object_new ();
2725 str = g_settings_get_string (settings, "desktop");
2726 g_assert_cmpstr (str, ==, "GNOME Classic");
2727 g_free (str);
2729 p = g_settings_get_mapped (settings, "desktop", string_map_func, NULL);
2731 str = g_variant_dup_string (p, NULL);
2732 g_assert_cmpstr (str, ==, "GNOME Classic");
2733 g_free (str);
2735 g_variant_unref (p);
2737 g_settings_bind (settings, "desktop", obj, "string", G_SETTINGS_BIND_DEFAULT);
2739 g_object_get (obj, "string", &str, NULL);
2740 g_assert_cmpstr (str, ==, "GNOME Classic");
2741 g_free (str);
2743 g_object_unref (settings);
2744 g_object_unref (obj);
2747 static void
2748 test_extended_schema (void)
2750 GSettingsSchema *schema;
2751 GSettings *settings;
2752 gchar **keys;
2754 settings = g_settings_new_with_path ("org.gtk.test.extends.extended", "/test/extendes/");
2755 g_object_get (settings, "settings-schema", &schema, NULL);
2756 keys = g_settings_schema_list_keys (schema);
2757 g_assert (strv_set_equal (keys, "int32", "string", "another-int32", NULL));
2758 g_strfreev (keys);
2759 g_object_unref (settings);
2760 g_settings_schema_unref (schema);
2764 main (int argc, char *argv[])
2766 gchar *schema_text;
2767 gchar *override_text;
2768 gchar *enums;
2769 gint result;
2771 /* Meson build sets this */
2772 #ifdef TEST_LOCALE_PATH
2773 if (g_str_has_suffix (TEST_LOCALE_PATH, "LC_MESSAGES"))
2775 locale_dir = TEST_LOCALE_PATH G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S "..";
2777 #endif
2779 setlocale (LC_ALL, "");
2781 g_test_init (&argc, &argv, NULL);
2783 if (!g_test_subprocess ())
2785 GError *local_error = NULL;
2786 /* A GVDB header is 6 guint32s, and requires a magic number in the first
2787 * two guint32s. A set of zero bytes of a greater length is considered
2788 * corrupt. */
2789 const guint8 gschemas_compiled_corrupt[sizeof (guint32) * 7] = { 0, };
2791 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
2793 g_setenv ("XDG_DATA_DIRS", ".", TRUE);
2794 g_setenv ("XDG_DATA_HOME", ".", TRUE);
2795 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
2796 g_setenv ("XDG_CURRENT_DESKTOP", "", TRUE);
2798 if (!backend_set)
2799 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
2801 /* Meson build defines this, autotools build does not */
2802 #ifndef GLIB_MKENUMS
2803 #define GLIB_MKENUMS "../../gobject/glib-mkenums"
2804 #endif
2806 g_remove ("org.gtk.test.enums.xml");
2807 g_assert (g_spawn_command_line_sync (GLIB_MKENUMS " "
2808 "--template " SRCDIR "/enums.xml.template "
2809 SRCDIR "/testenum.h",
2810 &enums, NULL, &result, NULL));
2811 g_assert (result == 0);
2812 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
2813 g_free (enums);
2815 g_assert (g_file_get_contents (SRCDIR "/org.gtk.test.gschema.xml.orig", &schema_text, NULL, NULL));
2816 g_assert (g_file_set_contents ("org.gtk.test.gschema.xml", schema_text, -1, NULL));
2817 g_free (schema_text);
2819 g_assert (g_file_get_contents (SRCDIR "/org.gtk.test.gschema.override.orig", &override_text, NULL, NULL));
2820 g_assert (g_file_set_contents ("org.gtk.test.gschema.override", override_text, -1, NULL));
2821 g_free (override_text);
2823 /* Meson build defines this, autotools build does not */
2824 #ifndef GLIB_COMPILE_SCHEMAS
2825 #define GLIB_COMPILE_SCHEMAS "../glib-compile-schemas"
2826 #endif
2828 g_remove ("gschemas.compiled");
2829 g_assert (g_spawn_command_line_sync (GLIB_COMPILE_SCHEMAS " --targetdir=. "
2830 "--schema-file=org.gtk.test.enums.xml "
2831 "--schema-file=org.gtk.test.gschema.xml "
2832 "--override-file=org.gtk.test.gschema.override",
2833 NULL, NULL, &result, NULL));
2834 g_assert (result == 0);
2836 g_remove ("schema-source/gschemas.compiled");
2837 g_mkdir ("schema-source", 0777);
2838 g_assert (g_spawn_command_line_sync (GLIB_COMPILE_SCHEMAS " --targetdir=schema-source "
2839 "--schema-file=" SRCDIR "/org.gtk.schemasourcecheck.gschema.xml",
2840 NULL, NULL, &result, NULL));
2841 g_assert (result == 0);
2843 g_remove ("schema-source-corrupt/gschemas.compiled");
2844 g_mkdir ("schema-source-corrupt", 0777);
2845 g_file_set_contents ("schema-source-corrupt/gschemas.compiled",
2846 (const gchar *) gschemas_compiled_corrupt,
2847 sizeof (gschemas_compiled_corrupt),
2848 &local_error);
2849 g_assert_no_error (local_error);
2851 g_remove ("schema-source-empty/gschemas.compiled");
2852 g_mkdir ("schema-source-empty", 0777);
2853 g_file_set_contents ("schema-source-empty/gschemas.compiled",
2854 "", 0,
2855 &local_error);
2856 g_assert_no_error (local_error);
2859 g_test_add_func ("/gsettings/basic", test_basic);
2861 if (!backend_set)
2863 g_test_add_func ("/gsettings/no-schema", test_no_schema);
2864 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
2865 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
2866 g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
2867 g_test_add_func ("/gsettings/no-path", test_no_path);
2870 g_test_add_func ("/gsettings/basic-types", test_basic_types);
2871 g_test_add_func ("/gsettings/complex-types", test_complex_types);
2872 g_test_add_func ("/gsettings/changes", test_changes);
2874 g_test_add_func ("/gsettings/l10n", test_l10n);
2875 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
2877 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
2878 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
2879 g_test_add_func ("/gsettings/delay-child", test_delay_child);
2880 g_test_add_func ("/gsettings/atomic", test_atomic);
2882 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
2883 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
2884 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
2885 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
2886 g_test_add_func ("/gsettings/unbinding", test_unbind);
2887 g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
2889 if (!backend_set)
2891 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
2892 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
2893 g_test_add_func ("/gsettings/no-read-binding/subprocess/fail", test_no_read_binding_fail);
2894 g_test_add_func ("/gsettings/no-read-binding/subprocess/pass", test_no_read_binding_pass);
2895 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
2896 g_test_add_func ("/gsettings/no-write-binding/subprocess/fail", test_no_write_binding_fail);
2897 g_test_add_func ("/gsettings/no-write-binding/subprocess/pass", test_no_write_binding_pass);
2900 g_test_add_func ("/gsettings/keyfile", test_keyfile);
2901 g_test_add_func ("/gsettings/child-schema", test_child_schema);
2902 g_test_add_func ("/gsettings/strinfo", test_strinfo);
2903 g_test_add_func ("/gsettings/enums", test_enums);
2904 g_test_add_func ("/gsettings/enums/subprocess/non-enum-key", test_enums_non_enum_key);
2905 g_test_add_func ("/gsettings/enums/subprocess/non-enum-value", test_enums_non_enum_value);
2906 g_test_add_func ("/gsettings/enums/subprocess/range", test_enums_range);
2907 g_test_add_func ("/gsettings/enums/subprocess/non-flags", test_enums_non_flags);
2908 g_test_add_func ("/gsettings/flags", test_flags);
2909 g_test_add_func ("/gsettings/flags/subprocess/non-flags-key", test_flags_non_flags_key);
2910 g_test_add_func ("/gsettings/flags/subprocess/non-flags-value", test_flags_non_flags_value);
2911 g_test_add_func ("/gsettings/flags/subprocess/range", test_flags_range);
2912 g_test_add_func ("/gsettings/flags/subprocess/non-enum", test_flags_non_enum);
2913 g_test_add_func ("/gsettings/range", test_range);
2914 g_test_add_func ("/gsettings/range/subprocess/high", test_range_high);
2915 g_test_add_func ("/gsettings/range/subprocess/low", test_range_low);
2916 g_test_add_func ("/gsettings/list-items", test_list_items);
2917 g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
2918 g_test_add_func ("/gsettings/mapped", test_get_mapped);
2919 g_test_add_func ("/gsettings/get-range", test_get_range);
2920 g_test_add_func ("/gsettings/schema-source", test_schema_source);
2921 g_test_add_func ("/gsettings/schema-list-keys", test_schema_list_keys);
2922 g_test_add_func ("/gsettings/actions", test_actions);
2923 g_test_add_func ("/gsettings/null-backend", test_null_backend);
2924 g_test_add_func ("/gsettings/memory-backend", test_memory_backend);
2925 g_test_add_func ("/gsettings/read-descriptions", test_read_descriptions);
2926 g_test_add_func ("/gsettings/test-extended-schema", test_extended_schema);
2927 g_test_add_func ("/gsettings/default-value", test_default_value);
2928 g_test_add_func ("/gsettings/per-desktop", test_per_desktop);
2929 g_test_add_func ("/gsettings/per-desktop/subprocess", test_per_desktop_subprocess);
2931 result = g_test_run ();
2933 g_settings_sync ();
2935 return result;