gsettings tests: use g_settings_schema_list_keys()
[glib.git] / gio / tests / gsettings.c
blobd0bc5aa258135dec9b9607991aa48dc869b04b06
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 GObject *b;
39 gchar *path;
40 gboolean has_unapplied;
41 gboolean delay_apply;
42 GSettings *settings;
44 settings = g_settings_new ("org.gtk.test");
46 g_object_get (settings,
47 "schema-id", &str,
48 "backend", &b,
49 "path", &path,
50 "has-unapplied", &has_unapplied,
51 "delay-apply", &delay_apply,
52 NULL);
53 g_assert_cmpstr (str, ==, "org.gtk.test");
54 g_assert (b != NULL);
55 g_assert_cmpstr (path, ==, "/tests/");
56 g_assert (!has_unapplied);
57 g_assert (!delay_apply);
58 g_free (str);
59 g_object_unref (b);
60 g_free (path);
62 g_settings_get (settings, "greeting", "s", &str);
63 g_assert_cmpstr (str, ==, "Hello, earthlings");
64 g_free (str);
66 g_settings_set (settings, "greeting", "s", "goodbye world");
67 g_settings_get (settings, "greeting", "s", &str);
68 g_assert_cmpstr (str, ==, "goodbye world");
69 g_free (str);
70 str = NULL;
72 if (!backend_set && g_test_undefined ())
74 GSettings *tmp_settings = g_settings_new ("org.gtk.test");
76 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
77 "*g_settings_set_value*expects type*");
78 g_settings_set (tmp_settings, "greeting", "i", 555);
79 g_test_assert_expected_messages ();
81 g_object_unref (tmp_settings);
84 g_settings_get (settings, "greeting", "s", &str);
85 g_assert_cmpstr (str, ==, "goodbye world");
86 g_free (str);
87 str = NULL;
89 g_settings_reset (settings, "greeting");
90 str = g_settings_get_string (settings, "greeting");
91 g_assert_cmpstr (str, ==, "Hello, earthlings");
92 g_free (str);
94 g_settings_set (settings, "greeting", "s", "this is the end");
95 g_object_unref (settings);
98 /* Check that we get an error when getting a key
99 * that is not in the schema
101 static void
102 test_unknown_key (void)
104 if (!g_test_undefined ())
105 return;
107 if (g_test_subprocess ())
109 GSettings *settings;
110 GVariant *value;
112 settings = g_settings_new ("org.gtk.test");
113 value = g_settings_get_value (settings, "no_such_key");
115 g_assert (value == NULL);
117 g_object_unref (settings);
118 return;
120 g_test_trap_subprocess (NULL, 0, 0);
121 g_test_trap_assert_failed ();
122 g_test_trap_assert_stderr ("*does not contain*");
125 /* Check that we get an error when the schema
126 * has not been installed
128 static void
129 test_no_schema (void)
131 if (!g_test_undefined ())
132 return;
134 if (g_test_subprocess ())
136 GSettings *settings;
138 settings = g_settings_new ("no.such.schema");
140 g_assert (settings == NULL);
141 return;
143 g_test_trap_subprocess (NULL, 0, 0);
144 g_test_trap_assert_failed ();
145 g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
148 /* Check that we get an error when passing a type string
149 * that does not match the schema
151 static void
152 test_wrong_type (void)
154 GSettings *settings;
155 gchar *str = NULL;
157 if (!g_test_undefined ())
158 return;
160 settings = g_settings_new ("org.gtk.test");
162 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
163 "*given value has a type of*");
164 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
165 "*valid_format_string*");
166 g_settings_get (settings, "greeting", "o", &str);
167 g_test_assert_expected_messages ();
169 g_assert (str == NULL);
171 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
172 "*expects type 's'*");
173 g_settings_set (settings, "greeting", "o", "/a/path");
174 g_test_assert_expected_messages ();
176 g_object_unref (settings);
179 /* Check errors with explicit paths */
180 static void
181 test_wrong_path (void)
183 if (!g_test_undefined ())
184 return;
186 if (g_test_subprocess ())
188 GSettings *settings G_GNUC_UNUSED;
190 settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
191 return;
193 g_test_trap_subprocess (NULL, 0, 0);
194 g_test_trap_assert_failed ();
195 g_test_trap_assert_stderr ("*but path * specified by schema*");
198 static void
199 test_no_path (void)
201 if (!g_test_undefined ())
202 return;
204 if (g_test_subprocess ())
206 GSettings *settings G_GNUC_UNUSED;
208 settings = g_settings_new ("org.gtk.test.no-path");
209 return;
211 g_test_trap_subprocess (NULL, 0, 0);
212 g_test_trap_assert_failed ();
213 g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
217 /* Check that we can successfully read and set the full
218 * range of all basic types
220 static void
221 test_basic_types (void)
223 GSettings *settings;
224 gboolean b;
225 guint8 byte;
226 gint16 i16;
227 guint16 u16;
228 gint32 i32;
229 guint32 u32;
230 gint64 i64;
231 guint64 u64;
232 gdouble d;
233 gchar *str;
235 settings = g_settings_new ("org.gtk.test.basic-types");
237 g_settings_get (settings, "test-boolean", "b", &b);
238 g_assert_cmpint (b, ==, 1);
240 g_settings_set (settings, "test-boolean", "b", 0);
241 g_settings_get (settings, "test-boolean", "b", &b);
242 g_assert_cmpint (b, ==, 0);
244 g_settings_get (settings, "test-byte", "y", &byte);
245 g_assert_cmpint (byte, ==, 25);
247 g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
248 g_settings_get (settings, "test-byte", "y", &byte);
249 g_assert_cmpint (byte, ==, G_MAXUINT8);
251 g_settings_get (settings, "test-int16", "n", &i16);
252 g_assert_cmpint (i16, ==, -1234);
254 g_settings_set (settings, "test-int16", "n", G_MININT16);
255 g_settings_get (settings, "test-int16", "n", &i16);
256 g_assert_cmpint (i16, ==, G_MININT16);
258 g_settings_set (settings, "test-int16", "n", G_MAXINT16);
259 g_settings_get (settings, "test-int16", "n", &i16);
260 g_assert_cmpint (i16, ==, G_MAXINT16);
262 g_settings_get (settings, "test-uint16", "q", &u16);
263 g_assert_cmpuint (u16, ==, 1234);
265 g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
266 g_settings_get (settings, "test-uint16", "q", &u16);
267 g_assert_cmpuint (u16, ==, G_MAXUINT16);
269 g_settings_get (settings, "test-int32", "i", &i32);
270 g_assert_cmpint (i32, ==, -123456);
272 g_settings_set (settings, "test-int32", "i", G_MININT32);
273 g_settings_get (settings, "test-int32", "i", &i32);
274 g_assert_cmpint (i32, ==, G_MININT32);
276 g_settings_set (settings, "test-int32", "i", G_MAXINT32);
277 g_settings_get (settings, "test-int32", "i", &i32);
278 g_assert_cmpint (i32, ==, G_MAXINT32);
280 g_settings_get (settings, "test-uint32", "u", &u32);
281 g_assert_cmpuint (u32, ==, 123456);
283 g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
284 g_settings_get (settings, "test-uint32", "u", &u32);
285 g_assert_cmpuint (u32, ==, G_MAXUINT32);
287 g_settings_get (settings, "test-int64", "x", &i64);
288 g_assert_cmpuint (i64, ==, -123456789);
290 g_settings_set (settings, "test-int64", "x", G_MININT64);
291 g_settings_get (settings, "test-int64", "x", &i64);
292 g_assert_cmpuint (i64, ==, G_MININT64);
294 g_settings_set (settings, "test-int64", "x", G_MAXINT64);
295 g_settings_get (settings, "test-int64", "x", &i64);
296 g_assert_cmpuint (i64, ==, G_MAXINT64);
298 g_settings_get (settings, "test-uint64", "t", &u64);
299 g_assert_cmpuint (u64, ==, 123456789);
301 g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
302 g_settings_get (settings, "test-uint64", "t", &u64);
303 g_assert_cmpuint (u64, ==, G_MAXUINT64);
305 g_settings_get (settings, "test-double", "d", &d);
306 g_assert_cmpfloat (d, ==, 123.456);
308 g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
309 g_settings_get (settings, "test-double", "d", &d);
310 g_assert_cmpfloat (d, ==, G_MINDOUBLE);
312 g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
313 g_settings_get (settings, "test-double", "d", &d);
314 g_assert_cmpfloat (d, ==, G_MAXDOUBLE);
316 g_settings_get (settings, "test-string", "s", &str);
317 g_assert_cmpstr (str, ==, "a string, it seems");
318 g_free (str);
319 str = NULL;
321 g_settings_get (settings, "test-objectpath", "o", &str);
322 g_assert_cmpstr (str, ==, "/a/object/path");
323 g_object_unref (settings);
324 g_free (str);
325 str = NULL;
328 /* Check that we can read an set complex types like
329 * tuples, arrays and dictionaries
331 static void
332 test_complex_types (void)
334 GSettings *settings;
335 gchar *s;
336 gint i1, i2;
337 GVariantIter *iter = NULL;
339 settings = g_settings_new ("org.gtk.test.complex-types");
341 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
342 g_assert_cmpstr (s, ==, "one");
343 g_assert_cmpint (i1,==, 2);
344 g_assert_cmpint (i2,==, 3);
345 g_free (s) ;
346 s = NULL;
348 g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
349 g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
350 g_assert_cmpstr (s, ==, "none");
351 g_assert_cmpint (i1,==, 0);
352 g_assert_cmpint (i2,==, 0);
353 g_free (s);
354 s = NULL;
356 g_settings_get (settings, "test-array", "ai", &iter);
357 g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
358 g_assert (g_variant_iter_next (iter, "i", &i1));
359 g_assert_cmpint (i1, ==, 0);
360 g_assert (g_variant_iter_next (iter, "i", &i1));
361 g_assert_cmpint (i1, ==, 1);
362 g_assert (g_variant_iter_next (iter, "i", &i1));
363 g_assert_cmpint (i1, ==, 2);
364 g_assert (g_variant_iter_next (iter, "i", &i1));
365 g_assert_cmpint (i1, ==, 3);
366 g_assert (g_variant_iter_next (iter, "i", &i1));
367 g_assert_cmpint (i1, ==, 4);
368 g_assert (g_variant_iter_next (iter, "i", &i1));
369 g_assert_cmpint (i1, ==, 5);
370 g_assert (!g_variant_iter_next (iter, "i", &i1));
371 g_variant_iter_free (iter);
373 g_object_unref (settings);
376 static gboolean changed_cb_called;
378 static void
379 changed_cb (GSettings *settings,
380 const gchar *key,
381 gpointer data)
383 changed_cb_called = TRUE;
385 g_assert_cmpstr (key, ==, data);
388 /* Test that basic change notification with the changed signal works.
390 static void
391 test_changes (void)
393 GSettings *settings;
394 GSettings *settings2;
396 settings = g_settings_new ("org.gtk.test");
398 g_signal_connect (settings, "changed",
399 G_CALLBACK (changed_cb), "greeting");
401 changed_cb_called = FALSE;
403 g_settings_set (settings, "greeting", "s", "new greeting");
404 g_assert (changed_cb_called);
406 settings2 = g_settings_new ("org.gtk.test");
408 changed_cb_called = FALSE;
410 g_settings_set (settings2, "greeting", "s", "hi");
411 g_assert (changed_cb_called);
413 g_object_unref (settings2);
414 g_object_unref (settings);
417 static gboolean changed_cb_called2;
419 static void
420 changed_cb2 (GSettings *settings,
421 const gchar *key,
422 gpointer data)
424 gboolean *p = data;
426 *p = TRUE;
429 /* Test that changes done to a delay-mode instance
430 * don't appear to the outside world until apply. Also
431 * check that we get change notification when they are
432 * applied.
433 * Also test that the has-unapplied property is properly
434 * maintained.
436 static void
437 test_delay_apply (void)
439 GSettings *settings;
440 GSettings *settings2;
441 gchar *str;
442 gboolean writable;
443 GVariant *v;
444 const gchar *s;
446 settings = g_settings_new ("org.gtk.test");
447 settings2 = g_settings_new ("org.gtk.test");
449 g_settings_set (settings2, "greeting", "s", "top o' the morning");
451 changed_cb_called = FALSE;
452 changed_cb_called2 = FALSE;
454 g_signal_connect (settings, "changed",
455 G_CALLBACK (changed_cb2), &changed_cb_called);
456 g_signal_connect (settings2, "changed",
457 G_CALLBACK (changed_cb2), &changed_cb_called2);
459 g_settings_delay (settings);
461 g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");
463 g_assert (changed_cb_called);
464 g_assert (!changed_cb_called2);
466 writable = g_settings_is_writable (settings, "greeting");
467 g_assert (writable);
469 g_settings_get (settings, "greeting", "s", &str);
470 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
471 g_free (str);
472 str = NULL;
474 v = g_settings_get_user_value (settings, "greeting");
475 s = g_variant_get_string (v, NULL);
476 g_assert_cmpstr (s, ==, "greetings from test_delay_apply");
477 g_variant_unref (v);
479 g_settings_get (settings2, "greeting", "s", &str);
480 g_assert_cmpstr (str, ==, "top o' the morning");
481 g_free (str);
482 str = NULL;
484 g_assert (g_settings_get_has_unapplied (settings));
485 g_assert (!g_settings_get_has_unapplied (settings2));
487 changed_cb_called = FALSE;
488 changed_cb_called2 = FALSE;
490 g_settings_apply (settings);
492 g_assert (!changed_cb_called);
493 g_assert (changed_cb_called2);
495 g_settings_get (settings, "greeting", "s", &str);
496 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
497 g_free (str);
498 str = NULL;
500 g_settings_get (settings2, "greeting", "s", &str);
501 g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
502 g_free (str);
503 str = NULL;
505 g_assert (!g_settings_get_has_unapplied (settings));
506 g_assert (!g_settings_get_has_unapplied (settings2));
508 g_settings_reset (settings, "greeting");
509 g_settings_apply (settings);
511 g_settings_get (settings, "greeting", "s", &str);
512 g_assert_cmpstr (str, ==, "Hello, earthlings");
513 g_free (str);
515 g_object_unref (settings2);
516 g_object_unref (settings);
519 /* Test that reverting unapplied changes in a delay-apply
520 * settings instance works.
522 static void
523 test_delay_revert (void)
525 GSettings *settings;
526 GSettings *settings2;
527 gchar *str;
529 settings = g_settings_new ("org.gtk.test");
530 settings2 = g_settings_new ("org.gtk.test");
532 g_settings_set (settings2, "greeting", "s", "top o' the morning");
534 g_settings_get (settings, "greeting", "s", &str);
535 g_assert_cmpstr (str, ==, "top o' the morning");
536 g_free (str);
538 g_settings_delay (settings);
540 g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");
542 g_settings_get (settings, "greeting", "s", &str);
543 g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
544 g_free (str);
545 str = NULL;
547 g_settings_get (settings2, "greeting", "s", &str);
548 g_assert_cmpstr (str, ==, "top o' the morning");
549 g_free (str);
550 str = NULL;
552 g_assert (g_settings_get_has_unapplied (settings));
554 g_settings_revert (settings);
556 g_assert (!g_settings_get_has_unapplied (settings));
558 g_settings_get (settings, "greeting", "s", &str);
559 g_assert_cmpstr (str, ==, "top o' the morning");
560 g_free (str);
561 str = NULL;
563 g_settings_get (settings2, "greeting", "s", &str);
564 g_assert_cmpstr (str, ==, "top o' the morning");
565 g_free (str);
566 str = NULL;
568 g_object_unref (settings2);
569 g_object_unref (settings);
572 static void
573 test_delay_child (void)
575 GSettings *base;
576 GSettings *settings;
577 GSettings *child;
578 guint8 byte;
579 gboolean delay;
581 base = g_settings_new ("org.gtk.test.basic-types");
582 g_settings_set (base, "test-byte", "y", 36);
584 settings = g_settings_new ("org.gtk.test");
585 g_settings_delay (settings);
586 g_object_get (settings, "delay-apply", &delay, NULL);
587 g_assert (delay);
589 child = g_settings_get_child (settings, "basic-types");
590 g_assert (child != NULL);
592 g_object_get (child, "delay-apply", &delay, NULL);
593 g_assert (!delay);
595 g_settings_get (child, "test-byte", "y", &byte);
596 g_assert_cmpuint (byte, ==, 36);
598 g_settings_set (child, "test-byte", "y", 42);
600 /* make sure the child was delayed too */
601 g_settings_get (base, "test-byte", "y", &byte);
602 g_assert_cmpuint (byte, ==, 36);
604 g_object_unref (child);
605 g_object_unref (settings);
606 g_object_unref (base);
609 static void
610 keys_changed_cb (GSettings *settings,
611 const GQuark *keys,
612 gint n_keys)
614 gchar *str;
616 g_assert_cmpint (n_keys, ==, 2);
618 g_assert ((keys[0] == g_quark_from_static_string ("greeting") &&
619 keys[1] == g_quark_from_static_string ("farewell")) ||
620 (keys[1] == g_quark_from_static_string ("greeting") &&
621 keys[0] == g_quark_from_static_string ("farewell")));
623 g_settings_get (settings, "greeting", "s", &str);
624 g_assert_cmpstr (str, ==, "greetings from test_atomic");
625 g_free (str);
626 str = NULL;
628 g_settings_get (settings, "farewell", "s", &str);
629 g_assert_cmpstr (str, ==, "atomic bye-bye");
630 g_free (str);
631 str = NULL;
634 /* Check that delay-applied changes appear atomically.
635 * More specifically, verify that all changed keys appear
636 * with their new value while handling the change-event signal.
638 static void
639 test_atomic (void)
641 GSettings *settings;
642 GSettings *settings2;
643 gchar *str;
645 settings = g_settings_new ("org.gtk.test");
646 settings2 = g_settings_new ("org.gtk.test");
648 g_settings_set (settings2, "greeting", "s", "top o' the morning");
650 changed_cb_called = FALSE;
651 changed_cb_called2 = FALSE;
653 g_signal_connect (settings2, "change-event",
654 G_CALLBACK (keys_changed_cb), NULL);
656 g_settings_delay (settings);
658 g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
659 g_settings_set (settings, "farewell", "s", "atomic bye-bye");
661 g_settings_apply (settings);
663 g_settings_get (settings, "greeting", "s", &str);
664 g_assert_cmpstr (str, ==, "greetings from test_atomic");
665 g_free (str);
666 str = NULL;
668 g_settings_get (settings, "farewell", "s", &str);
669 g_assert_cmpstr (str, ==, "atomic bye-bye");
670 g_free (str);
671 str = NULL;
673 g_settings_get (settings2, "greeting", "s", &str);
674 g_assert_cmpstr (str, ==, "greetings from test_atomic");
675 g_free (str);
676 str = NULL;
678 g_settings_get (settings2, "farewell", "s", &str);
679 g_assert_cmpstr (str, ==, "atomic bye-bye");
680 g_free (str);
681 str = NULL;
683 g_object_unref (settings2);
684 g_object_unref (settings);
687 /* On Windows the interaction between the C library locale and libintl
688 * (from GNU gettext) is not like on POSIX, so just skip these tests
689 * for now.
691 * There are several issues:
693 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
694 * in libintl (defined in its <libintl.h>).
696 * 2) The locale names that setlocale() accepts and returns aren't in
697 * the "de_DE" style, but like "German_Germany".
699 * 3) libintl looks at the Win32 thread locale and not the C library
700 * locale. (And even if libintl would use the C library's locale, as
701 * there are several alternative C library DLLs, libintl might be
702 * linked to a different one than the application code, so they
703 * wouldn't have the same C library locale anyway.)
706 /* Test that translations work for schema defaults.
708 * This test relies on the de.po file in the same directory
709 * to be compiled into ./de/LC_MESSAGES/test.mo
711 static void
712 test_l10n (void)
714 GSettings *settings;
715 gchar *str;
716 gchar *locale;
718 bindtextdomain ("test", ".");
719 bind_textdomain_codeset ("test", "UTF-8");
721 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
723 settings = g_settings_new ("org.gtk.test.localized");
725 setlocale (LC_MESSAGES, "C");
726 str = g_settings_get_string (settings, "error-message");
727 setlocale (LC_MESSAGES, locale);
729 g_assert_cmpstr (str, ==, "Unnamed");
730 g_free (str);
731 str = NULL;
733 setlocale (LC_MESSAGES, "de_DE");
734 /* Only do the test if translation is actually working... */
735 if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
737 str = g_settings_get_string (settings, "error-message");
739 g_assert_cmpstr (str, ==, "Unbenannt");
740 g_object_unref (settings);
741 g_free (str);
742 str = NULL;
744 else
745 g_printerr ("warning: translation is not working... skipping test. ");
747 setlocale (LC_MESSAGES, locale);
748 g_free (locale);
751 /* Test that message context works as expected with translated
752 * schema defaults. Also, verify that non-ASCII UTF-8 content
753 * works.
755 * This test relies on the de.po file in the same directory
756 * to be compiled into ./de/LC_MESSAGES/test.mo
758 static void
759 test_l10n_context (void)
761 GSettings *settings;
762 gchar *str;
763 gchar *locale;
765 bindtextdomain ("test", ".");
766 bind_textdomain_codeset ("test", "UTF-8");
768 locale = g_strdup (setlocale (LC_MESSAGES, NULL));
770 settings = g_settings_new ("org.gtk.test.localized");
772 setlocale (LC_MESSAGES, "C");
773 g_settings_get (settings, "backspace", "s", &str);
774 setlocale (LC_MESSAGES, locale);
776 g_assert_cmpstr (str, ==, "BackSpace");
777 g_free (str);
778 str = NULL;
780 setlocale (LC_MESSAGES, "de_DE");
781 /* Only do the test if translation is actually working... */
782 if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
784 g_settings_get (settings, "backspace", "s", &str);
786 g_assert_cmpstr (str, ==, "Löschen");
787 g_object_unref (settings);
788 g_free (str);
789 str = NULL;
791 else
792 g_printerr ("warning: translation is not working... skipping test. ");
794 setlocale (LC_MESSAGES, locale);
795 g_free (locale);
798 enum
800 PROP_0,
801 PROP_BOOL,
802 PROP_ANTI_BOOL,
803 PROP_BYTE,
804 PROP_INT16,
805 PROP_UINT16,
806 PROP_INT,
807 PROP_UINT,
808 PROP_INT64,
809 PROP_UINT64,
810 PROP_DOUBLE,
811 PROP_STRING,
812 PROP_NO_READ,
813 PROP_NO_WRITE,
814 PROP_STRV,
815 PROP_ENUM,
816 PROP_FLAGS
819 typedef struct
821 GObject parent_instance;
823 gboolean bool_prop;
824 gboolean anti_bool_prop;
825 gint8 byte_prop;
826 gint int16_prop;
827 guint16 uint16_prop;
828 gint int_prop;
829 guint uint_prop;
830 gint64 int64_prop;
831 guint64 uint64_prop;
832 gdouble double_prop;
833 gchar *string_prop;
834 gchar *no_read_prop;
835 gchar *no_write_prop;
836 gchar **strv_prop;
837 guint enum_prop;
838 guint flags_prop;
839 } TestObject;
841 typedef struct
843 GObjectClass parent_class;
844 } TestObjectClass;
846 static GType test_object_get_type (void);
847 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
849 static void
850 test_object_init (TestObject *object)
854 static void
855 test_object_finalize (GObject *object)
857 TestObject *testo = (TestObject*)object;
858 g_strfreev (testo->strv_prop);
859 g_free (testo->string_prop);
860 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
863 static void
864 test_object_get_property (GObject *object,
865 guint prop_id,
866 GValue *value,
867 GParamSpec *pspec)
869 TestObject *test_object = (TestObject *)object;
871 switch (prop_id)
873 case PROP_BOOL:
874 g_value_set_boolean (value, test_object->bool_prop);
875 break;
876 case PROP_ANTI_BOOL:
877 g_value_set_boolean (value, test_object->anti_bool_prop);
878 break;
879 case PROP_BYTE:
880 g_value_set_schar (value, test_object->byte_prop);
881 break;
882 case PROP_UINT16:
883 g_value_set_uint (value, test_object->uint16_prop);
884 break;
885 case PROP_INT16:
886 g_value_set_int (value, test_object->int16_prop);
887 break;
888 case PROP_INT:
889 g_value_set_int (value, test_object->int_prop);
890 break;
891 case PROP_UINT:
892 g_value_set_uint (value, test_object->uint_prop);
893 break;
894 case PROP_INT64:
895 g_value_set_int64 (value, test_object->int64_prop);
896 break;
897 case PROP_UINT64:
898 g_value_set_uint64 (value, test_object->uint64_prop);
899 break;
900 case PROP_DOUBLE:
901 g_value_set_double (value, test_object->double_prop);
902 break;
903 case PROP_STRING:
904 g_value_set_string (value, test_object->string_prop);
905 break;
906 case PROP_NO_WRITE:
907 g_value_set_string (value, test_object->no_write_prop);
908 break;
909 case PROP_STRV:
910 g_value_set_boxed (value, test_object->strv_prop);
911 break;
912 case PROP_ENUM:
913 g_value_set_enum (value, test_object->enum_prop);
914 break;
915 case PROP_FLAGS:
916 g_value_set_flags (value, test_object->flags_prop);
917 break;
918 default:
919 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
920 break;
924 static void
925 test_object_set_property (GObject *object,
926 guint prop_id,
927 const GValue *value,
928 GParamSpec *pspec)
930 TestObject *test_object = (TestObject *)object;
932 switch (prop_id)
934 case PROP_BOOL:
935 test_object->bool_prop = g_value_get_boolean (value);
936 break;
937 case PROP_ANTI_BOOL:
938 test_object->anti_bool_prop = g_value_get_boolean (value);
939 break;
940 case PROP_BYTE:
941 test_object->byte_prop = g_value_get_schar (value);
942 break;
943 case PROP_INT16:
944 test_object->int16_prop = g_value_get_int (value);
945 break;
946 case PROP_UINT16:
947 test_object->uint16_prop = g_value_get_uint (value);
948 break;
949 case PROP_INT:
950 test_object->int_prop = g_value_get_int (value);
951 break;
952 case PROP_UINT:
953 test_object->uint_prop = g_value_get_uint (value);
954 break;
955 case PROP_INT64:
956 test_object->int64_prop = g_value_get_int64 (value);
957 break;
958 case PROP_UINT64:
959 test_object->uint64_prop = g_value_get_uint64 (value);
960 break;
961 case PROP_DOUBLE:
962 test_object->double_prop = g_value_get_double (value);
963 break;
964 case PROP_STRING:
965 g_free (test_object->string_prop);
966 test_object->string_prop = g_value_dup_string (value);
967 break;
968 case PROP_NO_READ:
969 g_free (test_object->no_read_prop);
970 test_object->no_read_prop = g_value_dup_string (value);
971 break;
972 case PROP_STRV:
973 g_strfreev (test_object->strv_prop);
974 test_object->strv_prop = g_value_dup_boxed (value);
975 break;
976 case PROP_ENUM:
977 test_object->enum_prop = g_value_get_enum (value);
978 break;
979 case PROP_FLAGS:
980 test_object->flags_prop = g_value_get_flags (value);
981 break;
982 default:
983 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
984 break;
988 static GType
989 test_enum_get_type (void)
991 static volatile gsize define_type_id = 0;
993 if (g_once_init_enter (&define_type_id))
995 static const GEnumValue values[] = {
996 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
997 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
998 { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
999 { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
1000 { 0, NULL, NULL }
1003 GType type_id = g_enum_register_static ("TestEnum", values);
1004 g_once_init_leave (&define_type_id, type_id);
1007 return define_type_id;
1010 static GType
1011 test_flags_get_type (void)
1013 static volatile gsize define_type_id = 0;
1015 if (g_once_init_enter (&define_type_id))
1017 static const GFlagsValue values[] = {
1018 { TEST_FLAGS_NONE, "TEST_FLAGS_NONE", "none" },
1019 { TEST_FLAGS_MOURNING, "TEST_FLAGS_MOURNING", "mourning" },
1020 { TEST_FLAGS_LAUGHING, "TEST_FLAGS_LAUGHING", "laughing" },
1021 { TEST_FLAGS_WALKING, "TEST_FLAGS_WALKING", "walking" },
1022 { 0, NULL, NULL }
1025 GType type_id = g_flags_register_static ("TestFlags", values);
1026 g_once_init_leave (&define_type_id, type_id);
1029 return define_type_id;
1032 static void
1033 test_object_class_init (TestObjectClass *class)
1035 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
1037 gobject_class->get_property = test_object_get_property;
1038 gobject_class->set_property = test_object_set_property;
1039 gobject_class->finalize = test_object_finalize;
1041 g_object_class_install_property (gobject_class, PROP_BOOL,
1042 g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
1043 g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
1044 g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
1045 g_object_class_install_property (gobject_class, PROP_BYTE,
1046 g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
1047 g_object_class_install_property (gobject_class, PROP_INT16,
1048 g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
1049 g_object_class_install_property (gobject_class, PROP_UINT16,
1050 g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
1051 g_object_class_install_property (gobject_class, PROP_INT,
1052 g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
1053 g_object_class_install_property (gobject_class, PROP_UINT,
1054 g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
1055 g_object_class_install_property (gobject_class, PROP_INT64,
1056 g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
1057 g_object_class_install_property (gobject_class, PROP_UINT64,
1058 g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
1059 g_object_class_install_property (gobject_class, PROP_DOUBLE,
1060 g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
1061 g_object_class_install_property (gobject_class, PROP_STRING,
1062 g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
1063 g_object_class_install_property (gobject_class, PROP_NO_WRITE,
1064 g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
1065 g_object_class_install_property (gobject_class, PROP_NO_READ,
1066 g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
1067 g_object_class_install_property (gobject_class, PROP_STRV,
1068 g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
1069 g_object_class_install_property (gobject_class, PROP_ENUM,
1070 g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
1071 g_object_class_install_property (gobject_class, PROP_FLAGS,
1072 g_param_spec_flags ("flags", "", "", test_flags_get_type (), TEST_FLAGS_NONE, G_PARAM_READWRITE));
1075 static TestObject *
1076 test_object_new (void)
1078 return (TestObject*)g_object_new (test_object_get_type (), NULL);
1081 /* Test basic binding functionality for simple types.
1082 * Verify that with bidirectional bindings, changes on either side
1083 * are notified on the other end.
1085 static void
1086 test_simple_binding (void)
1088 TestObject *obj;
1089 GSettings *settings;
1090 gboolean b;
1091 gchar y;
1092 gint i;
1093 guint u;
1094 gint16 n;
1095 guint16 q;
1096 gint n2;
1097 guint q2;
1098 gint64 i64;
1099 guint64 u64;
1100 gdouble d;
1101 gchar *s;
1102 GVariant *value;
1103 gchar **strv;
1105 settings = g_settings_new ("org.gtk.test.binding");
1106 obj = test_object_new ();
1108 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
1109 g_object_set (obj, "bool", TRUE, NULL);
1110 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1112 g_settings_set_boolean (settings, "bool", FALSE);
1113 b = TRUE;
1114 g_object_get (obj, "bool", &b, NULL);
1115 g_assert_cmpint (b, ==, FALSE);
1117 g_settings_bind (settings, "anti-bool", obj, "anti-bool",
1118 G_SETTINGS_BIND_INVERT_BOOLEAN);
1119 g_object_set (obj, "anti-bool", FALSE, NULL);
1120 g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);
1122 g_settings_set_boolean (settings, "anti-bool", FALSE);
1123 b = FALSE;
1124 g_object_get (obj, "anti-bool", &b, NULL);
1125 g_assert_cmpint (b, ==, TRUE);
1127 g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);
1129 g_object_set (obj, "byte", 123, NULL);
1130 y = 'c';
1131 g_settings_get (settings, "byte", "y", &y);
1132 g_assert_cmpint (y, ==, 123);
1134 g_settings_set (settings, "byte", "y", 54);
1135 y = 'c';
1136 g_object_get (obj, "byte", &y, NULL);
1137 g_assert_cmpint (y, ==, 54);
1139 g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);
1141 g_object_set (obj, "int16", 1234, NULL);
1142 n = 4321;
1143 g_settings_get (settings, "int16", "n", &n);
1144 g_assert_cmpint (n, ==, 1234);
1146 g_settings_set (settings, "int16", "n", 4321);
1147 n2 = 1111;
1148 g_object_get (obj, "int16", &n2, NULL);
1149 g_assert_cmpint (n2, ==, 4321);
1151 g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);
1153 g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
1154 q = 1111;
1155 g_settings_get (settings, "uint16", "q", &q);
1156 g_assert_cmpuint (q, ==, G_MAXUINT16);
1158 g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1159 q2 = 1111;
1160 g_object_get (obj, "uint16", &q2, NULL);
1161 g_assert_cmpuint (q2, ==, (guint16) G_MAXINT16);
1163 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1165 g_object_set (obj, "int", 12345, NULL);
1166 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1168 g_settings_set_int (settings, "int", 54321);
1169 i = 1111;
1170 g_object_get (obj, "int", &i, NULL);
1171 g_assert_cmpint (i, ==, 54321);
1173 g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);
1175 g_object_set (obj, "uint", 12345, NULL);
1176 g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);
1178 g_settings_set_uint (settings, "uint", 54321);
1179 u = 1111;
1180 g_object_get (obj, "uint", &u, NULL);
1181 g_assert_cmpuint (u, ==, 54321);
1183 g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);
1185 g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
1186 i64 = 1111;
1187 g_settings_get (settings, "int64", "x", &i64);
1188 g_assert_cmpint (i64, ==, G_MAXINT64);
1190 g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
1191 i64 = 1111;
1192 g_object_get (obj, "int64", &i64, NULL);
1193 g_assert_cmpint (i64, ==, G_MININT64);
1195 g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);
1197 g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
1198 u64 = 1111;
1199 g_settings_get (settings, "uint64", "t", &u64);
1200 g_assert_cmpuint (u64, ==, G_MAXUINT64);
1202 g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
1203 u64 = 1111;
1204 g_object_get (obj, "uint64", &u64, NULL);
1205 g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1207 g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);
1209 g_object_set (obj, "string", "bu ba", NULL);
1210 s = g_settings_get_string (settings, "string");
1211 g_assert_cmpstr (s, ==, "bu ba");
1212 g_free (s);
1214 g_settings_set_string (settings, "string", "bla bla");
1215 g_object_get (obj, "string", &s, NULL);
1216 g_assert_cmpstr (s, ==, "bla bla");
1217 g_free (s);
1219 g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);
1221 g_object_set (obj, "string", "non-unicode:\315", NULL);
1222 value = g_settings_get_value (settings, "chararray");
1223 g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
1224 g_variant_unref (value);
1226 g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);
1228 g_object_set (obj, "double", G_MAXFLOAT, NULL);
1229 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
1231 g_settings_set_double (settings, "double", G_MINFLOAT);
1232 d = 1.0;
1233 g_object_get (obj, "double", &d, NULL);
1234 g_assert_cmpfloat (d, ==, G_MINFLOAT);
1236 g_object_set (obj, "double", G_MAXDOUBLE, NULL);
1237 g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);
1239 g_settings_set_double (settings, "double", -G_MINDOUBLE);
1240 d = 1.0;
1241 g_object_get (obj, "double", &d, NULL);
1242 g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
1244 strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
1245 g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
1246 g_object_set (obj, "strv", strv, NULL);
1247 g_strfreev (strv);
1248 strv = g_settings_get_strv (settings, "strv");
1249 s = g_strjoinv (",", strv);
1250 g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
1251 g_strfreev (strv);
1252 g_free (s);
1253 strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
1254 g_settings_set_strv (settings, "strv", (const gchar **) strv);
1255 g_strfreev (strv);
1256 g_object_get (obj, "strv", &strv, NULL);
1257 s = g_strjoinv (",", strv);
1258 g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
1259 g_strfreev (strv);
1260 g_free (s);
1261 g_settings_set_strv (settings, "strv", NULL);
1262 g_object_get (obj, "strv", &strv, NULL);
1263 g_assert (strv != NULL);
1264 g_assert_cmpint (g_strv_length (strv), ==, 0);
1265 g_strfreev (strv);
1267 g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
1268 g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1269 s = g_settings_get_string (settings, "enum");
1270 g_assert_cmpstr (s, ==, "baz");
1271 g_free (s);
1272 g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);
1274 g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
1275 i = 230;
1276 g_object_get (obj, "enum", &i, NULL);
1277 g_assert_cmpint (i, ==, TEST_ENUM_QUUX);
1279 g_settings_set_string (settings, "enum", "baz");
1280 i = 230;
1281 g_object_get (obj, "enum", &i, NULL);
1282 g_assert_cmpint (i, ==, TEST_ENUM_BAZ);
1284 g_settings_bind (settings, "flags", obj, "flags", G_SETTINGS_BIND_DEFAULT);
1285 g_object_set (obj, "flags", TEST_FLAGS_MOURNING, NULL);
1286 strv = g_settings_get_strv (settings, "flags");
1287 g_assert_cmpint (g_strv_length (strv), ==, 1);
1288 g_assert_cmpstr (strv[0], ==, "mourning");
1289 g_strfreev (strv);
1291 g_assert_cmpint (g_settings_get_flags (settings, "flags"), ==, TEST_FLAGS_MOURNING);
1293 g_settings_set_flags (settings, "flags", TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1294 i = 230;
1295 g_object_get (obj, "flags", &i, NULL);
1296 g_assert_cmpint (i, ==, TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
1298 g_object_unref (obj);
1299 g_object_unref (settings);
1302 static void
1303 test_unbind (void)
1305 TestObject *obj;
1306 GSettings *settings;
1308 settings = g_settings_new ("org.gtk.test.binding");
1309 obj = test_object_new ();
1311 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);
1313 g_object_set (obj, "int", 12345, NULL);
1314 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1316 g_settings_unbind (obj, "int");
1318 g_object_set (obj, "int", 54321, NULL);
1319 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);
1321 g_object_unref (obj);
1322 g_object_unref (settings);
1325 static void
1326 test_bind_writable (void)
1328 TestObject *obj;
1329 GSettings *settings;
1330 gboolean b;
1332 settings = g_settings_new ("org.gtk.test.binding");
1333 obj = test_object_new ();
1335 g_object_set (obj, "bool", FALSE, NULL);
1337 g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
1339 g_object_get (obj, "bool", &b, NULL);
1340 g_assert (b);
1342 g_settings_unbind (obj, "bool");
1344 g_settings_bind_writable (settings, "int", obj, "bool", TRUE);
1346 g_object_get (obj, "bool", &b, NULL);
1347 g_assert (!b);
1349 g_object_unref (obj);
1350 g_object_unref (settings);
1353 /* Test one-way bindings.
1354 * Verify that changes on one side show up on the other,
1355 * but not vice versa
1357 static void
1358 test_directional_binding (void)
1360 TestObject *obj;
1361 GSettings *settings;
1362 gboolean b;
1363 gint i;
1365 settings = g_settings_new ("org.gtk.test.binding");
1366 obj = test_object_new ();
1368 g_object_set (obj, "bool", FALSE, NULL);
1369 g_settings_set_boolean (settings, "bool", FALSE);
1371 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);
1373 g_settings_set_boolean (settings, "bool", TRUE);
1374 g_object_get (obj, "bool", &b, NULL);
1375 g_assert_cmpint (b, ==, TRUE);
1377 g_object_set (obj, "bool", FALSE, NULL);
1378 g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);
1380 g_object_set (obj, "int", 20, NULL);
1381 g_settings_set_int (settings, "int", 20);
1383 g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);
1385 g_object_set (obj, "int", 32, NULL);
1386 g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);
1388 g_settings_set_int (settings, "int", 20);
1389 g_object_get (obj, "int", &i, NULL);
1390 g_assert_cmpint (i, ==, 32);
1392 g_object_unref (obj);
1393 g_object_unref (settings);
1396 /* Test that type mismatch is caught when creating a binding */
1397 static void
1398 test_typesafe_binding (void)
1400 if (!g_test_undefined ())
1401 return;
1403 if (g_test_subprocess ())
1405 TestObject *obj;
1406 GSettings *settings;
1408 settings = g_settings_new ("org.gtk.test.binding");
1409 obj = test_object_new ();
1411 g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);
1413 g_object_unref (obj);
1414 g_object_unref (settings);
1415 return;
1417 g_test_trap_subprocess (NULL, 0, 0);
1418 g_test_trap_assert_failed ();
1419 g_test_trap_assert_stderr ("*not compatible*");
1422 static gboolean
1423 string_to_bool (GValue *value,
1424 GVariant *variant,
1425 gpointer user_data)
1427 const gchar *s;
1429 s = g_variant_get_string (variant, NULL);
1430 g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);
1432 return TRUE;
1435 static GVariant *
1436 bool_to_string (const GValue *value,
1437 const GVariantType *expected_type,
1438 gpointer user_data)
1440 if (g_value_get_boolean (value))
1441 return g_variant_new_string ("true");
1442 else
1443 return g_variant_new_string ("false");
1446 /* Test custom bindings.
1447 * Translate strings to booleans and back
1449 static void
1450 test_custom_binding (void)
1452 TestObject *obj;
1453 GSettings *settings;
1454 gchar *s;
1455 gboolean b;
1457 settings = g_settings_new ("org.gtk.test.binding");
1458 obj = test_object_new ();
1460 g_settings_set_string (settings, "string", "true");
1462 g_settings_bind_with_mapping (settings, "string",
1463 obj, "bool",
1464 G_SETTINGS_BIND_DEFAULT,
1465 string_to_bool,
1466 bool_to_string,
1467 NULL, NULL);
1469 g_settings_set_string (settings, "string", "false");
1470 g_object_get (obj, "bool", &b, NULL);
1471 g_assert_cmpint (b, ==, FALSE);
1473 g_settings_set_string (settings, "string", "not true");
1474 g_object_get (obj, "bool", &b, NULL);
1475 g_assert_cmpint (b, ==, FALSE);
1477 g_object_set (obj, "bool", TRUE, NULL);
1478 s = g_settings_get_string (settings, "string");
1479 g_assert_cmpstr (s, ==, "true");
1480 g_free (s);
1482 g_object_unref (obj);
1483 g_object_unref (settings);
1486 /* Test that with G_SETTINGS_BIND_NO_CHANGES, the
1487 * initial settings value is transported to the object
1488 * side, but later settings changes do not affect the
1489 * object
1491 static void
1492 test_no_change_binding (void)
1494 TestObject *obj;
1495 GSettings *settings;
1496 gboolean b;
1498 settings = g_settings_new ("org.gtk.test.binding");
1499 obj = test_object_new ();
1501 g_object_set (obj, "bool", TRUE, NULL);
1502 g_settings_set_boolean (settings, "bool", FALSE);
1504 g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);
1506 g_object_get (obj, "bool", &b, NULL);
1507 g_assert_cmpint (b, ==, FALSE);
1509 g_settings_set_boolean (settings, "bool", TRUE);
1510 g_object_get (obj, "bool", &b, NULL);
1511 g_assert_cmpint (b, ==, FALSE);
1513 g_settings_set_boolean (settings, "bool", FALSE);
1514 g_object_set (obj, "bool", TRUE, NULL);
1515 b = g_settings_get_boolean (settings, "bool");
1516 g_assert_cmpint (b, ==, TRUE);
1518 g_object_unref (obj);
1519 g_object_unref (settings);
1522 /* Test that binding a non-readable property only
1523 * works in 'GET' mode.
1525 static void
1526 test_no_read_binding_fail (void)
1528 TestObject *obj;
1529 GSettings *settings;
1531 settings = g_settings_new ("org.gtk.test.binding");
1532 obj = test_object_new ();
1534 g_settings_bind (settings, "string", obj, "no-read", 0);
1537 static void
1538 test_no_read_binding_pass (void)
1540 TestObject *obj;
1541 GSettings *settings;
1543 settings = g_settings_new ("org.gtk.test.binding");
1544 obj = test_object_new ();
1546 g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1548 exit (0);
1551 static void
1552 test_no_read_binding (void)
1554 if (g_test_undefined ())
1556 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/fail", 0, 0);
1557 g_test_trap_assert_failed ();
1558 g_test_trap_assert_stderr ("*property*is not readable*");
1561 g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/pass", 0, 0);
1562 g_test_trap_assert_passed ();
1565 /* Test that binding a non-writable property only
1566 * works in 'SET' mode.
1568 static void
1569 test_no_write_binding_fail (void)
1571 TestObject *obj;
1572 GSettings *settings;
1574 settings = g_settings_new ("org.gtk.test.binding");
1575 obj = test_object_new ();
1577 g_settings_bind (settings, "string", obj, "no-write", 0);
1580 static void
1581 test_no_write_binding_pass (void)
1583 TestObject *obj;
1584 GSettings *settings;
1586 settings = g_settings_new ("org.gtk.test.binding");
1587 obj = test_object_new ();
1589 g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1591 exit (0);
1594 static void
1595 test_no_write_binding (void)
1597 if (g_test_undefined ())
1599 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/fail", 0, 0);
1600 g_test_trap_assert_failed ();
1601 g_test_trap_assert_stderr ("*property*is not writable*");
1604 g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/pass", 0, 0);
1605 g_test_trap_assert_passed ();
1608 static void
1609 key_changed_cb (GSettings *settings, const gchar *key, gpointer data)
1611 gboolean *b = data;
1612 (*b) = TRUE;
1616 * Test that using a keyfile works
1618 static void
1619 test_keyfile (void)
1621 GSettingsBackend *kf_backend;
1622 GSettings *settings;
1623 GKeyFile *keyfile;
1624 gchar *str;
1625 gboolean writable;
1626 GError *error = NULL;
1627 gchar *data;
1628 gsize len;
1629 gboolean called = FALSE;
1631 g_remove ("keyfile/gsettings.store");
1632 g_rmdir ("keyfile");
1634 kf_backend = g_keyfile_settings_backend_new ("keyfile/gsettings.store", "/", "root");
1635 settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
1636 g_object_unref (kf_backend);
1638 g_settings_reset (settings, "greeting");
1639 str = g_settings_get_string (settings, "greeting");
1640 g_assert_cmpstr (str, ==, "Hello, earthlings");
1641 g_free (str);
1643 writable = g_settings_is_writable (settings, "greeting");
1644 g_assert (writable);
1645 g_settings_set (settings, "greeting", "s", "see if this works");
1647 str = g_settings_get_string (settings, "greeting");
1648 g_assert_cmpstr (str, ==, "see if this works");
1649 g_free (str);
1651 g_settings_delay (settings);
1652 g_settings_set (settings, "farewell", "s", "cheerio");
1653 g_settings_apply (settings);
1655 keyfile = g_key_file_new ();
1656 g_assert (g_key_file_load_from_file (keyfile, "keyfile/gsettings.store", 0, NULL));
1658 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1659 g_assert_cmpstr (str, ==, "'see if this works'");
1660 g_free (str);
1662 str = g_key_file_get_string (keyfile, "tests", "farewell", NULL);
1663 g_assert_cmpstr (str, ==, "'cheerio'");
1664 g_free (str);
1665 g_key_file_free (keyfile);
1667 g_settings_reset (settings, "greeting");
1668 g_settings_apply (settings);
1669 keyfile = g_key_file_new ();
1670 g_assert (g_key_file_load_from_file (keyfile, "keyfile/gsettings.store", 0, NULL));
1672 str = g_key_file_get_string (keyfile, "tests", "greeting", NULL);
1673 g_assert (str == NULL);
1675 called = FALSE;
1676 g_signal_connect (settings, "changed::greeting", G_CALLBACK (key_changed_cb), &called);
1678 g_key_file_set_string (keyfile, "tests", "greeting", "'howdy'");
1679 data = g_key_file_to_data (keyfile, &len, NULL);
1680 g_file_set_contents ("keyfile/gsettings.store", data, len, &error);
1681 g_assert_no_error (error);
1682 while (!called)
1683 g_main_context_iteration (NULL, FALSE);
1684 g_signal_handlers_disconnect_by_func (settings, key_changed_cb, &called);
1686 str = g_settings_get_string (settings, "greeting");
1687 g_assert_cmpstr (str, ==, "howdy");
1688 g_free (str);
1690 g_settings_set (settings, "farewell", "s", "cheerio");
1692 called = FALSE;
1693 g_signal_connect (settings, "writable-changed::greeting", G_CALLBACK (key_changed_cb), &called);
1695 g_chmod ("keyfile", 0500);
1696 while (!called)
1697 g_main_context_iteration (NULL, FALSE);
1698 g_signal_handlers_disconnect_by_func (settings, key_changed_cb, &called);
1700 writable = g_settings_is_writable (settings, "greeting");
1701 g_assert (!writable);
1703 g_key_file_free (keyfile);
1704 g_free (data);
1706 g_object_unref (settings);
1707 g_chmod ("keyfile", 0777);
1710 /* Test that getting child schemas works
1712 static void
1713 test_child_schema (void)
1715 GSettings *settings;
1716 GSettings *child;
1717 guint8 byte;
1719 /* first establish some known conditions */
1720 settings = g_settings_new ("org.gtk.test.basic-types");
1721 g_settings_set (settings, "test-byte", "y", 36);
1723 g_settings_get (settings, "test-byte", "y", &byte);
1724 g_assert_cmpint (byte, ==, 36);
1726 g_object_unref (settings);
1728 settings = g_settings_new ("org.gtk.test");
1729 child = g_settings_get_child (settings, "basic-types");
1730 g_assert (child != NULL);
1732 g_settings_get (child, "test-byte", "y", &byte);
1733 g_assert_cmpint (byte, ==, 36);
1735 g_object_unref (child);
1736 g_object_unref (settings);
1739 #include "../strinfo.c"
1741 static void
1742 test_strinfo (void)
1744 /* "foo" has a value of 1
1745 * "bar" has a value of 2
1746 * "baz" is an alias for "bar"
1748 gchar array[] =
1749 "\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
1750 "\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
1751 "\0\0\0\xff";
1752 const guint32 *strinfo = (guint32 *) array;
1753 guint length = sizeof array / 4;
1754 guint result;
1757 /* build it and compare */
1758 GString *builder;
1760 builder = g_string_new (NULL);
1761 strinfo_builder_append_item (builder, "foo", 1);
1762 strinfo_builder_append_item (builder, "bar", 2);
1763 g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
1764 g_assert_cmpint (builder->len % 4, ==, 0);
1765 g_assert_cmpint (builder->len / 4, ==, length);
1766 g_assert (memcmp (builder->str, strinfo, length * 4) == 0);
1767 g_string_free (builder, TRUE);
1770 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
1771 ==, NULL);
1772 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
1773 ==, NULL);
1774 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
1775 ==, "bar");
1776 g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
1777 ==, NULL);
1779 g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
1780 g_assert_cmpint (result, ==, 1);
1781 g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
1782 g_assert_cmpint (result, ==, 2);
1783 g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
1784 g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
1786 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
1787 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
1788 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
1789 g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
1791 g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
1792 g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
1793 g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
1794 g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
1797 static void
1798 test_enums_non_enum_key (void)
1800 GSettings *direct;
1802 direct = g_settings_new ("org.gtk.test.enums.direct");
1803 g_settings_get_enum (direct, "test");
1804 g_assert_not_reached ();
1807 static void
1808 test_enums_non_enum_value (void)
1810 GSettings *settings;
1812 settings = g_settings_new ("org.gtk.test.enums");
1813 g_settings_set_enum (settings, "test", 42);
1814 g_assert_not_reached ();
1817 static void
1818 test_enums_range (void)
1820 GSettings *settings;
1822 settings = g_settings_new ("org.gtk.test.enums");
1823 g_settings_set_string (settings, "test", "qux");
1824 g_assert_not_reached ();
1827 static void
1828 test_enums_non_flags (void)
1830 GSettings *settings;
1832 settings = g_settings_new ("org.gtk.test.enums");
1833 g_settings_get_flags (settings, "test");
1834 g_assert_not_reached ();
1837 static void
1838 test_enums (void)
1840 GSettings *settings, *direct;
1841 gchar *str;
1843 settings = g_settings_new ("org.gtk.test.enums");
1844 direct = g_settings_new ("org.gtk.test.enums.direct");
1846 if (g_test_undefined () && !backend_set)
1848 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-key", 0, 0);
1849 g_test_trap_assert_failed ();
1850 g_test_trap_assert_stderr ("*not associated with an enum*");
1852 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-enum-value", 0, 0);
1853 g_test_trap_assert_failed ();
1854 g_test_trap_assert_stderr ("*invalid enum value 42*");
1856 g_test_trap_subprocess ("/gsettings/enums/subprocess/range", 0, 0);
1857 g_test_trap_assert_failed ();
1858 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1860 g_test_trap_subprocess ("/gsettings/enums/subprocess/non-flags", 0, 0);
1861 g_test_trap_assert_failed ();
1862 g_test_trap_assert_stderr ("*not associated with a flags*");
1865 str = g_settings_get_string (settings, "test");
1866 g_assert_cmpstr (str, ==, "bar");
1867 g_free (str);
1869 g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
1871 str = g_settings_get_string (settings, "test");
1872 g_assert_cmpstr (str, ==, "foo");
1873 g_free (str);
1875 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
1877 g_settings_set_string (direct, "test", "qux");
1879 str = g_settings_get_string (direct, "test");
1880 g_assert_cmpstr (str, ==, "qux");
1881 g_free (str);
1883 str = g_settings_get_string (settings, "test");
1884 g_assert_cmpstr (str, ==, "quux");
1885 g_free (str);
1887 g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
1890 static void
1891 test_flags_non_flags_key (void)
1893 GSettings *direct;
1895 direct = g_settings_new ("org.gtk.test.enums.direct");
1896 g_settings_get_flags (direct, "test");
1897 g_assert_not_reached ();
1900 static void
1901 test_flags_non_flags_value (void)
1903 GSettings *settings;
1905 settings = g_settings_new ("org.gtk.test.enums");
1906 g_settings_set_flags (settings, "f-test", 0x42);
1907 g_assert_not_reached ();
1910 static void
1911 test_flags_range (void)
1913 GSettings *settings;
1915 settings = g_settings_new ("org.gtk.test.enums");
1916 g_settings_set_strv (settings, "f-test",
1917 (const gchar **) g_strsplit ("rock", ",", 0));
1918 g_assert_not_reached ();
1921 static void
1922 test_flags_non_enum (void)
1924 GSettings *settings;
1926 settings = g_settings_new ("org.gtk.test.enums");
1927 g_settings_get_enum (settings, "f-test");
1928 g_assert_not_reached ();
1931 static void
1932 test_flags (void)
1934 GSettings *settings, *direct;
1935 gchar **strv;
1936 gchar *str;
1938 settings = g_settings_new ("org.gtk.test.enums");
1939 direct = g_settings_new ("org.gtk.test.enums.direct");
1941 if (g_test_undefined () && !backend_set)
1943 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-key", 0, 0);
1944 g_test_trap_assert_failed ();
1945 g_test_trap_assert_stderr ("*not associated with a flags*");
1947 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-flags-value", 0, 0);
1948 g_test_trap_assert_failed ();
1949 g_test_trap_assert_stderr ("*invalid flags value 0x00000042*");
1951 g_test_trap_subprocess ("/gsettings/flags/subprocess/range", 0, 0);
1952 g_test_trap_assert_failed ();
1953 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
1955 g_test_trap_subprocess ("/gsettings/flags/subprocess/non-enum", 0, 0);
1956 g_test_trap_assert_failed ();
1957 g_test_trap_assert_stderr ("*not associated with an enum*");
1960 strv = g_settings_get_strv (settings, "f-test");
1961 str = g_strjoinv (",", strv);
1962 g_assert_cmpstr (str, ==, "");
1963 g_strfreev (strv);
1964 g_free (str);
1966 g_settings_set_flags (settings, "f-test",
1967 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1969 strv = g_settings_get_strv (settings, "f-test");
1970 str = g_strjoinv (",", strv);
1971 g_assert_cmpstr (str, ==, "talking,walking");
1972 g_strfreev (strv);
1973 g_free (str);
1975 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1976 TEST_FLAGS_WALKING | TEST_FLAGS_TALKING);
1978 strv = g_strsplit ("speaking,laughing", ",", 0);
1979 g_settings_set_strv (direct, "f-test", (const gchar **) strv);
1980 g_strfreev (strv);
1982 strv = g_settings_get_strv (direct, "f-test");
1983 str = g_strjoinv (",", strv);
1984 g_assert_cmpstr (str, ==, "speaking,laughing");
1985 g_strfreev (strv);
1986 g_free (str);
1988 strv = g_settings_get_strv (settings, "f-test");
1989 str = g_strjoinv (",", strv);
1990 g_assert_cmpstr (str, ==, "talking,laughing");
1991 g_strfreev (strv);
1992 g_free (str);
1994 g_assert_cmpint (g_settings_get_flags (settings, "f-test"), ==,
1995 TEST_FLAGS_TALKING | TEST_FLAGS_LAUGHING);
1998 static void
1999 test_range_high (void)
2001 GSettings *settings;
2003 settings = g_settings_new ("org.gtk.test.range");
2004 g_settings_set_int (settings, "val", 45);
2005 g_assert_not_reached ();
2008 static void
2009 test_range_low (void)
2011 GSettings *settings;
2013 settings = g_settings_new ("org.gtk.test.range");
2014 g_settings_set_int (settings, "val", 1);
2015 g_assert_not_reached ();
2018 static void
2019 test_range (void)
2021 GSettings *settings, *direct;
2022 GVariant *value;
2024 settings = g_settings_new ("org.gtk.test.range");
2025 direct = g_settings_new ("org.gtk.test.range.direct");
2027 if (g_test_undefined () && !backend_set)
2029 g_test_trap_subprocess ("/gsettings/range/subprocess/high", 0, 0);
2030 g_test_trap_assert_failed ();
2031 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2033 g_test_trap_subprocess ("/gsettings/range/subprocess/low", 0, 0);
2034 g_test_trap_assert_failed ();
2035 g_test_trap_assert_stderr ("*g_settings_set_value*valid range*");
2038 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2039 g_settings_set_int (direct, "val", 22);
2040 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
2041 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
2042 g_settings_set_int (direct, "val", 45);
2043 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
2044 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2045 g_settings_set_int (direct, "val", 1);
2046 g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
2047 g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
2049 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2050 value = g_variant_new_int32 (1);
2051 g_assert (!g_settings_range_check (settings, "val", value));
2052 g_variant_unref (value);
2053 value = g_variant_new_int32 (33);
2054 g_assert (g_settings_range_check (settings, "val", value));
2055 g_variant_unref (value);
2056 value = g_variant_new_int32 (45);
2057 g_assert (!g_settings_range_check (settings, "val", value));
2058 g_variant_unref (value);
2059 G_GNUC_END_IGNORE_DEPRECATIONS
2062 static gboolean
2063 strv_has_string (gchar **haystack,
2064 const gchar *needle)
2066 guint n;
2068 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
2070 if (g_strcmp0 (haystack[n], needle) == 0)
2071 return TRUE;
2073 return FALSE;
2076 static gboolean
2077 strv_set_equal (gchar **strv, ...)
2079 gint count;
2080 va_list list;
2081 const gchar *str;
2082 gboolean res;
2084 res = TRUE;
2085 count = 0;
2086 va_start (list, strv);
2087 while (1)
2089 str = va_arg (list, const gchar *);
2090 if (str == NULL)
2091 break;
2092 if (!strv_has_string (strv, str))
2094 res = FALSE;
2095 break;
2097 count++;
2099 va_end (list);
2101 if (res)
2102 res = g_strv_length ((gchar**)strv) == count;
2104 return res;
2107 static void
2108 test_list_items (void)
2110 GSettingsSchema *schema;
2111 GSettings *settings;
2112 gchar **children;
2113 gchar **keys;
2115 settings = g_settings_new ("org.gtk.test");
2116 g_object_get (settings, "settings-schema", &schema, NULL);
2117 children = g_settings_list_children (settings);
2118 keys = g_settings_schema_list_keys (schema);
2120 g_assert (strv_set_equal (children, "basic-types", "complex-types", "localized", NULL));
2121 g_assert (strv_set_equal (keys, "greeting", "farewell", NULL));
2123 g_strfreev (children);
2124 g_strfreev (keys);
2126 g_settings_schema_unref (schema);
2127 g_object_unref (settings);
2130 static void
2131 test_list_schemas (void)
2133 const gchar * const *schemas;
2134 const gchar * const *relocs;
2136 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2137 relocs = g_settings_list_relocatable_schemas ();
2138 schemas = g_settings_list_schemas ();
2139 G_GNUC_END_IGNORE_DEPRECATIONS
2141 g_assert (strv_set_equal ((gchar **)relocs,
2142 "org.gtk.test.no-path",
2143 "org.gtk.test.extends.base",
2144 "org.gtk.test.extends.extended",
2145 NULL));
2147 g_assert (strv_set_equal ((gchar **)schemas,
2148 "org.gtk.test",
2149 "org.gtk.test.basic-types",
2150 "org.gtk.test.complex-types",
2151 "org.gtk.test.localized",
2152 "org.gtk.test.binding",
2153 "org.gtk.test.enums",
2154 "org.gtk.test.enums.direct",
2155 "org.gtk.test.range",
2156 "org.gtk.test.range.direct",
2157 "org.gtk.test.mapped",
2158 "org.gtk.test.descriptions",
2159 NULL));
2162 static gboolean
2163 map_func (GVariant *value,
2164 gpointer *result,
2165 gpointer user_data)
2167 gint *state = user_data;
2168 gint v;
2170 if (value)
2171 v = g_variant_get_int32 (value);
2172 else
2173 v = -1;
2175 if (*state == 0)
2177 g_assert_cmpint (v, ==, 1);
2178 (*state)++;
2179 return FALSE;
2181 else if (*state == 1)
2183 g_assert_cmpint (v, ==, 0);
2184 (*state)++;
2185 return FALSE;
2187 else
2189 g_assert (value == NULL);
2190 *result = g_variant_new_int32 (5);
2191 return TRUE;
2195 static void
2196 test_get_mapped (void)
2198 GSettings *settings;
2199 gint state;
2200 gpointer p;
2201 gint val;
2203 settings = g_settings_new ("org.gtk.test.mapped");
2204 g_settings_set_int (settings, "val", 1);
2206 state = 0;
2207 p = g_settings_get_mapped (settings, "val", map_func, &state);
2208 val = g_variant_get_int32 ((GVariant*)p);
2209 g_assert_cmpint (val, ==, 5);
2211 g_variant_unref (p);
2212 g_object_unref (settings);
2215 static void
2216 test_get_range (void)
2218 GSettings *settings;
2219 GVariant *range;
2221 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2222 settings = g_settings_new ("org.gtk.test.range");
2223 range = g_settings_get_range (settings, "val");
2224 check_and_free (range, "('range', <(2, 44)>)");
2225 g_object_unref (settings);
2227 settings = g_settings_new ("org.gtk.test.enums");
2228 range = g_settings_get_range (settings, "test");
2229 check_and_free (range, "('enum', <['foo', 'bar', 'baz', 'quux']>)");
2230 g_object_unref (settings);
2232 settings = g_settings_new ("org.gtk.test.enums");
2233 range = g_settings_get_range (settings, "f-test");
2234 check_and_free (range, "('flags', "
2235 "<['mourning', 'laughing', 'talking', 'walking']>)");
2236 g_object_unref (settings);
2238 settings = g_settings_new ("org.gtk.test");
2239 range = g_settings_get_range (settings, "greeting");
2240 check_and_free (range, "('type', <@as []>)");
2241 g_object_unref (settings);
2242 G_GNUC_END_IGNORE_DEPRECATIONS
2245 static void
2246 test_schema_source (void)
2248 GSettingsSchemaSource *parent;
2249 GSettingsSchemaSource *source;
2250 GSettingsBackend *backend;
2251 GSettingsSchema *schema;
2252 GError *error = NULL;
2253 GSettings *settings;
2254 gboolean enabled;
2256 backend = g_settings_backend_get_default ();
2258 /* make sure it fails properly */
2259 parent = g_settings_schema_source_get_default ();
2260 source = g_settings_schema_source_new_from_directory ("/path/that/does/not/exist", parent, TRUE, &error);
2261 g_assert (source == NULL);
2262 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
2263 g_clear_error (&error);
2265 /* create a source with the parent */
2266 source = g_settings_schema_source_new_from_directory ("schema-source", parent, TRUE, &error);
2267 g_assert_no_error (error);
2268 g_assert (source != NULL);
2270 /* check recursive lookups are working */
2271 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2272 g_assert (schema != NULL);
2273 g_settings_schema_unref (schema);
2275 /* check recursive lookups for non-existent schemas */
2276 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", TRUE);
2277 g_assert (schema == NULL);
2279 /* check non-recursive for schema that only exists in lower layers */
2280 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2281 g_assert (schema == NULL);
2283 /* check non-recursive lookup for non-existent */
2284 schema = g_settings_schema_source_lookup (source, "org.gtk.doesnotexist", FALSE);
2285 g_assert (schema == NULL);
2287 /* check non-recursive for schema that exists in toplevel */
2288 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2289 g_assert (schema != NULL);
2290 g_settings_schema_unref (schema);
2292 /* check recursive for schema that exists in toplevel */
2293 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2294 g_assert (schema != NULL);
2296 /* try to use it for something */
2297 settings = g_settings_new_full (schema, backend, g_settings_schema_get_path (schema));
2298 g_settings_schema_unref (schema);
2299 enabled = FALSE;
2300 g_settings_get (settings, "enabled", "b", &enabled);
2301 g_assert (enabled);
2302 g_object_unref (settings);
2304 g_settings_schema_source_unref (source);
2306 /* try again, but with no parent */
2307 source = g_settings_schema_source_new_from_directory ("schema-source", NULL, FALSE, NULL);
2308 g_assert (source != NULL);
2310 /* should not find it this time, even if recursive... */
2311 schema = g_settings_schema_source_lookup (source, "org.gtk.test", FALSE);
2312 g_assert (schema == NULL);
2313 schema = g_settings_schema_source_lookup (source, "org.gtk.test", TRUE);
2314 g_assert (schema == NULL);
2316 /* should still find our own... */
2317 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", TRUE);
2318 g_assert (schema != NULL);
2319 g_settings_schema_unref (schema);
2320 schema = g_settings_schema_source_lookup (source, "org.gtk.schemasourcecheck", FALSE);
2321 g_assert (schema != NULL);
2322 g_settings_schema_unref (schema);
2324 g_settings_schema_source_unref (source);
2327 static void
2328 test_schema_list_keys (void)
2330 gchar **keys;
2331 GSettingsSchemaSource *src = g_settings_schema_source_get_default ();
2332 GSettingsSchema *schema = g_settings_schema_source_lookup (src, "org.gtk.test", TRUE);
2333 g_assert (schema != NULL);
2335 keys = g_settings_schema_list_keys (schema);
2337 g_assert (strv_set_equal ((gchar **)keys,
2338 "greeting",
2339 "farewell",
2340 NULL));
2343 g_strfreev (keys);
2344 g_settings_schema_unref (schema);
2345 g_settings_schema_source_unref (src);
2348 static void
2349 test_actions (void)
2351 GAction *string, *toggle;
2352 gboolean c1, c2, c3;
2353 GSettings *settings;
2354 gchar *name;
2355 GVariantType *param_type;
2356 gboolean enabled;
2357 GVariantType *state_type;
2358 GVariant *state;
2360 settings = g_settings_new ("org.gtk.test.basic-types");
2361 string = g_settings_create_action (settings, "test-string");
2362 toggle = g_settings_create_action (settings, "test-boolean");
2363 g_object_unref (settings); /* should be held by the actions */
2365 g_signal_connect (settings, "changed", G_CALLBACK (changed_cb2), &c1);
2366 g_signal_connect (string, "notify::state", G_CALLBACK (changed_cb2), &c2);
2367 g_signal_connect (toggle, "notify::state", G_CALLBACK (changed_cb2), &c3);
2369 c1 = c2 = c3 = FALSE;
2370 g_settings_set_string (settings, "test-string", "hello world");
2371 check_and_free (g_action_get_state (string), "'hello world'");
2372 g_assert (c1 && c2 && !c3);
2373 c1 = c2 = c3 = FALSE;
2375 g_action_activate (string, g_variant_new_string ("hihi"));
2376 check_and_free (g_settings_get_value (settings, "test-string"), "'hihi'");
2377 g_assert (c1 && c2 && !c3);
2378 c1 = c2 = c3 = FALSE;
2380 g_action_change_state (string, g_variant_new_string ("kthxbye"));
2381 check_and_free (g_settings_get_value (settings, "test-string"), "'kthxbye'");
2382 g_assert (c1 && c2 && !c3);
2383 c1 = c2 = c3 = FALSE;
2385 g_action_change_state (toggle, g_variant_new_boolean (TRUE));
2386 g_assert (g_settings_get_boolean (settings, "test-boolean"));
2387 g_assert (c1 && !c2 && c3);
2388 c1 = c2 = c3 = FALSE;
2390 g_action_activate (toggle, NULL);
2391 g_assert (!g_settings_get_boolean (settings, "test-boolean"));
2392 g_assert (c1 && !c2 && c3);
2394 g_object_get (string,
2395 "name", &name,
2396 "parameter-type", &param_type,
2397 "enabled", &enabled,
2398 "state-type", &state_type,
2399 "state", &state,
2400 NULL);
2402 g_assert_cmpstr (name, ==, "test-string");
2403 g_assert (g_variant_type_equal (param_type, G_VARIANT_TYPE_STRING));
2404 g_assert (enabled);
2405 g_assert (g_variant_type_equal (state_type, G_VARIANT_TYPE_STRING));
2406 g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "kthxbye");
2408 g_free (name);
2409 g_variant_unref (state);
2411 g_object_unref (string);
2412 g_object_unref (toggle);
2415 static void
2416 test_null_backend (void)
2418 GSettingsBackend *backend;
2419 GSettings *settings;
2420 gchar *str;
2421 gboolean writable;
2423 backend = g_null_settings_backend_new ();
2424 settings = g_settings_new_with_backend_and_path ("org.gtk.test", backend, "/tests/");
2426 g_object_get (settings, "schema-id", &str, NULL);
2427 g_assert_cmpstr (str, ==, "org.gtk.test");
2428 g_free (str);
2430 g_settings_get (settings, "greeting", "s", &str);
2431 g_assert_cmpstr (str, ==, "Hello, earthlings");
2432 g_free (str);
2434 g_settings_set (settings, "greeting", "s", "goodbye world");
2435 g_settings_get (settings, "greeting", "s", &str);
2436 g_assert_cmpstr (str, ==, "Hello, earthlings");
2437 g_free (str);
2439 writable = g_settings_is_writable (settings, "greeting");
2440 g_assert (!writable);
2442 g_settings_reset (settings, "greeting");
2444 g_settings_delay (settings);
2445 g_settings_set (settings, "greeting", "s", "goodbye world");
2446 g_settings_apply (settings);
2447 g_settings_get (settings, "greeting", "s", &str);
2448 g_assert_cmpstr (str, ==, "Hello, earthlings");
2449 g_free (str);
2451 g_object_unref (settings);
2452 g_object_unref (backend);
2455 static void
2456 test_memory_backend (void)
2458 GSettingsBackend *backend;
2460 backend = g_memory_settings_backend_new ();
2461 g_assert (G_IS_SETTINGS_BACKEND (backend));
2462 g_object_unref (backend);
2465 static void
2466 test_read_descriptions (void)
2468 GSettingsSchema *schema;
2469 GSettingsSchemaKey *key;
2470 GSettings *settings;
2472 settings = g_settings_new ("org.gtk.test");
2473 g_object_get (settings, "settings-schema", &schema, NULL);
2474 key = g_settings_schema_get_key (schema, "greeting");
2476 g_assert_cmpstr (g_settings_schema_key_get_summary (key), ==, "A greeting");
2477 g_assert_cmpstr (g_settings_schema_key_get_description (key), ==, "Greeting of the invading martians");
2479 g_settings_schema_key_unref (key);
2480 g_settings_schema_unref (schema);
2482 g_object_unref (settings);
2484 settings = g_settings_new ("org.gtk.test.descriptions");
2485 g_object_get (settings, "settings-schema", &schema, NULL);
2486 key = g_settings_schema_get_key (schema, "a");
2488 g_assert_cmpstr (g_settings_schema_key_get_summary (key), ==,
2489 "a paragraph.\n\n"
2490 "with some whitespace.\n\n"
2491 "because not everyone has a great editor.\n\n"
2492 "lots of space is as one.");
2494 g_settings_schema_key_unref (key);
2495 g_settings_schema_unref (schema);
2497 g_object_unref (settings);
2500 static void
2501 test_default_value (void)
2503 GSettings *settings;
2504 GSettingsSchema *schema;
2505 GSettingsSchemaKey *key;
2506 GVariant *v;
2507 const gchar *str;
2508 gchar *s;
2510 settings = g_settings_new ("org.gtk.test");
2511 g_object_get (settings, "settings-schema", &schema, NULL);
2512 key = g_settings_schema_get_key (schema, "greeting");
2513 g_settings_schema_unref (schema);
2514 g_settings_schema_key_ref (key);
2516 g_assert (g_variant_type_equal (g_settings_schema_key_get_value_type (key), G_VARIANT_TYPE_STRING));
2518 v = g_settings_schema_key_get_default_value (key);
2519 str = g_variant_get_string (v, NULL);
2520 g_assert_cmpstr (str, ==, "Hello, earthlings");
2521 g_variant_unref (v);
2523 g_settings_schema_key_unref (key);
2524 g_settings_schema_key_unref (key);
2526 g_settings_set (settings, "greeting", "s", "goodbye world");
2528 v = g_settings_get_user_value (settings, "greeting");
2529 str = g_variant_get_string (v, NULL);
2530 g_assert_cmpstr (str, ==, "goodbye world");
2531 g_variant_unref (v);
2533 v = g_settings_get_default_value (settings, "greeting");
2534 str = g_variant_get_string (v, NULL);
2535 g_assert_cmpstr (str, ==, "Hello, earthlings");
2536 g_variant_unref (v);
2538 g_settings_reset (settings, "greeting");
2540 v = g_settings_get_user_value (settings, "greeting");
2541 g_assert_null (v);
2543 s = g_settings_get_string (settings, "greeting");
2544 g_assert_cmpstr (s, ==, "Hello, earthlings");
2545 g_free (s);
2547 g_object_unref (settings);
2550 static void
2551 test_extended_schema (void)
2553 GSettingsSchema *schema;
2554 GSettings *settings;
2555 gchar **keys;
2557 settings = g_settings_new_with_path ("org.gtk.test.extends.extended", "/test/extendes/");
2558 g_object_get (settings, "settings-schema", &schema, NULL);
2559 keys = g_settings_schema_list_keys (schema);
2560 g_assert (strv_set_equal (keys, "int32", "string", "another-int32", NULL));
2561 g_strfreev (keys);
2562 g_object_unref (settings);
2563 g_settings_schema_unref (schema);
2567 main (int argc, char *argv[])
2569 gchar *schema_text;
2570 gchar *enums;
2571 gint result;
2573 setlocale (LC_ALL, "");
2575 g_test_init (&argc, &argv, NULL);
2577 if (!g_test_subprocess ())
2579 backend_set = g_getenv ("GSETTINGS_BACKEND") != NULL;
2581 g_setenv ("XDG_DATA_DIRS", ".", TRUE);
2582 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
2584 if (!backend_set)
2585 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
2587 g_remove ("org.gtk.test.enums.xml");
2588 g_assert (g_spawn_command_line_sync ("../../gobject/glib-mkenums "
2589 "--template " SRCDIR "/enums.xml.template "
2590 SRCDIR "/testenum.h",
2591 &enums, NULL, &result, NULL));
2592 g_assert (result == 0);
2593 g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
2594 g_free (enums);
2596 g_assert (g_file_get_contents (SRCDIR "/org.gtk.test.gschema.xml.orig", &schema_text, NULL, NULL));
2597 g_assert (g_file_set_contents ("org.gtk.test.gschema.xml", schema_text, -1, NULL));
2599 g_remove ("gschemas.compiled");
2600 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=. "
2601 "--schema-file=org.gtk.test.enums.xml "
2602 "--schema-file=org.gtk.test.gschema.xml",
2603 NULL, NULL, &result, NULL));
2604 g_assert (result == 0);
2606 g_remove ("schema-source/gschemas.compiled");
2607 g_mkdir ("schema-source", 0777);
2608 g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=schema-source "
2609 "--schema-file=" SRCDIR "/org.gtk.schemasourcecheck.gschema.xml",
2610 NULL, NULL, &result, NULL));
2611 g_assert (result == 0);
2614 g_test_add_func ("/gsettings/basic", test_basic);
2616 if (!backend_set)
2618 g_test_add_func ("/gsettings/no-schema", test_no_schema);
2619 g_test_add_func ("/gsettings/unknown-key", test_unknown_key);
2620 g_test_add_func ("/gsettings/wrong-type", test_wrong_type);
2621 g_test_add_func ("/gsettings/wrong-path", test_wrong_path);
2622 g_test_add_func ("/gsettings/no-path", test_no_path);
2625 g_test_add_func ("/gsettings/basic-types", test_basic_types);
2626 g_test_add_func ("/gsettings/complex-types", test_complex_types);
2627 g_test_add_func ("/gsettings/changes", test_changes);
2629 g_test_add_func ("/gsettings/l10n", test_l10n);
2630 g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
2632 g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
2633 g_test_add_func ("/gsettings/delay-revert", test_delay_revert);
2634 g_test_add_func ("/gsettings/delay-child", test_delay_child);
2635 g_test_add_func ("/gsettings/atomic", test_atomic);
2637 g_test_add_func ("/gsettings/simple-binding", test_simple_binding);
2638 g_test_add_func ("/gsettings/directional-binding", test_directional_binding);
2639 g_test_add_func ("/gsettings/custom-binding", test_custom_binding);
2640 g_test_add_func ("/gsettings/no-change-binding", test_no_change_binding);
2641 g_test_add_func ("/gsettings/unbinding", test_unbind);
2642 g_test_add_func ("/gsettings/writable-binding", test_bind_writable);
2644 if (!backend_set)
2646 g_test_add_func ("/gsettings/typesafe-binding", test_typesafe_binding);
2647 g_test_add_func ("/gsettings/no-read-binding", test_no_read_binding);
2648 g_test_add_func ("/gsettings/no-read-binding/subprocess/fail", test_no_read_binding_fail);
2649 g_test_add_func ("/gsettings/no-read-binding/subprocess/pass", test_no_read_binding_pass);
2650 g_test_add_func ("/gsettings/no-write-binding", test_no_write_binding);
2651 g_test_add_func ("/gsettings/no-write-binding/subprocess/fail", test_no_write_binding_fail);
2652 g_test_add_func ("/gsettings/no-write-binding/subprocess/pass", test_no_write_binding_pass);
2655 g_test_add_func ("/gsettings/keyfile", test_keyfile);
2656 g_test_add_func ("/gsettings/child-schema", test_child_schema);
2657 g_test_add_func ("/gsettings/strinfo", test_strinfo);
2658 g_test_add_func ("/gsettings/enums", test_enums);
2659 g_test_add_func ("/gsettings/enums/subprocess/non-enum-key", test_enums_non_enum_key);
2660 g_test_add_func ("/gsettings/enums/subprocess/non-enum-value", test_enums_non_enum_value);
2661 g_test_add_func ("/gsettings/enums/subprocess/range", test_enums_range);
2662 g_test_add_func ("/gsettings/enums/subprocess/non-flags", test_enums_non_flags);
2663 g_test_add_func ("/gsettings/flags", test_flags);
2664 g_test_add_func ("/gsettings/flags/subprocess/non-flags-key", test_flags_non_flags_key);
2665 g_test_add_func ("/gsettings/flags/subprocess/non-flags-value", test_flags_non_flags_value);
2666 g_test_add_func ("/gsettings/flags/subprocess/range", test_flags_range);
2667 g_test_add_func ("/gsettings/flags/subprocess/non-enum", test_flags_non_enum);
2668 g_test_add_func ("/gsettings/range", test_range);
2669 g_test_add_func ("/gsettings/range/subprocess/high", test_range_high);
2670 g_test_add_func ("/gsettings/range/subprocess/low", test_range_low);
2671 g_test_add_func ("/gsettings/list-items", test_list_items);
2672 g_test_add_func ("/gsettings/list-schemas", test_list_schemas);
2673 g_test_add_func ("/gsettings/mapped", test_get_mapped);
2674 g_test_add_func ("/gsettings/get-range", test_get_range);
2675 g_test_add_func ("/gsettings/schema-source", test_schema_source);
2676 g_test_add_func ("/gsettings/schema-list-keys", test_schema_list_keys);
2677 g_test_add_func ("/gsettings/actions", test_actions);
2678 g_test_add_func ("/gsettings/null-backend", test_null_backend);
2679 g_test_add_func ("/gsettings/memory-backend", test_memory_backend);
2680 g_test_add_func ("/gsettings/read-descriptions", test_read_descriptions);
2681 g_test_add_func ("/gsettings/test-extended-schema", test_extended_schema);
2682 g_test_add_func ("/gsettings/default-value", test_default_value);
2684 result = g_test_run ();
2686 g_settings_sync ();
2688 return result;