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