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