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