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