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