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