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