Fix up gsettings test
[platform/upstream/glib.git] / gio / tests / gsettings.c
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>
8
9 #include "testenum.h"
10
11 static gboolean backend_set;
12
13 /* These tests rely on the schemas in org.gtk.test.gschema.xml
14  * to be compiled and installed in the same directory.
15  */
16
17 static void
18 check_and_free (GVariant    *value,
19                 const gchar *expected)
20 {
21   gchar *printed;
22
23   printed = g_variant_print (value, TRUE);
24   g_assert_cmpstr (printed, ==, expected);
25   g_free (printed);
26
27   g_variant_unref (value);
28 }
29
30
31 /* Just to get warmed up: Read and set a string, and
32  * verify that can read the changed string back
33  */
34 static void
35 test_basic (void)
36 {
37   gchar *str = NULL;
38   GObject *b;
39   gchar *path;
40   gboolean has_unapplied;
41   gboolean delay_apply;
42   GSettings *settings;
43
44   settings = g_settings_new ("org.gtk.test");
45
46   g_object_get (settings,
47                 "schema", &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);
61
62   g_settings_get (settings, "greeting", "s", &str);
63   g_assert_cmpstr (str, ==, "Hello, earthlings");
64   g_free (str);
65
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;
71
72   if (!backend_set && g_test_undefined ())
73     {
74       GSettings *tmp_settings = g_settings_new ("org.gtk.test");
75
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 ();
80
81       g_object_unref (tmp_settings);
82     }
83
84   g_settings_get (settings, "greeting", "s", &str);
85   g_assert_cmpstr (str, ==, "goodbye world");
86   g_free (str);
87   str = NULL;
88
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);
93
94   g_settings_set (settings, "greeting", "s", "this is the end");
95   g_object_unref (settings);
96 }
97
98 /* Check that we get an error when getting a key
99  * that is not in the schema
100  */
101 static void
102 test_unknown_key (void)
103 {
104   if (!g_test_undefined ())
105     return;
106
107   if (g_test_subprocess ())
108     {
109       GSettings *settings;
110       GVariant *value;
111
112       settings = g_settings_new ("org.gtk.test");
113       value = g_settings_get_value (settings, "no_such_key");
114
115       g_assert (value == NULL);
116
117       g_object_unref (settings);
118       return;
119     }
120   g_test_trap_subprocess (NULL, 0, 0);
121   g_test_trap_assert_failed ();
122   g_test_trap_assert_stderr ("*does not contain*");
123 }
124
125 /* Check that we get an error when the schema
126  * has not been installed
127  */
128 static void
129 test_no_schema (void)
130 {
131   if (!g_test_undefined ())
132     return;
133
134   if (g_test_subprocess ())
135     {
136       GSettings *settings;
137
138       settings = g_settings_new ("no.such.schema");
139
140       g_assert (settings == NULL);
141       return;
142     }
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*");
146 }
147
148 /* Check that we get an error when passing a type string
149  * that does not match the schema
150  */
151 static void
152 test_wrong_type (void)
153 {
154   GSettings *settings;
155   gchar *str = NULL;
156
157   if (!g_test_undefined ())
158     return;
159
160   settings = g_settings_new ("org.gtk.test");
161
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 ();
168
169   g_assert (str == NULL);
170
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 ();
175
176   g_object_unref (settings);
177 }
178
179 /* Check errors with explicit paths */
180 static void
181 test_wrong_path (void)
182 {
183   if (!g_test_undefined ())
184     return;
185
186   if (g_test_subprocess ())
187     {
188       GSettings *settings G_GNUC_UNUSED;
189
190       settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
191       return;
192     }
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*");
196 }
197
198 static void
199 test_no_path (void)
200 {
201   if (!g_test_undefined ())
202     return;
203
204   if (g_test_subprocess ())
205     {
206       GSettings *settings G_GNUC_UNUSED;
207
208       settings = g_settings_new ("org.gtk.test.no-path");
209       return;
210     }
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**");
214 }
215
216
217 /* Check that we can successfully read and set the full
218  * range of all basic types
219  */
220 static void
221 test_basic_types (void)
222 {
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;
234
235   settings = g_settings_new ("org.gtk.test.basic-types");
236
237   g_settings_get (settings, "test-boolean", "b", &b);
238   g_assert_cmpint (b, ==, 1);
239
240   g_settings_set (settings, "test-boolean", "b", 0);
241   g_settings_get (settings, "test-boolean", "b", &b);
242   g_assert_cmpint (b, ==, 0);
243
244   g_settings_get (settings, "test-byte", "y", &byte);
245   g_assert_cmpint (byte, ==, 25);
246
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);
250
251   g_settings_get (settings, "test-int16", "n", &i16);
252   g_assert_cmpint (i16, ==, -1234);
253
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);
257
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);
261
262   g_settings_get (settings, "test-uint16", "q", &u16);
263   g_assert_cmpuint (u16, ==, 1234);
264
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);
268
269   g_settings_get (settings, "test-int32", "i", &i32);
270   g_assert_cmpint (i32, ==, -123456);
271
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);
275
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);
279
280   g_settings_get (settings, "test-uint32", "u", &u32);
281   g_assert_cmpuint (u32, ==, 123456);
282
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);
286
287   g_settings_get (settings, "test-int64", "x", &i64);
288   g_assert_cmpuint (i64, ==, -123456789);
289
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);
293
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);
297
298   g_settings_get (settings, "test-uint64", "t", &u64);
299   g_assert_cmpuint (u64, ==, 123456789);
300
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);
304
305   g_settings_get (settings, "test-double", "d", &d);
306   g_assert_cmpfloat (d, ==, 123.456);
307
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);
311
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);
315
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;
320
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;
326 }
327
328 /* Check that we can read an set complex types like
329  * tuples, arrays and dictionaries
330  */
331 static void
332 test_complex_types (void)
333 {
334   GSettings *settings;
335   gchar *s;
336   gint i1, i2;
337   GVariantIter *iter = NULL;
338
339   settings = g_settings_new ("org.gtk.test.complex-types");
340
341   g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
342   g_assert_cmpstr (s, ==, "one");
343   g_assert_cmpint (i1,==, 2);
344   g_assert_cmpint (i2,==, 3);
345   g_free (s) ;
346   s = NULL;
347
348   g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
349   g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
350   g_assert_cmpstr (s, ==, "none");
351   g_assert_cmpint (i1,==, 0);
352   g_assert_cmpint (i2,==, 0);
353   g_free (s);
354   s = NULL;
355
356   g_settings_get (settings, "test-array", "ai", &iter);
357   g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
358   g_assert (g_variant_iter_next (iter, "i", &i1));
359   g_assert_cmpint (i1, ==, 0);
360   g_assert (g_variant_iter_next (iter, "i", &i1));
361   g_assert_cmpint (i1, ==, 1);
362   g_assert (g_variant_iter_next (iter, "i", &i1));
363   g_assert_cmpint (i1, ==, 2);
364   g_assert (g_variant_iter_next (iter, "i", &i1));
365   g_assert_cmpint (i1, ==, 3);
366   g_assert (g_variant_iter_next (iter, "i", &i1));
367   g_assert_cmpint (i1, ==, 4);
368   g_assert (g_variant_iter_next (iter, "i", &i1));
369   g_assert_cmpint (i1, ==, 5);
370   g_assert (!g_variant_iter_next (iter, "i", &i1));
371   g_variant_iter_free (iter);
372
373   g_object_unref (settings);
374 }
375
376 static gboolean changed_cb_called;
377
378 static void
379 changed_cb (GSettings   *settings,
380             const gchar *key,
381             gpointer     data)
382 {
383   changed_cb_called = TRUE;
384
385   g_assert_cmpstr (key, ==, data);
386 }
387
388 /* Test that basic change notification with the changed signal works.
389  */
390 static void
391 test_changes (void)
392 {
393   GSettings *settings;
394   GSettings *settings2;
395
396   settings = g_settings_new ("org.gtk.test");
397
398   g_signal_connect (settings, "changed",
399                     G_CALLBACK (changed_cb), "greeting");
400
401   changed_cb_called = FALSE;
402
403   g_settings_set (settings, "greeting", "s", "new greeting");
404   g_assert (changed_cb_called);
405
406   settings2 = g_settings_new ("org.gtk.test");
407
408   changed_cb_called = FALSE;
409
410   g_settings_set (settings2, "greeting", "s", "hi");
411   g_assert (changed_cb_called);
412
413   g_object_unref (settings2);
414   g_object_unref (settings);
415 }
416
417 static gboolean changed_cb_called2;
418
419 static void
420 changed_cb2 (GSettings   *settings,
421              const gchar *key,
422              gpointer     data)
423 {
424   gboolean *p = data;
425
426   *p = TRUE;
427 }
428
429 /* Test that changes done to a delay-mode instance
430  * don't appear to the outside world until apply. Also
431  * check that we get change notification when they are
432  * applied.
433  * Also test that the has-unapplied property is properly
434  * maintained.
435  */
436 static void
437 test_delay_apply (void)
438 {
439   GSettings *settings;
440   GSettings *settings2;
441   gchar *str;
442   gboolean writable;
443
444   settings = g_settings_new ("org.gtk.test");
445   settings2 = g_settings_new ("org.gtk.test");
446
447   g_settings_set (settings2, "greeting", "s", "top o' the morning");
448
449   changed_cb_called = FALSE;
450   changed_cb_called2 = FALSE;
451
452   g_signal_connect (settings, "changed",
453                     G_CALLBACK (changed_cb2), &changed_cb_called);
454   g_signal_connect (settings2, "changed",
455                     G_CALLBACK (changed_cb2), &changed_cb_called2);
456
457   g_settings_delay (settings);
458
459   g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
460
461   g_assert (changed_cb_called);
462   g_assert (!changed_cb_called2);
463
464   writable = g_settings_is_writable (settings, "greeting");
465   g_assert (writable);
466
467   g_settings_get (settings, "greeting", "s", &str);
468   g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
469   g_free (str);
470   str = NULL;
471
472   g_settings_get (settings2, "greeting", "s", &str);
473   g_assert_cmpstr (str, ==, "top o' the morning");
474   g_free (str);
475   str = NULL;
476
477   g_assert (g_settings_get_has_unapplied (settings));
478   g_assert (!g_settings_get_has_unapplied (settings2));
479
480   changed_cb_called = FALSE;
481   changed_cb_called2 = FALSE;
482
483   g_settings_apply (settings);
484
485   g_assert (!changed_cb_called);
486   g_assert (changed_cb_called2);
487
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;
492
493   g_settings_get (settings2, "greeting", "s", &str);
494   g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
495   g_free (str);
496   str = NULL;
497
498   g_assert (!g_settings_get_has_unapplied (settings));
499   g_assert (!g_settings_get_has_unapplied (settings2));
500
501   g_settings_reset (settings, "greeting");
502   g_settings_apply (settings);
503
504   g_settings_get (settings, "greeting", "s", &str);
505   g_assert_cmpstr (str, ==, "Hello, earthlings");
506   g_free (str);
507
508   g_object_unref (settings2);
509   g_object_unref (settings);
510 }
511
512 /* Test that reverting unapplied changes in a delay-apply
513  * settings instance works.
514  */
515 static void
516 test_delay_revert (void)
517 {
518   GSettings *settings;
519   GSettings *settings2;
520   gchar *str;
521
522   settings = g_settings_new ("org.gtk.test");
523   settings2 = g_settings_new ("org.gtk.test");
524
525   g_settings_set (settings2, "greeting", "s", "top o' the morning");
526
527   g_settings_get (settings, "greeting", "s", &str);
528   g_assert_cmpstr (str, ==, "top o' the morning");
529   g_free (str);
530
531   g_settings_delay (settings);
532
533   g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
534
535   g_settings_get (settings, "greeting", "s", &str);
536   g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
537   g_free (str);
538   str = NULL;
539
540   g_settings_get (settings2, "greeting", "s", &str);
541   g_assert_cmpstr (str, ==, "top o' the morning");
542   g_free (str);
543   str = NULL;
544
545   g_assert (g_settings_get_has_unapplied (settings));
546
547   g_settings_revert (settings);
548
549   g_assert (!g_settings_get_has_unapplied (settings));
550
551   g_settings_get (settings, "greeting", "s", &str);
552   g_assert_cmpstr (str, ==, "top o' the morning");
553   g_free (str);
554   str = NULL;
555
556   g_settings_get (settings2, "greeting", "s", &str);
557   g_assert_cmpstr (str, ==, "top o' the morning");
558   g_free (str);
559   str = NULL;
560
561   g_object_unref (settings2);
562   g_object_unref (settings);
563 }
564
565 static void
566 keys_changed_cb (GSettings    *settings,
567                  const GQuark *keys,
568                  gint          n_keys)
569 {
570   gchar *str;
571
572   g_assert_cmpint (n_keys, ==, 2);
573
574   g_assert ((keys[0] == g_quark_from_static_string ("greeting") &&
575              keys[1] == g_quark_from_static_string ("farewell")) ||
576             (keys[1] == g_quark_from_static_string ("greeting") &&
577              keys[0] == g_quark_from_static_string ("farewell")));
578
579   g_settings_get (settings, "greeting", "s", &str);
580   g_assert_cmpstr (str, ==, "greetings from test_atomic");
581   g_free (str);
582   str = NULL;
583
584   g_settings_get (settings, "farewell", "s", &str);
585   g_assert_cmpstr (str, ==, "atomic bye-bye");
586   g_free (str);
587   str = NULL;
588 }
589
590 /* Check that delay-applied changes appear atomically.
591  * More specifically, verify that all changed keys appear
592  * with their new value while handling the change-event signal.
593  */
594 static void
595 test_atomic (void)
596 {
597   GSettings *settings;
598   GSettings *settings2;
599   gchar *str;
600
601   settings = g_settings_new ("org.gtk.test");
602   settings2 = g_settings_new ("org.gtk.test");
603
604   g_settings_set (settings2, "greeting", "s", "top o' the morning");
605
606   changed_cb_called = FALSE;
607   changed_cb_called2 = FALSE;
608
609   g_signal_connect (settings2, "change-event",
610                     G_CALLBACK (keys_changed_cb), NULL);
611
612   g_settings_delay (settings);
613
614   g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
615   g_settings_set (settings, "farewell", "s", "atomic bye-bye");
616
617   g_settings_apply (settings);
618
619   g_settings_get (settings, "greeting", "s", &str);
620   g_assert_cmpstr (str, ==, "greetings from test_atomic");
621   g_free (str);
622   str = NULL;
623
624   g_settings_get (settings, "farewell", "s", &str);
625   g_assert_cmpstr (str, ==, "atomic bye-bye");
626   g_free (str);
627   str = NULL;
628
629   g_settings_get (settings2, "greeting", "s", &str);
630   g_assert_cmpstr (str, ==, "greetings from test_atomic");
631   g_free (str);
632   str = NULL;
633
634   g_settings_get (settings2, "farewell", "s", &str);
635   g_assert_cmpstr (str, ==, "atomic bye-bye");
636   g_free (str);
637   str = NULL;
638
639   g_object_unref (settings2);
640   g_object_unref (settings);
641 }
642
643 /* On Windows the interaction between the C library locale and libintl
644  * (from GNU gettext) is not like on POSIX, so just skip these tests
645  * for now.
646  *
647  * There are several issues:
648  *
649  * 1) The C library doesn't use LC_MESSAGES, that is implemented only
650  * in libintl (defined in its <libintl.h>).
651  *
652  * 2) The locale names that setlocale() accepts and returns aren't in
653  * the "de_DE" style, but like "German_Germany".
654  *
655  * 3) libintl looks at the Win32 thread locale and not the C library
656  * locale. (And even if libintl would use the C library's locale, as
657  * there are several alternative C library DLLs, libintl might be
658  * linked to a different one than the application code, so they
659  * wouldn't have the same C library locale anyway.)
660  */
661
662 /* Test that translations work for schema defaults.
663  *
664  * This test relies on the de.po file in the same directory
665  * to be compiled into ./de/LC_MESSAGES/test.mo
666  */
667 static void
668 test_l10n (void)
669 {
670   GSettings *settings;
671   gchar *str;
672   gchar *locale;
673
674   bindtextdomain ("test", ".");
675   bind_textdomain_codeset ("test", "UTF-8");
676
677   locale = g_strdup (setlocale (LC_MESSAGES, NULL));
678
679   settings = g_settings_new ("org.gtk.test.localized");
680
681   setlocale (LC_MESSAGES, "C");
682   str = g_settings_get_string (settings, "error-message");
683   setlocale (LC_MESSAGES, locale);
684
685   g_assert_cmpstr (str, ==, "Unnamed");
686   g_free (str);
687   str = NULL;
688
689   setlocale (LC_MESSAGES, "de_DE");
690   /* Only do the test if translation is actually working... */
691   if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
692     {
693       str = g_settings_get_string (settings, "error-message");
694
695       g_assert_cmpstr (str, ==, "Unbenannt");
696       g_object_unref (settings);
697       g_free (str);
698       str = NULL;
699     }
700   else
701     g_printerr ("warning: translation is not working... skipping test. ");
702
703   setlocale (LC_MESSAGES, locale);
704   g_free (locale);
705 }
706
707 /* Test that message context works as expected with translated
708  * schema defaults. Also, verify that non-ASCII UTF-8 content
709  * works.
710  *
711  * This test relies on the de.po file in the same directory
712  * to be compiled into ./de/LC_MESSAGES/test.mo
713  */
714 static void
715 test_l10n_context (void)
716 {
717   GSettings *settings;
718   gchar *str;
719   gchar *locale;
720
721   bindtextdomain ("test", ".");
722   bind_textdomain_codeset ("test", "UTF-8");
723
724   locale = g_strdup (setlocale (LC_MESSAGES, NULL));
725
726   settings = g_settings_new ("org.gtk.test.localized");
727
728   setlocale (LC_MESSAGES, "C");
729   g_settings_get (settings, "backspace", "s", &str);
730   setlocale (LC_MESSAGES, locale);
731
732   g_assert_cmpstr (str, ==, "BackSpace");
733   g_free (str);
734   str = NULL;
735
736   setlocale (LC_MESSAGES, "de_DE");
737   /* Only do the test if translation is actually working... */
738   if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
739     {
740       g_settings_get (settings, "backspace", "s", &str);
741
742       g_assert_cmpstr (str, ==, "Löschen");
743       g_object_unref (settings);
744       g_free (str);
745       str = NULL;
746     }
747   else
748     g_printerr ("warning: translation is not working... skipping test.  ");
749
750   setlocale (LC_MESSAGES, locale);
751   g_free (locale);
752 }
753
754 enum
755 {
756   PROP_0,
757   PROP_BOOL,
758   PROP_ANTI_BOOL,
759   PROP_BYTE,
760   PROP_INT16,
761   PROP_UINT16,
762   PROP_INT,
763   PROP_UINT,
764   PROP_INT64,
765   PROP_UINT64,
766   PROP_DOUBLE,
767   PROP_STRING,
768   PROP_NO_READ,
769   PROP_NO_WRITE,
770   PROP_STRV,
771   PROP_ENUM,
772   PROP_FLAGS
773 };
774
775 typedef struct
776 {
777   GObject parent_instance;
778
779   gboolean bool_prop;
780   gboolean anti_bool_prop;
781   gint8 byte_prop;
782   gint int16_prop;
783   guint16 uint16_prop;
784   gint int_prop;
785   guint uint_prop;
786   gint64 int64_prop;
787   guint64 uint64_prop;
788   gdouble double_prop;
789   gchar *string_prop;
790   gchar *no_read_prop;
791   gchar *no_write_prop;
792   gchar **strv_prop;
793   guint enum_prop;
794   guint flags_prop;
795 } TestObject;
796
797 typedef struct
798 {
799   GObjectClass parent_class;
800 } TestObjectClass;
801
802 static GType test_object_get_type (void);
803 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
804
805 static void
806 test_object_init (TestObject *object)
807 {
808 }
809
810 static void
811 test_object_finalize (GObject *object)
812 {
813   TestObject *testo = (TestObject*)object;
814   g_strfreev (testo->strv_prop);
815   g_free (testo->string_prop);
816   G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
817 }
818
819 static void
820 test_object_get_property (GObject    *object,
821                           guint       prop_id,
822                           GValue     *value,
823                           GParamSpec *pspec)
824 {
825   TestObject *test_object = (TestObject *)object;
826
827   switch (prop_id)
828     {
829     case PROP_BOOL:
830       g_value_set_boolean (value, test_object->bool_prop);
831       break;
832     case PROP_ANTI_BOOL:
833       g_value_set_boolean (value, test_object->anti_bool_prop);
834       break;
835     case PROP_BYTE:
836       g_value_set_schar (value, test_object->byte_prop);
837       break;
838     case PROP_UINT16:
839       g_value_set_uint (value, test_object->uint16_prop);
840       break;
841     case PROP_INT16:
842       g_value_set_int (value, test_object->int16_prop);
843       break;
844     case PROP_INT:
845       g_value_set_int (value, test_object->int_prop);
846       break;
847     case PROP_UINT:
848       g_value_set_uint (value, test_object->uint_prop);
849       break;
850     case PROP_INT64:
851       g_value_set_int64 (value, test_object->int64_prop);
852       break;
853     case PROP_UINT64:
854       g_value_set_uint64 (value, test_object->uint64_prop);
855       break;
856     case PROP_DOUBLE:
857       g_value_set_double (value, test_object->double_prop);
858       break;
859     case PROP_STRING:
860       g_value_set_string (value, test_object->string_prop);
861       break;
862     case PROP_NO_WRITE:
863       g_value_set_string (value, test_object->no_write_prop);
864       break;
865     case PROP_STRV:
866       g_value_set_boxed (value, test_object->strv_prop);
867       break;
868     case PROP_ENUM:
869       g_value_set_enum (value, test_object->enum_prop);
870       break;
871     case PROP_FLAGS:
872       g_value_set_flags (value, test_object->flags_prop);
873       break;
874     default:
875       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
876       break;
877     }
878 }
879
880 static void
881 test_object_set_property (GObject      *object,
882                           guint         prop_id,
883                           const GValue *value,
884                           GParamSpec   *pspec)
885 {
886   TestObject *test_object = (TestObject *)object;
887
888   switch (prop_id)
889     {
890     case PROP_BOOL:
891       test_object->bool_prop = g_value_get_boolean (value);
892       break;
893     case PROP_ANTI_BOOL:
894       test_object->anti_bool_prop = g_value_get_boolean (value);
895       break;
896     case PROP_BYTE:
897       test_object->byte_prop = g_value_get_schar (value);
898       break;
899     case PROP_INT16:
900       test_object->int16_prop = g_value_get_int (value);
901       break;
902     case PROP_UINT16:
903       test_object->uint16_prop = g_value_get_uint (value);
904       break;
905     case PROP_INT:
906       test_object->int_prop = g_value_get_int (value);
907       break;
908     case PROP_UINT:
909       test_object->uint_prop = g_value_get_uint (value);
910       break;
911     case PROP_INT64:
912       test_object->int64_prop = g_value_get_int64 (value);
913       break;
914     case PROP_UINT64:
915       test_object->uint64_prop = g_value_get_uint64 (value);
916       break;
917     case PROP_DOUBLE:
918       test_object->double_prop = g_value_get_double (value);
919       break;
920     case PROP_STRING:
921       g_free (test_object->string_prop);
922       test_object->string_prop = g_value_dup_string (value);
923       break;
924     case PROP_NO_READ:
925       g_free (test_object->no_read_prop);
926       test_object->no_read_prop = g_value_dup_string (value);
927       break;
928     case PROP_STRV:
929       g_strfreev (test_object->strv_prop);
930       test_object->strv_prop = g_value_dup_boxed (value);
931       break;
932     case PROP_ENUM:
933       test_object->enum_prop = g_value_get_enum (value);
934       break;
935     case PROP_FLAGS:
936       test_object->flags_prop = g_value_get_flags (value);
937       break;
938     default:
939       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
940       break;
941     }
942 }
943
944 static GType
945 test_enum_get_type (void)
946 {
947   static volatile gsize define_type_id = 0;
948
949   if (g_once_init_enter (&define_type_id))
950     {
951       static const GEnumValue values[] = {
952         { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
953         { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
954         { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
955         { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
956         { 0, NULL, NULL }
957       };
958
959       GType type_id = g_enum_register_static ("TestEnum", values);
960       g_once_init_leave (&define_type_id, type_id);
961     }
962
963   return define_type_id;
964 }
965
966 static GType
967 test_flags_get_type (void)
968 {
969   static volatile gsize define_type_id = 0;
970
971   if (g_once_init_enter (&define_type_id))
972     {
973       static const GFlagsValue values[] = {
974         { TEST_FLAGS_NONE, "TEST_FLAGS_NONE", "none" },
975         { TEST_FLAGS_MOURNING, "TEST_FLAGS_MOURNING", "mourning" },
976         { TEST_FLAGS_LAUGHING, "TEST_FLAGS_LAUGHING", "laughing" },
977         { TEST_FLAGS_WALKING, "TEST_FLAGS_WALKING", "walking" },
978         { 0, NULL, NULL }
979       };
980
981       GType type_id = g_flags_register_static ("TestFlags", values);
982       g_once_init_leave (&define_type_id, type_id);
983     }
984
985   return define_type_id;
986 }
987
988 static void
989 test_object_class_init (TestObjectClass *class)
990 {
991   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
992
993   gobject_class->get_property = test_object_get_property;
994   gobject_class->set_property = test_object_set_property;
995   gobject_class->finalize = test_object_finalize;
996
997   g_object_class_install_property (gobject_class, PROP_BOOL,
998     g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
999   g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
1000     g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
1001   g_object_class_install_property (gobject_class, PROP_BYTE,
1002     g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
1003   g_object_class_install_property (gobject_class, PROP_INT16,
1004     g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
1005   g_object_class_install_property (gobject_class, PROP_UINT16,
1006     g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
1007   g_object_class_install_property (gobject_class, PROP_INT,
1008     g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
1009   g_object_class_install_property (gobject_class, PROP_UINT,
1010     g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
1011   g_object_class_install_property (gobject_class, PROP_INT64,
1012     g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
1013   g_object_class_install_property (gobject_class, PROP_UINT64,
1014     g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
1015   g_object_class_install_property (gobject_class, PROP_DOUBLE,
1016     g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
1017   g_object_class_install_property (gobject_class, PROP_STRING,
1018     g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
1019   g_object_class_install_property (gobject_class, PROP_NO_WRITE,
1020     g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
1021   g_object_class_install_property (gobject_class, PROP_NO_READ,
1022     g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
1023   g_object_class_install_property (gobject_class, PROP_STRV,
1024     g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
1025   g_object_class_install_property (gobject_class, PROP_ENUM,
1026     g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
1027   g_object_class_install_property (gobject_class, PROP_FLAGS,
1028     g_param_spec_flags ("flags", "", "", test_flags_get_type (), TEST_FLAGS_NONE, G_PARAM_READWRITE));
1029 }
1030
1031 static TestObject *
1032 test_object_new (void)
1033 {
1034   return (TestObject*)g_object_new (test_object_get_type (), NULL);
1035 }
1036
1037 /* Test basic binding functionality for simple types.
1038  * Verify that with bidirectional bindings, changes on either side
1039  * are notified on the other end.
1040  */
1041 static void
1042 test_simple_binding (void)
1043 {
1044   TestObject *obj;
1045   GSettings *settings;
1046   gboolean b;
1047   gchar y;
1048   gint i;
1049   guint u;
1050   gint16 n;
1051   guint16 q;
1052   gint n2;
1053   guint q2;
1054   gint64 i64;
1055   guint64 u64;
1056   gdouble d;
1057   gchar *s;
1058   GVariant *value;
1059   gchar **strv;
1060
1061   settings = g_settings_new ("org.gtk.test.binding");
1062   obj = test_object_new ();
1063
1064   g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
1065   g_object_set (obj, "bool", TRUE, NULL);
1066   g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1067
1068   g_settings_set_boolean (settings, "bool", FALSE);
1069   b = TRUE;
1070   g_object_get (obj, "bool", &b, NULL);
1071   g_assert_cmpint (b, ==, FALSE);
1072
1073   g_settings_bind (settings, "anti-bool", obj, "anti-bool",
1074                    G_SETTINGS_BIND_INVERT_BOOLEAN);
1075   g_object_set (obj, "anti-bool", FALSE, NULL);
1076   g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
1077
1078   g_settings_set_boolean (settings, "anti-bool", FALSE);
1079   b = FALSE;
1080   g_object_get (obj, "anti-bool", &b, NULL);
1081   g_assert_cmpint (b, ==, TRUE);
1082
1083   g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
1084
1085   g_object_set (obj, "byte", 123, NULL);
1086   y = 'c';
1087   g_settings_get (settings, "byte", "y", &y);
1088   g_assert_cmpint (y, ==, 123);
1089
1090   g_settings_set (settings, "byte", "y", 54);
1091   y = 'c';
1092   g_object_get (obj, "byte", &y, NULL);
1093   g_assert_cmpint (y, ==, 54);
1094
1095   g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
1096
1097   g_object_set (obj, "int16", 1234, NULL);
1098   n = 4321;
1099   g_settings_get (settings, "int16", "n", &n);
1100   g_assert_cmpint (n, ==, 1234);
1101
1102   g_settings_set (settings, "int16", "n", 4321);
1103   n2 = 1111;
1104   g_object_get (obj, "int16", &n2, NULL);
1105   g_assert_cmpint (n2, ==, 4321);
1106
1107   g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
1108
1109   g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1110   q = 1111;
1111   g_settings_get (settings, "uint16", "q", &q);
1112   g_assert_cmpuint (q, ==, G_MAXUINT16);
1113
1114   g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1115   q2 = 1111;
1116   g_object_get (obj, "uint16", &q2, NULL);
1117   g_assert_cmpuint (q2, ==, (guint16) G_MAXINT16);
1118
1119   g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1120
1121   g_object_set (obj, "int", 12345, NULL);
1122   g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1123
1124   g_settings_set_int (settings, "int", 54321);
1125   i = 1111;
1126   g_object_get (obj, "int", &i, NULL);
1127   g_assert_cmpint (i, ==, 54321);
1128
1129   g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1130
1131   g_object_set (obj, "uint", 12345, NULL);
1132   g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);
1133
1134   g_settings_set_uint (settings, "uint", 54321);
1135   u = 1111;
1136   g_object_get (obj, "uint", &u, NULL);
1137   g_assert_cmpuint (u, ==, 54321);
1138
1139   g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1140
1141   g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1142   i64 = 1111;
1143   g_settings_get (settings, "int64", "x", &i64);
1144   g_assert_cmpint (i64, ==, G_MAXINT64);
1145
1146   g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1147   i64 = 1111;
1148   g_object_get (obj, "int64", &i64, NULL);
1149   g_assert_cmpint (i64, ==, G_MININT64);
1150
1151   g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1152
1153   g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1154   u64 = 1111;
1155   g_settings_get (settings, "uint64", "t", &u64);
1156   g_assert_cmpuint (u64, ==, G_MAXUINT64);
1157
1158   g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1159   u64 = 1111;
1160   g_object_get (obj, "uint64", &u64, NULL);
1161   g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1162
1163   g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1164
1165   g_object_set (obj, "string", "bu ba", NULL);
1166   s = g_settings_get_string (settings, "string");
1167   g_assert_cmpstr (s, ==, "bu ba");
1168   g_free (s);
1169
1170   g_settings_set_string (settings, "string", "bla bla");
1171   g_object_get (obj, "string", &s, NULL);
1172   g_assert_cmpstr (s, ==, "bla bla");
1173   g_free (s);
1174
1175   g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1176
1177   g_object_set (obj, "string", "non-unicode:\315", NULL);
1178   value = g_settings_get_value (settings, "chararray");
1179   g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1180   g_variant_unref (value);
1181
1182   g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1183
1184   g_object_set (obj, "double", G_MAXFLOAT, NULL);
1185   g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1186
1187   g_settings_set_double (settings, "double", G_MINFLOAT);
1188   d = 1.0;
1189   g_object_get (obj, "double", &d, NULL);
1190   g_assert_cmpfloat (d, ==, G_MINFLOAT);
1191
1192   g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1193   g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1194
1195   g_settings_set_double (settings, "double", -G_MINDOUBLE);
1196   d = 1.0;
1197   g_object_get (obj, "double", &d, NULL);
1198   g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1199
1200   strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1201   g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1202   g_object_set (obj, "strv", strv, NULL);
1203   g_strfreev (strv);
1204   strv = g_settings_get_strv (settings, "strv");
1205   s = g_strjoinv (",", strv);
1206   g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1207   g_strfreev (strv);
1208   g_free (s);
1209   strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1210   g_settings_set_strv (settings, "strv", (const gchar **) strv);
1211   g_strfreev (strv);
1212   g_object_get (obj, "strv", &strv, NULL);
1213   s = g_strjoinv (",", strv);
1214   g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1215   g_strfreev (strv);
1216   g_free (s);
1217
1218   g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1219   g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1220   s = g_settings_get_string (settings, "enum");
1221   g_assert_cmpstr (s, ==, "baz");
1222   g_free (s);
1223   g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1224
1225   g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1226   i = 230;
1227   g_object_get (obj, "enum", &i, NULL);
1228   g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1229
1230   g_settings_set_string (settings, "enum", "baz");
1231   i = 230;
1232   g_object_get (obj, "enum", &i, NULL);
1233   g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1234
1235   g_settings_bind (settings, "flags", obj, "flags", G_SETTINGS_BIND_DEFAULT);
1236   g_object_set (obj, "flags", TEST_FLAGS_MOURNING, NULL);
1237   strv = g_settings_get_strv (settings, "flags");
1238   g_assert_cmpint (g_strv_length (strv), ==, 1);
1239   g_assert_cmpstr (strv[0], ==, "mourning");
1240   g_strfreev (strv);
1241
1242   g_assert_cmpint (g_settings_get_flags (settings, "flags"), ==, TEST_FLAGS_MOURNING);
1243
1244   g_settings_set_flags (settings, "flags", TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1245   i = 230;
1246   g_object_get (obj, "flags", &i, NULL);
1247   g_assert_cmpint (i, ==, TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1248
1249   g_object_unref (obj);
1250   g_object_unref (settings);
1251 }
1252
1253 static void
1254 test_unbind (void)
1255 {
1256   TestObject *obj;
1257   GSettings *settings;
1258
1259   settings = g_settings_new ("org.gtk.test.binding");
1260   obj = test_object_new ();
1261
1262   g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1263
1264   g_object_set (obj, "int", 12345, NULL);
1265   g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1266
1267   g_settings_unbind (obj, "int");
1268
1269   g_object_set (obj, "int", 54321, NULL);
1270   g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1271
1272   g_object_unref (obj);
1273   g_object_unref (settings);
1274 }
1275
1276 static void
1277 test_bind_writable (void)
1278 {
1279   TestObject *obj;
1280   GSettings *settings;
1281   gboolean b;
1282
1283   settings = g_settings_new ("org.gtk.test.binding");
1284   obj = test_object_new ();
1285
1286   g_object_set (obj, "bool", FALSE, NULL);
1287
1288   g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1289
1290   g_object_get (obj, "bool", &b, NULL);
1291   g_assert (b);
1292
1293   g_settings_unbind (obj, "bool");
1294
1295   g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1296
1297   g_object_get (obj, "bool", &b, NULL);
1298   g_assert (!b);
1299
1300   g_object_unref (obj);
1301   g_object_unref (settings);
1302 }
1303
1304 /* Test one-way bindings.
1305  * Verify that changes on one side show up on the other,
1306  * but not vice versa
1307  */
1308 static void
1309 test_directional_binding (void)
1310 {
1311   TestObject *obj;
1312   GSettings *settings;
1313   gboolean b;
1314   gint i;
1315
1316   settings = g_settings_new ("org.gtk.test.binding");
1317   obj = test_object_new ();
1318
1319   g_object_set (obj, "bool", FALSE, NULL);
1320   g_settings_set_boolean (settings, "bool", FALSE);
1321
1322   g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1323
1324   g_settings_set_boolean (settings, "bool", TRUE);
1325   g_object_get (obj, "bool", &b, NULL);
1326   g_assert_cmpint (b, ==, TRUE);
1327
1328   g_object_set (obj, "bool", FALSE, NULL);
1329   g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1330
1331   g_object_set (obj, "int", 20, NULL);
1332   g_settings_set_int (settings, "int", 20);
1333
1334   g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1335
1336   g_object_set (obj, "int", 32, NULL);
1337   g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1338
1339   g_settings_set_int (settings, "int", 20);
1340   g_object_get (obj, "int", &i, NULL);
1341   g_assert_cmpint (i, ==, 32);
1342
1343   g_object_unref (obj);
1344   g_object_unref (settings);
1345 }
1346
1347 /* Test that type mismatch is caught when creating a binding */
1348 static void
1349 test_typesafe_binding (void)
1350 {
1351   if (!g_test_undefined ())
1352     return;
1353
1354   if (g_test_subprocess ())
1355     {
1356       TestObject *obj;
1357       GSettings *settings;
1358
1359       settings = g_settings_new ("org.gtk.test.binding");
1360       obj = test_object_new ();
1361
1362       g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1363
1364       g_object_unref (obj);
1365       g_object_unref (settings);
1366       return;
1367     }
1368   g_test_trap_subprocess (NULL, 0, 0);
1369   g_test_trap_assert_failed ();
1370   g_test_trap_assert_stderr ("*not compatible*");
1371 }
1372
1373 static gboolean
1374 string_to_bool (GValue   *value,
1375                 GVariant *variant,
1376                 gpointer  user_data)
1377 {
1378   const gchar *s;
1379
1380   s = g_variant_get_string (variant, NULL);
1381   g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1382
1383   return TRUE;
1384 }
1385
1386 static GVariant *
1387 bool_to_string (const GValue       *value,
1388                 const GVariantType *expected_type,
1389                 gpointer            user_data)
1390 {
1391   if (g_value_get_boolean (value))
1392     return g_variant_new_string ("true");
1393   else
1394     return g_variant_new_string ("false");
1395 }
1396
1397 /* Test custom bindings.
1398  * Translate strings to booleans and back
1399  */
1400 static void
1401 test_custom_binding (void)
1402 {
1403   TestObject *obj;
1404   GSettings *settings;
1405   gchar *s;
1406   gboolean b;
1407
1408   settings = g_settings_new ("org.gtk.test.binding");
1409   obj = test_object_new ();
1410
1411   g_settings_set_string (settings, "string", "true");
1412
1413   g_settings_bind_with_mapping (settings, "string",
1414                                 obj, "bool",
1415                                 G_SETTINGS_BIND_DEFAULT,
1416                                 string_to_bool,
1417                                 bool_to_string,
1418                                 NULL, NULL);
1419
1420   g_settings_set_string (settings, "string", "false");
1421   g_object_get (obj, "bool", &b, NULL);
1422   g_assert_cmpint (b, ==, FALSE);
1423
1424   g_settings_set_string (settings, "string", "not true");
1425   g_object_get (obj, "bool", &b, NULL);
1426   g_assert_cmpint (b, ==, FALSE);
1427
1428   g_object_set (obj, "bool", TRUE, NULL);
1429   s = g_settings_get_string (settings, "string");
1430   g_assert_cmpstr (s, ==, "true");
1431   g_free (s);
1432
1433   g_object_unref (obj);
1434   g_object_unref (settings);
1435 }
1436
1437 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1438  * initial settings value is transported to the object
1439  * side, but later settings changes do not affect the
1440  * object
1441  */
1442 static void
1443 test_no_change_binding (void)
1444 {
1445   TestObject *obj;
1446   GSettings *settings;
1447   gboolean b;
1448
1449   settings = g_settings_new ("org.gtk.test.binding");
1450   obj = test_object_new ();
1451
1452   g_object_set (obj, "bool", TRUE, NULL);
1453   g_settings_set_boolean (settings, "bool", FALSE);
1454
1455   g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1456
1457   g_object_get (obj, "bool", &b, NULL);
1458   g_assert_cmpint (b, ==, FALSE);
1459
1460   g_settings_set_boolean (settings, "bool", TRUE);
1461   g_object_get (obj, "bool", &b, NULL);
1462   g_assert_cmpint (b, ==, FALSE);
1463
1464   g_settings_set_boolean (settings, "bool", FALSE);
1465   g_object_set (obj, "bool", TRUE, NULL);
1466   b = g_settings_get_boolean (settings, "bool");
1467   g_assert_cmpint (b, ==, TRUE);
1468
1469   g_object_unref (obj);
1470   g_object_unref (settings);
1471 }
1472
1473 /* Test that binding a non-readable property only
1474  * works in 'GET' mode.
1475  */
1476 static void
1477 test_no_read_binding_fail (void)
1478 {
1479   TestObject *obj;
1480   GSettings *settings;
1481
1482   settings = g_settings_new ("org.gtk.test.binding");
1483   obj = test_object_new ();
1484
1485   g_settings_bind (settings, "string", obj, "no-read", 0);
1486 }
1487
1488 static void
1489 test_no_read_binding_pass (void)
1490 {
1491   TestObject *obj;
1492   GSettings *settings;
1493
1494   settings = g_settings_new ("org.gtk.test.binding");
1495   obj = test_object_new ();
1496
1497   g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1498
1499   exit (0);
1500 }
1501
1502 static void
1503 test_no_read_binding (void)
1504 {
1505   if (g_test_undefined ())
1506     {
1507       g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/fail", 0, 0);
1508       g_test_trap_assert_failed ();
1509       g_test_trap_assert_stderr ("*property*is not readable*");
1510     }
1511
1512   g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/pass", 0, 0);
1513   g_test_trap_assert_passed ();
1514 }
1515
1516 /* Test that binding a non-writable property only
1517  * works in 'SET' mode.
1518  */
1519 static void
1520 test_no_write_binding_fail (void)
1521 {
1522   TestObject *obj;
1523   GSettings *settings;
1524
1525   settings = g_settings_new ("org.gtk.test.binding");
1526   obj = test_object_new ();
1527
1528   g_settings_bind (settings, "string", obj, "no-write", 0);
1529 }
1530
1531 static void
1532 test_no_write_binding_pass (void)
1533 {
1534   TestObject *obj;
1535   GSettings *settings;
1536
1537   settings = g_settings_new ("org.gtk.test.binding");
1538   obj = test_object_new ();
1539
1540   g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1541
1542   exit (0);
1543 }
1544
1545 static void
1546 test_no_write_binding (void)
1547 {
1548   if (g_test_undefined ())
1549     {
1550       g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/fail", 0, 0);
1551       g_test_trap_assert_failed ();
1552       g_test_trap_assert_stderr ("*property*is not writable*");
1553     }
1554
1555   g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/pass", 0, 0);
1556   g_test_trap_assert_passed ();
1557 }
1558
1559 static void
1560 key_changed_cb (GSettings *settings, const gchar *key, gpointer data)
1561 {
1562   gboolean *b = data;
1563   (*b) = TRUE;
1564 }
1565
1566 /*
1567  * Test that using a keyfile works
1568  */
1569 static void
1570 test_keyfile (void)
1571 {
1572   GSettingsBackend *kf_backend;
1573   GSettings *settings;
1574   GKeyFile *keyfile;
1575   gchar *str;
1576   gboolean writable;
1577   GError *error = NULL;
1578   gchar *data;
1579   gsize len;
1580   gboolean called = FALSE;
1581
1582   g_remove ("gsettings.store");
1583
1584   kf_backend = g_keyfile_settings_backend_new ("gsettings.store", "/", "root");
1585   settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1586   g_object_unref (kf_backend);
1587
1588   g_settings_reset (settings, "greeting");
1589   str = g_settings_get_string (settings, "greeting");
1590   g_assert_cmpstr (str, ==, "Hello, earthlings");
1591   g_free (str);
1592
1593   writable = g_settings_is_writable (settings, "greeting");
1594   g_assert (writable);
1595   g_settings_set (settings, "greeting", "s", "see if this works");
1596
1597   str = g_settings_get_string (settings, "greeting");
1598   g_assert_cmpstr (str, ==, "see if this works");
1599   g_free (str);
1600
1601   g_settings_delay (settings);
1602   g_settings_set (settings, "farewell", "s", "cheerio");
1603   g_settings_apply (settings);
1604
1605   keyfile = g_key_file_new ();
1606   g_assert (g_key_file_load_from_file (keyfile, "gsettings.store", 0, NULL));
1607
1608   str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1609   g_assert_cmpstr (str, ==, "'see if this works'");
1610   g_free (str);
1611
1612   str = g_key_file_get_string (keyfile, "tests", "farewell", NULL);
1613   g_assert_cmpstr (str, ==, "'cheerio'");
1614   g_free (str);
1615
1616   g_signal_connect (settings, "changed::greeting", G_CALLBACK (key_changed_cb), &called);
1617
1618   g_key_file_set_string (keyfile, "tests", "greeting", "howdy");
1619   data = g_key_file_to_data (keyfile, &len, NULL);
1620   g_file_set_contents ("gsettings.store", data, len, &error);
1621   g_assert_no_error (error);
1622   while (!called)
1623     g_main_context_iteration (NULL, FALSE);
1624
1625   g_key_file_free (keyfile);
1626   g_free (data);
1627
1628   g_object_unref (settings);
1629 }
1630
1631 /* Test that getting child schemas works
1632  */
1633 static void
1634 test_child_schema (void)
1635 {
1636   GSettings *settings;
1637   GSettings *child;
1638   guint8 byte;
1639
1640   /* first establish some known conditions */
1641   settings = g_settings_new ("org.gtk.test.basic-types");
1642   g_settings_set (settings, "test-byte", "y", 36);
1643
1644   g_settings_get (settings, "test-byte", "y", &byte);
1645   g_assert_cmpint (byte, ==, 36);
1646
1647   g_object_unref (settings);
1648
1649   settings = g_settings_new ("org.gtk.test");
1650   child = g_settings_get_child (settings, "basic-types");
1651   g_assert (child != NULL);
1652
1653   g_settings_get (child, "test-byte", "y", &byte);
1654   g_assert_cmpint (byte, ==, 36);
1655
1656   g_object_unref (child);
1657   g_object_unref (settings);
1658 }
1659
1660 #include "../strinfo.c"
1661
1662 static void
1663 test_strinfo (void)
1664 {
1665   /*  "foo" has a value of 1
1666    *  "bar" has a value of 2
1667    *  "baz" is an alias for "bar"
1668    */
1669   gchar array[] =
1670     "\1\0\0\0"      "\xff""foo"     "\0\0\0\xff"    "\2\0\0\0"
1671     "\xff" "bar"    "\0\0\0\xff"    "\3\0\0\0"      "\xfe""baz"
1672     "\0\0\0\xff";
1673   const guint32 *strinfo = (guint32 *) array;
1674   guint length = sizeof array / 4;
1675   guint result;
1676
1677   {
1678     /* build it and compare */
1679     GString *builder;
1680
1681     builder = g_string_new (NULL);
1682     strinfo_builder_append_item (builder, "foo", 1);
1683     strinfo_builder_append_item (builder, "bar", 2);
1684     g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
1685     g_assert_cmpint (builder->len % 4, ==, 0);
1686     g_assert_cmpint (builder->len / 4, ==, length);
1687     g_assert (memcmp (builder->str, strinfo, length * 4) == 0);
1688     g_string_free (builder, TRUE);
1689   }
1690
1691   g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
1692                    ==, NULL);
1693   g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
1694                    ==, NULL);
1695   g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
1696                    ==, "bar");
1697   g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
1698                    ==, NULL);
1699
1700   g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
1701   g_assert_cmpint (result, ==, 1);
1702   g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
1703   g_assert_cmpint (result, ==, 2);
1704   g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
1705   g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
1706
1707   g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
1708   g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
1709   g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
1710   g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
1711
1712   g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
1713   g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
1714   g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
1715   g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
1716 }
1717
1718 static void
1719 test_enums_non_enum_key (void)
1720 {
1721   GSettings *direct;
1722
1723   direct = g_settings_new ("org.gtk.test.enums.direct");
1724   g_settings_get_enum (direct, "test");
1725   g_assert_not_reached ();
1726 }
1727
1728 static void
1729 test_enums_non_enum_value (void)
1730 {
1731   GSettings *settings;
1732
1733   settings = g_settings_new ("org.gtk.test.enums");
1734   g_settings_set_enum (settings, "test", 42);
1735   g_assert_not_reached ();
1736 }
1737
1738 static void
1739 test_enums_range (void)
1740 {
1741   GSettings *settings;
1742
1743   settings = g_settings_new ("org.gtk.test.enums");
1744   g_settings_set_string (settings, "test", "qux");
1745   g_assert_not_reached ();
1746 }
1747
1748 static void
1749 test_enums_non_flags (void)
1750 {
1751   GSettings *settings;
1752
1753   settings = g_settings_new ("org.gtk.test.enums");
1754   g_settings_get_flags (settings, "test");
1755   g_assert_not_reached ();
1756 }
1757
1758 static void
1759 test_enums (void)
1760 {
1761   GSettings *settings, *direct;
1762   gchar *str;
1763
1764   settings = g_settings_new ("org.gtk.test.enums");
1765   direct = g_settings_new ("org.gtk.test.enums.direct");
1766
1767   if (g_test_undefined () && !backend_set)
1768     {
1769       g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-key", 0, 0);
1770       g_test_trap_assert_failed ();
1771       g_test_trap_assert_stderr ("*not associated with an enum*");
1772
1773       g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-value", 0, 0);
1774       g_test_trap_assert_failed ();
1775       g_test_trap_assert_stderr ("*invalid enum value 42*");
1776
1777       g_test_trap_subprocess ("/gsettings/enums/subprocess/range", 0, 0);
1778       g_test_trap_assert_failed ();
1779       g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1780
1781       g_test_trap_subprocess ("/gsettings/enums/subprocess/non-flags", 0, 0);
1782       g_test_trap_assert_failed ();
1783       g_test_trap_assert_stderr ("*not associated with a flags*");
1784     }
1785
1786   str = g_settings_get_string (settings, "test");
1787   g_assert_cmpstr (str, ==, "bar");
1788   g_free (str);
1789
1790   g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
1791
1792   str = g_settings_get_string (settings, "test");
1793   g_assert_cmpstr (str, ==, "foo");
1794   g_free (str);
1795
1796   g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
1797
1798   g_settings_set_string (direct, "test", "qux");
1799
1800   str = g_settings_get_string (direct, "test");
1801   g_assert_cmpstr (str, ==, "qux");
1802   g_free (str);
1803
1804   str = g_settings_get_string (settings, "test");
1805   g_assert_cmpstr (str, ==, "quux");
1806   g_free (str);
1807
1808   g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
1809 }
1810
1811 static void
1812 test_flags_non_flags_key (void)
1813 {
1814   GSettings *direct;
1815
1816   direct = g_settings_new ("org.gtk.test.enums.direct");
1817   g_settings_get_flags (direct, "test");
1818   g_assert_not_reached ();
1819 }
1820
1821 static void
1822 test_flags_non_flags_value (void)
1823 {
1824   GSettings *settings;
1825
1826   settings = g_settings_new ("org.gtk.test.enums");
1827   g_settings_set_flags (settings, "f-test", 0x42);
1828   g_assert_not_reached ();
1829 }
1830
1831 static void
1832 test_flags_range (void)
1833 {
1834   GSettings *settings;
1835
1836   settings = g_settings_new ("org.gtk.test.enums");
1837   g_settings_set_strv (settings, "f-test",
1838                        (const gchar **) g_strsplit ("rock", ",", 0));
1839   g_assert_not_reached ();
1840 }
1841
1842 static void
1843 test_flags_non_enum (void)
1844 {
1845   GSettings *settings;
1846
1847   settings = g_settings_new ("org.gtk.test.enums");
1848   g_settings_get_enum (settings, "f-test");
1849   g_assert_not_reached ();
1850 }
1851
1852 static void
1853 test_flags (void)
1854 {
1855   GSettings *settings, *direct;
1856   gchar **strv;
1857   gchar *str;
1858
1859   settings = g_settings_new ("org.gtk.test.enums");
1860   direct = g_settings_new ("org.gtk.test.enums.direct");
1861
1862   if (g_test_undefined () && !backend_set)
1863     {
1864       g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-key", 0, 0);
1865       g_test_trap_assert_failed ();
1866       g_test_trap_assert_stderr ("*not associated with a flags*");
1867
1868       g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-value", 0, 0);
1869       g_test_trap_assert_failed ();
1870       g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
1871
1872       g_test_trap_subprocess ("/gsettings/flags/subprocess/range", 0, 0);
1873       g_test_trap_assert_failed ();
1874       g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1875
1876       g_test_trap_subprocess ("/gsettings/flags/subprocess/non-enum", 0, 0);
1877       g_test_trap_assert_failed ();
1878       g_test_trap_assert_stderr ("*not associated with an enum*");
1879     }
1880
1881   strv = g_settings_get_strv (settings, "f-test");
1882   str = g_strjoinv (",", strv);
1883   g_assert_cmpstr (str, ==, "");
1884   g_strfreev (strv);
1885   g_free (str);
1886
1887   g_settings_set_flags (settings, "f-test",
1888                         TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1889
1890   strv = g_settings_get_strv (settings, "f-test");
1891   str = g_strjoinv (",", strv);
1892   g_assert_cmpstr (str, ==, "talking,walking");
1893   g_strfreev (strv);
1894   g_free (str);
1895
1896   g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1897                    TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1898
1899   strv = g_strsplit ("speaking,laughing", ",", 0);
1900   g_settings_set_strv (direct, "f-test", (const gchar **) strv);
1901   g_strfreev (strv);
1902
1903   strv = g_settings_get_strv (direct, "f-test");
1904   str = g_strjoinv (",", strv);
1905   g_assert_cmpstr (str, ==, "speaking,laughing");
1906   g_strfreev (strv);
1907   g_free (str);
1908
1909   strv = g_settings_get_strv (settings, "f-test");
1910   str = g_strjoinv (",", strv);
1911   g_assert_cmpstr (str, ==, "talking,laughing");
1912   g_strfreev (strv);
1913   g_free (str);
1914
1915   g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1916                    TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
1917 }
1918
1919 static void
1920 test_range_high (void)
1921 {
1922   GSettings *settings;
1923
1924   settings = g_settings_new ("org.gtk.test.range");
1925   g_settings_set_int (settings, "val", 45);
1926   g_assert_not_reached ();
1927 }
1928
1929 static void
1930 test_range_low (void)
1931 {
1932   GSettings *settings;
1933
1934   settings = g_settings_new ("org.gtk.test.range");
1935   g_settings_set_int (settings, "val", 1);
1936   g_assert_not_reached ();
1937 }
1938
1939 static void
1940 test_range (void)
1941 {
1942   GSettings *settings, *direct;
1943   GVariant *value;
1944
1945   settings = g_settings_new ("org.gtk.test.range");
1946   direct = g_settings_new ("org.gtk.test.range.direct");
1947
1948   if (g_test_undefined () && !backend_set)
1949     {
1950       g_test_trap_subprocess ("/gsettings/range/subprocess/high", 0, 0);
1951       g_test_trap_assert_failed ();
1952       g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1953
1954       g_test_trap_subprocess ("/gsettings/range/subprocess/low", 0, 0);
1955       g_test_trap_assert_failed ();
1956       g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1957     }
1958
1959   g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1960   g_settings_set_int (direct, "val", 22);
1961   g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
1962   g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
1963   g_settings_set_int (direct, "val", 45);
1964   g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
1965   g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1966   g_settings_set_int (direct, "val", 1);
1967   g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
1968   g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1969
1970   value = g_variant_new_int32 (1);
1971   g_assert (!g_settings_range_check (settings, "val", value));
1972   g_variant_unref (value);
1973   value = g_variant_new_int32 (33);
1974   g_assert (g_settings_range_check (settings, "val", value));
1975   g_variant_unref (value);
1976   value = g_variant_new_int32 (45);
1977   g_assert (!g_settings_range_check (settings, "val", value));
1978   g_variant_unref (value);
1979 }
1980
1981 static gboolean
1982 strv_has_string (gchar       **haystack,
1983                  const gchar  *needle)
1984 {
1985   guint n;
1986
1987   for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
1988     {
1989       if (g_strcmp0 (haystack[n], needle) == 0)
1990         return TRUE;
1991     }
1992   return FALSE;
1993 }
1994
1995 static gboolean
1996 strv_set_equal (gchar **strv, ...)
1997 {
1998   gint count;
1999   va_list list;
2000   const gchar *str;
2001   gboolean res;
2002
2003   res = TRUE;
2004   count = 0;
2005   va_start (list, strv);
2006   while (1)
2007     {
2008       str = va_arg (list, const gchar *);
2009       if (str == NULL)
2010         break;
2011       if (!strv_has_string (strv, str))
2012         {
2013           res = FALSE;
2014           break;
2015         }
2016       count++;
2017     }
2018   va_end (list);
2019
2020   if (res)
2021     res = g_strv_length ((gchar**)strv) == count;
2022
2023   return res;
2024 }
2025
2026 static void
2027 test_list_items (void)
2028 {
2029   GSettings *settings;
2030   gchar **children;
2031   gchar **keys;
2032
2033   settings = g_settings_new ("org.gtk.test");
2034   children = g_settings_list_children (settings);
2035   keys = g_settings_list_keys (settings);
2036
2037   g_assert (strv_set_equal (children, "basic-types", "complex-types", "localized", NULL));
2038   g_assert (strv_set_equal (keys, "greeting", "farewell", NULL));
2039
2040   g_strfreev (children);
2041   g_strfreev (keys);
2042
2043   g_object_unref (settings);
2044 }
2045
2046 static void
2047 test_list_schemas (void)
2048 {
2049   const gchar * const *schemas;
2050   const gchar * const *relocs;
2051
2052   relocs = g_settings_list_relocatable_schemas ();
2053   schemas = g_settings_list_schemas ();
2054
2055   g_assert (strv_set_equal ((gchar **)relocs,
2056                             "org.gtk.test.no-path",
2057                             "org.gtk.test.extends.base",
2058                             "org.gtk.test.extends.extended",
2059                             NULL));
2060
2061   g_assert (strv_set_equal ((gchar **)schemas,
2062                             "org.gtk.test",
2063                             "org.gtk.test.basic-types",
2064                             "org.gtk.test.complex-types",
2065                             "org.gtk.test.localized",
2066                             "org.gtk.test.binding",
2067                             "org.gtk.test.enums",
2068                             "org.gtk.test.enums.direct",
2069                             "org.gtk.test.range",
2070                             "org.gtk.test.range.direct",
2071                             "org.gtk.test.mapped",
2072                             "org.gtk.test.descriptions",
2073                             NULL));
2074 }
2075
2076 static gboolean
2077 map_func (GVariant *value,
2078           gpointer *result,
2079           gpointer  user_data)
2080 {
2081   gint *state = user_data;
2082   gint v;
2083
2084   if (value)
2085     v = g_variant_get_int32 (value);
2086   else
2087     v = -1;
2088
2089   if (*state == 0)
2090     {
2091       g_assert_cmpint (v, ==, 1);
2092       (*state)++;
2093       return FALSE;
2094     }
2095   else if (*state == 1)
2096     {
2097       g_assert_cmpint (v, ==, 0);
2098       (*state)++;
2099       return FALSE;
2100     }
2101   else
2102     {
2103       g_assert (value == NULL);
2104       *result = g_variant_new_int32 (5);
2105       return TRUE;
2106     }
2107 }
2108
2109 static void
2110 test_get_mapped (void)
2111 {
2112   GSettings *settings;
2113   gint state;
2114   gpointer p;
2115   gint val;
2116
2117   settings = g_settings_new ("org.gtk.test.mapped");
2118   g_settings_set_int (settings, "val", 1);
2119
2120   state = 0;
2121   p = g_settings_get_mapped (settings, "val", map_func, &state);
2122   val = g_variant_get_int32 ((GVariant*)p);
2123   g_assert_cmpint (val, ==, 5);
2124
2125   g_variant_unref (p);
2126   g_object_unref (settings);
2127 }
2128
2129 static void
2130 test_get_range (void)
2131 {
2132   GSettings *settings;
2133   GVariant *range;
2134
2135   settings = g_settings_new ("org.gtk.test.range");
2136   range = g_settings_get_range (settings, "val");
2137   check_and_free (range, "('range', <(2, 44)>)");
2138   g_object_unref (settings);
2139
2140   settings = g_settings_new ("org.gtk.test.enums");
2141   range = g_settings_get_range (settings, "test");
2142   check_and_free (range, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
2143   g_object_unref (settings);
2144
2145   settings = g_settings_new ("org.gtk.test.enums");
2146   range = g_settings_get_range (settings, "f-test");
2147   check_and_free (range, "('flags', "
2148                   "<['mourning', 'laughing', 'talking', 'walking']>)");
2149   g_object_unref (settings);
2150
2151   settings = g_settings_new ("org.gtk.test");
2152   range = g_settings_get_range (settings, "greeting");
2153   check_and_free (range, "('type', <@as []>)");
2154   g_object_unref (settings);
2155 }
2156
2157 static void
2158 test_schema_source (void)
2159 {
2160   GSettingsSchemaSource *parent;
2161   GSettingsSchemaSource *source;
2162   GSettingsBackend *backend;
2163   GSettingsSchema *schema;
2164   GError *error = NULL;
2165   GSettings *settings;
2166   gboolean enabled;
2167
2168   backend = g_settings_backend_get_default ();
2169
2170   /* make sure it fails properly */
2171   parent = g_settings_schema_source_get_default ();
2172   source = g_settings_schema_source_new_from_directory ("/path/that/does/not/exist", parent,  TRUE, &error);
2173   g_assert (source == NULL);
2174   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
2175   g_clear_error (&error);
2176
2177   /* create a source with the parent */
2178   source = g_settings_schema_source_new_from_directory ("schema-source", parent, TRUE, &error);
2179   g_assert_no_error (error);
2180   g_assert (source != NULL);
2181
2182   /* check recursive lookups are working */
2183   schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2184   g_assert (schema != NULL);
2185   g_settings_schema_unref (schema);
2186
2187   /* check recursive lookups for non-existent schemas */
2188   schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", TRUE);
2189   g_assert (schema == NULL);
2190
2191   /* check non-recursive for schema that only exists in lower layers */
2192   schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2193   g_assert (schema == NULL);
2194
2195   /* check non-recursive lookup for non-existent */
2196   schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", FALSE);
2197   g_assert (schema == NULL);
2198
2199   /* check non-recursive for schema that exists in toplevel */
2200   schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2201   g_assert (schema != NULL);
2202   g_settings_schema_unref (schema);
2203
2204   /* check recursive for schema that exists in toplevel */
2205   schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2206   g_assert (schema != NULL);
2207
2208   /* try to use it for something */
2209   settings = g_settings_new_full (schema, backend, g_settings_schema_get_path (schema));
2210   g_settings_schema_unref (schema);
2211   enabled = FALSE;
2212   g_settings_get (settings, "enabled", "b", &enabled);
2213   g_assert (enabled);
2214   g_object_unref (settings);
2215
2216   g_settings_schema_source_unref (source);
2217
2218   /* try again, but with no parent */
2219   source = g_settings_schema_source_new_from_directory ("schema-source", NULL, FALSE, NULL);
2220   g_assert (source != NULL);
2221
2222   /* should not find it this time, even if recursive... */
2223   schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2224   g_assert (schema == NULL);
2225   schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2226   g_assert (schema == NULL);
2227
2228   /* should still find our own... */
2229   schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2230   g_assert (schema != NULL);
2231   g_settings_schema_unref (schema);
2232   schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2233   g_assert (schema != NULL);
2234   g_settings_schema_unref (schema);
2235
2236   g_settings_schema_source_unref (source);
2237 }
2238
2239 static void
2240 test_actions (void)
2241 {
2242   GAction *string, *toggle;
2243   gboolean c1, c2, c3;
2244   GSettings *settings;
2245   gchar *name;
2246   GVariantType *param_type;
2247   gboolean enabled;
2248   GVariantType *state_type;
2249   GVariant *state;
2250
2251   settings = g_settings_new ("org.gtk.test.basic-types");
2252   string = g_settings_create_action (settings, "test-string");
2253   toggle = g_settings_create_action (settings, "test-boolean");
2254   g_object_unref (settings); /* should be held by the actions */
2255
2256   g_signal_connect (settings, "changed", G_CALLBACK (changed_cb2), &c1);
2257   g_signal_connect (string, "notify::state", G_CALLBACK (changed_cb2), &c2);
2258   g_signal_connect (toggle, "notify::state", G_CALLBACK (changed_cb2), &c3);
2259
2260   c1 = c2 = c3 = FALSE;
2261   g_settings_set_string (settings, "test-string", "hello world");
2262   check_and_free (g_action_get_state (string), "'hello world'");
2263   g_assert (c1 && c2 && !c3);
2264   c1 = c2 = c3 = FALSE;
2265
2266   g_action_activate (string, g_variant_new_string ("hihi"));
2267   check_and_free (g_settings_get_value (settings, "test-string"), "'hihi'");
2268   g_assert (c1 && c2 && !c3);
2269   c1 = c2 = c3 = FALSE;
2270
2271   g_action_change_state (string, g_variant_new_string ("kthxbye"));
2272   check_and_free (g_settings_get_value (settings, "test-string"), "'kthxbye'");
2273   g_assert (c1 && c2 && !c3);
2274   c1 = c2 = c3 = FALSE;
2275
2276   g_action_change_state (toggle, g_variant_new_boolean (TRUE));
2277   g_assert (g_settings_get_boolean (settings, "test-boolean"));
2278   g_assert (c1 && !c2 && c3);
2279   c1 = c2 = c3 = FALSE;
2280
2281   g_action_activate (toggle, NULL);
2282   g_assert (!g_settings_get_boolean (settings, "test-boolean"));
2283   g_assert (c1 && !c2 && c3);
2284
2285   g_object_get (string,
2286                 "name", &name,
2287                 "parameter-type", &param_type,
2288                 "enabled", &enabled,
2289                 "state-type", &state_type,
2290                 "state", &state,
2291                 NULL);
2292
2293   g_assert_cmpstr (name, ==, "test-string");
2294   g_assert (g_variant_type_equal (param_type, G_VARIANT_TYPE_STRING));
2295   g_assert (enabled);
2296   g_assert (g_variant_type_equal (state_type, G_VARIANT_TYPE_STRING));
2297   g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "kthxbye");
2298
2299   g_free (name);
2300   g_variant_unref (state);
2301
2302   g_object_unref (string);
2303   g_object_unref (toggle);
2304 }
2305
2306 static void
2307 test_null_backend (void)
2308 {
2309   GSettingsBackend *backend;
2310   GSettings *settings;
2311   gchar *str;
2312   gboolean writable;
2313
2314   backend = g_null_settings_backend_new ();
2315   settings = g_settings_new_with_backend_and_path ("org.gtk.test", backend, "/tests/");
2316
2317   g_object_get (settings, "schema", &str, NULL);
2318   g_assert_cmpstr (str, ==, "org.gtk.test");
2319   g_free (str);
2320
2321   g_settings_get (settings, "greeting", "s", &str);
2322   g_assert_cmpstr (str, ==, "Hello, earthlings");
2323   g_free (str);
2324
2325   g_settings_set (settings, "greeting", "s", "goodbye world");
2326   g_settings_get (settings, "greeting", "s", &str);
2327   g_assert_cmpstr (str, ==, "Hello, earthlings");
2328   g_free (str);
2329
2330   writable = g_settings_is_writable (settings, "greeting");
2331   g_assert (!writable);
2332
2333   g_object_unref (settings);
2334   g_object_unref (backend);
2335 }
2336
2337 static void
2338 test_memory_backend (void)
2339 {
2340   GSettingsBackend *backend;
2341
2342   backend = g_memory_settings_backend_new ();
2343   g_assert (G_IS_SETTINGS_BACKEND (backend));
2344   g_object_unref (backend);
2345 }
2346
2347 static void
2348 test_read_descriptions (void)
2349 {
2350   GSettingsSchema *schema;
2351   GSettingsSchemaKey *key;
2352   GSettings *settings;
2353
2354   settings = g_settings_new ("org.gtk.test");
2355   g_object_get (settings, "settings-schema", &schema, NULL);
2356   key = g_settings_schema_get_key (schema, "greeting");
2357
2358   g_assert_cmpstr (g_settings_schema_key_get_summary (key), ==, "A greeting");
2359   g_assert_cmpstr (g_settings_schema_key_get_description (key), ==, "Greeting of the invading martians");
2360
2361   g_settings_schema_key_unref (key);
2362   g_settings_schema_unref (schema);
2363
2364   g_object_unref (settings);
2365
2366   settings = g_settings_new ("org.gtk.test.descriptions");
2367   g_object_get (settings, "settings-schema", &schema, NULL);
2368   key = g_settings_schema_get_key (schema, "a");
2369
2370   g_assert_cmpstr (g_settings_schema_key_get_summary (key), ==,
2371                    "a paragraph.\n\n"
2372                    "with some whitespace.\n\n"
2373                    "because not everyone has a great editor.\n\n"
2374                    "lots of space is as one.");
2375
2376   g_settings_schema_key_unref (key);
2377   g_settings_schema_unref (schema);
2378
2379   g_object_unref (settings);
2380 }
2381
2382 static void
2383 test_default_value (void)
2384 {
2385   GSettings *settings;
2386   GSettingsSchema *schema;
2387   GSettingsSchemaKey *key;
2388   GVariant *v;
2389   const gchar *str;
2390
2391   settings = g_settings_new ("org.gtk.test");
2392   g_object_get (settings, "settings-schema", &schema, NULL);
2393   key = g_settings_schema_get_key (schema, "greeting");
2394   g_settings_schema_unref (schema);
2395   g_settings_schema_key_ref (key);
2396
2397   g_assert_cmpstr (g_settings_schema_key_get_value_type (key), ==, G_VARIANT_TYPE_STRING);
2398
2399   v = g_settings_schema_key_get_default_value (key);
2400   str = g_variant_get_string (v, NULL);
2401   g_assert_cmpstr (str, ==, "Hello, earthlings");
2402   g_variant_unref (v);
2403
2404   g_settings_schema_key_unref (key);
2405   g_settings_schema_key_unref (key);
2406
2407   g_settings_set (settings, "greeting", "s", "goodbye world");
2408
2409   v = g_settings_get_user_value (settings, "greeting");
2410   str = g_variant_get_string (v, NULL);
2411   g_assert_cmpstr (str, ==, "goodbye world");
2412   g_variant_unref (v);
2413
2414   v = g_settings_get_default_value (settings, "greeting");
2415   str = g_variant_get_string (v, NULL);
2416   g_assert_cmpstr (str, ==, "Hello, earthlings");
2417   g_variant_unref (v);
2418
2419   g_settings_reset (settings, "greeting");
2420
2421   v = g_settings_get_user_value (settings, "greeting");
2422   g_assert_null (v);
2423
2424   str = g_settings_get_string (settings, "greeting");
2425   g_assert_cmpstr (str, ==, "Hello, earthlings");
2426   g_free (str);
2427
2428   g_object_unref (settings);
2429 }
2430
2431 static void
2432 test_extended_schema (void)
2433 {
2434   GSettings *settings;
2435   gchar **keys;
2436
2437   settings = g_settings_new_with_path ("org.gtk.test.extends.extended", "/test/extendes/");
2438   keys = g_settings_list_keys (settings);
2439   g_assert (strv_set_equal (keys, "int32", "string", "another-int32", NULL));
2440   g_strfreev (keys);
2441   g_object_unref (settings);
2442 }
2443
2444 int
2445 main (int argc, char *argv[])
2446 {
2447   gchar *schema_text;
2448   gchar *enums;
2449   gint result;
2450
2451   setlocale (LC_ALL, "");
2452
2453   g_test_init (&argc, &argv, NULL);
2454
2455   if (!g_test_subprocess ())
2456     {
2457       backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
2458
2459       g_setenv ("XDG_DATA_DIRS", ".", TRUE);
2460       g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
2461
2462       if (!backend_set)
2463         g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
2464
2465       g_remove ("org.gtk.test.enums.xml");
2466       g_assert (g_spawn_command_line_sync ("../../gobject/glib-mkenums "
2467                                            "--template " SRCDIR "/enums.xml.template "
2468                                            SRCDIR "/testenum.h",
2469                                            &enums, NULL, &result, NULL));
2470       g_assert (result == 0);
2471       g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
2472       g_free (enums);
2473
2474       g_assert (g_file_get_contents (SRCDIR "/org.gtk.test.gschema.xml.orig", &schema_text, NULL, NULL));
2475       g_assert (g_file_set_contents ("org.gtk.test.gschema.xml", schema_text, -1, NULL));
2476
2477       g_remove ("gschemas.compiled");
2478       g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=. "
2479                                            "--schema-file=org.gtk.test.enums.xml "
2480                                            "--schema-file=org.gtk.test.gschema.xml",
2481                                            NULL, NULL, &result, NULL));
2482       g_assert (result == 0);
2483
2484       g_remove ("schema-source/gschemas.compiled");
2485       g_mkdir ("schema-source", 0777);
2486       g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=schema-source "
2487                                            "--schema-file=" SRCDIR "/org.gtk.schemasourcecheck.gschema.xml",
2488                                            NULL, NULL, &result, NULL));
2489       g_assert (result == 0);
2490    }
2491
2492   g_test_add_func ("/gsettings/basic", test_basic);
2493
2494   if (!backend_set)
2495     {
2496       g_test_add_func ("/gsettings/no-schema", test_no_schema);
2497       g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
2498       g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
2499       g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
2500       g_test_add_func ("/gsettings/no-path", test_no_path);
2501     }
2502
2503   g_test_add_func ("/gsettings/basic-types", test_basic_types);
2504   g_test_add_func ("/gsettings/complex-types", test_complex_types);
2505   g_test_add_func ("/gsettings/changes", test_changes);
2506
2507   g_test_add_func ("/gsettings/l10n", test_l10n);
2508   g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
2509
2510   g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
2511   g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
2512   g_test_add_func ("/gsettings/atomic", test_atomic);
2513
2514   g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
2515   g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
2516   g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
2517   g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
2518   g_test_add_func ("/gsettings/unbinding", test_unbind);
2519   g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
2520
2521   if (!backend_set)
2522     {
2523       g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
2524       g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
2525       g_test_add_func ("/gsettings/no-read-binding/subprocess/fail", test_no_read_binding_fail);
2526       g_test_add_func ("/gsettings/no-read-binding/subprocess/pass", test_no_read_binding_pass);
2527       g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
2528       g_test_add_func ("/gsettings/no-write-binding/subprocess/fail", test_no_write_binding_fail);
2529       g_test_add_func ("/gsettings/no-write-binding/subprocess/pass", test_no_write_binding_pass);
2530     }
2531
2532   g_test_add_func ("/gsettings/keyfile", test_keyfile);
2533   g_test_add_func ("/gsettings/child-schema", test_child_schema);
2534   g_test_add_func ("/gsettings/strinfo", test_strinfo);
2535   g_test_add_func ("/gsettings/enums", test_enums);
2536   g_test_add_func ("/gsettings/enums/subprocess/non-enum-key", test_enums_non_enum_key);
2537   g_test_add_func ("/gsettings/enums/subprocess/non-enum-value", test_enums_non_enum_value);
2538   g_test_add_func ("/gsettings/enums/subprocess/range", test_enums_range);
2539   g_test_add_func ("/gsettings/enums/subprocess/non-flags", test_enums_non_flags);
2540   g_test_add_func ("/gsettings/flags", test_flags);
2541   g_test_add_func ("/gsettings/flags/subprocess/non-flags-key", test_flags_non_flags_key);
2542   g_test_add_func ("/gsettings/flags/subprocess/non-flags-value", test_flags_non_flags_value);
2543   g_test_add_func ("/gsettings/flags/subprocess/range", test_flags_range);
2544   g_test_add_func ("/gsettings/flags/subprocess/non-enum", test_flags_non_enum);
2545   g_test_add_func ("/gsettings/range", test_range);
2546   g_test_add_func ("/gsettings/range/subprocess/high", test_range_high);
2547   g_test_add_func ("/gsettings/range/subprocess/low", test_range_low);
2548   g_test_add_func ("/gsettings/list-items", test_list_items);
2549   g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
2550   g_test_add_func ("/gsettings/mapped", test_get_mapped);
2551   g_test_add_func ("/gsettings/get-range", test_get_range);
2552   g_test_add_func ("/gsettings/schema-source", test_schema_source);
2553   g_test_add_func ("/gsettings/actions", test_actions);
2554   g_test_add_func ("/gsettings/null-backend", test_null_backend);
2555   g_test_add_func ("/gsettings/memory-backend", test_memory_backend);
2556   g_test_add_func ("/gsettings/read-descriptions", test_read_descriptions);
2557   g_test_add_func ("/gsettings/test-extended-schema", test_extended_schema);
2558   g_test_add_func ("/gsettings/default-value", test_default_value);
2559
2560   result = g_test_run ();
2561
2562   g_settings_sync ();
2563
2564   return result;
2565 }