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