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