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