various tests: do not provoke SIGTRAP with -m no-undefined
[glib.git] / gio / tests / gsettings.c
blob3c42c904ac044f8eef0ef465cfd8f57b3ed66d3e
1 #include <stdlib.h>
2 #include <locale.h>
3 #include <libintl.h>
4 #include <gio/gio.h>
5 #include <gstdio.h>
6 #define G_SETTINGS_ENABLE_BACKEND
7 #include <gio/gsettingsbackend.h>
9 #include "testenum.h"
11 static gboolean backend_set;
13 /* These tests rely on the schemas in org.gtk.test.gschema.xml
14 * to be compiled and installed in the same directory.
17 static void
18 check_and_free (GVariant *value,
19 const gchar *expected)
21 gchar *printed;
23 printed = g_variant_print (value, TRUE);
24 g_assert_cmpstr (printed, ==, expected);
25 g_free (printed);
27 g_variant_unref (value);
31 /* Just to get warmed up: Read and set a string, and
32 * verify that can read the changed string back
34 static void
35 test_basic (void)
37 gchar *str = NULL;
38 GSettings *settings;
40 settings = g_settings_new ("org.gtk.test");
42 g_object_get (settings, "schema", &str, NULL);
43 g_assert_cmpstr (str, ==, "org.gtk.test");
44 g_free (str);
46 g_settings_get (settings, "greeting", "s", &str);
47 g_assert_cmpstr (str, ==, "Hello, earthlings");
48 g_free (str);
50 g_settings_set (settings, "greeting", "s", "goodbye world");
51 g_settings_get (settings, "greeting", "s", &str);
52 g_assert_cmpstr (str, ==, "goodbye world");
53 g_free (str);
54 str = NULL;
56 if (!backend_set && g_test_undefined ())
58 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
60 settings = g_settings_new ("org.gtk.test");
61 g_settings_set (settings, "greeting", "i", 555);
62 abort ();
64 g_test_trap_assert_failed ();
65 g_test_trap_assert_stderr ("*g_settings_set_value*expects type*");
68 g_settings_get (settings, "greeting", "s", &str);
69 g_assert_cmpstr (str, ==, "goodbye world");
70 g_free (str);
71 str = NULL;
73 g_settings_reset (settings, "greeting");
74 str = g_settings_get_string (settings, "greeting");
75 g_assert_cmpstr (str, ==, "Hello, earthlings");
76 g_free (str);
78 g_settings_set (settings, "greeting", "s", "this is the end");
79 g_object_unref (settings);
82 /* Check that we get an error when getting a key
83 * that is not in the schema
85 static void
86 test_unknown_key (void)
88 if (!g_test_undefined ())
89 return;
91 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
93 GSettings *settings;
94 GVariant *value;
96 settings = g_settings_new ("org.gtk.test");
97 value = g_settings_get_value (settings, "no_such_key");
99 g_assert (value == NULL);
101 g_object_unref (settings);
103 g_test_trap_assert_failed ();
104 g_test_trap_assert_stderr ("*does not contain*");
107 /* Check that we get an error when the schema
108 * has not been installed
110 static void
111 test_no_schema (void)
113 if (!g_test_undefined ())
114 return;
116 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
118 GSettings *settings;
120 settings = g_settings_new ("no.such.schema");
122 g_assert (settings == NULL);
125 g_test_trap_assert_failed ();
126 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
129 /* Check that we get an error when passing a type string
130 * that does not match the schema
132 static void
133 test_wrong_type (void)
135 if (!g_test_undefined ())
136 return;
138 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
140 GSettings *settings;
141 gchar *str = NULL;
143 settings = g_settings_new ("org.gtk.test");
145 g_settings_get (settings, "greeting", "o", &str);
147 g_assert (str == NULL);
149 g_test_trap_assert_failed ();
150 g_test_trap_assert_stderr ("*CRITICAL*");
152 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
154 GSettings *settings;
156 settings = g_settings_new ("org.gtk.test");
158 g_settings_set (settings, "greeting", "o", "/a/path");
160 g_test_trap_assert_failed ();
161 g_test_trap_assert_stderr ("*CRITICAL*");
164 /* Check errors with explicit paths */
165 static void
166 test_wrong_path (void)
168 if (!g_test_undefined ())
169 return;
171 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
173 GSettings *settings G_GNUC_UNUSED;
175 settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
178 g_test_trap_assert_failed ();
179 g_test_trap_assert_stderr ("*but path * specified by schema*");
182 static void
183 test_no_path (void)
185 if (!g_test_undefined ())
186 return;
188 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
190 GSettings *settings G_GNUC_UNUSED;
192 settings = g_settings_new ("org.gtk.test.no-path");
195 g_test_trap_assert_failed ();
196 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
200 /* Check that we can successfully read and set the full
201 * range of all basic types
203 static void
204 test_basic_types (void)
206 GSettings *settings;
207 gboolean b;
208 guint8 byte;
209 gint16 i16;
210 guint16 u16;
211 gint32 i32;
212 guint32 u32;
213 gint64 i64;
214 guint64 u64;
215 gdouble d;
216 gchar *str;
218 settings = g_settings_new ("org.gtk.test.basic-types");
220 g_settings_get (settings, "test-boolean", "b", &b);
221 g_assert_cmpint (b, ==, 1);
223 g_settings_set (settings, "test-boolean", "b", 0);
224 g_settings_get (settings, "test-boolean", "b", &b);
225 g_assert_cmpint (b, ==, 0);
227 g_settings_get (settings, "test-byte", "y", &byte);
228 g_assert_cmpint (byte, ==, 25);
230 g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
231 g_settings_get (settings, "test-byte", "y", &byte);
232 g_assert_cmpint (byte, ==, G_MAXUINT8);
234 g_settings_get (settings, "test-int16", "n", &i16);
235 g_assert_cmpint (i16, ==, -1234);
237 g_settings_set (settings, "test-int16", "n", G_MININT16);
238 g_settings_get (settings, "test-int16", "n", &i16);
239 g_assert_cmpint (i16, ==, G_MININT16);
241 g_settings_set (settings, "test-int16", "n", G_MAXINT16);
242 g_settings_get (settings, "test-int16", "n", &i16);
243 g_assert_cmpint (i16, ==, G_MAXINT16);
245 g_settings_get (settings, "test-uint16", "q", &u16);
246 g_assert_cmpuint (u16, ==, 1234);
248 g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
249 g_settings_get (settings, "test-uint16", "q", &u16);
250 g_assert_cmpuint (u16, ==, G_MAXUINT16);
252 g_settings_get (settings, "test-int32", "i", &i32);
253 g_assert_cmpint (i32, ==, -123456);
255 g_settings_set (settings, "test-int32", "i", G_MININT32);
256 g_settings_get (settings, "test-int32", "i", &i32);
257 g_assert_cmpint (i32, ==, G_MININT32);
259 g_settings_set (settings, "test-int32", "i", G_MAXINT32);
260 g_settings_get (settings, "test-int32", "i", &i32);
261 g_assert_cmpint (i32, ==, G_MAXINT32);
263 g_settings_get (settings, "test-uint32", "u", &u32);
264 g_assert_cmpuint (u32, ==, 123456);
266 g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
267 g_settings_get (settings, "test-uint32", "u", &u32);
268 g_assert_cmpuint (u32, ==, G_MAXUINT32);
270 g_settings_get (settings, "test-int64", "x", &i64);
271 g_assert_cmpuint (i64, ==, -123456789);
273 g_settings_set (settings, "test-int64", "x", G_MININT64);
274 g_settings_get (settings, "test-int64", "x", &i64);
275 g_assert_cmpuint (i64, ==, G_MININT64);
277 g_settings_set (settings, "test-int64", "x", G_MAXINT64);
278 g_settings_get (settings, "test-int64", "x", &i64);
279 g_assert_cmpuint (i64, ==, G_MAXINT64);
281 g_settings_get (settings, "test-uint64", "t", &u64);
282 g_assert_cmpuint (u64, ==, 123456789);
284 g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
285 g_settings_get (settings, "test-uint64", "t", &u64);
286 g_assert_cmpuint (u64, ==, G_MAXUINT64);
288 g_settings_get (settings, "test-double", "d", &d);
289 g_assert_cmpfloat (d, ==, 123.456);
291 g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
292 g_settings_get (settings, "test-double", "d", &d);
293 g_assert_cmpfloat (d, ==, G_MINDOUBLE);
295 g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
296 g_settings_get (settings, "test-double", "d", &d);
297 g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
299 g_settings_get (settings, "test-string", "s", &str);
300 g_assert_cmpstr (str, ==, "a string, it seems");
301 g_free (str);
302 str = NULL;
304 g_settings_get (settings, "test-objectpath", "o", &str);
305 g_assert_cmpstr (str, ==, "/a/object/path");
306 g_object_unref (settings);
307 g_free (str);
308 str = NULL;
311 /* Check that we can read an set complex types like
312 * tuples, arrays and dictionaries
314 static void
315 test_complex_types (void)
317 GSettings *settings;
318 gchar *s;
319 gint i1, i2;
320 GVariantIter *iter = NULL;
322 settings = g_settings_new ("org.gtk.test.complex-types");
324 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
325 g_assert_cmpstr (s, ==, "one");
326 g_assert_cmpint (i1,==, 2);
327 g_assert_cmpint (i2,==, 3);
328 g_free (s) ;
329 s = NULL;
331 g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
332 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
333 g_assert_cmpstr (s, ==, "none");
334 g_assert_cmpint (i1,==, 0);
335 g_assert_cmpint (i2,==, 0);
336 g_free (s);
337 s = NULL;
339 g_settings_get (settings, "test-array", "ai", &iter);
340 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
341 g_assert (g_variant_iter_next (iter, "i", &i1));
342 g_assert_cmpint (i1, ==, 0);
343 g_assert (g_variant_iter_next (iter, "i", &i1));
344 g_assert_cmpint (i1, ==, 1);
345 g_assert (g_variant_iter_next (iter, "i", &i1));
346 g_assert_cmpint (i1, ==, 2);
347 g_assert (g_variant_iter_next (iter, "i", &i1));
348 g_assert_cmpint (i1, ==, 3);
349 g_assert (g_variant_iter_next (iter, "i", &i1));
350 g_assert_cmpint (i1, ==, 4);
351 g_assert (g_variant_iter_next (iter, "i", &i1));
352 g_assert_cmpint (i1, ==, 5);
353 g_assert (!g_variant_iter_next (iter, "i", &i1));
354 g_variant_iter_free (iter);
356 g_object_unref (settings);
359 static gboolean changed_cb_called;
361 static void
362 changed_cb (GSettings *settings,
363 const gchar *key,
364 gpointer data)
366 changed_cb_called = TRUE;
368 g_assert_cmpstr (key, ==, data);
371 /* Test that basic change notification with the changed signal works.
373 static void
374 test_changes (void)
376 GSettings *settings;
377 GSettings *settings2;
379 settings = g_settings_new ("org.gtk.test");
381 g_signal_connect (settings, "changed",
382 G_CALLBACK (changed_cb), "greeting");
384 changed_cb_called = FALSE;
386 g_settings_set (settings, "greeting", "s", "new greeting");
387 g_assert (changed_cb_called);
389 settings2 = g_settings_new ("org.gtk.test");
391 changed_cb_called = FALSE;
393 g_settings_set (settings2, "greeting", "s", "hi");
394 g_assert (changed_cb_called);
396 g_object_unref (settings2);
397 g_object_unref (settings);
400 static gboolean changed_cb_called2;
402 static void
403 changed_cb2 (GSettings *settings,
404 const gchar *key,
405 gpointer data)
407 gboolean *p = data;
409 *p = TRUE;
412 /* Test that changes done to a delay-mode instance
413 * don't appear to the outside world until apply. Also
414 * check that we get change notification when they are
415 * applied.
416 * Also test that the has-unapplied property is properly
417 * maintained.
419 static void
420 test_delay_apply (void)
422 GSettings *settings;
423 GSettings *settings2;
424 gchar *str;
426 settings = g_settings_new ("org.gtk.test");
427 settings2 = g_settings_new ("org.gtk.test");
429 g_settings_set (settings2, "greeting", "s", "top o' the morning");
431 changed_cb_called = FALSE;
432 changed_cb_called2 = FALSE;
434 g_signal_connect (settings, "changed",
435 G_CALLBACK (changed_cb2), &changed_cb_called);
436 g_signal_connect (settings2, "changed",
437 G_CALLBACK (changed_cb2), &changed_cb_called2);
439 g_settings_delay (settings);
441 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
443 g_assert (changed_cb_called);
444 g_assert (!changed_cb_called2);
446 g_settings_get (settings, "greeting", "s", &str);
447 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
448 g_free (str);
449 str = NULL;
451 g_settings_get (settings2, "greeting", "s", &str);
452 g_assert_cmpstr (str, ==, "top o' the morning");
453 g_free (str);
454 str = NULL;
456 g_assert (g_settings_get_has_unapplied (settings));
457 g_assert (!g_settings_get_has_unapplied (settings2));
459 changed_cb_called = FALSE;
460 changed_cb_called2 = FALSE;
462 g_settings_apply (settings);
464 g_assert (!changed_cb_called);
465 g_assert (changed_cb_called2);
467 g_settings_get (settings, "greeting", "s", &str);
468 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
469 g_free (str);
470 str = NULL;
472 g_settings_get (settings2, "greeting", "s", &str);
473 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
474 g_free (str);
475 str = NULL;
477 g_assert (!g_settings_get_has_unapplied (settings));
478 g_assert (!g_settings_get_has_unapplied (settings2));
480 g_object_unref (settings2);
481 g_object_unref (settings);
484 /* Test that reverting unapplied changes in a delay-apply
485 * settings instance works.
487 static void
488 test_delay_revert (void)
490 GSettings *settings;
491 GSettings *settings2;
492 gchar *str;
494 settings = g_settings_new ("org.gtk.test");
495 settings2 = g_settings_new ("org.gtk.test");
497 g_settings_set (settings2, "greeting", "s", "top o' the morning");
499 g_settings_delay (settings);
501 g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
503 g_settings_get (settings, "greeting", "s", &str);
504 g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
505 g_free (str);
506 str = NULL;
508 g_settings_get (settings2, "greeting", "s", &str);
509 g_assert_cmpstr (str, ==, "top o' the morning");
510 g_free (str);
511 str = NULL;
513 g_assert (g_settings_get_has_unapplied (settings));
515 g_settings_revert (settings);
517 g_assert (!g_settings_get_has_unapplied (settings));
519 g_settings_get (settings, "greeting", "s", &str);
520 g_assert_cmpstr (str, ==, "top o' the morning");
521 g_free (str);
522 str = NULL;
524 g_settings_get (settings2, "greeting", "s", &str);
525 g_assert_cmpstr (str, ==, "top o' the morning");
526 g_free (str);
527 str = NULL;
529 g_object_unref (settings2);
530 g_object_unref (settings);
533 static void
534 keys_changed_cb (GSettings *settings,
535 const GQuark *keys,
536 gint n_keys)
538 gchar *str;
540 g_assert_cmpint (n_keys, ==, 2);
542 g_assert ((keys[0] == g_quark_from_static_string ("greeting") &&
543 keys[1] == g_quark_from_static_string ("farewell")) ||
544 (keys[1] == g_quark_from_static_string ("greeting") &&
545 keys[0] == g_quark_from_static_string ("farewell")));
547 g_settings_get (settings, "greeting", "s", &str);
548 g_assert_cmpstr (str, ==, "greetings from test_atomic");
549 g_free (str);
550 str = NULL;
552 g_settings_get (settings, "farewell", "s", &str);
553 g_assert_cmpstr (str, ==, "atomic bye-bye");
554 g_free (str);
555 str = NULL;
558 /* Check that delay-applied changes appear atomically.
559 * More specifically, verify that all changed keys appear
560 * with their new value while handling the change-event signal.
562 static void
563 test_atomic (void)
565 GSettings *settings;
566 GSettings *settings2;
567 gchar *str;
569 settings = g_settings_new ("org.gtk.test");
570 settings2 = g_settings_new ("org.gtk.test");
572 g_settings_set (settings2, "greeting", "s", "top o' the morning");
574 changed_cb_called = FALSE;
575 changed_cb_called2 = FALSE;
577 g_signal_connect (settings2, "change-event",
578 G_CALLBACK (keys_changed_cb), NULL);
580 g_settings_delay (settings);
582 g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
583 g_settings_set (settings, "farewell", "s", "atomic bye-bye");
585 g_settings_apply (settings);
587 g_settings_get (settings, "greeting", "s", &str);
588 g_assert_cmpstr (str, ==, "greetings from test_atomic");
589 g_free (str);
590 str = NULL;
592 g_settings_get (settings, "farewell", "s", &str);
593 g_assert_cmpstr (str, ==, "atomic bye-bye");
594 g_free (str);
595 str = NULL;
597 g_settings_get (settings2, "greeting", "s", &str);
598 g_assert_cmpstr (str, ==, "greetings from test_atomic");
599 g_free (str);
600 str = NULL;
602 g_settings_get (settings2, "farewell", "s", &str);
603 g_assert_cmpstr (str, ==, "atomic bye-bye");
604 g_free (str);
605 str = NULL;
607 g_object_unref (settings2);
608 g_object_unref (settings);
611 /* On Windows the interaction between the C library locale and libintl
612 * (from GNU gettext) is not like on POSIX, so just skip these tests
613 * for now.
615 * There are several issues:
617 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
618 * in libintl (defined in its <libintl.h>).
620 * 2) The locale names that setlocale() accepts and returns aren't in
621 * the "de_DE" style, but like "German_Germany".
623 * 3) libintl looks at the Win32 thread locale and not the C library
624 * locale. (And even if libintl would use the C library's locale, as
625 * there are several alternative C library DLLs, libintl might be
626 * linked to a different one than the application code, so they
627 * wouldn't have the same C library locale anyway.)
630 /* Test that translations work for schema defaults.
632 * This test relies on the de.po file in the same directory
633 * to be compiled into ./de/LC_MESSAGES/test.mo
635 static void
636 test_l10n (void)
638 GSettings *settings;
639 gchar *str;
640 gchar *locale;
642 bindtextdomain ("test", ".");
643 bind_textdomain_codeset ("test", "UTF-8");
645 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
647 settings = g_settings_new ("org.gtk.test.localized");
649 setlocale (LC_MESSAGES, "C");
650 str = g_settings_get_string (settings, "error-message");
651 setlocale (LC_MESSAGES, locale);
653 g_assert_cmpstr (str, ==, "Unnamed");
654 g_free (str);
655 str = NULL;
657 setlocale (LC_MESSAGES, "de_DE");
658 str = g_settings_get_string (settings, "error-message");
659 setlocale (LC_MESSAGES, locale);
661 g_assert_cmpstr (str, ==, "Unbenannt");
662 g_object_unref (settings);
663 g_free (str);
664 str = NULL;
666 g_free (locale);
669 /* Test that message context works as expected with translated
670 * schema defaults. Also, verify that non-ASCII UTF-8 content
671 * works.
673 * This test relies on the de.po file in the same directory
674 * to be compiled into ./de/LC_MESSAGES/test.mo
676 static void
677 test_l10n_context (void)
679 GSettings *settings;
680 gchar *str;
681 gchar *locale;
683 bindtextdomain ("test", ".");
684 bind_textdomain_codeset ("test", "UTF-8");
686 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
688 settings = g_settings_new ("org.gtk.test.localized");
690 setlocale (LC_MESSAGES, "C");
691 g_settings_get (settings, "backspace", "s", &str);
692 setlocale (LC_MESSAGES, locale);
694 g_assert_cmpstr (str, ==, "BackSpace");
695 g_free (str);
696 str = NULL;
698 setlocale (LC_MESSAGES, "de_DE");
699 g_settings_get (settings, "backspace", "s", &str);
700 setlocale (LC_MESSAGES, locale);
702 g_assert_cmpstr (str, ==, "Löschen");
703 g_object_unref (settings);
704 g_free (str);
705 str = NULL;
707 g_free (locale);
710 enum
712 PROP_0,
713 PROP_BOOL,
714 PROP_ANTI_BOOL,
715 PROP_BYTE,
716 PROP_INT16,
717 PROP_UINT16,
718 PROP_INT,
719 PROP_UINT,
720 PROP_INT64,
721 PROP_UINT64,
722 PROP_DOUBLE,
723 PROP_STRING,
724 PROP_NO_READ,
725 PROP_NO_WRITE,
726 PROP_STRV,
727 PROP_ENUM
730 typedef struct
732 GObject parent_instance;
734 gboolean bool_prop;
735 gboolean anti_bool_prop;
736 gint8 byte_prop;
737 gint int16_prop;
738 guint16 uint16_prop;
739 gint int_prop;
740 guint uint_prop;
741 gint64 int64_prop;
742 guint64 uint64_prop;
743 gdouble double_prop;
744 gchar *string_prop;
745 gchar *no_read_prop;
746 gchar *no_write_prop;
747 gchar **strv_prop;
748 guint enum_prop;
749 } TestObject;
751 typedef struct
753 GObjectClass parent_class;
754 } TestObjectClass;
756 static GType test_object_get_type (void);
757 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
759 static void
760 test_object_init (TestObject *object)
764 static void
765 test_object_finalize (GObject *object)
767 TestObject *testo = (TestObject*)object;
768 g_strfreev (testo->strv_prop);
769 g_free (testo->string_prop);
770 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
773 static void
774 test_object_get_property (GObject *object,
775 guint prop_id,
776 GValue *value,
777 GParamSpec *pspec)
779 TestObject *test_object = (TestObject *)object;
781 switch (prop_id)
783 case PROP_BOOL:
784 g_value_set_boolean (value, test_object->bool_prop);
785 break;
786 case PROP_ANTI_BOOL:
787 g_value_set_boolean (value, test_object->anti_bool_prop);
788 break;
789 case PROP_BYTE:
790 g_value_set_schar (value, test_object->byte_prop);
791 break;
792 case PROP_UINT16:
793 g_value_set_uint (value, test_object->uint16_prop);
794 break;
795 case PROP_INT16:
796 g_value_set_int (value, test_object->int16_prop);
797 break;
798 case PROP_INT:
799 g_value_set_int (value, test_object->int_prop);
800 break;
801 case PROP_UINT:
802 g_value_set_uint (value, test_object->uint_prop);
803 break;
804 case PROP_INT64:
805 g_value_set_int64 (value, test_object->int64_prop);
806 break;
807 case PROP_UINT64:
808 g_value_set_uint64 (value, test_object->uint64_prop);
809 break;
810 case PROP_DOUBLE:
811 g_value_set_double (value, test_object->double_prop);
812 break;
813 case PROP_STRING:
814 g_value_set_string (value, test_object->string_prop);
815 break;
816 case PROP_NO_WRITE:
817 g_value_set_string (value, test_object->no_write_prop);
818 break;
819 case PROP_STRV:
820 g_value_set_boxed (value, test_object->strv_prop);
821 break;
822 case PROP_ENUM:
823 g_value_set_enum (value, test_object->enum_prop);
824 break;
825 default:
826 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
827 break;
831 static void
832 test_object_set_property (GObject *object,
833 guint prop_id,
834 const GValue *value,
835 GParamSpec *pspec)
837 TestObject *test_object = (TestObject *)object;
839 switch (prop_id)
841 case PROP_BOOL:
842 test_object->bool_prop = g_value_get_boolean (value);
843 break;
844 case PROP_ANTI_BOOL:
845 test_object->anti_bool_prop = g_value_get_boolean (value);
846 break;
847 case PROP_BYTE:
848 test_object->byte_prop = g_value_get_schar (value);
849 break;
850 case PROP_INT16:
851 test_object->int16_prop = g_value_get_int (value);
852 break;
853 case PROP_UINT16:
854 test_object->uint16_prop = g_value_get_uint (value);
855 break;
856 case PROP_INT:
857 test_object->int_prop = g_value_get_int (value);
858 break;
859 case PROP_UINT:
860 test_object->uint_prop = g_value_get_uint (value);
861 break;
862 case PROP_INT64:
863 test_object->int64_prop = g_value_get_int64 (value);
864 break;
865 case PROP_UINT64:
866 test_object->uint64_prop = g_value_get_uint64 (value);
867 break;
868 case PROP_DOUBLE:
869 test_object->double_prop = g_value_get_double (value);
870 break;
871 case PROP_STRING:
872 g_free (test_object->string_prop);
873 test_object->string_prop = g_value_dup_string (value);
874 break;
875 case PROP_NO_READ:
876 g_free (test_object->no_read_prop);
877 test_object->no_read_prop = g_value_dup_string (value);
878 break;
879 case PROP_STRV:
880 g_strfreev (test_object->strv_prop);
881 test_object->strv_prop = g_value_dup_boxed (value);
882 break;
883 case PROP_ENUM:
884 test_object->enum_prop = g_value_get_enum (value);
885 break;
886 default:
887 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
888 break;
892 static GType
893 test_enum_get_type (void)
895 static volatile gsize define_type_id = 0;
897 if (g_once_init_enter (&define_type_id))
899 static const GEnumValue values[] = {
900 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
901 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
902 { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
903 { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
904 { 0, NULL, NULL }
907 GType type_id = g_enum_register_static ("TestEnum", values);
908 g_once_init_leave (&define_type_id, type_id);
911 return define_type_id;
914 static void
915 test_object_class_init (TestObjectClass *class)
917 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
919 gobject_class->get_property = test_object_get_property;
920 gobject_class->set_property = test_object_set_property;
921 gobject_class->finalize = test_object_finalize;
923 g_object_class_install_property (gobject_class, PROP_BOOL,
924 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
925 g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
926 g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
927 g_object_class_install_property (gobject_class, PROP_BYTE,
928 g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
929 g_object_class_install_property (gobject_class, PROP_INT16,
930 g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
931 g_object_class_install_property (gobject_class, PROP_UINT16,
932 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
933 g_object_class_install_property (gobject_class, PROP_INT,
934 g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
935 g_object_class_install_property (gobject_class, PROP_UINT,
936 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
937 g_object_class_install_property (gobject_class, PROP_INT64,
938 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
939 g_object_class_install_property (gobject_class, PROP_UINT64,
940 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
941 g_object_class_install_property (gobject_class, PROP_DOUBLE,
942 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
943 g_object_class_install_property (gobject_class, PROP_STRING,
944 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
945 g_object_class_install_property (gobject_class, PROP_NO_WRITE,
946 g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
947 g_object_class_install_property (gobject_class, PROP_NO_READ,
948 g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
949 g_object_class_install_property (gobject_class, PROP_STRV,
950 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
951 g_object_class_install_property (gobject_class, PROP_ENUM,
952 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
955 static TestObject *
956 test_object_new (void)
958 return (TestObject*)g_object_new (test_object_get_type (), NULL);
961 /* Test basic binding functionality for simple types.
962 * Verify that with bidirectional bindings, changes on either side
963 * are notified on the other end.
965 static void
966 test_simple_binding (void)
968 TestObject *obj;
969 GSettings *settings;
970 gboolean b;
971 gchar y;
972 gint i;
973 gint16 n;
974 guint16 q;
975 gint n2;
976 guint q2;
977 gint64 i64;
978 guint64 u64;
979 gdouble d;
980 gchar *s;
981 GVariant *value;
982 gchar **strv;
984 settings = g_settings_new ("org.gtk.test.binding");
985 obj = test_object_new ();
987 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
988 g_object_set (obj, "bool", TRUE, NULL);
989 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
991 g_settings_set_boolean (settings, "bool", FALSE);
992 b = TRUE;
993 g_object_get (obj, "bool", &b, NULL);
994 g_assert_cmpint (b, ==, FALSE);
996 g_settings_bind (settings, "anti-bool", obj, "anti-bool",
997 G_SETTINGS_BIND_INVERT_BOOLEAN);
998 g_object_set (obj, "anti-bool", FALSE, NULL);
999 g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
1001 g_settings_set_boolean (settings, "anti-bool", FALSE);
1002 b = FALSE;
1003 g_object_get (obj, "anti-bool", &b, NULL);
1004 g_assert_cmpint (b, ==, TRUE);
1006 g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
1008 g_object_set (obj, "byte", 123, NULL);
1009 y = 'c';
1010 g_settings_get (settings, "byte", "y", &y);
1011 g_assert_cmpint (y, ==, 123);
1013 g_settings_set (settings, "byte", "y", 54);
1014 y = 'c';
1015 g_object_get (obj, "byte", &y, NULL);
1016 g_assert_cmpint (y, ==, 54);
1018 g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
1020 g_object_set (obj, "int16", 1234, NULL);
1021 n = 4321;
1022 g_settings_get (settings, "int16", "n", &n);
1023 g_assert_cmpint (n, ==, 1234);
1025 g_settings_set (settings, "int16", "n", 4321);
1026 n2 = 1111;
1027 g_object_get (obj, "int16", &n2, NULL);
1028 g_assert_cmpint (n2, ==, 4321);
1030 g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
1032 g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1033 q = 1111;
1034 g_settings_get (settings, "uint16", "q", &q);
1035 g_assert_cmpuint (q, ==, G_MAXUINT16);
1037 g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1038 q2 = 1111;
1039 g_object_get (obj, "uint16", &q2, NULL);
1040 g_assert_cmpuint (q2, ==, (guint16) G_MAXINT16);
1042 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1044 g_object_set (obj, "int", 12345, NULL);
1045 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1047 g_settings_set_int (settings, "int", 54321);
1048 i = 1111;
1049 g_object_get (obj, "int", &i, NULL);
1050 g_assert_cmpint (i, ==, 54321);
1052 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1054 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1055 i64 = 1111;
1056 g_settings_get (settings, "int64", "x", &i64);
1057 g_assert_cmpint (i64, ==, G_MAXINT64);
1059 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1060 i64 = 1111;
1061 g_object_get (obj, "int64", &i64, NULL);
1062 g_assert_cmpint (i64, ==, G_MININT64);
1064 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1066 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1067 u64 = 1111;
1068 g_settings_get (settings, "uint64", "t", &u64);
1069 g_assert_cmpuint (u64, ==, G_MAXUINT64);
1071 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1072 u64 = 1111;
1073 g_object_get (obj, "uint64", &u64, NULL);
1074 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1076 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1078 g_object_set (obj, "string", "bu ba", NULL);
1079 s = g_settings_get_string (settings, "string");
1080 g_assert_cmpstr (s, ==, "bu ba");
1081 g_free (s);
1083 g_settings_set_string (settings, "string", "bla bla");
1084 g_object_get (obj, "string", &s, NULL);
1085 g_assert_cmpstr (s, ==, "bla bla");
1086 g_free (s);
1088 g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1090 g_object_set (obj, "string", "non-unicode:\315", NULL);
1091 value = g_settings_get_value (settings, "chararray");
1092 g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1093 g_variant_unref (value);
1095 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1097 g_object_set (obj, "double", G_MAXFLOAT, NULL);
1098 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1100 g_settings_set_double (settings, "double", G_MINFLOAT);
1101 d = 1.0;
1102 g_object_get (obj, "double", &d, NULL);
1103 g_assert_cmpfloat (d, ==, G_MINFLOAT);
1105 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1106 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1108 g_settings_set_double (settings, "double", -G_MINDOUBLE);
1109 d = 1.0;
1110 g_object_get (obj, "double", &d, NULL);
1111 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1113 strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1114 g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1115 g_object_set (obj, "strv", strv, NULL);
1116 g_strfreev (strv);
1117 strv = g_settings_get_strv (settings, "strv");
1118 s = g_strjoinv (",", strv);
1119 g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1120 g_strfreev (strv);
1121 g_free (s);
1122 strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1123 g_settings_set_strv (settings, "strv", (const gchar **) strv);
1124 g_strfreev (strv);
1125 g_object_get (obj, "strv", &strv, NULL);
1126 s = g_strjoinv (",", strv);
1127 g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1128 g_strfreev (strv);
1129 g_free (s);
1131 g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1132 g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1133 s = g_settings_get_string (settings, "enum");
1134 g_assert_cmpstr (s, ==, "baz");
1135 g_free (s);
1136 g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1138 g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1139 i = 230;
1140 g_object_get (obj, "enum", &i, NULL);
1141 g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1143 g_settings_set_string (settings, "enum", "baz");
1144 i = 230;
1145 g_object_get (obj, "enum", &i, NULL);
1146 g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1148 g_object_unref (obj);
1149 g_object_unref (settings);
1152 static void
1153 test_unbind (void)
1155 TestObject *obj;
1156 GSettings *settings;
1158 settings = g_settings_new ("org.gtk.test.binding");
1159 obj = test_object_new ();
1161 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1163 g_object_set (obj, "int", 12345, NULL);
1164 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1166 g_settings_unbind (obj, "int");
1168 g_object_set (obj, "int", 54321, NULL);
1169 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1171 g_object_unref (obj);
1172 g_object_unref (settings);
1175 static void
1176 test_bind_writable (void)
1178 TestObject *obj;
1179 GSettings *settings;
1180 gboolean b;
1182 settings = g_settings_new ("org.gtk.test.binding");
1183 obj = test_object_new ();
1185 g_object_set (obj, "bool", FALSE, NULL);
1187 g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1189 g_object_get (obj, "bool", &b, NULL);
1190 g_assert (b);
1192 g_settings_unbind (obj, "bool");
1194 g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1196 g_object_get (obj, "bool", &b, NULL);
1197 g_assert (!b);
1199 g_object_unref (obj);
1200 g_object_unref (settings);
1203 /* Test one-way bindings.
1204 * Verify that changes on one side show up on the other,
1205 * but not vice versa
1207 static void
1208 test_directional_binding (void)
1210 TestObject *obj;
1211 GSettings *settings;
1212 gboolean b;
1213 gint i;
1215 settings = g_settings_new ("org.gtk.test.binding");
1216 obj = test_object_new ();
1218 g_object_set (obj, "bool", FALSE, NULL);
1219 g_settings_set_boolean (settings, "bool", FALSE);
1221 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1223 g_settings_set_boolean (settings, "bool", TRUE);
1224 g_object_get (obj, "bool", &b, NULL);
1225 g_assert_cmpint (b, ==, TRUE);
1227 g_object_set (obj, "bool", FALSE, NULL);
1228 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1230 g_object_set (obj, "int", 20, NULL);
1231 g_settings_set_int (settings, "int", 20);
1233 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1235 g_object_set (obj, "int", 32, NULL);
1236 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1238 g_settings_set_int (settings, "int", 20);
1239 g_object_get (obj, "int", &i, NULL);
1240 g_assert_cmpint (i, ==, 32);
1242 g_object_unref (obj);
1243 g_object_unref (settings);
1246 /* Test that type mismatch is caught when creating a binding
1248 static void
1249 test_typesafe_binding (void)
1251 if (!g_test_undefined ())
1252 return;
1254 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1256 TestObject *obj;
1257 GSettings *settings;
1259 settings = g_settings_new ("org.gtk.test.binding");
1260 obj = test_object_new ();
1262 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1264 g_object_unref (obj);
1265 g_object_unref (settings);
1267 g_test_trap_assert_failed ();
1268 g_test_trap_assert_stderr ("*not compatible*");
1271 static gboolean
1272 string_to_bool (GValue *value,
1273 GVariant *variant,
1274 gpointer user_data)
1276 const gchar *s;
1278 s = g_variant_get_string (variant, NULL);
1279 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1281 return TRUE;
1284 static GVariant *
1285 bool_to_string (const GValue *value,
1286 const GVariantType *expected_type,
1287 gpointer user_data)
1289 if (g_value_get_boolean (value))
1290 return g_variant_new_string ("true");
1291 else
1292 return g_variant_new_string ("false");
1295 /* Test custom bindings.
1296 * Translate strings to booleans and back
1298 static void
1299 test_custom_binding (void)
1301 TestObject *obj;
1302 GSettings *settings;
1303 gchar *s;
1304 gboolean b;
1306 settings = g_settings_new ("org.gtk.test.binding");
1307 obj = test_object_new ();
1309 g_settings_set_string (settings, "string", "true");
1311 g_settings_bind_with_mapping (settings, "string",
1312 obj, "bool",
1313 G_SETTINGS_BIND_DEFAULT,
1314 string_to_bool,
1315 bool_to_string,
1316 NULL, NULL);
1318 g_settings_set_string (settings, "string", "false");
1319 g_object_get (obj, "bool", &b, NULL);
1320 g_assert_cmpint (b, ==, FALSE);
1322 g_settings_set_string (settings, "string", "not true");
1323 g_object_get (obj, "bool", &b, NULL);
1324 g_assert_cmpint (b, ==, FALSE);
1326 g_object_set (obj, "bool", TRUE, NULL);
1327 s = g_settings_get_string (settings, "string");
1328 g_assert_cmpstr (s, ==, "true");
1329 g_free (s);
1331 g_object_unref (obj);
1332 g_object_unref (settings);
1335 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1336 * initial settings value is transported to the object
1337 * side, but later settings changes do not affect the
1338 * object
1340 static void
1341 test_no_change_binding (void)
1343 TestObject *obj;
1344 GSettings *settings;
1345 gboolean b;
1347 settings = g_settings_new ("org.gtk.test.binding");
1348 obj = test_object_new ();
1350 g_object_set (obj, "bool", TRUE, NULL);
1351 g_settings_set_boolean (settings, "bool", FALSE);
1353 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1355 g_object_get (obj, "bool", &b, NULL);
1356 g_assert_cmpint (b, ==, FALSE);
1358 g_settings_set_boolean (settings, "bool", TRUE);
1359 g_object_get (obj, "bool", &b, NULL);
1360 g_assert_cmpint (b, ==, FALSE);
1362 g_settings_set_boolean (settings, "bool", FALSE);
1363 g_object_set (obj, "bool", TRUE, NULL);
1364 b = g_settings_get_boolean (settings, "bool");
1365 g_assert_cmpint (b, ==, TRUE);
1367 g_object_unref (obj);
1368 g_object_unref (settings);
1371 /* Test that binding a non-readable property only
1372 * works in 'GET' mode.
1374 static void
1375 test_no_read_binding (void)
1377 if (g_test_undefined ())
1379 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1381 TestObject *obj;
1382 GSettings *settings;
1384 settings = g_settings_new ("org.gtk.test.binding");
1385 obj = test_object_new ();
1387 g_settings_bind (settings, "string", obj, "no-read", 0);
1389 g_test_trap_assert_failed ();
1390 g_test_trap_assert_stderr ("*property*is not readable*");
1393 if (g_test_trap_fork (0, 0))
1395 TestObject *obj;
1396 GSettings *settings;
1398 settings = g_settings_new ("org.gtk.test.binding");
1399 obj = test_object_new ();
1401 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1403 exit (0);
1405 g_test_trap_assert_passed ();
1408 /* Test that binding a non-writable property only
1409 * works in 'SET' mode.
1411 static void
1412 test_no_write_binding (void)
1414 if (g_test_undefined ())
1416 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1418 TestObject *obj;
1419 GSettings *settings;
1421 settings = g_settings_new ("org.gtk.test.binding");
1422 obj = test_object_new ();
1424 g_settings_bind (settings, "string", obj, "no-write", 0);
1426 g_test_trap_assert_failed ();
1427 g_test_trap_assert_stderr ("*property*is not writable*");
1430 if (g_test_trap_fork (0, 0))
1432 TestObject *obj;
1433 GSettings *settings;
1435 settings = g_settings_new ("org.gtk.test.binding");
1436 obj = test_object_new ();
1438 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1440 exit (0);
1442 g_test_trap_assert_passed ();
1446 * Test that using a keyfile works
1448 static void
1449 test_keyfile (void)
1451 GSettingsBackend *kf_backend;
1452 GSettings *settings;
1453 GKeyFile *keyfile;
1454 gchar *str;
1456 g_remove ("gsettings.store");
1458 kf_backend = g_keyfile_settings_backend_new ("gsettings.store", "/", "root");
1459 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1460 g_object_unref (kf_backend);
1462 g_settings_set (settings, "greeting", "s", "see if this works");
1464 keyfile = g_key_file_new ();
1465 g_assert (g_key_file_load_from_file (keyfile, "gsettings.store", 0, NULL));
1467 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1468 g_assert_cmpstr (str, ==, "'see if this works'");
1470 g_free (str);
1471 g_key_file_free (keyfile);
1472 g_object_unref (settings);
1475 /* Test that getting child schemas works
1477 static void
1478 test_child_schema (void)
1480 GSettings *settings;
1481 GSettings *child;
1482 guint8 byte;
1484 /* first establish some known conditions */
1485 settings = g_settings_new ("org.gtk.test.basic-types");
1486 g_settings_set (settings, "test-byte", "y", 36);
1488 g_settings_get (settings, "test-byte", "y", &byte);
1489 g_assert_cmpint (byte, ==, 36);
1491 g_object_unref (settings);
1493 settings = g_settings_new ("org.gtk.test");
1494 child = g_settings_get_child (settings, "basic-types");
1495 g_assert (child != NULL);
1497 g_settings_get (child, "test-byte", "y", &byte);
1498 g_assert_cmpint (byte, ==, 36);
1500 g_object_unref (child);
1501 g_object_unref (settings);
1504 static gboolean
1505 glib_translations_work (void)
1507 gboolean works;
1508 gchar *locale;
1509 gchar *orig = "Unnamed";
1511 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
1512 if (!setlocale (LC_MESSAGES, "de"))
1513 works = FALSE;
1514 else
1515 works = dgettext ("glib20", orig) != orig;
1516 setlocale (LC_MESSAGES, locale);
1517 g_free (locale);
1519 return works;
1522 #include "../strinfo.c"
1524 static void
1525 test_strinfo (void)
1527 /* "foo" has a value of 1
1528 * "bar" has a value of 2
1529 * "baz" is an alias for "bar"
1531 gchar array[] =
1532 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1533 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1534 "\0\0\0\xff";
1535 const guint32 *strinfo = (guint32 *) array;
1536 guint length = sizeof array / 4;
1537 guint result;
1540 /* build it and compare */
1541 GString *builder;
1543 builder = g_string_new (NULL);
1544 strinfo_builder_append_item (builder, "foo", 1);
1545 strinfo_builder_append_item (builder, "bar", 2);
1546 g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
1547 g_assert_cmpint (builder->len % 4, ==, 0);
1548 g_assert_cmpint (builder->len / 4, ==, length);
1549 g_assert (memcmp (builder->str, strinfo, length * 4) == 0);
1550 g_string_free (builder, TRUE);
1553 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
1554 ==, NULL);
1555 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
1556 ==, NULL);
1557 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
1558 ==, "bar");
1559 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
1560 ==, NULL);
1562 g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
1563 g_assert_cmpint (result, ==, 1);
1564 g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
1565 g_assert_cmpint (result, ==, 2);
1566 g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
1567 g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
1569 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
1570 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
1571 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
1572 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
1574 g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
1575 g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
1576 g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
1577 g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
1580 static void
1581 test_enums (void)
1583 GSettings *settings, *direct;
1584 gchar *str;
1586 settings = g_settings_new ("org.gtk.test.enums");
1587 direct = g_settings_new ("org.gtk.test.enums.direct");
1589 if (g_test_undefined () && !backend_set)
1591 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1592 g_settings_get_enum (direct, "test");
1593 g_test_trap_assert_failed ();
1594 g_test_trap_assert_stderr ("*not associated with an enum*");
1596 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1597 g_settings_set_enum (settings, "test", 42);
1598 g_test_trap_assert_failed ();
1599 g_test_trap_assert_stderr ("*invalid enum value 42*");
1601 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1602 g_settings_set_string (settings, "test", "qux");
1603 g_test_trap_assert_failed ();
1604 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1606 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1607 g_settings_get_flags (settings, "test");
1608 g_test_trap_assert_failed ();
1609 g_test_trap_assert_stderr ("*not associated with a flags*");
1612 str = g_settings_get_string (settings, "test");
1613 g_assert_cmpstr (str, ==, "bar");
1614 g_free (str);
1616 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
1618 str = g_settings_get_string (settings, "test");
1619 g_assert_cmpstr (str, ==, "foo");
1620 g_free (str);
1622 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
1624 g_settings_set_string (direct, "test", "qux");
1626 str = g_settings_get_string (direct, "test");
1627 g_assert_cmpstr (str, ==, "qux");
1628 g_free (str);
1630 str = g_settings_get_string (settings, "test");
1631 g_assert_cmpstr (str, ==, "quux");
1632 g_free (str);
1634 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
1637 static void
1638 test_flags (void)
1640 GSettings *settings, *direct;
1641 gchar **strv;
1642 gchar *str;
1644 settings = g_settings_new ("org.gtk.test.enums");
1645 direct = g_settings_new ("org.gtk.test.enums.direct");
1647 if (g_test_undefined () && !backend_set)
1649 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1650 g_settings_get_flags (direct, "test");
1651 g_test_trap_assert_failed ();
1652 g_test_trap_assert_stderr ("*not associated with a flags*");
1654 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1655 g_settings_set_flags (settings, "f-test", 0x42);
1656 g_test_trap_assert_failed ();
1657 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
1659 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1660 g_settings_set_strv (settings, "f-test",
1661 (const gchar **) g_strsplit ("rock", ",", 0));
1662 g_test_trap_assert_failed ();
1663 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1665 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1666 g_settings_get_enum (settings, "f-test");
1667 g_test_trap_assert_failed ();
1668 g_test_trap_assert_stderr ("*not associated with an enum*");
1671 strv = g_settings_get_strv (settings, "f-test");
1672 str = g_strjoinv (",", strv);
1673 g_assert_cmpstr (str, ==, "");
1674 g_strfreev (strv);
1675 g_free (str);
1677 g_settings_set_flags (settings, "f-test",
1678 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1680 strv = g_settings_get_strv (settings, "f-test");
1681 str = g_strjoinv (",", strv);
1682 g_assert_cmpstr (str, ==, "talking,walking");
1683 g_strfreev (strv);
1684 g_free (str);
1686 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1687 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1689 strv = g_strsplit ("speaking,laughing", ",", 0);
1690 g_settings_set_strv (direct, "f-test", (const gchar **) strv);
1691 g_strfreev (strv);
1693 strv = g_settings_get_strv (direct, "f-test");
1694 str = g_strjoinv (",", strv);
1695 g_assert_cmpstr (str, ==, "speaking,laughing");
1696 g_strfreev (strv);
1697 g_free (str);
1699 strv = g_settings_get_strv (settings, "f-test");
1700 str = g_strjoinv (",", strv);
1701 g_assert_cmpstr (str, ==, "talking,laughing");
1702 g_strfreev (strv);
1703 g_free (str);
1705 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1706 TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
1709 static void
1710 test_range (void)
1712 GSettings *settings, *direct;
1714 settings = g_settings_new ("org.gtk.test.range");
1715 direct = g_settings_new ("org.gtk.test.range.direct");
1717 if (g_test_undefined () && !backend_set)
1719 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1720 g_settings_set_int (settings, "val", 45);
1721 g_test_trap_assert_failed ();
1722 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1724 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1725 g_settings_set_int (settings, "val", 1);
1726 g_test_trap_assert_failed ();
1727 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1730 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1731 g_settings_set_int (direct, "val", 22);
1732 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
1733 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
1734 g_settings_set_int (direct, "val", 45);
1735 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
1736 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1737 g_settings_set_int (direct, "val", 1);
1738 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
1739 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
1742 static gboolean
1743 strv_has_string (gchar **haystack,
1744 const gchar *needle)
1746 guint n;
1748 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
1750 if (g_strcmp0 (haystack[n], needle) == 0)
1751 return TRUE;
1753 return FALSE;
1756 static gboolean
1757 strv_set_equal (gchar **strv, ...)
1759 gint count;
1760 va_list list;
1761 const gchar *str;
1762 gboolean res;
1764 res = TRUE;
1765 count = 0;
1766 va_start (list, strv);
1767 while (1)
1769 str = va_arg (list, const gchar *);
1770 if (str == NULL)
1771 break;
1772 if (!strv_has_string (strv, str))
1774 res = FALSE;
1775 break;
1777 count++;
1779 va_end (list);
1781 if (res)
1782 res = g_strv_length ((gchar**)strv) == count;
1784 return res;
1787 static void
1788 test_list_items (void)
1790 GSettings *settings;
1791 gchar **children;
1792 gchar **keys;
1794 settings = g_settings_new ("org.gtk.test");
1795 children = g_settings_list_children (settings);
1796 keys = g_settings_list_keys (settings);
1798 g_assert (strv_set_equal (children, "basic-types", "complex-types", "localized", NULL));
1799 g_assert (strv_set_equal (keys, "greeting", "farewell", NULL));
1801 g_strfreev (children);
1802 g_strfreev (keys);
1804 g_object_unref (settings);
1807 static void
1808 test_list_schemas (void)
1810 const gchar * const *schemas;
1811 const gchar * const *relocs;
1813 relocs = g_settings_list_relocatable_schemas ();
1814 schemas = g_settings_list_schemas ();
1816 g_assert (strv_set_equal ((gchar **)relocs,
1817 "org.gtk.test.no-path",
1818 NULL));
1820 g_assert (strv_set_equal ((gchar **)schemas,
1821 "org.gtk.test",
1822 "org.gtk.test.basic-types",
1823 "org.gtk.test.complex-types",
1824 "org.gtk.test.localized",
1825 "org.gtk.test.binding",
1826 "org.gtk.test.enums",
1827 "org.gtk.test.enums.direct",
1828 "org.gtk.test.range",
1829 "org.gtk.test.range.direct",
1830 "org.gtk.test.mapped",
1831 NULL));
1834 static gboolean
1835 map_func (GVariant *value,
1836 gpointer *result,
1837 gpointer user_data)
1839 gint *state = user_data;
1840 gint v;
1842 if (value)
1843 v = g_variant_get_int32 (value);
1844 else
1845 v = -1;
1847 if (*state == 0)
1849 g_assert_cmpint (v, ==, 1);
1850 (*state)++;
1851 return FALSE;
1853 else if (*state == 1)
1855 g_assert_cmpint (v, ==, 0);
1856 (*state)++;
1857 return FALSE;
1859 else
1861 g_assert (value == NULL);
1862 *result = g_variant_new_int32 (5);
1863 return TRUE;
1867 static void
1868 test_get_mapped (void)
1870 GSettings *settings;
1871 gint state;
1872 gpointer p;
1873 gint val;
1875 settings = g_settings_new ("org.gtk.test.mapped");
1876 g_settings_set_int (settings, "val", 1);
1878 state = 0;
1879 p = g_settings_get_mapped (settings, "val", map_func, &state);
1880 val = g_variant_get_int32 ((GVariant*)p);
1881 g_assert_cmpint (val, ==, 5);
1883 g_variant_unref (p);
1884 g_object_unref (settings);
1887 static void
1888 test_get_range (void)
1890 GSettings *settings;
1891 GVariant *range;
1893 settings = g_settings_new ("org.gtk.test.range");
1894 range = g_settings_get_range (settings, "val");
1895 check_and_free (range, "('range', <(2, 44)>)");
1896 g_object_unref (settings);
1898 settings = g_settings_new ("org.gtk.test.enums");
1899 range = g_settings_get_range (settings, "test");
1900 check_and_free (range, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
1901 g_object_unref (settings);
1903 settings = g_settings_new ("org.gtk.test.enums");
1904 range = g_settings_get_range (settings, "f-test");
1905 check_and_free (range, "('flags', "
1906 "<['mourning', 'laughing', 'talking', 'walking']>)");
1907 g_object_unref (settings);
1909 settings = g_settings_new ("org.gtk.test");
1910 range = g_settings_get_range (settings, "greeting");
1911 check_and_free (range, "('type', <@as []>)");
1912 g_object_unref (settings);
1915 static void
1916 test_schema_source (void)
1918 GSettingsSchemaSource *parent;
1919 GSettingsSchemaSource *source;
1920 GSettingsBackend *backend;
1921 GSettingsSchema *schema;
1922 GError *error = NULL;
1923 GSettings *settings;
1924 gboolean enabled;
1926 backend = g_settings_backend_get_default ();
1928 /* make sure it fails properly */
1929 parent = g_settings_schema_source_get_default ();
1930 source = g_settings_schema_source_new_from_directory ("/path/that/does/not/exist", parent, TRUE, &error);
1931 g_assert (source == NULL);
1932 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
1933 g_clear_error (&error);
1935 /* create a source with the parent */
1936 source = g_settings_schema_source_new_from_directory ("schema-source", parent, TRUE, &error);
1937 g_assert_no_error (error);
1938 g_assert (source != NULL);
1940 /* check recursive lookups are working */
1941 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
1942 g_assert (schema != NULL);
1943 g_settings_schema_unref (schema);
1945 /* check recursive lookups for non-existent schemas */
1946 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", TRUE);
1947 g_assert (schema == NULL);
1949 /* check non-recursive for schema that only exists in lower layers */
1950 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
1951 g_assert (schema == NULL);
1953 /* check non-recursive lookup for non-existent */
1954 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", FALSE);
1955 g_assert (schema == NULL);
1957 /* check non-recursive for schema that exists in toplevel */
1958 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
1959 g_assert (schema != NULL);
1960 g_settings_schema_unref (schema);
1962 /* check recursive for schema that exists in toplevel */
1963 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
1964 g_assert (schema != NULL);
1966 /* try to use it for something */
1967 settings = g_settings_new_full (schema, backend, g_settings_schema_get_path (schema));
1968 g_settings_schema_unref (schema);
1969 enabled = FALSE;
1970 g_settings_get (settings, "enabled", "b", &enabled);
1971 g_assert (enabled);
1972 g_object_unref (settings);
1974 g_settings_schema_source_unref (source);
1976 /* try again, but with no parent */
1977 source = g_settings_schema_source_new_from_directory ("schema-source", NULL, FALSE, NULL);
1978 g_assert (source != NULL);
1980 /* should not find it this time, even if recursive... */
1981 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
1982 g_assert (schema == NULL);
1983 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
1984 g_assert (schema == NULL);
1986 /* should still find our own... */
1987 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
1988 g_assert (schema != NULL);
1989 g_settings_schema_unref (schema);
1990 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
1991 g_assert (schema != NULL);
1992 g_settings_schema_unref (schema);
1994 g_settings_schema_source_unref (source);
1998 main (int argc, char *argv[])
2000 gchar *enums;
2001 gint result;
2003 setlocale (LC_ALL, "");
2005 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
2007 g_setenv ("XDG_DATA_DIRS", ".", TRUE);
2008 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
2010 if (!backend_set)
2011 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
2013 g_type_init ();
2014 g_test_init (&argc, &argv, NULL);
2016 g_remove ("org.gtk.test.enums.xml");
2017 g_assert (g_spawn_command_line_sync ("../../gobject/glib-mkenums "
2018 "--template " SRCDIR "/enums.xml.template "
2019 SRCDIR "/testenum.h",
2020 &enums, NULL, &result, NULL));
2021 g_assert (result == 0);
2022 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
2023 g_free (enums);
2025 g_remove ("gschemas.compiled");
2026 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=. "
2027 "--schema-file=org.gtk.test.enums.xml "
2028 "--schema-file=" SRCDIR "/org.gtk.test.gschema.xml",
2029 NULL, NULL, &result, NULL));
2030 g_assert (result == 0);
2032 g_remove ("schema-source/gschemas.compiled");
2033 g_mkdir ("schema-source", 0777);
2034 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=schema-source "
2035 "--schema-file=" SRCDIR "/org.gtk.schemasourcecheck.gschema.xml",
2036 NULL, NULL, &result, NULL));
2037 g_assert (result == 0);
2039 g_test_add_func ("/gsettings/basic", test_basic);
2041 if (!backend_set)
2043 g_test_add_func ("/gsettings/no-schema", test_no_schema);
2044 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
2045 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
2046 g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
2047 g_test_add_func ("/gsettings/no-path", test_no_path);
2050 g_test_add_func ("/gsettings/basic-types", test_basic_types);
2051 g_test_add_func ("/gsettings/complex-types", test_complex_types);
2052 g_test_add_func ("/gsettings/changes", test_changes);
2054 if (glib_translations_work ())
2056 g_test_add_func ("/gsettings/l10n", test_l10n);
2057 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
2060 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
2061 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
2062 g_test_add_func ("/gsettings/atomic", test_atomic);
2064 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
2065 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
2066 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
2067 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
2068 g_test_add_func ("/gsettings/unbinding", test_unbind);
2069 g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
2071 if (!backend_set)
2073 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
2074 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
2075 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
2078 g_test_add_func ("/gsettings/keyfile", test_keyfile);
2079 g_test_add_func ("/gsettings/child-schema", test_child_schema);
2080 g_test_add_func ("/gsettings/strinfo", test_strinfo);
2081 g_test_add_func ("/gsettings/enums", test_enums);
2082 g_test_add_func ("/gsettings/flags", test_flags);
2083 g_test_add_func ("/gsettings/range", test_range);
2084 g_test_add_func ("/gsettings/list-items", test_list_items);
2085 g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
2086 g_test_add_func ("/gsettings/mapped", test_get_mapped);
2087 g_test_add_func ("/gsettings/get-range", test_get_range);
2088 g_test_add_func ("/gsettings/schema-source", test_schema_source);
2090 result = g_test_run ();
2092 g_settings_sync ();
2094 return result;