gkdbus: Fix underflow and unreachable code bug
[platform/upstream/glib.git] / gio / tests / gsettings.c
1 #include "config.h"
2
3 #include <stdlib.h>
4 #include <locale.h>
5 #include <libintl.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <gio/gio.h>
9 #include <gstdio.h>
10 #define G_SETTINGS_ENABLE_BACKEND
11 #include <gio/gsettingsbackend.h>
12
13 #include "testenum.h"
14
15 #ifdef HAVE_XLOCALE_H
16 /* Needed on macOS and FreeBSD for uselocale() */
17 #include <xlocale.h>
18 #endif
19
20 static const gchar *locale_dir = ".";
21
22 static gboolean backend_set;
23
24 /* These tests rely on the schemas in org.gtk.test.gschema.xml
25  * to be compiled and installed in the same directory.
26  */
27
28 typedef struct
29 {
30   gchar *tmp_dir;
31 } Fixture;
32
33 static void
34 setup (Fixture       *fixture,
35        gconstpointer  user_data)
36 {
37   GError *error = NULL;
38
39   fixture->tmp_dir = g_dir_make_tmp ("gio-test-gsettings_XXXXXX", &error);
40   g_assert_no_error (error);
41
42   g_test_message ("Using temporary directory: %s", fixture->tmp_dir);
43 }
44
45 static void
46 teardown (Fixture       *fixture,
47           gconstpointer  user_data)
48 {
49   g_assert_no_errno (g_rmdir (fixture->tmp_dir));
50   g_clear_pointer (&fixture->tmp_dir, g_free);
51 }
52
53 static void
54 check_and_free (GVariant    *value,
55                 const gchar *expected)
56 {
57   gchar *printed;
58
59   printed = g_variant_print (value, TRUE);
60   g_assert_cmpstr (printed, ==, expected);
61   g_free (printed);
62
63   g_variant_unref (value);
64 }
65
66 /* Wrapper around g_assert_cmpstr() which gets a setting from a #GSettings
67  * using g_settings_get(). */
68 #define settings_assert_cmpstr(settings, key, op, expected_value) G_STMT_START { \
69   gchar *__str; \
70   g_settings_get ((settings), (key), "s", &__str); \
71   g_assert_cmpstr (__str, op, (expected_value)); \
72   g_free (__str); \
73 } G_STMT_END
74
75
76 /* Just to get warmed up: Read and set a string, and
77  * verify that can read the changed string back
78  */
79 static void
80 test_basic (void)
81 {
82   gchar *str = NULL;
83   GObject *b;
84   gchar *path;
85   gboolean has_unapplied;
86   gboolean delay_apply;
87   GSettings *settings;
88
89   settings = g_settings_new ("org.gtk.test");
90
91   g_object_get (settings,
92                 "schema-id", &str,
93                 "backend", &b,
94                 "path", &path,
95                 "has-unapplied", &has_unapplied,
96                 "delay-apply", &delay_apply,
97                 NULL);
98   g_assert_cmpstr (str, ==, "org.gtk.test");
99   g_assert_nonnull (b);
100   g_assert_cmpstr (path, ==, "/tests/");
101   g_assert_false (has_unapplied);
102   g_assert_false (delay_apply);
103   g_free (str);
104   g_object_unref (b);
105   g_free (path);
106
107   settings_assert_cmpstr (settings, "greeting", ==, "Hello, earthlings");
108
109   g_settings_set (settings, "greeting", "s", "goodbye world");
110   settings_assert_cmpstr (settings, "greeting", ==, "goodbye world");
111
112   if (!backend_set && g_test_undefined ())
113     {
114       GSettings *tmp_settings = g_settings_new ("org.gtk.test");
115
116       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
117                              "*g_settings_set_value*expects type*");
118       g_settings_set (tmp_settings, "greeting", "i", 555);
119       g_test_assert_expected_messages ();
120
121       g_object_unref (tmp_settings);
122     }
123
124   settings_assert_cmpstr (settings, "greeting", ==, "goodbye world");
125
126   g_settings_reset (settings, "greeting");
127   str = g_settings_get_string (settings, "greeting");
128   g_assert_cmpstr (str, ==, "Hello, earthlings");
129   g_free (str);
130
131   g_settings_set (settings, "greeting", "s", "this is the end");
132   g_object_unref (settings);
133 }
134
135 /* Check that we get an error when getting a key
136  * that is not in the schema
137  */
138 static void
139 test_unknown_key (void)
140 {
141   if (!g_test_undefined ())
142     return;
143
144   if (g_test_subprocess ())
145     {
146       GSettings *settings;
147       GVariant *value;
148
149       settings = g_settings_new ("org.gtk.test");
150       value = g_settings_get_value (settings, "no_such_key");
151
152       g_assert_null (value);
153
154       g_object_unref (settings);
155       return;
156     }
157   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
158   g_test_trap_assert_failed ();
159   g_test_trap_assert_stderr ("*does not contain*");
160 }
161
162 /* Check that we get an error when the schema
163  * has not been installed
164  */
165 static void
166 test_no_schema (void)
167 {
168   if (!g_test_undefined ())
169     return;
170
171   if (g_test_subprocess ())
172     {
173       GSettings *settings;
174
175       settings = g_settings_new ("no.such.schema");
176
177       g_assert_null (settings);
178       return;
179     }
180   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
181   g_test_trap_assert_failed ();
182   g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
183 }
184
185 /* Check that we get an error when passing a type string
186  * that does not match the schema
187  */
188 static void
189 test_wrong_type (void)
190 {
191   GSettings *settings;
192   gchar *str = NULL;
193
194   if (!g_test_undefined ())
195     return;
196
197   settings = g_settings_new ("org.gtk.test");
198
199   g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
200                          "*given value has a type of*");
201   g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
202                          "*valid_format_string*");
203   g_settings_get (settings, "greeting", "o", &str);
204   g_test_assert_expected_messages ();
205
206   g_assert_null (str);
207
208   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
209                          "*expects type 's'*");
210   g_settings_set (settings, "greeting", "o", "/a/path");
211   g_test_assert_expected_messages ();
212
213   g_object_unref (settings);
214 }
215
216 /* Check errors with explicit paths */
217 static void
218 test_wrong_path (void)
219 {
220   if (!g_test_undefined ())
221     return;
222
223   if (g_test_subprocess ())
224     {
225       GSettings *settings G_GNUC_UNUSED;
226
227       settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
228       return;
229     }
230   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
231   g_test_trap_assert_failed ();
232   g_test_trap_assert_stderr ("*but path * specified by schema*");
233 }
234
235 static void
236 test_no_path (void)
237 {
238   if (!g_test_undefined ())
239     return;
240
241   if (g_test_subprocess ())
242     {
243       GSettings *settings G_GNUC_UNUSED;
244
245       settings = g_settings_new ("org.gtk.test.no-path");
246       return;
247     }
248   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
249   g_test_trap_assert_failed ();
250   g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
251 }
252
253
254 /* Check that we can successfully read and set the full
255  * range of all basic types
256  */
257 static void
258 test_basic_types (void)
259 {
260   GSettings *settings;
261   gboolean b;
262   guint8 byte;
263   gint16 i16;
264   guint16 u16;
265   gint32 i32;
266   guint32 u32;
267   gint64 i64;
268   guint64 u64;
269   gdouble d;
270   gchar *str;
271
272   settings = g_settings_new ("org.gtk.test.basic-types");
273
274   g_settings_get (settings, "test-boolean", "b", &b);
275   g_assert_cmpint (b, ==, 1);
276
277   g_settings_set (settings, "test-boolean", "b", 0);
278   g_settings_get (settings, "test-boolean", "b", &b);
279   g_assert_cmpint (b, ==, 0);
280
281   g_settings_get (settings, "test-byte", "y", &byte);
282   g_assert_cmpint (byte, ==, 25);
283
284   g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
285   g_settings_get (settings, "test-byte", "y", &byte);
286   g_assert_cmpint (byte, ==, G_MAXUINT8);
287
288   g_settings_get (settings, "test-int16", "n", &i16);
289   g_assert_cmpint (i16, ==, -1234);
290
291   g_settings_set (settings, "test-int16", "n", G_MININT16);
292   g_settings_get (settings, "test-int16", "n", &i16);
293   g_assert_cmpint (i16, ==, G_MININT16);
294
295   g_settings_set (settings, "test-int16", "n", G_MAXINT16);
296   g_settings_get (settings, "test-int16", "n", &i16);
297   g_assert_cmpint (i16, ==, G_MAXINT16);
298
299   g_settings_get (settings, "test-uint16", "q", &u16);
300   g_assert_cmpuint (u16, ==, 1234);
301
302   g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
303   g_settings_get (settings, "test-uint16", "q", &u16);
304   g_assert_cmpuint (u16, ==, G_MAXUINT16);
305
306   g_settings_get (settings, "test-int32", "i", &i32);
307   g_assert_cmpint (i32, ==, -123456);
308
309   g_settings_set (settings, "test-int32", "i", G_MININT32);
310   g_settings_get (settings, "test-int32", "i", &i32);
311   g_assert_cmpint (i32, ==, G_MININT32);
312
313   g_settings_set (settings, "test-int32", "i", G_MAXINT32);
314   g_settings_get (settings, "test-int32", "i", &i32);
315   g_assert_cmpint (i32, ==, G_MAXINT32);
316
317   g_settings_get (settings, "test-uint32", "u", &u32);
318   g_assert_cmpuint (u32, ==, 123456);
319
320   g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
321   g_settings_get (settings, "test-uint32", "u", &u32);
322   g_assert_cmpuint (u32, ==, G_MAXUINT32);
323
324   g_settings_get (settings, "test-int64", "x", &i64);
325   g_assert_cmpuint (i64, ==, -123456789);
326
327   g_settings_set (settings, "test-int64", "x", G_MININT64);
328   g_settings_get (settings, "test-int64", "x", &i64);
329   g_assert_cmpuint (i64, ==, G_MININT64);
330
331   g_settings_set (settings, "test-int64", "x", G_MAXINT64);
332   g_settings_get (settings, "test-int64", "x", &i64);
333   g_assert_cmpuint (i64, ==, G_MAXINT64);
334
335   g_settings_get (settings, "test-uint64", "t", &u64);
336   g_assert_cmpuint (u64, ==, 123456789);
337
338   g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
339   g_settings_get (settings, "test-uint64", "t", &u64);
340   g_assert_cmpuint (u64, ==, G_MAXUINT64);
341
342   g_settings_get (settings, "test-double", "d", &d);
343   g_assert_cmpfloat (d, ==, 123.456);
344
345   g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
346   g_settings_get (settings, "test-double", "d", &d);
347   g_assert_cmpfloat (d, ==, G_MINDOUBLE);
348
349   g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
350   g_settings_get (settings, "test-double", "d", &d);
351   g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
352
353   settings_assert_cmpstr (settings, "test-string", ==, "a string, it seems");
354
355   g_settings_get (settings, "test-objectpath", "o", &str);
356   g_assert_cmpstr (str, ==, "/a/object/path");
357   g_object_unref (settings);
358   g_free (str);
359   str = NULL;
360 }
361
362 /* Check that we can read an set complex types like
363  * tuples, arrays and dictionaries
364  */
365 static void
366 test_complex_types (void)
367 {
368   GSettings *settings;
369   gchar *s;
370   gint i1, i2;
371   GVariantIter *iter = NULL;
372   GVariant *v = NULL;
373
374   settings = g_settings_new ("org.gtk.test.complex-types");
375
376   g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
377   g_assert_cmpstr (s, ==, "one");
378   g_assert_cmpint (i1,==, 2);
379   g_assert_cmpint (i2,==, 3);
380   g_free (s) ;
381   s = NULL;
382
383   g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
384   g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
385   g_assert_cmpstr (s, ==, "none");
386   g_assert_cmpint (i1,==, 0);
387   g_assert_cmpint (i2,==, 0);
388   g_free (s);
389   s = NULL;
390
391   g_settings_get (settings, "test-array", "ai", &iter);
392   g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
393   g_assert_true (g_variant_iter_next (iter, "i", &i1));
394   g_assert_cmpint (i1, ==, 0);
395   g_assert_true (g_variant_iter_next (iter, "i", &i1));
396   g_assert_cmpint (i1, ==, 1);
397   g_assert_true (g_variant_iter_next (iter, "i", &i1));
398   g_assert_cmpint (i1, ==, 2);
399   g_assert_true (g_variant_iter_next (iter, "i", &i1));
400   g_assert_cmpint (i1, ==, 3);
401   g_assert_true (g_variant_iter_next (iter, "i", &i1));
402   g_assert_cmpint (i1, ==, 4);
403   g_assert_true (g_variant_iter_next (iter, "i", &i1));
404   g_assert_cmpint (i1, ==, 5);
405   g_assert_false (g_variant_iter_next (iter, "i", &i1));
406   g_variant_iter_free (iter);
407
408   g_settings_get (settings, "test-dict", "a{sau}", &iter);
409   g_assert_cmpint (g_variant_iter_n_children (iter), ==, 2);
410   g_assert_true (g_variant_iter_next (iter, "{&s@au}", &s, &v));
411   g_assert_cmpstr (s, ==, "AC");
412   g_assert_cmpstr ((char *)g_variant_get_type (v), ==, "au");
413   g_variant_unref (v);
414   g_assert_true (g_variant_iter_next (iter, "{&s@au}", &s, &v));
415   g_assert_cmpstr (s, ==, "IV");
416   g_assert_cmpstr ((char *)g_variant_get_type (v), ==, "au");
417   g_variant_unref (v);
418   g_variant_iter_free (iter);
419
420   v = g_settings_get_value (settings, "test-dict");
421   g_assert_cmpstr ((char *)g_variant_get_type (v), ==, "a{sau}");
422   g_variant_unref (v);
423
424   g_object_unref (settings);
425 }
426
427 static gboolean changed_cb_called;
428
429 static void
430 changed_cb (GSettings   *settings,
431             const gchar *key,
432             gpointer     data)
433 {
434   changed_cb_called = TRUE;
435
436   g_assert_cmpstr (key, ==, data);
437 }
438
439 /* Test that basic change notification with the changed signal works.
440  */
441 static void
442 test_changes (void)
443 {
444   GSettings *settings;
445   GSettings *settings2;
446
447   settings = g_settings_new ("org.gtk.test");
448
449   g_signal_connect (settings, "changed",
450                     G_CALLBACK (changed_cb), "greeting");
451
452   changed_cb_called = FALSE;
453
454   g_settings_set (settings, "greeting", "s", "new greeting");
455   g_assert_true (changed_cb_called);
456
457   settings2 = g_settings_new ("org.gtk.test");
458
459   changed_cb_called = FALSE;
460
461   g_settings_set (settings2, "greeting", "s", "hi");
462   g_assert_true (changed_cb_called);
463
464   g_object_unref (settings2);
465   g_object_unref (settings);
466 }
467
468 static gboolean changed_cb_called2;
469
470 static void
471 changed_cb2 (GSettings   *settings,
472              const gchar *key,
473              gpointer     data)
474 {
475   gboolean *p = data;
476
477   *p = TRUE;
478 }
479
480 /* Test that changes done to a delay-mode instance
481  * don't appear to the outside world until apply. Also
482  * check that we get change notification when they are
483  * applied.
484  * Also test that the has-unapplied property is properly
485  * maintained.
486  */
487 static void
488 test_delay_apply (void)
489 {
490   GSettings *settings;
491   GSettings *settings2;
492   gboolean writable;
493   GVariant *v;
494   const gchar *s;
495
496   settings = g_settings_new ("org.gtk.test");
497   settings2 = g_settings_new ("org.gtk.test");
498
499   g_settings_set (settings2, "greeting", "s", "top o' the morning");
500
501   changed_cb_called = FALSE;
502   changed_cb_called2 = FALSE;
503
504   g_signal_connect (settings, "changed",
505                     G_CALLBACK (changed_cb2), &changed_cb_called);
506   g_signal_connect (settings2, "changed",
507                     G_CALLBACK (changed_cb2), &changed_cb_called2);
508
509   g_settings_delay (settings);
510
511   g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
512
513   g_assert_true (changed_cb_called);
514   g_assert_false (changed_cb_called2);
515
516   /* Try resetting the key and ensure a notification is emitted on the delayed #GSettings object. */
517   changed_cb_called = FALSE;
518   changed_cb_called2 = FALSE;
519
520   g_settings_reset (settings, "greeting");
521
522   g_assert_true (changed_cb_called);
523   g_assert_false (changed_cb_called2);
524
525   /* Locally change the greeting again. */
526   changed_cb_called = FALSE;
527   changed_cb_called2 = FALSE;
528
529   g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
530
531   g_assert_true (changed_cb_called);
532   g_assert_false (changed_cb_called2);
533
534   writable = g_settings_is_writable (settings, "greeting");
535   g_assert_true (writable);
536
537   settings_assert_cmpstr (settings, "greeting", ==, "greetings from test_delay_apply");
538
539   v = g_settings_get_user_value (settings, "greeting");
540   s = g_variant_get_string (v, NULL);
541   g_assert_cmpstr (s, ==, "greetings from test_delay_apply");
542   g_variant_unref (v);
543
544   settings_assert_cmpstr (settings2, "greeting", ==, "top o' the morning");
545
546   g_assert_true (g_settings_get_has_unapplied (settings));
547   g_assert_false (g_settings_get_has_unapplied (settings2));
548
549   changed_cb_called = FALSE;
550   changed_cb_called2 = FALSE;
551
552   g_settings_apply (settings);
553
554   g_assert_false (changed_cb_called);
555   g_assert_true (changed_cb_called2);
556
557   settings_assert_cmpstr (settings, "greeting", ==, "greetings from test_delay_apply");
558   settings_assert_cmpstr (settings2, "greeting", ==, "greetings from test_delay_apply");
559
560   g_assert_false (g_settings_get_has_unapplied (settings));
561   g_assert_false (g_settings_get_has_unapplied (settings2));
562
563   g_settings_reset (settings, "greeting");
564   g_settings_apply (settings);
565
566   settings_assert_cmpstr (settings, "greeting", ==, "Hello, earthlings");
567
568   g_object_unref (settings2);
569   g_object_unref (settings);
570 }
571
572 /* Test that reverting unapplied changes in a delay-apply
573  * settings instance works.
574  */
575 static void
576 test_delay_revert (void)
577 {
578   GSettings *settings;
579   GSettings *settings2;
580
581   settings = g_settings_new ("org.gtk.test");
582   settings2 = g_settings_new ("org.gtk.test");
583
584   g_settings_set (settings2, "greeting", "s", "top o' the morning");
585
586   settings_assert_cmpstr (settings, "greeting", ==, "top o' the morning");
587
588   g_settings_delay (settings);
589
590   g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
591
592   settings_assert_cmpstr (settings, "greeting", ==, "greetings from test_delay_revert");
593   settings_assert_cmpstr (settings2, "greeting", ==, "top o' the morning");
594
595   g_assert_true (g_settings_get_has_unapplied (settings));
596
597   g_settings_revert (settings);
598
599   g_assert_false (g_settings_get_has_unapplied (settings));
600
601   settings_assert_cmpstr (settings, "greeting", ==, "top o' the morning");
602   settings_assert_cmpstr (settings2, "greeting", ==, "top o' the morning");
603
604   g_object_unref (settings2);
605   g_object_unref (settings);
606 }
607
608 static void
609 test_delay_child (void)
610 {
611   GSettings *base;
612   GSettings *settings;
613   GSettings *child;
614   guint8 byte;
615   gboolean delay;
616
617   base = g_settings_new ("org.gtk.test.basic-types");
618   g_settings_set (base, "test-byte", "y", 36);
619
620   settings = g_settings_new ("org.gtk.test");
621   g_settings_delay (settings);
622   g_object_get (settings, "delay-apply", &delay, NULL);
623   g_assert_true (delay);
624
625   child = g_settings_get_child (settings, "basic-types");
626   g_assert_nonnull (child);
627
628   g_object_get (child, "delay-apply", &delay, NULL);
629   g_assert_true (delay);
630
631   g_settings_get (child, "test-byte", "y", &byte);
632   g_assert_cmpuint (byte, ==, 36);
633
634   g_settings_set (child, "test-byte", "y", 42);
635
636   /* make sure the child was delayed too */
637   g_settings_get (base, "test-byte", "y", &byte);
638   g_assert_cmpuint (byte, ==, 36);
639
640   /* apply the child and the changes should be saved */
641   g_settings_apply (child);
642   g_settings_get (base, "test-byte", "y", &byte);
643   g_assert_cmpuint (byte, ==, 42);
644
645   g_object_unref (child);
646   g_object_unref (settings);
647   g_object_unref (base);
648 }
649
650 static void
651 test_delay_reset_key (void)
652 {
653   GSettings *direct_settings = NULL, *delayed_settings = NULL;
654
655   g_test_summary ("Test that resetting a key on a delayed settings instance works");
656
657   delayed_settings = g_settings_new ("org.gtk.test");
658   direct_settings = g_settings_new ("org.gtk.test");
659
660   g_settings_set (direct_settings, "greeting", "s", "ey up");
661
662   settings_assert_cmpstr (delayed_settings, "greeting", ==, "ey up");
663
664   /* Set up a delayed settings backend. */
665   g_settings_delay (delayed_settings);
666
667   g_settings_set (delayed_settings, "greeting", "s", "how do");
668
669   settings_assert_cmpstr (delayed_settings, "greeting", ==, "how do");
670   settings_assert_cmpstr (direct_settings, "greeting", ==, "ey up");
671
672   g_assert_true (g_settings_get_has_unapplied (delayed_settings));
673
674   g_settings_reset (delayed_settings, "greeting");
675
676   /* There are still unapplied settings, because the reset is resetting to the
677    * value from the schema, not the value from @direct_settings. */
678   g_assert_true (g_settings_get_has_unapplied (delayed_settings));
679
680   settings_assert_cmpstr (delayed_settings, "greeting", ==, "Hello, earthlings");
681   settings_assert_cmpstr (direct_settings, "greeting", ==, "ey up");
682
683   /* Apply the settings changes (i.e. the reset). */
684   g_settings_apply (delayed_settings);
685
686   g_assert_false (g_settings_get_has_unapplied (delayed_settings));
687
688   settings_assert_cmpstr (delayed_settings, "greeting", ==, "Hello, earthlings");
689   settings_assert_cmpstr (direct_settings, "greeting", ==, "Hello, earthlings");
690
691   g_object_unref (direct_settings);
692   g_object_unref (delayed_settings);
693 }
694
695 static void
696 keys_changed_cb (GSettings    *settings,
697                  const GQuark *keys,
698                  gint          n_keys)
699 {
700   g_assert_cmpint (n_keys, ==, 2);
701
702   g_assert_true ((keys[0] == g_quark_from_static_string ("greeting") &&
703                   keys[1] == g_quark_from_static_string ("farewell")) ||
704                  (keys[1] == g_quark_from_static_string ("greeting") &&
705                   keys[0] == g_quark_from_static_string ("farewell")));
706
707   settings_assert_cmpstr (settings, "greeting", ==, "greetings from test_atomic");
708   settings_assert_cmpstr (settings, "farewell", ==, "atomic bye-bye");
709 }
710
711 /* Check that delay-applied changes appear atomically.
712  * More specifically, verify that all changed keys appear
713  * with their new value while handling the change-event signal.
714  */
715 static void
716 test_atomic (void)
717 {
718   GSettings *settings;
719   GSettings *settings2;
720
721   settings = g_settings_new ("org.gtk.test");
722   settings2 = g_settings_new ("org.gtk.test");
723
724   g_settings_set (settings2, "greeting", "s", "top o' the morning");
725
726   changed_cb_called = FALSE;
727   changed_cb_called2 = FALSE;
728
729   g_signal_connect (settings2, "change-event",
730                     G_CALLBACK (keys_changed_cb), NULL);
731
732   g_settings_delay (settings);
733
734   g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
735   g_settings_set (settings, "farewell", "s", "atomic bye-bye");
736
737   g_settings_apply (settings);
738
739   settings_assert_cmpstr (settings, "greeting", ==, "greetings from test_atomic");
740   settings_assert_cmpstr (settings, "farewell", ==, "atomic bye-bye");
741   settings_assert_cmpstr (settings2, "greeting", ==, "greetings from test_atomic");
742   settings_assert_cmpstr (settings2, "farewell", ==, "atomic bye-bye");
743
744   g_object_unref (settings2);
745   g_object_unref (settings);
746 }
747
748 /* On Windows the interaction between the C library locale and libintl
749  * (from GNU gettext) is not like on POSIX, so just skip these tests
750  * for now.
751  *
752  * There are several issues:
753  *
754  * 1) The C library doesn't use LC_MESSAGES, that is implemented only
755  * in libintl (defined in its <libintl.h>).
756  *
757  * 2) The locale names that uselocale() accepts and returns aren't in
758  * the "de_DE" style, but like "German_Germany".
759  *
760  * 3) libintl looks at the Win32 thread locale and not the C library
761  * locale. (And even if libintl would use the C library's locale, as
762  * there are several alternative C library DLLs, libintl might be
763  * linked to a different one than the application code, so they
764  * wouldn't have the same C library locale anyway.)
765  */
766
767 /* Test that translations work for schema defaults.
768  *
769  * This test relies on the de.po file in the same directory
770  * to be compiled into ./de/LC_MESSAGES/test.mo
771  */
772 static void
773 test_l10n (void)
774 {
775 #ifndef HAVE_USELOCALE
776   g_test_skip ("Unsafe to change locale because platform does not support uselocale()");
777 #else
778   GSettings *settings;
779   gchar *str;
780   locale_t original_locale;
781   locale_t new_locale;
782   locale_t result;
783
784   bindtextdomain ("test", locale_dir);
785   bind_textdomain_codeset ("test", "UTF-8");
786
787   original_locale = uselocale ((locale_t) 0);
788   g_assert_true (original_locale != (locale_t) 0);
789   new_locale = newlocale (LC_MESSAGES_MASK, "C", (locale_t) 0);
790   g_assert_true (new_locale != (locale_t) 0);
791   result = uselocale (new_locale);
792   g_assert_true (result == original_locale);
793
794   settings = g_settings_new ("org.gtk.test.localized");
795   str = g_settings_get_string (settings, "error-message");
796
797   result = uselocale (original_locale);
798   g_assert_true (result == new_locale);
799   freelocale (new_locale);
800
801   g_assert_cmpstr (str, ==, "Unnamed");
802   g_free (str);
803   str = NULL;
804
805   new_locale = newlocale (LC_MESSAGES_MASK, "de_DE.UTF-8", (locale_t) 0);
806   if (new_locale == (locale_t) 0)
807     {
808       g_test_skip ("Cannot run test becaues de_DE.UTF-8 locale is not available");
809       g_object_unref (settings);
810       return;
811     }
812   result = uselocale (new_locale);
813   g_assert_true (result == original_locale);
814
815   /* Only do the test if translation is actually working... */
816   if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
817     {
818       str = g_settings_get_string (settings, "error-message");
819
820       g_assert_cmpstr (str, ==, "Unbenannt");
821       g_free (str);
822       str = NULL;
823     }
824   else
825     {
826       g_test_skip ("translation is not working");
827     }
828
829   result = uselocale (original_locale);
830   g_assert_true (result == new_locale);
831   freelocale (new_locale);
832
833   g_object_unref (settings);
834 #endif
835 }
836
837 /* Test that message context works as expected with translated
838  * schema defaults. Also, verify that non-ASCII UTF-8 content
839  * works.
840  *
841  * This test relies on the de.po file in the same directory
842  * to be compiled into ./de/LC_MESSAGES/test.mo
843  */
844 static void
845 test_l10n_context (void)
846 {
847 #ifndef HAVE_USELOCALE
848   g_test_skip ("Unsafe to change locale because platform does not support uselocale()");
849 #else
850   GSettings *settings;
851   gchar *str;
852   locale_t original_locale;
853   locale_t new_locale;
854   locale_t result;
855
856   bindtextdomain ("test", locale_dir);
857   bind_textdomain_codeset ("test", "UTF-8");
858
859   settings = g_settings_new ("org.gtk.test.localized");
860
861   original_locale = uselocale ((locale_t) 0);
862   g_assert_true (original_locale != (locale_t) 0);
863   new_locale = newlocale (LC_MESSAGES_MASK, "C", (locale_t) 0);
864   g_assert_true (new_locale != (locale_t) 0);
865   result = uselocale (new_locale);
866   g_assert_true (result == original_locale);
867
868   g_settings_get (settings, "backspace", "s", &str);
869
870   result = uselocale (original_locale);
871   g_assert_true (result == new_locale);
872   freelocale (new_locale);
873
874   g_assert_cmpstr (str, ==, "BackSpace");
875   g_free (str);
876   str = NULL;
877
878   new_locale = newlocale (LC_MESSAGES_MASK, "de_DE.UTF-8", (locale_t) 0);
879   if (new_locale == (locale_t) 0)
880     {
881       g_test_skip ("Cannot run test becaues de_DE.UTF-8 locale is not available");
882       g_object_unref (settings);
883       return;
884     }
885   result = uselocale (new_locale);
886   g_assert_true (result == original_locale);
887
888   /* Only do the test if translation is actually working... */
889   if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
890     settings_assert_cmpstr (settings, "backspace", ==, "Löschen");
891   else
892     g_test_skip ("translation is not working");
893
894   result = uselocale (original_locale);
895   g_assert_true (result == new_locale);
896   freelocale (new_locale);
897
898   g_object_unref (settings);
899 #endif
900 }
901
902 /* Test use of l10n="time" and LC_TIME. */
903 static void
904 test_l10n_time (void)
905 {
906 #ifndef HAVE_USELOCALE
907   g_test_skip ("Unsafe to change locale because platform does not support uselocale()");
908 #else
909   GSettings *settings;
910   gchar *str;
911   locale_t original_locale;
912   locale_t new_locale;
913   locale_t result;
914
915   g_test_summary ("Test that l10n='time' attribute uses the correct category for translations");
916   g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2575");
917
918   bindtextdomain ("test", locale_dir);
919   bind_textdomain_codeset ("test", "UTF-8");
920
921   settings = g_settings_new ("org.gtk.test.localized");
922
923   original_locale = uselocale ((locale_t) 0);
924   g_assert_true (original_locale != (locale_t) 0);
925   new_locale = duplocale (original_locale);
926   g_assert_true (new_locale != (locale_t) 0);
927   new_locale = newlocale (LC_TIME_MASK, "C", new_locale);
928   g_assert_true (new_locale != (locale_t) 0);
929   result = uselocale (new_locale);
930   g_assert_true (result == original_locale);
931
932   str = g_settings_get_string (settings, "midnight");
933
934   result = uselocale (original_locale);
935   g_assert_true (result == new_locale);
936
937   g_assert_cmpstr (str, ==, "12:00 AM");
938   g_free (str);
939   str = NULL;
940
941   new_locale = newlocale (LC_TIME_MASK, "de_DE.UTF-8", new_locale);
942   if (new_locale == (locale_t) 0)
943     {
944       g_test_skip ("Cannot run test becaues de_DE.UTF-8 locale is not available");
945       g_object_unref (settings);
946       return;
947     }
948   result = uselocale (new_locale);
949   g_assert_true (result != (locale_t) 0);
950
951   /* Only do the test if translation is actually working... */
952   if (g_str_equal (dgettext ("test", "\"12:00 AM\""), "\"00:00\""))
953     {
954       str = g_settings_get_string (settings, "midnight");
955
956       g_assert_cmpstr (str, ==, "00:00");
957       g_free (str);
958       str = NULL;
959     }
960   else
961     {
962       g_test_skip ("translation is not working");
963     }
964
965   result = uselocale (original_locale);
966   g_assert_true (result == new_locale);
967   freelocale (new_locale);
968
969   g_object_unref (settings);
970 #endif
971 }
972
973 enum
974 {
975   PROP_0,
976   PROP_BOOL,
977   PROP_ANTI_BOOL,
978   PROP_BYTE,
979   PROP_INT16,
980   PROP_UINT16,
981   PROP_INT,
982   PROP_UINT,
983   PROP_INT64,
984   PROP_UINT64,
985   PROP_DOUBLE,
986   PROP_STRING,
987   PROP_NO_READ,
988   PROP_NO_WRITE,
989   PROP_STRV,
990   PROP_ENUM,
991   PROP_FLAGS
992 };
993
994 typedef struct
995 {
996   GObject parent_instance;
997
998   gboolean bool_prop;
999   gboolean anti_bool_prop;
1000   gint8 byte_prop;
1001   gint int16_prop;
1002   guint16 uint16_prop;
1003   gint int_prop;
1004   guint uint_prop;
1005   gint64 int64_prop;
1006   guint64 uint64_prop;
1007   gdouble double_prop;
1008   gchar *string_prop;
1009   gchar *no_read_prop;
1010   gchar *no_write_prop;
1011   gchar **strv_prop;
1012   guint enum_prop;
1013   guint flags_prop;
1014 } TestObject;
1015
1016 typedef struct
1017 {
1018   GObjectClass parent_class;
1019 } TestObjectClass;
1020
1021 static GType test_object_get_type (void);
1022 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
1023
1024 static void
1025 test_object_init (TestObject *object)
1026 {
1027 }
1028
1029 static void
1030 test_object_finalize (GObject *object)
1031 {
1032   TestObject *testo = (TestObject*)object;
1033   g_strfreev (testo->strv_prop);
1034   g_free (testo->string_prop);
1035   G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
1036 }
1037
1038 static void
1039 test_object_get_property (GObject    *object,
1040                           guint       prop_id,
1041                           GValue     *value,
1042                           GParamSpec *pspec)
1043 {
1044   TestObject *test_object = (TestObject *)object;
1045
1046   switch (prop_id)
1047     {
1048     case PROP_BOOL:
1049       g_value_set_boolean (value, test_object->bool_prop);
1050       break;
1051     case PROP_ANTI_BOOL:
1052       g_value_set_boolean (value, test_object->anti_bool_prop);
1053       break;
1054     case PROP_BYTE:
1055       g_value_set_schar (value, test_object->byte_prop);
1056       break;
1057     case PROP_UINT16:
1058       g_value_set_uint (value, test_object->uint16_prop);
1059       break;
1060     case PROP_INT16:
1061       g_value_set_int (value, test_object->int16_prop);
1062       break;
1063     case PROP_INT:
1064       g_value_set_int (value, test_object->int_prop);
1065       break;
1066     case PROP_UINT:
1067       g_value_set_uint (value, test_object->uint_prop);
1068       break;
1069     case PROP_INT64:
1070       g_value_set_int64 (value, test_object->int64_prop);
1071       break;
1072     case PROP_UINT64:
1073       g_value_set_uint64 (value, test_object->uint64_prop);
1074       break;
1075     case PROP_DOUBLE:
1076       g_value_set_double (value, test_object->double_prop);
1077       break;
1078     case PROP_STRING:
1079       g_value_set_string (value, test_object->string_prop);
1080       break;
1081     case PROP_NO_WRITE:
1082       g_value_set_string (value, test_object->no_write_prop);
1083       break;
1084     case PROP_STRV:
1085       g_value_set_boxed (value, test_object->strv_prop);
1086       break;
1087     case PROP_ENUM:
1088       g_value_set_enum (value, test_object->enum_prop);
1089       break;
1090     case PROP_FLAGS:
1091       g_value_set_flags (value, test_object->flags_prop);
1092       break;
1093     default:
1094       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1095       break;
1096     }
1097 }
1098
1099 static void
1100 test_object_set_property (GObject      *object,
1101                           guint         prop_id,
1102                           const GValue *value,
1103                           GParamSpec   *pspec)
1104 {
1105   TestObject *test_object = (TestObject *)object;
1106
1107   switch (prop_id)
1108     {
1109     case PROP_BOOL:
1110       test_object->bool_prop = g_value_get_boolean (value);
1111       break;
1112     case PROP_ANTI_BOOL:
1113       test_object->anti_bool_prop = g_value_get_boolean (value);
1114       break;
1115     case PROP_BYTE:
1116       test_object->byte_prop = g_value_get_schar (value);
1117       break;
1118     case PROP_INT16:
1119       test_object->int16_prop = g_value_get_int (value);
1120       break;
1121     case PROP_UINT16:
1122       test_object->uint16_prop = g_value_get_uint (value);
1123       break;
1124     case PROP_INT:
1125       test_object->int_prop = g_value_get_int (value);
1126       break;
1127     case PROP_UINT:
1128       test_object->uint_prop = g_value_get_uint (value);
1129       break;
1130     case PROP_INT64:
1131       test_object->int64_prop = g_value_get_int64 (value);
1132       break;
1133     case PROP_UINT64:
1134       test_object->uint64_prop = g_value_get_uint64 (value);
1135       break;
1136     case PROP_DOUBLE:
1137       test_object->double_prop = g_value_get_double (value);
1138       break;
1139     case PROP_STRING:
1140       g_free (test_object->string_prop);
1141       test_object->string_prop = g_value_dup_string (value);
1142       break;
1143     case PROP_NO_READ:
1144       g_free (test_object->no_read_prop);
1145       test_object->no_read_prop = g_value_dup_string (value);
1146       break;
1147     case PROP_STRV:
1148       g_strfreev (test_object->strv_prop);
1149       test_object->strv_prop = g_value_dup_boxed (value);
1150       break;
1151     case PROP_ENUM:
1152       test_object->enum_prop = g_value_get_enum (value);
1153       break;
1154     case PROP_FLAGS:
1155       test_object->flags_prop = g_value_get_flags (value);
1156       break;
1157     default:
1158       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1159       break;
1160     }
1161 }
1162
1163 static GType
1164 test_enum_get_type (void)
1165 {
1166   static gsize define_type_id = 0;
1167
1168   if (g_once_init_enter (&define_type_id))
1169     {
1170       static const GEnumValue values[] = {
1171         { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
1172         { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
1173         { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
1174         { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
1175         { 0, NULL, NULL }
1176       };
1177
1178       GType type_id = g_enum_register_static ("TestEnum", values);
1179       g_once_init_leave (&define_type_id, type_id);
1180     }
1181
1182   return define_type_id;
1183 }
1184
1185 static GType
1186 test_flags_get_type (void)
1187 {
1188   static gsize define_type_id = 0;
1189
1190   if (g_once_init_enter (&define_type_id))
1191     {
1192       static const GFlagsValue values[] = {
1193         { TEST_FLAGS_NONE, "TEST_FLAGS_NONE", "none" },
1194         { TEST_FLAGS_MOURNING, "TEST_FLAGS_MOURNING", "mourning" },
1195         { TEST_FLAGS_LAUGHING, "TEST_FLAGS_LAUGHING", "laughing" },
1196         { TEST_FLAGS_WALKING, "TEST_FLAGS_WALKING", "walking" },
1197         { 0, NULL, NULL }
1198       };
1199
1200       GType type_id = g_flags_register_static ("TestFlags", values);
1201       g_once_init_leave (&define_type_id, type_id);
1202     }
1203
1204   return define_type_id;
1205 }
1206
1207 static void
1208 test_object_class_init (TestObjectClass *class)
1209 {
1210   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
1211
1212   gobject_class->get_property = test_object_get_property;
1213   gobject_class->set_property = test_object_set_property;
1214   gobject_class->finalize = test_object_finalize;
1215
1216   g_object_class_install_property (gobject_class, PROP_BOOL,
1217     g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
1218   g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
1219     g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
1220   g_object_class_install_property (gobject_class, PROP_BYTE,
1221     g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
1222   g_object_class_install_property (gobject_class, PROP_INT16,
1223     g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
1224   g_object_class_install_property (gobject_class, PROP_UINT16,
1225     g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
1226   g_object_class_install_property (gobject_class, PROP_INT,
1227     g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
1228   g_object_class_install_property (gobject_class, PROP_UINT,
1229     g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
1230   g_object_class_install_property (gobject_class, PROP_INT64,
1231     g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
1232   g_object_class_install_property (gobject_class, PROP_UINT64,
1233     g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
1234   g_object_class_install_property (gobject_class, PROP_DOUBLE,
1235     g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
1236   g_object_class_install_property (gobject_class, PROP_STRING,
1237     g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
1238   g_object_class_install_property (gobject_class, PROP_NO_WRITE,
1239     g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
1240   g_object_class_install_property (gobject_class, PROP_NO_READ,
1241     g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
1242   g_object_class_install_property (gobject_class, PROP_STRV,
1243     g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
1244   g_object_class_install_property (gobject_class, PROP_ENUM,
1245     g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
1246   g_object_class_install_property (gobject_class, PROP_FLAGS,
1247     g_param_spec_flags ("flags", "", "", test_flags_get_type (), TEST_FLAGS_NONE, G_PARAM_READWRITE));
1248 }
1249
1250 static TestObject *
1251 test_object_new (void)
1252 {
1253   return (TestObject*)g_object_new (test_object_get_type (), NULL);
1254 }
1255
1256 /* Test basic binding functionality for simple types.
1257  * Verify that with bidirectional bindings, changes on either side
1258  * are notified on the other end.
1259  */
1260 static void
1261 test_simple_binding (void)
1262 {
1263   TestObject *obj;
1264   GSettings *settings;
1265   gboolean b;
1266   gchar y;
1267   gint i;
1268   guint u;
1269   gint16 n;
1270   guint16 q;
1271   gint n2;
1272   guint q2;
1273   gint64 i64;
1274   guint64 u64;
1275   gdouble d;
1276   gchar *s;
1277   GVariant *value;
1278   gchar **strv;
1279
1280   settings = g_settings_new ("org.gtk.test.binding");
1281   obj = test_object_new ();
1282
1283   g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
1284   g_object_set (obj, "bool", TRUE, NULL);
1285   g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1286
1287   g_settings_set_boolean (settings, "bool", FALSE);
1288   b = TRUE;
1289   g_object_get (obj, "bool", &b, NULL);
1290   g_assert_cmpint (b, ==, FALSE);
1291
1292   g_settings_bind (settings, "anti-bool", obj, "anti-bool",
1293                    G_SETTINGS_BIND_INVERT_BOOLEAN);
1294   g_object_set (obj, "anti-bool", FALSE, NULL);
1295   g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
1296
1297   g_settings_set_boolean (settings, "anti-bool", FALSE);
1298   b = FALSE;
1299   g_object_get (obj, "anti-bool", &b, NULL);
1300   g_assert_cmpint (b, ==, TRUE);
1301
1302   g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
1303
1304   g_object_set (obj, "byte", 123, NULL);
1305   y = 'c';
1306   g_settings_get (settings, "byte", "y", &y);
1307   g_assert_cmpint (y, ==, 123);
1308
1309   g_settings_set (settings, "byte", "y", 54);
1310   y = 'c';
1311   g_object_get (obj, "byte", &y, NULL);
1312   g_assert_cmpint (y, ==, 54);
1313
1314   g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
1315
1316   g_object_set (obj, "int16", 1234, NULL);
1317   n = 4321;
1318   g_settings_get (settings, "int16", "n", &n);
1319   g_assert_cmpint (n, ==, 1234);
1320
1321   g_settings_set (settings, "int16", "n", 4321);
1322   n2 = 1111;
1323   g_object_get (obj, "int16", &n2, NULL);
1324   g_assert_cmpint (n2, ==, 4321);
1325
1326   g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
1327
1328   g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1329   q = 1111;
1330   g_settings_get (settings, "uint16", "q", &q);
1331   g_assert_cmpuint (q, ==, G_MAXUINT16);
1332
1333   g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1334   q2 = 1111;
1335   g_object_get (obj, "uint16", &q2, NULL);
1336   g_assert_cmpuint (q2, ==, (guint16) G_MAXINT16);
1337
1338   g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1339
1340   g_object_set (obj, "int", 12345, NULL);
1341   g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1342
1343   g_settings_set_int (settings, "int", 54321);
1344   i = 1111;
1345   g_object_get (obj, "int", &i, NULL);
1346   g_assert_cmpint (i, ==, 54321);
1347
1348   g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1349
1350   g_object_set (obj, "uint", 12345, NULL);
1351   g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);
1352
1353   g_settings_set_uint (settings, "uint", 54321);
1354   u = 1111;
1355   g_object_get (obj, "uint", &u, NULL);
1356   g_assert_cmpuint (u, ==, 54321);
1357
1358   g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1359
1360   g_object_set (obj, "uint64", (guint64) 12345, NULL);
1361   g_assert_cmpuint (g_settings_get_uint64 (settings, "uint64"), ==, 12345);
1362
1363   g_settings_set_uint64 (settings, "uint64", 54321);
1364   u64 = 1111;
1365   g_object_get (obj, "uint64", &u64, NULL);
1366   g_assert_cmpuint (u64, ==, 54321);
1367
1368   g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1369
1370   g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1371   i64 = 1111;
1372   g_settings_get (settings, "int64", "x", &i64);
1373   g_assert_cmpint (i64, ==, G_MAXINT64);
1374
1375   g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1376   i64 = 1111;
1377   g_object_get (obj, "int64", &i64, NULL);
1378   g_assert_cmpint (i64, ==, G_MININT64);
1379
1380   g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1381
1382   g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1383   u64 = 1111;
1384   g_settings_get (settings, "uint64", "t", &u64);
1385   g_assert_cmpuint (u64, ==, G_MAXUINT64);
1386
1387   g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1388   u64 = 1111;
1389   g_object_get (obj, "uint64", &u64, NULL);
1390   g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1391
1392   g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1393
1394   g_object_set (obj, "string", "bu ba", NULL);
1395   s = g_settings_get_string (settings, "string");
1396   g_assert_cmpstr (s, ==, "bu ba");
1397   g_free (s);
1398
1399   g_settings_set_string (settings, "string", "bla bla");
1400   g_object_get (obj, "string", &s, NULL);
1401   g_assert_cmpstr (s, ==, "bla bla");
1402   g_free (s);
1403
1404   g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1405
1406   g_object_set (obj, "string", "non-unicode:\315", NULL);
1407   value = g_settings_get_value (settings, "chararray");
1408   g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1409   g_variant_unref (value);
1410
1411   g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1412
1413   g_object_set (obj, "double", G_MAXFLOAT, NULL);
1414   g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1415
1416   g_settings_set_double (settings, "double", G_MINFLOAT);
1417   d = 1.0;
1418   g_object_get (obj, "double", &d, NULL);
1419   g_assert_cmpfloat (d, ==, G_MINFLOAT);
1420
1421   g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1422   g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1423
1424   g_settings_set_double (settings, "double", -G_MINDOUBLE);
1425   d = 1.0;
1426   g_object_get (obj, "double", &d, NULL);
1427   g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1428
1429   strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1430   g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1431   g_object_set (obj, "strv", strv, NULL);
1432   g_strfreev (strv);
1433   strv = g_settings_get_strv (settings, "strv");
1434   s = g_strjoinv (",", strv);
1435   g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1436   g_strfreev (strv);
1437   g_free (s);
1438   strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1439   g_settings_set_strv (settings, "strv", (const gchar **) strv);
1440   g_strfreev (strv);
1441   g_object_get (obj, "strv", &strv, NULL);
1442   s = g_strjoinv (",", strv);
1443   g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1444   g_strfreev (strv);
1445   g_free (s);
1446   g_settings_set_strv (settings, "strv", NULL);
1447   g_object_get (obj, "strv", &strv, NULL);
1448   g_assert_nonnull (strv);
1449   g_assert_cmpint (g_strv_length (strv), ==, 0);
1450   g_strfreev (strv);
1451
1452   g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1453   g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1454   s = g_settings_get_string (settings, "enum");
1455   g_assert_cmpstr (s, ==, "baz");
1456   g_free (s);
1457   g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1458
1459   g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1460   i = 230;
1461   g_object_get (obj, "enum", &i, NULL);
1462   g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1463
1464   g_settings_set_string (settings, "enum", "baz");
1465   i = 230;
1466   g_object_get (obj, "enum", &i, NULL);
1467   g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1468
1469   g_settings_bind (settings, "flags", obj, "flags", G_SETTINGS_BIND_DEFAULT);
1470   g_object_set (obj, "flags", TEST_FLAGS_MOURNING, NULL);
1471   strv = g_settings_get_strv (settings, "flags");
1472   g_assert_cmpint (g_strv_length (strv), ==, 1);
1473   g_assert_cmpstr (strv[0], ==, "mourning");
1474   g_strfreev (strv);
1475
1476   g_assert_cmpint (g_settings_get_flags (settings, "flags"), ==, TEST_FLAGS_MOURNING);
1477
1478   g_settings_set_flags (settings, "flags", TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1479   i = 230;
1480   g_object_get (obj, "flags", &i, NULL);
1481   g_assert_cmpint (i, ==, TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1482
1483   g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1484
1485   g_object_set (obj, "uint", 12345, NULL);
1486   g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);
1487
1488   g_settings_set_uint (settings, "uint", 54321);
1489   u = 1111;
1490   g_object_get (obj, "uint", &u, NULL);
1491   g_assert_cmpuint (u, ==, 54321);
1492
1493   g_settings_bind (settings, "range", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1494   g_object_set (obj, "uint", 22, NULL);
1495   u = 1111;
1496   g_assert_cmpuint (g_settings_get_uint (settings, "range"), ==, 22);
1497   g_object_get (obj, "uint", &u, NULL);
1498   g_assert_cmpuint (u, ==, 22);
1499
1500   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1501                          "* is out of schema-specified range for*");
1502   g_object_set (obj, "uint", 45, NULL);
1503   g_test_assert_expected_messages ();
1504   u = 1111;
1505   g_object_get (obj, "uint", &u, NULL);
1506   g_assert_cmpuint (g_settings_get_uint (settings, "range"), ==, 22);
1507   /* The value of the object is currently not reset back to its initial value
1508   g_assert_cmpuint (u, ==, 22); */
1509
1510   g_object_unref (obj);
1511   g_object_unref (settings);
1512 }
1513
1514 static void
1515 test_unbind (void)
1516 {
1517   TestObject *obj;
1518   GSettings *settings;
1519
1520   settings = g_settings_new ("org.gtk.test.binding");
1521   obj = test_object_new ();
1522
1523   g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1524
1525   g_object_set (obj, "int", 12345, NULL);
1526   g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1527
1528   g_settings_unbind (obj, "int");
1529
1530   g_object_set (obj, "int", 54321, NULL);
1531   g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1532
1533   g_object_unref (obj);
1534   g_object_unref (settings);
1535 }
1536
1537 static void
1538 test_bind_writable (void)
1539 {
1540   TestObject *obj;
1541   GSettings *settings;
1542   gboolean b;
1543
1544   settings = g_settings_new ("org.gtk.test.binding");
1545   obj = test_object_new ();
1546
1547   g_object_set (obj, "bool", FALSE, NULL);
1548
1549   g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1550
1551   g_object_get (obj, "bool", &b, NULL);
1552   g_assert_true (b);
1553
1554   g_settings_unbind (obj, "bool");
1555
1556   g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1557
1558   g_object_get (obj, "bool", &b, NULL);
1559   g_assert_false (b);
1560
1561   g_object_unref (obj);
1562   g_object_unref (settings);
1563 }
1564
1565 /* Test one-way bindings.
1566  * Verify that changes on one side show up on the other,
1567  * but not vice versa
1568  */
1569 static void
1570 test_directional_binding (void)
1571 {
1572   TestObject *obj;
1573   GSettings *settings;
1574   gboolean b;
1575   gint i;
1576
1577   settings = g_settings_new ("org.gtk.test.binding");
1578   obj = test_object_new ();
1579
1580   g_object_set (obj, "bool", FALSE, NULL);
1581   g_settings_set_boolean (settings, "bool", FALSE);
1582
1583   g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1584
1585   g_settings_set_boolean (settings, "bool", TRUE);
1586   g_object_get (obj, "bool", &b, NULL);
1587   g_assert_cmpint (b, ==, TRUE);
1588
1589   g_object_set (obj, "bool", FALSE, NULL);
1590   g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1591
1592   g_object_set (obj, "int", 20, NULL);
1593   g_settings_set_int (settings, "int", 20);
1594
1595   g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1596
1597   g_object_set (obj, "int", 32, NULL);
1598   g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1599
1600   g_settings_set_int (settings, "int", 20);
1601   g_object_get (obj, "int", &i, NULL);
1602   g_assert_cmpint (i, ==, 32);
1603
1604   g_object_unref (obj);
1605   g_object_unref (settings);
1606 }
1607
1608 /* Test that type mismatch is caught when creating a binding */
1609 static void
1610 test_typesafe_binding (void)
1611 {
1612   if (!g_test_undefined ())
1613     return;
1614
1615   if (g_test_subprocess ())
1616     {
1617       TestObject *obj;
1618       GSettings *settings;
1619
1620       settings = g_settings_new ("org.gtk.test.binding");
1621       obj = test_object_new ();
1622
1623       g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1624
1625       g_object_unref (obj);
1626       g_object_unref (settings);
1627       return;
1628     }
1629   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
1630   g_test_trap_assert_failed ();
1631   g_test_trap_assert_stderr ("*not compatible*");
1632 }
1633
1634 static gboolean
1635 string_to_bool (GValue   *value,
1636                 GVariant *variant,
1637                 gpointer  user_data)
1638 {
1639   const gchar *s;
1640
1641   s = g_variant_get_string (variant, NULL);
1642   g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1643
1644   return TRUE;
1645 }
1646
1647 static GVariant *
1648 bool_to_string (const GValue       *value,
1649                 const GVariantType *expected_type,
1650                 gpointer            user_data)
1651 {
1652   if (g_value_get_boolean (value))
1653     return g_variant_new_string ("true");
1654   else
1655     return g_variant_new_string ("false");
1656 }
1657
1658 static GVariant *
1659 bool_to_bool (const GValue       *value,
1660               const GVariantType *expected_type,
1661               gpointer            user_data)
1662 {
1663   return g_variant_new_boolean (g_value_get_boolean (value));
1664 }
1665
1666 /* Test custom bindings.
1667  * Translate strings to booleans and back
1668  */
1669 static void
1670 test_custom_binding (void)
1671 {
1672   TestObject *obj;
1673   GSettings *settings;
1674   gchar *s;
1675   gboolean b;
1676
1677   settings = g_settings_new ("org.gtk.test.binding");
1678   obj = test_object_new ();
1679
1680   g_settings_set_string (settings, "string", "true");
1681
1682   g_settings_bind_with_mapping (settings, "string",
1683                                 obj, "bool",
1684                                 G_SETTINGS_BIND_DEFAULT,
1685                                 string_to_bool,
1686                                 bool_to_string,
1687                                 NULL, NULL);
1688
1689   g_settings_set_string (settings, "string", "false");
1690   g_object_get (obj, "bool", &b, NULL);
1691   g_assert_cmpint (b, ==, FALSE);
1692
1693   g_settings_set_string (settings, "string", "not true");
1694   g_object_get (obj, "bool", &b, NULL);
1695   g_assert_cmpint (b, ==, FALSE);
1696
1697   g_object_set (obj, "bool", TRUE, NULL);
1698   s = g_settings_get_string (settings, "string");
1699   g_assert_cmpstr (s, ==, "true");
1700   g_free (s);
1701
1702   g_settings_bind_with_mapping (settings, "string",
1703                                 obj, "bool",
1704                                 G_SETTINGS_BIND_DEFAULT,
1705                                 string_to_bool, bool_to_bool,
1706                                 NULL, NULL);
1707   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1708                          "*binding mapping function for key 'string' returned"
1709                          " GVariant of type 'b' when type 's' was requested*");
1710   g_object_set (obj, "bool", FALSE, NULL);
1711   g_test_assert_expected_messages ();
1712
1713   g_object_unref (obj);
1714   g_object_unref (settings);
1715 }
1716
1717 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1718  * initial settings value is transported to the object
1719  * side, but later settings changes do not affect the
1720  * object
1721  */
1722 static void
1723 test_no_change_binding (void)
1724 {
1725   TestObject *obj;
1726   GSettings *settings;
1727   gboolean b;
1728
1729   settings = g_settings_new ("org.gtk.test.binding");
1730   obj = test_object_new ();
1731
1732   g_object_set (obj, "bool", TRUE, NULL);
1733   g_settings_set_boolean (settings, "bool", FALSE);
1734
1735   g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1736
1737   g_object_get (obj, "bool", &b, NULL);
1738   g_assert_cmpint (b, ==, FALSE);
1739
1740   g_settings_set_boolean (settings, "bool", TRUE);
1741   g_object_get (obj, "bool", &b, NULL);
1742   g_assert_cmpint (b, ==, FALSE);
1743
1744   g_settings_set_boolean (settings, "bool", FALSE);
1745   g_object_set (obj, "bool", TRUE, NULL);
1746   b = g_settings_get_boolean (settings, "bool");
1747   g_assert_cmpint (b, ==, TRUE);
1748
1749   g_object_unref (obj);
1750   g_object_unref (settings);
1751 }
1752
1753 /* Test that binding a non-readable property only
1754  * works in 'GET' mode.
1755  */
1756 static void
1757 test_no_read_binding_fail (void)
1758 {
1759   TestObject *obj;
1760   GSettings *settings;
1761
1762   settings = g_settings_new ("org.gtk.test.binding");
1763   obj = test_object_new ();
1764
1765   g_settings_bind (settings, "string", obj, "no-read", 0);
1766 }
1767
1768 static void
1769 test_no_read_binding_pass (void)
1770 {
1771   TestObject *obj;
1772   GSettings *settings;
1773
1774   settings = g_settings_new ("org.gtk.test.binding");
1775   obj = test_object_new ();
1776
1777   g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1778
1779   exit (0);
1780 }
1781
1782 static void
1783 test_no_read_binding (void)
1784 {
1785   if (g_test_undefined ())
1786     {
1787       g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/fail", 0,
1788                               G_TEST_SUBPROCESS_DEFAULT);
1789       g_test_trap_assert_failed ();
1790       g_test_trap_assert_stderr ("*property*is not readable*");
1791     }
1792
1793   g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/pass", 0,
1794                           G_TEST_SUBPROCESS_DEFAULT);
1795   g_test_trap_assert_passed ();
1796 }
1797
1798 /* Test that binding a non-writable property only
1799  * works in 'SET' mode.
1800  */
1801 static void
1802 test_no_write_binding_fail (void)
1803 {
1804   TestObject *obj;
1805   GSettings *settings;
1806
1807   settings = g_settings_new ("org.gtk.test.binding");
1808   obj = test_object_new ();
1809
1810   g_settings_bind (settings, "string", obj, "no-write", 0);
1811 }
1812
1813 static void
1814 test_no_write_binding_pass (void)
1815 {
1816   TestObject *obj;
1817   GSettings *settings;
1818
1819   settings = g_settings_new ("org.gtk.test.binding");
1820   obj = test_object_new ();
1821
1822   g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1823
1824   exit (0);
1825 }
1826
1827 static void
1828 test_no_write_binding (void)
1829 {
1830   if (g_test_undefined ())
1831     {
1832       g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/fail", 0,
1833                               G_TEST_SUBPROCESS_DEFAULT);
1834       g_test_trap_assert_failed ();
1835       g_test_trap_assert_stderr ("*property*is not writable*");
1836     }
1837
1838   g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/pass", 0,
1839                           G_TEST_SUBPROCESS_DEFAULT);
1840   g_test_trap_assert_passed ();
1841 }
1842
1843 static void
1844 key_changed_cb (GSettings *settings, const gchar *key, gpointer data)
1845 {
1846   gboolean *b = data;
1847   (*b) = TRUE;
1848 }
1849
1850 typedef struct
1851 {
1852   const gchar *path;
1853   const gchar *root_group;
1854   const gchar *keyfile_group;
1855   const gchar *root_path;
1856 } KeyfileTestData;
1857
1858 /*
1859  * Test that using a keyfile works
1860  */
1861 static void
1862 test_keyfile (Fixture       *fixture,
1863               gconstpointer  user_data)
1864 {
1865   GSettingsBackend *kf_backend;
1866   GSettings *settings;
1867   GKeyFile *keyfile;
1868   gchar *str;
1869   gboolean writable;
1870   GError *error = NULL;
1871   gchar *data;
1872   gsize len;
1873   gboolean called = FALSE;
1874   gchar *keyfile_path = NULL, *store_path = NULL;
1875
1876   keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
1877   store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
1878   kf_backend = g_keyfile_settings_backend_new (store_path, "/", "root");
1879   settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1880   g_object_unref (kf_backend);
1881
1882   g_settings_reset (settings, "greeting");
1883   str = g_settings_get_string (settings, "greeting");
1884   g_assert_cmpstr (str, ==, "Hello, earthlings");
1885   g_free (str);
1886
1887   writable = g_settings_is_writable (settings, "greeting");
1888   g_assert_true (writable);
1889   g_settings_set (settings, "greeting", "s", "see if this works");
1890
1891   str = g_settings_get_string (settings, "greeting");
1892   g_assert_cmpstr (str, ==, "see if this works");
1893   g_free (str);
1894
1895   g_settings_delay (settings);
1896   g_settings_set (settings, "farewell", "s", "cheerio");
1897   g_settings_apply (settings);
1898
1899   keyfile = g_key_file_new ();
1900   g_assert_true (g_key_file_load_from_file (keyfile, store_path, 0, NULL));
1901
1902   str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1903   g_assert_cmpstr (str, ==, "'see if this works'");
1904   g_free (str);
1905
1906   str = g_key_file_get_string (keyfile, "tests", "farewell", NULL);
1907   g_assert_cmpstr (str, ==, "'cheerio'");
1908   g_free (str);
1909   g_key_file_free (keyfile);
1910
1911   g_settings_reset (settings, "greeting");
1912   g_settings_apply (settings);
1913   keyfile = g_key_file_new ();
1914   g_assert_true (g_key_file_load_from_file (keyfile, store_path, 0, NULL));
1915
1916   str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1917   g_assert_null (str);
1918
1919   called = FALSE;
1920   g_signal_connect (settings, "changed::greeting", G_CALLBACK (key_changed_cb), &called);
1921
1922   g_key_file_set_string (keyfile, "tests", "greeting", "'howdy'");
1923   data = g_key_file_to_data (keyfile, &len, NULL);
1924   g_file_set_contents (store_path, data, len, &error);
1925   g_assert_no_error (error);
1926   while (!called)
1927     g_main_context_iteration (NULL, FALSE);
1928   g_signal_handlers_disconnect_by_func (settings, key_changed_cb, &called);
1929
1930   str = g_settings_get_string (settings, "greeting");
1931   g_assert_cmpstr (str, ==, "howdy");
1932   g_free (str);
1933
1934   /* Now check setting a string without quotes */
1935   called = FALSE;
1936   g_signal_connect (settings, "changed::greeting", G_CALLBACK (key_changed_cb), &called);
1937
1938   g_key_file_set_string (keyfile, "tests", "greeting", "he\"l🤗uÅ„");
1939   g_free (data);
1940   data = g_key_file_to_data (keyfile, &len, NULL);
1941   g_file_set_contents (store_path, data, len, &error);
1942   g_assert_no_error (error);
1943   while (!called)
1944     g_main_context_iteration (NULL, FALSE);
1945   g_signal_handlers_disconnect_by_func (settings, key_changed_cb, &called);
1946
1947   str = g_settings_get_string (settings, "greeting");
1948   g_assert_cmpstr (str, ==, "he\"l🤗uÅ„");
1949   g_free (str);
1950
1951   g_settings_set (settings, "farewell", "s", "cheerio");
1952
1953   /* Check that empty keys/groups are not allowed. */
1954   g_assert_false (g_settings_is_writable (settings, ""));
1955   g_assert_false (g_settings_is_writable (settings, "/"));
1956
1957   /* When executing as root, changing the mode of the keyfile will have
1958    * no effect on the writability of the settings.
1959    */
1960   if (geteuid () != 0)
1961     {
1962       called = FALSE;
1963       g_signal_connect (settings, "writable-changed::greeting",
1964                         G_CALLBACK (key_changed_cb), &called);
1965
1966       g_assert_no_errno (g_chmod (keyfile_path, 0500));
1967       while (!called)
1968         g_main_context_iteration (NULL, FALSE);
1969       g_signal_handlers_disconnect_by_func (settings, key_changed_cb, &called);
1970
1971       writable = g_settings_is_writable (settings, "greeting");
1972       g_assert_false (writable);
1973     }
1974
1975   g_key_file_free (keyfile);
1976   g_free (data);
1977
1978   g_object_unref (settings);
1979
1980   /* Clean up the temporary directory. */
1981   g_assert_no_errno (g_chmod (keyfile_path, 0777));
1982   g_assert_no_errno (g_remove (store_path));
1983   g_assert_no_errno (g_rmdir (keyfile_path));
1984   g_free (store_path);
1985   g_free (keyfile_path);
1986 }
1987
1988 /*
1989  * Test that using a keyfile works with a schema with no path set.
1990  */
1991 static void
1992 test_keyfile_no_path (Fixture       *fixture,
1993                       gconstpointer  user_data)
1994 {
1995   const KeyfileTestData *test_data = user_data;
1996   GSettingsBackend *kf_backend;
1997   GSettings *settings;
1998   GKeyFile *keyfile;
1999   gboolean writable;
2000   gchar *key = NULL;
2001   GError *error = NULL;
2002   gchar *keyfile_path = NULL, *store_path = NULL;
2003
2004   keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
2005   store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
2006   kf_backend = g_keyfile_settings_backend_new (store_path, test_data->root_path, test_data->root_group);
2007   settings = g_settings_new_with_backend_and_path ("org.gtk.test.no-path", kf_backend, test_data->path);
2008   g_object_unref (kf_backend);
2009
2010   g_settings_reset (settings, "test-boolean");
2011   g_assert_true (g_settings_get_boolean (settings, "test-boolean"));
2012
2013   writable = g_settings_is_writable (settings, "test-boolean");
2014   g_assert_true (writable);
2015   g_settings_set (settings, "test-boolean", "b", FALSE);
2016
2017   g_assert_false (g_settings_get_boolean (settings, "test-boolean"));
2018
2019   g_settings_delay (settings);
2020   g_settings_set (settings, "test-boolean", "b", TRUE);
2021   g_settings_apply (settings);
2022
2023   keyfile = g_key_file_new ();
2024   g_assert_true (g_key_file_load_from_file (keyfile, store_path, 0, NULL));
2025
2026   g_assert_true (g_key_file_get_boolean (keyfile, test_data->keyfile_group, "test-boolean", NULL));
2027
2028   g_key_file_free (keyfile);
2029
2030   g_settings_reset (settings, "test-boolean");
2031   g_settings_apply (settings);
2032   keyfile = g_key_file_new ();
2033   g_assert_true (g_key_file_load_from_file (keyfile, store_path, 0, NULL));
2034
2035   g_assert_false (g_key_file_get_string (keyfile, test_data->keyfile_group, "test-boolean", &error));
2036   g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
2037   g_clear_error (&error);
2038
2039   /* Check that empty keys/groups are not allowed. */
2040   g_assert_false (g_settings_is_writable (settings, ""));
2041   g_assert_false (g_settings_is_writable (settings, "/"));
2042
2043   /* Keys which ghost the root group name are not allowed. This can only be
2044    * tested when the path is `/` as otherwise it acts as a prefix and prevents
2045    * any ghosting. */
2046   if (g_str_equal (test_data->path, "/"))
2047     {
2048       key = g_strdup_printf ("%s/%s", test_data->root_group, "");
2049       g_assert_false (g_settings_is_writable (settings, key));
2050       g_free (key);
2051
2052       key = g_strdup_printf ("%s/%s", test_data->root_group, "/");
2053       g_assert_false (g_settings_is_writable (settings, key));
2054       g_free (key);
2055
2056       key = g_strdup_printf ("%s/%s", test_data->root_group, "test-boolean");
2057       g_assert_false (g_settings_is_writable (settings, key));
2058       g_free (key);
2059     }
2060
2061   g_key_file_free (keyfile);
2062   g_object_unref (settings);
2063
2064   /* Clean up the temporary directory. */
2065   g_assert_no_errno (g_chmod (keyfile_path, 0777));
2066   g_assert_no_errno (g_remove (store_path));
2067   g_assert_no_errno (g_rmdir (keyfile_path));
2068   g_free (store_path);
2069   g_free (keyfile_path);
2070 }
2071
2072 /*
2073  * Test that a keyfile rejects writes to keys outside its root path.
2074  */
2075 static void
2076 test_keyfile_outside_root_path (Fixture       *fixture,
2077                                 gconstpointer  user_data)
2078 {
2079   GSettingsBackend *kf_backend;
2080   GSettings *settings;
2081   gchar *keyfile_path = NULL, *store_path = NULL;
2082
2083   keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
2084   store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
2085   kf_backend = g_keyfile_settings_backend_new (store_path, "/tests/basic-types/", "root");
2086   settings = g_settings_new_with_backend_and_path ("org.gtk.test.no-path", kf_backend, "/tests/");
2087   g_object_unref (kf_backend);
2088
2089   g_assert_false (g_settings_is_writable (settings, "test-boolean"));
2090
2091   g_object_unref (settings);
2092
2093   /* Clean up the temporary directory. The keyfile probably doesn’t exist, so
2094    * don’t error on failure. */
2095   g_remove (store_path);
2096   g_assert_no_errno (g_rmdir (keyfile_path));
2097   g_free (store_path);
2098   g_free (keyfile_path);
2099 }
2100
2101 /*
2102  * Test that a keyfile rejects writes to keys in the root if no root group is set.
2103  */
2104 static void
2105 test_keyfile_no_root_group (Fixture       *fixture,
2106                             gconstpointer  user_data)
2107 {
2108   GSettingsBackend *kf_backend;
2109   GSettings *settings;
2110   gchar *keyfile_path = NULL, *store_path = NULL;
2111
2112   keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
2113   store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
2114   kf_backend = g_keyfile_settings_backend_new (store_path, "/", NULL);
2115   settings = g_settings_new_with_backend_and_path ("org.gtk.test.no-path", kf_backend, "/");
2116   g_object_unref (kf_backend);
2117
2118   g_assert_false (g_settings_is_writable (settings, "test-boolean"));
2119   g_assert_true (g_settings_is_writable (settings, "child/test-boolean"));
2120
2121   g_object_unref (settings);
2122
2123   /* Clean up the temporary directory. The keyfile probably doesn’t exist, so
2124    * don’t error on failure. */
2125   g_remove (store_path);
2126   g_assert_no_errno (g_rmdir (keyfile_path));
2127   g_free (store_path);
2128   g_free (keyfile_path);
2129 }
2130
2131 /* Test that getting child schemas works
2132  */
2133 static void
2134 test_child_schema (void)
2135 {
2136   GSettings *settings;
2137   GSettings *child;
2138   guint8 byte;
2139
2140   /* first establish some known conditions */
2141   settings = g_settings_new ("org.gtk.test.basic-types");
2142   g_settings_set (settings, "test-byte", "y", 36);
2143
2144   g_settings_get (settings, "test-byte", "y", &byte);
2145   g_assert_cmpint (byte, ==, 36);
2146
2147   g_object_unref (settings);
2148
2149   settings = g_settings_new ("org.gtk.test");
2150   child = g_settings_get_child (settings, "basic-types");
2151   g_assert_nonnull (child);
2152
2153   g_settings_get (child, "test-byte", "y", &byte);
2154   g_assert_cmpint (byte, ==, 36);
2155
2156   g_object_unref (child);
2157   g_object_unref (settings);
2158 }
2159
2160 #include "../strinfo.c"
2161
2162 static void
2163 test_strinfo (void)
2164 {
2165   /*  "foo" has a value of 1
2166    *  "bar" has a value of 2
2167    *  "baz" is an alias for "bar"
2168    */
2169   gchar array[] =
2170     "\1\0\0\0"      "\xff""foo"     "\0\0\0\xff"    "\2\0\0\0"
2171     "\xff" "bar"    "\0\0\0\xff"    "\3\0\0\0"      "\xfe""baz"
2172     "\0\0\0\xff";
2173   const guint32 *strinfo = (guint32 *) array;
2174   guint length = sizeof array / 4;
2175   guint result = 0;
2176
2177   {
2178     /* build it and compare */
2179     GString *builder;
2180
2181     builder = g_string_new (NULL);
2182     strinfo_builder_append_item (builder, "foo", 1);
2183     strinfo_builder_append_item (builder, "bar", 2);
2184     g_assert_true (strinfo_builder_append_alias (builder, "baz", "bar"));
2185     g_assert_cmpmem (builder->str, builder->len, strinfo, length * 4);
2186     g_string_free (builder, TRUE);
2187   }
2188
2189   g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
2190                    ==, NULL);
2191   g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
2192                    ==, NULL);
2193   g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
2194                    ==, "bar");
2195   g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
2196                    ==, NULL);
2197
2198   g_assert_true (strinfo_enum_from_string (strinfo, length, "foo", &result));
2199   g_assert_cmpint (result, ==, 1);
2200   g_assert_true (strinfo_enum_from_string (strinfo, length, "bar", &result));
2201   g_assert_cmpint (result, ==, 2);
2202   g_assert_false (strinfo_enum_from_string (strinfo, length, "baz", &result));
2203   g_assert_false (strinfo_enum_from_string (strinfo, length, "quux", &result));
2204
2205   g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
2206   g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
2207   g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
2208   g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
2209
2210   g_assert_true (strinfo_is_string_valid (strinfo, length, "foo"));
2211   g_assert_true (strinfo_is_string_valid (strinfo, length, "bar"));
2212   g_assert_false (strinfo_is_string_valid (strinfo, length, "baz"));
2213   g_assert_false (strinfo_is_string_valid (strinfo, length, "quux"));
2214 }
2215
2216 static void
2217 test_enums_non_enum_key (void)
2218 {
2219   GSettings *direct;
2220
2221   direct = g_settings_new ("org.gtk.test.enums.direct");
2222   g_settings_get_enum (direct, "test");
2223   g_assert_not_reached ();
2224 }
2225
2226 static void
2227 test_enums_non_enum_value (void)
2228 {
2229   GSettings *settings;
2230
2231   settings = g_settings_new ("org.gtk.test.enums");
2232   g_settings_set_enum (settings, "test", 42);
2233   g_assert_not_reached ();
2234 }
2235
2236 static void
2237 test_enums_range (void)
2238 {
2239   GSettings *settings;
2240
2241   settings = g_settings_new ("org.gtk.test.enums");
2242   g_settings_set_string (settings, "test", "qux");
2243   g_assert_not_reached ();
2244 }
2245
2246 static void
2247 test_enums_non_flags (void)
2248 {
2249   GSettings *settings;
2250
2251   settings = g_settings_new ("org.gtk.test.enums");
2252   g_settings_get_flags (settings, "test");
2253   g_assert_not_reached ();
2254 }
2255
2256 static void
2257 test_enums (void)
2258 {
2259   GSettings *settings, *direct;
2260   gchar *str;
2261
2262   settings = g_settings_new ("org.gtk.test.enums");
2263   direct = g_settings_new ("org.gtk.test.enums.direct");
2264
2265   if (g_test_undefined () && !backend_set)
2266     {
2267       g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-key", 0,
2268                               G_TEST_SUBPROCESS_DEFAULT);
2269       g_test_trap_assert_failed ();
2270       g_test_trap_assert_stderr ("*not associated with an enum*");
2271
2272       g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-value", 0,
2273                               G_TEST_SUBPROCESS_DEFAULT);
2274       g_test_trap_assert_failed ();
2275       g_test_trap_assert_stderr ("*invalid enum value 42*");
2276
2277       g_test_trap_subprocess ("/gsettings/enums/subprocess/range", 0,
2278                               G_TEST_SUBPROCESS_DEFAULT);
2279       g_test_trap_assert_failed ();
2280       g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2281
2282       g_test_trap_subprocess ("/gsettings/enums/subprocess/non-flags", 0,
2283                               G_TEST_SUBPROCESS_DEFAULT);
2284       g_test_trap_assert_failed ();
2285       g_test_trap_assert_stderr ("*not associated with a flags*");
2286     }
2287
2288   str = g_settings_get_string (settings, "test");
2289   g_assert_cmpstr (str, ==, "bar");
2290   g_free (str);
2291
2292   g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
2293
2294   str = g_settings_get_string (settings, "test");
2295   g_assert_cmpstr (str, ==, "foo");
2296   g_free (str);
2297
2298   g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
2299
2300   g_settings_set_string (direct, "test", "qux");
2301
2302   str = g_settings_get_string (direct, "test");
2303   g_assert_cmpstr (str, ==, "qux");
2304   g_free (str);
2305
2306   str = g_settings_get_string (settings, "test");
2307   g_assert_cmpstr (str, ==, "quux");
2308   g_free (str);
2309
2310   g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
2311
2312   g_object_unref (direct);
2313   g_object_unref (settings);
2314 }
2315
2316 static void
2317 test_flags_non_flags_key (void)
2318 {
2319   GSettings *direct;
2320
2321   direct = g_settings_new ("org.gtk.test.enums.direct");
2322   g_settings_get_flags (direct, "test");
2323   g_assert_not_reached ();
2324 }
2325
2326 static void
2327 test_flags_non_flags_value (void)
2328 {
2329   GSettings *settings;
2330
2331   settings = g_settings_new ("org.gtk.test.enums");
2332   g_settings_set_flags (settings, "f-test", 0x42);
2333   g_assert_not_reached ();
2334 }
2335
2336 static void
2337 test_flags_range (void)
2338 {
2339   GSettings *settings;
2340
2341   settings = g_settings_new ("org.gtk.test.enums");
2342   g_settings_set_strv (settings, "f-test",
2343                        (const gchar **) g_strsplit ("rock", ",", 0));
2344   g_assert_not_reached ();
2345 }
2346
2347 static void
2348 test_flags_non_enum (void)
2349 {
2350   GSettings *settings;
2351
2352   settings = g_settings_new ("org.gtk.test.enums");
2353   g_settings_get_enum (settings, "f-test");
2354   g_assert_not_reached ();
2355 }
2356
2357 static void
2358 test_flags (void)
2359 {
2360   GSettings *settings, *direct;
2361   gchar **strv;
2362   gchar *str;
2363
2364   settings = g_settings_new ("org.gtk.test.enums");
2365   direct = g_settings_new ("org.gtk.test.enums.direct");
2366
2367   if (g_test_undefined () && !backend_set)
2368     {
2369       g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-key", 0,
2370                               G_TEST_SUBPROCESS_DEFAULT);
2371       g_test_trap_assert_failed ();
2372       g_test_trap_assert_stderr ("*not associated with a flags*");
2373
2374       g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-value", 0,
2375                               G_TEST_SUBPROCESS_DEFAULT);
2376       g_test_trap_assert_failed ();
2377       g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
2378
2379       g_test_trap_subprocess ("/gsettings/flags/subprocess/range", 0,
2380                               G_TEST_SUBPROCESS_DEFAULT);
2381       g_test_trap_assert_failed ();
2382       g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2383
2384       g_test_trap_subprocess ("/gsettings/flags/subprocess/non-enum", 0,
2385                               G_TEST_SUBPROCESS_DEFAULT);
2386       g_test_trap_assert_failed ();
2387       g_test_trap_assert_stderr ("*not associated with an enum*");
2388     }
2389
2390   strv = g_settings_get_strv (settings, "f-test");
2391   str = g_strjoinv (",", strv);
2392   g_assert_cmpstr (str, ==, "");
2393   g_strfreev (strv);
2394   g_free (str);
2395
2396   g_settings_set_flags (settings, "f-test",
2397                         TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
2398
2399   strv = g_settings_get_strv (settings, "f-test");
2400   str = g_strjoinv (",", strv);
2401   g_assert_cmpstr (str, ==, "talking,walking");
2402   g_strfreev (strv);
2403   g_free (str);
2404
2405   g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
2406                    TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
2407
2408   strv = g_strsplit ("speaking,laughing", ",", 0);
2409   g_settings_set_strv (direct, "f-test", (const gchar **) strv);
2410   g_strfreev (strv);
2411
2412   strv = g_settings_get_strv (direct, "f-test");
2413   str = g_strjoinv (",", strv);
2414   g_assert_cmpstr (str, ==, "speaking,laughing");
2415   g_strfreev (strv);
2416   g_free (str);
2417
2418   strv = g_settings_get_strv (settings, "f-test");
2419   str = g_strjoinv (",", strv);
2420   g_assert_cmpstr (str, ==, "talking,laughing");
2421   g_strfreev (strv);
2422   g_free (str);
2423
2424   g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
2425                    TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
2426
2427   g_object_unref (direct);
2428   g_object_unref (settings);
2429 }
2430
2431 static void
2432 test_range_high (void)
2433 {
2434   GSettings *settings;
2435
2436   settings = g_settings_new ("org.gtk.test.range");
2437   g_settings_set_int (settings, "val", 45);
2438   g_assert_not_reached ();
2439 }
2440
2441 static void
2442 test_range_low (void)
2443 {
2444   GSettings *settings;
2445
2446   settings = g_settings_new ("org.gtk.test.range");
2447   g_settings_set_int (settings, "val", 1);
2448   g_assert_not_reached ();
2449 }
2450
2451 static void
2452 test_range (void)
2453 {
2454   GSettings *settings, *direct;
2455   GVariant *value;
2456
2457   settings = g_settings_new ("org.gtk.test.range");
2458   direct = g_settings_new ("org.gtk.test.range.direct");
2459
2460   if (g_test_undefined () && !backend_set)
2461     {
2462       g_test_trap_subprocess ("/gsettings/range/subprocess/high", 0,
2463                               G_TEST_SUBPROCESS_DEFAULT);
2464       g_test_trap_assert_failed ();
2465       g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2466
2467       g_test_trap_subprocess ("/gsettings/range/subprocess/low", 0,
2468                               G_TEST_SUBPROCESS_DEFAULT);
2469       g_test_trap_assert_failed ();
2470       g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2471     }
2472
2473   g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2474   g_settings_set_int (direct, "val", 22);
2475   g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
2476   g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
2477   g_settings_set_int (direct, "val", 45);
2478   g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
2479   g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2480   g_settings_set_int (direct, "val", 1);
2481   g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
2482   g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2483
2484 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2485   value = g_variant_new_int32 (1);
2486   g_assert_false (g_settings_range_check (settings, "val", value));
2487   g_variant_unref (value);
2488   value = g_variant_new_int32 (33);
2489   g_assert_true (g_settings_range_check (settings, "val", value));
2490   g_variant_unref (value);
2491   value = g_variant_new_int32 (45);
2492   g_assert_false (g_settings_range_check (settings, "val", value));
2493   g_variant_unref (value);
2494 G_GNUC_END_IGNORE_DEPRECATIONS
2495
2496   g_object_unref (direct);
2497   g_object_unref (settings);
2498 }
2499
2500 static gboolean
2501 strv_set_equal (const gchar * const *strv, ...)
2502 {
2503   gsize count;
2504   va_list list;
2505   const gchar *str;
2506   gboolean res;
2507
2508   res = TRUE;
2509   count = 0;
2510   va_start (list, strv);
2511   while (1)
2512     {
2513       str = va_arg (list, const gchar *);
2514       if (str == NULL)
2515         break;
2516       if (!g_strv_contains (strv, str))
2517         {
2518           res = FALSE;
2519           break;
2520         }
2521       count++;
2522     }
2523   va_end (list);
2524
2525   if (res)
2526     res = g_strv_length ((gchar**)strv) == count;
2527
2528   return res;
2529 }
2530
2531 static void
2532 test_list_items (void)
2533 {
2534   GSettingsSchema *schema;
2535   GSettings *settings;
2536   gchar **children;
2537   gchar **keys;
2538
2539   settings = g_settings_new ("org.gtk.test");
2540   g_object_get (settings, "settings-schema", &schema, NULL);
2541   children = g_settings_list_children (settings);
2542   keys = g_settings_schema_list_keys (schema);
2543
2544   g_assert_true (strv_set_equal ((const gchar * const *) children, "basic-types", "complex-types", "localized", NULL));
2545   g_assert_true (strv_set_equal ((const gchar * const *) keys, "greeting", "farewell", NULL));
2546
2547   g_strfreev (children);
2548   g_strfreev (keys);
2549
2550   g_settings_schema_unref (schema);
2551   g_object_unref (settings);
2552 }
2553
2554 static void
2555 test_list_schemas (void)
2556 {
2557   const gchar * const *schemas;
2558   const gchar * const *relocs;
2559
2560 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2561   relocs = g_settings_list_relocatable_schemas ();
2562   schemas = g_settings_list_schemas ();
2563 G_GNUC_END_IGNORE_DEPRECATIONS
2564
2565   g_assert_true (strv_set_equal (relocs,
2566                                  "org.gtk.test.no-path",
2567                                  "org.gtk.test.extends.base",
2568                                  "org.gtk.test.extends.extended",
2569                                  NULL));
2570
2571   g_assert_true (strv_set_equal (schemas,
2572                                  "org.gtk.test",
2573                                  "org.gtk.test.basic-types",
2574                                  "org.gtk.test.complex-types",
2575                                  "org.gtk.test.localized",
2576                                  "org.gtk.test.binding",
2577                                  "org.gtk.test.enums",
2578                                  "org.gtk.test.enums.direct",
2579                                  "org.gtk.test.range",
2580                                  "org.gtk.test.range.direct",
2581                                  "org.gtk.test.mapped",
2582                                  "org.gtk.test.descriptions",
2583                                  "org.gtk.test.per-desktop",
2584                                  NULL));
2585 }
2586
2587 static gboolean
2588 map_func (GVariant *value,
2589           gpointer *result,
2590           gpointer  user_data)
2591 {
2592   gint *state = user_data;
2593   gint v;
2594
2595   if (value)
2596     v = g_variant_get_int32 (value);
2597   else
2598     v = -1;
2599
2600   if (*state == 0)
2601     {
2602       g_assert_cmpint (v, ==, 1);
2603       (*state)++;
2604       return FALSE;
2605     }
2606   else if (*state == 1)
2607     {
2608       g_assert_cmpint (v, ==, 0);
2609       (*state)++;
2610       return FALSE;
2611     }
2612   else
2613     {
2614       g_assert_null (value);
2615       *result = g_variant_new_int32 (5);
2616       return TRUE;
2617     }
2618 }
2619
2620 static void
2621 test_get_mapped (void)
2622 {
2623   GSettings *settings;
2624   gint state;
2625   gpointer p;
2626   gint val;
2627
2628   settings = g_settings_new ("org.gtk.test.mapped");
2629   g_settings_set_int (settings, "val", 1);
2630
2631   state = 0;
2632   p = g_settings_get_mapped (settings, "val", map_func, &state);
2633   val = g_variant_get_int32 ((GVariant*)p);
2634   g_assert_cmpint (val, ==, 5);
2635
2636   g_variant_unref (p);
2637   g_object_unref (settings);
2638 }
2639
2640 static void
2641 test_get_range (void)
2642 {
2643   GSettings *settings;
2644   GVariant *range;
2645
2646 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2647   settings = g_settings_new ("org.gtk.test.range");
2648   range = g_settings_get_range (settings, "val");
2649   check_and_free (range, "('range', <(2, 44)>)");
2650   g_object_unref (settings);
2651
2652   settings = g_settings_new ("org.gtk.test.enums");
2653   range = g_settings_get_range (settings, "test");
2654   check_and_free (range, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
2655   g_object_unref (settings);
2656
2657   settings = g_settings_new ("org.gtk.test.enums");
2658   range = g_settings_get_range (settings, "f-test");
2659   check_and_free (range, "('flags', "
2660                   "<['mourning', 'laughing', 'talking', 'walking']>)");
2661   g_object_unref (settings);
2662
2663   settings = g_settings_new ("org.gtk.test");
2664   range = g_settings_get_range (settings, "greeting");
2665   check_and_free (range, "('type', <@as []>)");
2666   g_object_unref (settings);
2667 G_GNUC_END_IGNORE_DEPRECATIONS
2668 }
2669
2670 static void
2671 test_schema_source (void)
2672 {
2673   GSettingsSchemaSource *parent;
2674   GSettingsSchemaSource *source;
2675   GSettingsBackend *backend;
2676   GSettingsSchema *schema;
2677   GError *error = NULL;
2678   GSettings *settings, *child;
2679   gboolean enabled;
2680
2681   backend = g_settings_backend_get_default ();
2682
2683   /* make sure it fails properly */
2684   parent = g_settings_schema_source_get_default ();
2685   source = g_settings_schema_source_new_from_directory ("/path/that/does/not/exist", parent,  TRUE, &error);
2686   g_assert_null (source);
2687   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
2688   g_clear_error (&error);
2689
2690   /* Test error handling of corrupt compiled files. */
2691   source = g_settings_schema_source_new_from_directory ("schema-source-corrupt", parent, TRUE, &error);
2692   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
2693   g_assert_null (source);
2694   g_clear_error (&error);
2695
2696   /* Test error handling of empty compiled files. */
2697   source = g_settings_schema_source_new_from_directory ("schema-source-empty", parent, TRUE, &error);
2698   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
2699   g_assert_null (source);
2700   g_clear_error (&error);
2701
2702   /* create a source with the parent */
2703   source = g_settings_schema_source_new_from_directory ("schema-source", parent, TRUE, &error);
2704   g_assert_no_error (error);
2705   g_assert_nonnull (source);
2706
2707   /* check recursive lookups are working */
2708   schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2709   g_assert_nonnull (schema);
2710   g_settings_schema_unref (schema);
2711
2712   /* check recursive lookups for non-existent schemas */
2713   schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", TRUE);
2714   g_assert_null (schema);
2715
2716   /* check non-recursive for schema that only exists in lower layers */
2717   schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2718   g_assert_null (schema);
2719
2720   /* check non-recursive lookup for non-existent */
2721   schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", FALSE);
2722   g_assert_null (schema);
2723
2724   /* check non-recursive for schema that exists in toplevel */
2725   schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2726   g_assert_nonnull (schema);
2727   g_settings_schema_unref (schema);
2728
2729   /* check recursive for schema that exists in toplevel */
2730   schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2731   g_assert_nonnull (schema);
2732
2733   /* try to use it for something */
2734   settings = g_settings_new_full (schema, backend, "/test/");
2735   g_settings_schema_unref (schema);
2736   enabled = FALSE;
2737   g_settings_get (settings, "enabled", "b", &enabled);
2738   g_assert_true (enabled);
2739
2740   /* Check that child schemas are resolved from the correct schema source, see glib#1884 */
2741   child = g_settings_get_child (settings, "child");
2742   g_settings_get (settings, "enabled", "b", &enabled);
2743
2744   g_object_unref (child);
2745   g_object_unref (settings);
2746   g_settings_schema_source_unref (source);
2747
2748   /* try again, but with no parent */
2749   source = g_settings_schema_source_new_from_directory ("schema-source", NULL, FALSE, NULL);
2750   g_assert_nonnull (source);
2751
2752   /* should not find it this time, even if recursive... */
2753   schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2754   g_assert_null (schema);
2755   schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2756   g_assert_null (schema);
2757
2758   /* should still find our own... */
2759   schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2760   g_assert_nonnull (schema);
2761   g_settings_schema_unref (schema);
2762   schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2763   g_assert_nonnull (schema);
2764   g_settings_schema_unref (schema);
2765
2766   g_settings_schema_source_unref (source);
2767   g_object_unref (backend);
2768 }
2769
2770 static void
2771 test_schema_list_keys (void)
2772 {
2773   gchar                 **keys;
2774   GSettingsSchemaSource  *src    = g_settings_schema_source_get_default ();
2775   GSettingsSchema        *schema = g_settings_schema_source_lookup (src, "org.gtk.test", TRUE);
2776   g_assert_nonnull (schema);
2777
2778   keys = g_settings_schema_list_keys (schema);
2779
2780   g_assert_true (strv_set_equal ((const gchar * const *) keys,
2781                                  "greeting",
2782                                  "farewell",
2783                                  NULL));
2784
2785   g_strfreev (keys);
2786   g_settings_schema_unref (schema);
2787 }
2788
2789 static void
2790 test_actions (void)
2791 {
2792   GAction *string, *toggle;
2793   gboolean c1, c2, c3;
2794   GSettings *settings;
2795   gchar *name;
2796   GVariantType *param_type;
2797   gboolean enabled;
2798   GVariantType *state_type;
2799   GVariant *state;
2800
2801   settings = g_settings_new ("org.gtk.test.basic-types");
2802   string = g_settings_create_action (settings, "test-string");
2803   toggle = g_settings_create_action (settings, "test-boolean");
2804   g_object_unref (settings); /* should be held by the actions */
2805
2806   g_signal_connect (settings, "changed", G_CALLBACK (changed_cb2), &c1);
2807   g_signal_connect (string, "notify::state", G_CALLBACK (changed_cb2), &c2);
2808   g_signal_connect (toggle, "notify::state", G_CALLBACK (changed_cb2), &c3);
2809
2810   c1 = c2 = c3 = FALSE;
2811   g_settings_set_string (settings, "test-string", "hello world");
2812   check_and_free (g_action_get_state (string), "'hello world'");
2813   g_assert_true (c1 && c2 && !c3);
2814   c1 = c2 = c3 = FALSE;
2815
2816   g_action_activate (string, g_variant_new_string ("hihi"));
2817   check_and_free (g_settings_get_value (settings, "test-string"), "'hihi'");
2818   g_assert_true (c1 && c2 && !c3);
2819   c1 = c2 = c3 = FALSE;
2820
2821   g_action_change_state (string, g_variant_new_string ("kthxbye"));
2822   check_and_free (g_settings_get_value (settings, "test-string"), "'kthxbye'");
2823   g_assert_true (c1 && c2 && !c3);
2824   c1 = c2 = c3 = FALSE;
2825
2826   g_action_change_state (toggle, g_variant_new_boolean (TRUE));
2827   g_assert_true (g_settings_get_boolean (settings, "test-boolean"));
2828   g_assert_true (c1 && !c2 && c3);
2829   c1 = c2 = c3 = FALSE;
2830
2831   g_action_activate (toggle, NULL);
2832   g_assert_false (g_settings_get_boolean (settings, "test-boolean"));
2833   g_assert_true (c1 && !c2 && c3);
2834
2835   g_object_get (string,
2836                 "name", &name,
2837                 "parameter-type", &param_type,
2838                 "enabled", &enabled,
2839                 "state-type", &state_type,
2840                 "state", &state,
2841                 NULL);
2842
2843   g_assert_cmpstr (name, ==, "test-string");
2844   g_assert_true (g_variant_type_equal (param_type, G_VARIANT_TYPE_STRING));
2845   g_assert_true (enabled);
2846   g_assert_true (g_variant_type_equal (state_type, G_VARIANT_TYPE_STRING));
2847   g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "kthxbye");
2848
2849   g_free (name);
2850   g_variant_type_free (param_type);
2851   g_variant_type_free (state_type);
2852   g_variant_unref (state);
2853
2854   g_object_unref (string);
2855   g_object_unref (toggle);
2856 }
2857
2858 static void
2859 test_null_backend (void)
2860 {
2861   GSettingsBackend *backend;
2862   GSettings *settings;
2863   gchar *str;
2864   gboolean writable;
2865
2866   backend = g_null_settings_backend_new ();
2867   settings = g_settings_new_with_backend_and_path ("org.gtk.test", backend, "/tests/");
2868
2869   g_object_get (settings, "schema-id", &str, NULL);
2870   g_assert_cmpstr (str, ==, "org.gtk.test");
2871   g_free (str);
2872
2873   settings_assert_cmpstr (settings, "greeting", ==, "Hello, earthlings");
2874
2875   g_settings_set (settings, "greeting", "s", "goodbye world");
2876   settings_assert_cmpstr (settings, "greeting", ==, "Hello, earthlings");
2877
2878   writable = g_settings_is_writable (settings, "greeting");
2879   g_assert_false (writable);
2880
2881   g_settings_reset (settings, "greeting");
2882
2883   g_settings_delay (settings);
2884   g_settings_set (settings, "greeting", "s", "goodbye world");
2885   g_settings_apply (settings);
2886   settings_assert_cmpstr (settings, "greeting", ==, "Hello, earthlings");
2887
2888   g_object_unref (settings);
2889   g_object_unref (backend);
2890 }
2891
2892 static void
2893 test_memory_backend (void)
2894 {
2895   GSettingsBackend *backend;
2896
2897   backend = g_memory_settings_backend_new ();
2898   g_assert_true (G_IS_SETTINGS_BACKEND (backend));
2899   g_object_unref (backend);
2900 }
2901
2902 static void
2903 test_read_descriptions (void)
2904 {
2905   GSettingsSchema *schema;
2906   GSettingsSchemaKey *key;
2907   GSettings *settings;
2908
2909   settings = g_settings_new ("org.gtk.test");
2910   g_object_get (settings, "settings-schema", &schema, NULL);
2911   key = g_settings_schema_get_key (schema, "greeting");
2912
2913   g_assert_cmpstr (g_settings_schema_key_get_summary (key), ==, "A greeting");
2914   g_assert_cmpstr (g_settings_schema_key_get_description (key), ==, "Greeting of the invading martians");
2915
2916   g_settings_schema_key_unref (key);
2917   g_settings_schema_unref (schema);
2918
2919   g_object_unref (settings);
2920
2921   settings = g_settings_new ("org.gtk.test.descriptions");
2922   g_object_get (settings, "settings-schema", &schema, NULL);
2923   key = g_settings_schema_get_key (schema, "a");
2924
2925   g_assert_cmpstr (g_settings_schema_key_get_summary (key), ==,
2926                    "a paragraph.\n\n"
2927                    "with some whitespace.\n\n"
2928                    "because not everyone has a great editor.\n\n"
2929                    "lots of space is as one.");
2930
2931   g_settings_schema_key_unref (key);
2932   g_settings_schema_unref (schema);
2933
2934   g_object_unref (settings);
2935 }
2936
2937 static void
2938 test_default_value (void)
2939 {
2940   GSettings *settings;
2941   GSettingsSchema *schema;
2942   GSettingsSchemaKey *key;
2943   GVariant *v;
2944   const gchar *str;
2945   gchar *s;
2946
2947   settings = g_settings_new ("org.gtk.test");
2948   g_object_get (settings, "settings-schema", &schema, NULL);
2949   key = g_settings_schema_get_key (schema, "greeting");
2950   g_settings_schema_unref (schema);
2951   g_settings_schema_key_ref (key);
2952
2953   g_assert_true (g_variant_type_equal (g_settings_schema_key_get_value_type (key), G_VARIANT_TYPE_STRING));
2954
2955   v = g_settings_schema_key_get_default_value (key);
2956   str = g_variant_get_string (v, NULL);
2957   g_assert_cmpstr (str, ==, "Hello, earthlings");
2958   g_variant_unref (v);
2959
2960   g_settings_schema_key_unref (key);
2961   g_settings_schema_key_unref (key);
2962
2963   g_settings_set (settings, "greeting", "s", "goodbye world");
2964
2965   v = g_settings_get_user_value (settings, "greeting");
2966   str = g_variant_get_string (v, NULL);
2967   g_assert_cmpstr (str, ==, "goodbye world");
2968   g_variant_unref (v);
2969
2970   v = g_settings_get_default_value (settings, "greeting");
2971   str = g_variant_get_string (v, NULL);
2972   g_assert_cmpstr (str, ==, "Hello, earthlings");
2973   g_variant_unref (v);
2974
2975   g_settings_reset (settings, "greeting");
2976
2977   v = g_settings_get_user_value (settings, "greeting");
2978   g_assert_null (v);
2979
2980   s = g_settings_get_string (settings, "greeting");
2981   g_assert_cmpstr (s, ==, "Hello, earthlings");
2982   g_free (s);
2983
2984   g_object_unref (settings);
2985 }
2986
2987 static gboolean
2988 string_map_func (GVariant *value,
2989                  gpointer *result,
2990                  gpointer  user_data)
2991 {
2992   const gchar *str;
2993
2994   str = g_variant_get_string (value, NULL);
2995   *result = g_variant_new_string (str);
2996
2997   return TRUE;
2998 }
2999
3000 /* Test that per-desktop values from org.gtk.test.gschema.override
3001  * does not change default value if current desktop is not listed in
3002  * $XDG_CURRENT_DESKTOP.
3003  */
3004 static void
3005 test_per_desktop (void)
3006 {
3007   GSettings *settings;
3008   GAction *action_string;
3009   TestObject *obj;
3010   gpointer p;
3011   gchar *str;
3012
3013   settings = g_settings_new ("org.gtk.test.per-desktop");
3014   obj = test_object_new ();
3015
3016   if (!g_test_subprocess ())
3017     {
3018       g_test_trap_subprocess ("/gsettings/per-desktop/subprocess", 0,
3019                               G_TEST_SUBPROCESS_DEFAULT);
3020       g_test_trap_assert_passed ();
3021     }
3022
3023   str = g_settings_get_string (settings, "desktop");
3024   g_assert_cmpstr (str, ==, "GNOME");
3025   g_free (str);
3026
3027   p = g_settings_get_mapped (settings, "desktop", string_map_func, NULL);
3028
3029   str = g_variant_dup_string (p, NULL);
3030   g_assert_cmpstr (str, ==, "GNOME");
3031   g_free (str);
3032
3033   g_variant_unref (p);
3034
3035   g_settings_bind (settings, "desktop", obj, "string", G_SETTINGS_BIND_DEFAULT);
3036
3037   g_object_get (obj, "string", &str, NULL);
3038   g_assert_cmpstr (str, ==, "GNOME");
3039   g_free (str);
3040
3041   action_string = g_settings_create_action (settings, "desktop");
3042   check_and_free (g_action_get_state (action_string), "'GNOME'");
3043
3044   g_clear_object (&action_string);
3045   g_object_unref (settings);
3046   g_object_unref (obj);
3047 }
3048
3049 /* Test that per-desktop values from org.gtk.test.gschema.override
3050  * are successfully loaded based on the value of $XDG_CURRENT_DESKTOP.
3051  */
3052 static void
3053 test_per_desktop_subprocess (void)
3054 {
3055   GSettings *settings;
3056   GAction *action_string;
3057   TestObject *obj;
3058   gpointer p;
3059   gchar *str;
3060
3061   g_setenv ("XDG_CURRENT_DESKTOP", "GNOME-Classic:GNOME", TRUE);
3062
3063   settings = g_settings_new ("org.gtk.test.per-desktop");
3064   obj = test_object_new ();
3065
3066   str = g_settings_get_string (settings, "desktop");
3067   g_assert_cmpstr (str, ==, "GNOME Classic");
3068   g_free (str);
3069
3070   p = g_settings_get_mapped (settings, "desktop", string_map_func, NULL);
3071
3072   str = g_variant_dup_string (p, NULL);
3073   g_assert_cmpstr (str, ==, "GNOME Classic");
3074   g_free (str);
3075
3076   g_variant_unref (p);
3077
3078   g_settings_bind (settings, "desktop", obj, "string", G_SETTINGS_BIND_DEFAULT);
3079
3080   g_object_get (obj, "string", &str, NULL);
3081   g_assert_cmpstr (str, ==, "GNOME Classic");
3082   g_free (str);
3083
3084   action_string = g_settings_create_action (settings, "desktop");
3085   check_and_free (g_action_get_state (action_string), "'GNOME Classic'");
3086
3087   g_clear_object (&action_string);
3088   g_object_unref (settings);
3089   g_object_unref (obj);
3090 }
3091
3092 static void
3093 test_extended_schema (void)
3094 {
3095   GSettingsSchema *schema;
3096   GSettings *settings;
3097   gchar **keys;
3098
3099   settings = g_settings_new_with_path ("org.gtk.test.extends.extended", "/test/extendes/");
3100   g_object_get (settings, "settings-schema", &schema, NULL);
3101   keys = g_settings_schema_list_keys (schema);
3102   g_assert_true (strv_set_equal ((const gchar * const *) keys, "int32", "string", "another-int32", NULL));
3103   g_strfreev (keys);
3104   g_object_unref (settings);
3105   g_settings_schema_unref (schema);
3106 }
3107
3108 int
3109 main (int argc, char *argv[])
3110 {
3111   gchar *schema_text;
3112   gchar *override_text;
3113   gchar *enums;
3114   gint result;
3115   const KeyfileTestData keyfile_test_data_explicit_path = { "/tests/", "root", "tests", "/" };
3116   const KeyfileTestData keyfile_test_data_empty_path = { "/", "root", "root", "/" };
3117   const KeyfileTestData keyfile_test_data_long_path = {
3118     "/tests/path/is/very/long/and/this/makes/some/comparisons/take/a/different/branch/",
3119     "root",
3120     "tests/path/is/very/long/and/this/makes/some/comparisons/take/a/different/branch",
3121     "/"
3122   };
3123
3124 /* Meson build sets this */
3125 #ifdef TEST_LOCALE_PATH
3126   if (g_str_has_suffix (TEST_LOCALE_PATH, "LC_MESSAGES"))
3127     {
3128       locale_dir = TEST_LOCALE_PATH G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S "..";
3129     }
3130 #endif
3131
3132   setlocale (LC_ALL, "");
3133
3134   g_test_init (&argc, &argv, NULL);
3135
3136   if (!g_test_subprocess ())
3137     {
3138       GError *local_error = NULL;
3139       char *subprocess_stdout = NULL;
3140
3141       /* A GVDB header is 6 guint32s, and requires a magic number in the first
3142        * two guint32s. A set of zero bytes of a greater length is considered
3143        * corrupt. */
3144       const guint8 gschemas_compiled_corrupt[sizeof (guint32) * 7] = { 0, };
3145
3146       backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
3147
3148       g_setenv ("XDG_DATA_DIRS", ".", TRUE);
3149       g_setenv ("XDG_DATA_HOME", ".", TRUE);
3150       g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
3151       g_setenv ("XDG_CURRENT_DESKTOP", "", TRUE);
3152
3153       if (!backend_set)
3154         g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
3155
3156       g_remove ("org.gtk.test.enums.xml");
3157       /* #GLIB_MKENUMS is defined in meson.build */
3158       g_assert_true (g_spawn_command_line_sync (GLIB_MKENUMS " "
3159                                                 "--template " SRCDIR "/enums.xml.template "
3160                                                 SRCDIR "/testenum.h",
3161                                                 &enums, NULL, &result, NULL));
3162       g_assert_cmpint (result, ==, 0);
3163       g_assert_true (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
3164       g_free (enums);
3165
3166       g_assert_true (g_file_get_contents (SRCDIR "/org.gtk.test.gschema.xml.orig", &schema_text, NULL, NULL));
3167       g_assert_true (g_file_set_contents ("org.gtk.test.gschema.xml", schema_text, -1, NULL));
3168       g_free (schema_text);
3169
3170       g_assert_true (g_file_get_contents (SRCDIR "/org.gtk.test.gschema.override.orig", &override_text, NULL, NULL));
3171       g_assert_true (g_file_set_contents ("org.gtk.test.gschema.override", override_text, -1, NULL));
3172       g_free (override_text);
3173
3174       g_remove ("gschemas.compiled");
3175       /* #GLIB_COMPILE_SCHEMAS is defined in meson.build */
3176       g_assert_true (g_spawn_command_line_sync (GLIB_COMPILE_SCHEMAS " --targetdir=. "
3177                                                 "--schema-file=org.gtk.test.enums.xml "
3178                                                 "--schema-file=org.gtk.test.gschema.xml "
3179                                                 "--override-file=org.gtk.test.gschema.override",
3180                                                 &subprocess_stdout, NULL, &result, NULL));
3181       if (subprocess_stdout && *g_strstrip (subprocess_stdout) != '\0')
3182         g_test_message ("%s", subprocess_stdout);
3183       g_clear_pointer (&subprocess_stdout, g_free);
3184       g_assert_cmpint (result, ==, 0);
3185
3186       g_remove ("schema-source/gschemas.compiled");
3187       g_mkdir ("schema-source", 0777);
3188       g_assert_true (g_spawn_command_line_sync (GLIB_COMPILE_SCHEMAS " --targetdir=schema-source "
3189                                                 "--schema-file=" SRCDIR "/org.gtk.schemasourcecheck.gschema.xml",
3190                                                 &subprocess_stdout, NULL, &result, NULL));
3191       if (subprocess_stdout && *g_strstrip (subprocess_stdout) != '\0')
3192         g_test_message ("%s", subprocess_stdout);
3193       g_clear_pointer (&subprocess_stdout, g_free);
3194       g_assert_cmpint (result, ==, 0);
3195
3196       g_remove ("schema-source-corrupt/gschemas.compiled");
3197       g_mkdir ("schema-source-corrupt", 0777);
3198       g_file_set_contents ("schema-source-corrupt/gschemas.compiled",
3199                            (const gchar *) gschemas_compiled_corrupt,
3200                            sizeof (gschemas_compiled_corrupt),
3201                            &local_error);
3202       g_assert_no_error (local_error);
3203
3204       g_remove ("schema-source-empty/gschemas.compiled");
3205       g_mkdir ("schema-source-empty", 0777);
3206       g_file_set_contents ("schema-source-empty/gschemas.compiled",
3207                            "", 0,
3208                            &local_error);
3209       g_assert_no_error (local_error);
3210    }
3211
3212   g_test_add_func ("/gsettings/basic", test_basic);
3213
3214   if (!backend_set)
3215     {
3216       g_test_add_func ("/gsettings/no-schema", test_no_schema);
3217       g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
3218       g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
3219       g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
3220       g_test_add_func ("/gsettings/no-path", test_no_path);
3221     }
3222
3223   g_test_add_func ("/gsettings/basic-types", test_basic_types);
3224   g_test_add_func ("/gsettings/complex-types", test_complex_types);
3225   g_test_add_func ("/gsettings/changes", test_changes);
3226
3227   g_test_add_func ("/gsettings/l10n", test_l10n);
3228   g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
3229   g_test_add_func ("/gsettings/l10n-time", test_l10n_time);
3230
3231   g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
3232   g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
3233   g_test_add_func ("/gsettings/delay-child", test_delay_child);
3234   g_test_add_func ("/gsettings/delay-reset-key", test_delay_reset_key);
3235   g_test_add_func ("/gsettings/atomic", test_atomic);
3236
3237   g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
3238   g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
3239   g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
3240   g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
3241   g_test_add_func ("/gsettings/unbinding", test_unbind);
3242   g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
3243
3244   if (!backend_set)
3245     {
3246       g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
3247       g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
3248       g_test_add_func ("/gsettings/no-read-binding/subprocess/fail", test_no_read_binding_fail);
3249       g_test_add_func ("/gsettings/no-read-binding/subprocess/pass", test_no_read_binding_pass);
3250       g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
3251       g_test_add_func ("/gsettings/no-write-binding/subprocess/fail", test_no_write_binding_fail);
3252       g_test_add_func ("/gsettings/no-write-binding/subprocess/pass", test_no_write_binding_pass);
3253     }
3254
3255   g_test_add ("/gsettings/keyfile", Fixture, NULL, setup, test_keyfile, teardown);
3256   g_test_add ("/gsettings/keyfile/explicit-path", Fixture, &keyfile_test_data_explicit_path, setup, test_keyfile_no_path, teardown);
3257   g_test_add ("/gsettings/keyfile/empty-path", Fixture, &keyfile_test_data_empty_path, setup, test_keyfile_no_path, teardown);
3258   g_test_add ("/gsettings/keyfile/long-path", Fixture, &keyfile_test_data_long_path, setup, test_keyfile_no_path, teardown);
3259   g_test_add ("/gsettings/keyfile/outside-root-path", Fixture, NULL, setup, test_keyfile_outside_root_path, teardown);
3260   g_test_add ("/gsettings/keyfile/no-root-group", Fixture, NULL, setup, test_keyfile_no_root_group, teardown);
3261   g_test_add_func ("/gsettings/child-schema", test_child_schema);
3262   g_test_add_func ("/gsettings/strinfo", test_strinfo);
3263   g_test_add_func ("/gsettings/enums", test_enums);
3264   g_test_add_func ("/gsettings/enums/subprocess/non-enum-key", test_enums_non_enum_key);
3265   g_test_add_func ("/gsettings/enums/subprocess/non-enum-value", test_enums_non_enum_value);
3266   g_test_add_func ("/gsettings/enums/subprocess/range", test_enums_range);
3267   g_test_add_func ("/gsettings/enums/subprocess/non-flags", test_enums_non_flags);
3268   g_test_add_func ("/gsettings/flags", test_flags);
3269   g_test_add_func ("/gsettings/flags/subprocess/non-flags-key", test_flags_non_flags_key);
3270   g_test_add_func ("/gsettings/flags/subprocess/non-flags-value", test_flags_non_flags_value);
3271   g_test_add_func ("/gsettings/flags/subprocess/range", test_flags_range);
3272   g_test_add_func ("/gsettings/flags/subprocess/non-enum", test_flags_non_enum);
3273   g_test_add_func ("/gsettings/range", test_range);
3274   g_test_add_func ("/gsettings/range/subprocess/high", test_range_high);
3275   g_test_add_func ("/gsettings/range/subprocess/low", test_range_low);
3276   g_test_add_func ("/gsettings/list-items", test_list_items);
3277   g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
3278   g_test_add_func ("/gsettings/mapped", test_get_mapped);
3279   g_test_add_func ("/gsettings/get-range", test_get_range);
3280   g_test_add_func ("/gsettings/schema-source", test_schema_source);
3281   g_test_add_func ("/gsettings/schema-list-keys", test_schema_list_keys);
3282   g_test_add_func ("/gsettings/actions", test_actions);
3283   g_test_add_func ("/gsettings/null-backend", test_null_backend);
3284   g_test_add_func ("/gsettings/memory-backend", test_memory_backend);
3285   g_test_add_func ("/gsettings/read-descriptions", test_read_descriptions);
3286   g_test_add_func ("/gsettings/test-extended-schema", test_extended_schema);
3287   g_test_add_func ("/gsettings/default-value", test_default_value);
3288   g_test_add_func ("/gsettings/per-desktop", test_per_desktop);
3289   g_test_add_func ("/gsettings/per-desktop/subprocess", test_per_desktop_subprocess);
3290
3291   result = g_test_run ();
3292
3293   g_settings_sync ();
3294
3295   /* FIXME: Due to the way #GSettings objects can be used without specifying a
3296    * backend, the default backend is leaked. In order to be able to run this
3297    * test under valgrind and get meaningful checking for real leaks, use this
3298    * hack to drop the final reference to the default #GSettingsBackend.
3299    *
3300    * This should not be used in production code. */
3301     {
3302       GSettingsBackend *backend;
3303
3304       backend = g_settings_backend_get_default ();
3305       g_object_unref (backend);  /* reference from the *_get_default() call */
3306       g_assert_finalize_object (backend);  /* singleton reference owned by GLib */
3307     }
3308
3309   return result;
3310 }