gsettings.c 87 KB
Newer Older
Matthias Clasen's avatar
Matthias Clasen committed
1 2 3
#include <stdlib.h>
#include <locale.h>
#include <libintl.h>
4 5
#include <unistd.h>
#include <sys/types.h>
6
#include <gio/gio.h>
Matthias Clasen's avatar
Matthias Clasen committed
7
#include <gstdio.h>
Matthias Clasen's avatar
Matthias Clasen committed
8 9
#define G_SETTINGS_ENABLE_BACKEND
#include <gio/gsettingsbackend.h>
Matthias Clasen's avatar
Matthias Clasen committed
10

11 12
#include "testenum.h"

13 14
static const gchar *locale_dir = ".";

15 16
static gboolean backend_set;

Matthias Clasen's avatar
Matthias Clasen committed
17 18 19 20
/* These tests rely on the schemas in org.gtk.test.gschema.xml
 * to be compiled and installed in the same directory.
 */

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
typedef struct
{
  gchar *tmp_dir;
} Fixture;

static void
setup (Fixture       *fixture,
       gconstpointer  user_data)
{
  GError *error = NULL;

  fixture->tmp_dir = g_dir_make_tmp ("gio-test-gsettings_XXXXXX", &error);
  g_assert_no_error (error);

  g_test_message ("Using temporary directory: %s", fixture->tmp_dir);
}

static void
teardown (Fixture       *fixture,
          gconstpointer  user_data)
{
  g_assert_cmpint (g_rmdir (fixture->tmp_dir), ==, 0);
  g_clear_pointer (&fixture->tmp_dir, g_free);
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59
static void
check_and_free (GVariant    *value,
                const gchar *expected)
{
  gchar *printed;

  printed = g_variant_print (value, TRUE);
  g_assert_cmpstr (printed, ==, expected);
  g_free (printed);

  g_variant_unref (value);
}


Matthias Clasen's avatar
Matthias Clasen committed
60 61 62
/* Just to get warmed up: Read and set a string, and
 * verify that can read the changed string back
 */
Matthias Clasen's avatar
Matthias Clasen committed
63 64 65 66
static void
test_basic (void)
{
  gchar *str = NULL;
67 68 69 70
  GObject *b;
  gchar *path;
  gboolean has_unapplied;
  gboolean delay_apply;
Matthias Clasen's avatar
Matthias Clasen committed
71 72 73 74
  GSettings *settings;

  settings = g_settings_new ("org.gtk.test");

75
  g_object_get (settings,
76
                "schema-id", &str,
77 78 79 80 81
                "backend", &b,
                "path", &path,
                "has-unapplied", &has_unapplied,
                "delay-apply", &delay_apply,
                NULL);
82
  g_assert_cmpstr (str, ==, "org.gtk.test");
83
  g_assert_nonnull (b);
84
  g_assert_cmpstr (path, ==, "/tests/");
85 86
  g_assert_false (has_unapplied);
  g_assert_false (delay_apply);
87
  g_free (str);
88 89
  g_object_unref (b);
  g_free (path);
90

Matthias Clasen's avatar
Matthias Clasen committed
91 92
  g_settings_get (settings, "greeting", "s", &str);
  g_assert_cmpstr (str, ==, "Hello, earthlings");
93
  g_free (str);
Matthias Clasen's avatar
Matthias Clasen committed
94 95 96 97 98 99 100

  g_settings_set (settings, "greeting", "s", "goodbye world");
  g_settings_get (settings, "greeting", "s", &str);
  g_assert_cmpstr (str, ==, "goodbye world");
  g_free (str);
  str = NULL;

101
  if (!backend_set && g_test_undefined ())
Matthias Clasen's avatar
Matthias Clasen committed
102
    {
103 104 105 106 107 108 109 110
      GSettings *tmp_settings = g_settings_new ("org.gtk.test");

      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
                             "*g_settings_set_value*expects type*");
      g_settings_set (tmp_settings, "greeting", "i", 555);
      g_test_assert_expected_messages ();

      g_object_unref (tmp_settings);
Matthias Clasen's avatar
Matthias Clasen committed
111 112 113 114 115 116 117
    }

  g_settings_get (settings, "greeting", "s", &str);
  g_assert_cmpstr (str, ==, "goodbye world");
  g_free (str);
  str = NULL;

118 119 120 121 122
  g_settings_reset (settings, "greeting");
  str = g_settings_get_string (settings, "greeting");
  g_assert_cmpstr (str, ==, "Hello, earthlings");
  g_free (str);

Matthias Clasen's avatar
Matthias Clasen committed
123 124 125 126
  g_settings_set (settings, "greeting", "s", "this is the end");
  g_object_unref (settings);
}

Matthias Clasen's avatar
Matthias Clasen committed
127 128 129
/* Check that we get an error when getting a key
 * that is not in the schema
 */
130 131 132 133 134 135
static void
test_unknown_key (void)
{
  if (!g_test_undefined ())
    return;

136 137 138 139 140 141 142 143
  if (g_test_subprocess ())
    {
      GSettings *settings;
      GVariant *value;

      settings = g_settings_new ("org.gtk.test");
      value = g_settings_get_value (settings, "no_such_key");

144
      g_assert_null (value);
145 146 147 148 149

      g_object_unref (settings);
      return;
    }
  g_test_trap_subprocess (NULL, 0, 0);
Matthias Clasen's avatar
Matthias Clasen committed
150 151 152 153
  g_test_trap_assert_failed ();
  g_test_trap_assert_stderr ("*does not contain*");
}

Matthias Clasen's avatar
Matthias Clasen committed
154 155 156
/* Check that we get an error when the schema
 * has not been installed
 */
157 158 159 160 161
static void
test_no_schema (void)
{
  if (!g_test_undefined ())
    return;
162

163 164 165 166 167 168
  if (g_test_subprocess ())
    {
      GSettings *settings;

      settings = g_settings_new ("no.such.schema");

169
      g_assert_null (settings);
170 171 172
      return;
    }
  g_test_trap_subprocess (NULL, 0, 0);
Matthias Clasen's avatar
Matthias Clasen committed
173 174 175 176
  g_test_trap_assert_failed ();
  g_test_trap_assert_stderr ("*Settings schema 'no.such.schema' is not installed*");
}

Matthias Clasen's avatar
Matthias Clasen committed
177 178 179
/* Check that we get an error when passing a type string
 * that does not match the schema
 */
Matthias Clasen's avatar
Matthias Clasen committed
180 181 182
static void
test_wrong_type (void)
{
183 184 185
  GSettings *settings;
  gchar *str = NULL;

186 187 188
  if (!g_test_undefined ())
    return;

189
  settings = g_settings_new ("org.gtk.test");
Matthias Clasen's avatar
Matthias Clasen committed
190

191 192 193 194 195 196
  g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
                         "*given value has a type of*");
  g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
                         "*valid_format_string*");
  g_settings_get (settings, "greeting", "o", &str);
  g_test_assert_expected_messages ();
Matthias Clasen's avatar
Matthias Clasen committed
197

198
  g_assert_null (str);
Matthias Clasen's avatar
Matthias Clasen committed
199

200 201 202 203
  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
                         "*expects type 's'*");
  g_settings_set (settings, "greeting", "o", "/a/path");
  g_test_assert_expected_messages ();
Matthias Clasen's avatar
Matthias Clasen committed
204

205
  g_object_unref (settings);
Matthias Clasen's avatar
Matthias Clasen committed
206 207
}

208 209 210 211
/* Check errors with explicit paths */
static void
test_wrong_path (void)
{
212 213 214
  if (!g_test_undefined ())
    return;

215 216 217 218 219 220 221 222
  if (g_test_subprocess ())
    {
      GSettings *settings G_GNUC_UNUSED;

      settings = g_settings_new_with_path ("org.gtk.test", "/wrong-path/");
      return;
    }
  g_test_trap_subprocess (NULL, 0, 0);
223 224
  g_test_trap_assert_failed ();
  g_test_trap_assert_stderr ("*but path * specified by schema*");
225 226
}

227 228 229
static void
test_no_path (void)
{
230 231 232
  if (!g_test_undefined ())
    return;

233 234 235 236 237 238 239 240
  if (g_test_subprocess ())
    {
      GSettings *settings G_GNUC_UNUSED;

      settings = g_settings_new ("org.gtk.test.no-path");
      return;
    }
  g_test_trap_subprocess (NULL, 0, 0);
241 242 243 244 245
  g_test_trap_assert_failed ();
  g_test_trap_assert_stderr ("*attempting to create schema * without a path**");
}


Matthias Clasen's avatar
Matthias Clasen committed
246 247 248
/* Check that we can successfully read and set the full
 * range of all basic types
 */
Matthias Clasen's avatar
Matthias Clasen committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
static void
test_basic_types (void)
{
  GSettings *settings;
  gboolean b;
  guint8 byte;
  gint16 i16;
  guint16 u16;
  gint32 i32;
  guint32 u32;
  gint64 i64;
  guint64 u64;
  gdouble d;
  gchar *str;

  settings = g_settings_new ("org.gtk.test.basic-types");

  g_settings_get (settings, "test-boolean", "b", &b);
  g_assert_cmpint (b, ==, 1);

  g_settings_set (settings, "test-boolean", "b", 0);
  g_settings_get (settings, "test-boolean", "b", &b);
  g_assert_cmpint (b, ==, 0);

  g_settings_get (settings, "test-byte", "y", &byte);
  g_assert_cmpint (byte, ==, 25);

  g_settings_set (settings, "test-byte", "y", G_MAXUINT8);
  g_settings_get (settings, "test-byte", "y", &byte);
  g_assert_cmpint (byte, ==, G_MAXUINT8);

  g_settings_get (settings, "test-int16", "n", &i16);
  g_assert_cmpint (i16, ==, -1234);

  g_settings_set (settings, "test-int16", "n", G_MININT16);
  g_settings_get (settings, "test-int16", "n", &i16);
  g_assert_cmpint (i16, ==, G_MININT16);

  g_settings_set (settings, "test-int16", "n", G_MAXINT16);
  g_settings_get (settings, "test-int16", "n", &i16);
  g_assert_cmpint (i16, ==, G_MAXINT16);

  g_settings_get (settings, "test-uint16", "q", &u16);
  g_assert_cmpuint (u16, ==, 1234);

  g_settings_set (settings, "test-uint16", "q", G_MAXUINT16);
  g_settings_get (settings, "test-uint16", "q", &u16);
  g_assert_cmpuint (u16, ==, G_MAXUINT16);

  g_settings_get (settings, "test-int32", "i", &i32);
  g_assert_cmpint (i32, ==, -123456);

  g_settings_set (settings, "test-int32", "i", G_MININT32);
  g_settings_get (settings, "test-int32", "i", &i32);
  g_assert_cmpint (i32, ==, G_MININT32);

  g_settings_set (settings, "test-int32", "i", G_MAXINT32);
  g_settings_get (settings, "test-int32", "i", &i32);
  g_assert_cmpint (i32, ==, G_MAXINT32);

  g_settings_get (settings, "test-uint32", "u", &u32);
  g_assert_cmpuint (u32, ==, 123456);

  g_settings_set (settings, "test-uint32", "u", G_MAXUINT32);
  g_settings_get (settings, "test-uint32", "u", &u32);
  g_assert_cmpuint (u32, ==, G_MAXUINT32);

  g_settings_get (settings, "test-int64", "x", &i64);
  g_assert_cmpuint (i64, ==, -123456789);

  g_settings_set (settings, "test-int64", "x", G_MININT64);
  g_settings_get (settings, "test-int64", "x", &i64);
  g_assert_cmpuint (i64, ==, G_MININT64);

  g_settings_set (settings, "test-int64", "x", G_MAXINT64);
  g_settings_get (settings, "test-int64", "x", &i64);
  g_assert_cmpuint (i64, ==, G_MAXINT64);

  g_settings_get (settings, "test-uint64", "t", &u64);
  g_assert_cmpuint (u64, ==, 123456789);

  g_settings_set (settings, "test-uint64", "t", G_MAXUINT64);
  g_settings_get (settings, "test-uint64", "t", &u64);
  g_assert_cmpuint (u64, ==, G_MAXUINT64);

  g_settings_get (settings, "test-double", "d", &d);
  g_assert_cmpfloat (d, ==, 123.456);

  g_settings_set (settings, "test-double", "d", G_MINDOUBLE);
  g_settings_get (settings, "test-double", "d", &d);
  g_assert_cmpfloat (d, ==, G_MINDOUBLE);

  g_settings_set (settings, "test-double", "d", G_MAXDOUBLE);
  g_settings_get (settings, "test-double", "d", &d);
  g_assert_cmpfloat (d, ==, G_MAXDOUBLE);

  g_settings_get (settings, "test-string", "s", &str);
  g_assert_cmpstr (str, ==, "a string, it seems");
  g_free (str);
  str = NULL;

  g_settings_get (settings, "test-objectpath", "o", &str);
  g_assert_cmpstr (str, ==, "/a/object/path");
  g_object_unref (settings);
  g_free (str);
  str = NULL;
}

Matthias Clasen's avatar
Matthias Clasen committed
357 358 359
/* Check that we can read an set complex types like
 * tuples, arrays and dictionaries
 */
Matthias Clasen's avatar
Matthias Clasen committed
360 361 362 363 364 365 366
static void
test_complex_types (void)
{
  GSettings *settings;
  gchar *s;
  gint i1, i2;
  GVariantIter *iter = NULL;
367
  GVariant *v = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

  settings = g_settings_new ("org.gtk.test.complex-types");

  g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
  g_assert_cmpstr (s, ==, "one");
  g_assert_cmpint (i1,==, 2);
  g_assert_cmpint (i2,==, 3);
  g_free (s) ;
  s = NULL;

  g_settings_set (settings, "test-tuple", "(s(ii))", "none", 0, 0);
  g_settings_get (settings, "test-tuple", "(s(ii))", &s, &i1, &i2);
  g_assert_cmpstr (s, ==, "none");
  g_assert_cmpint (i1,==, 0);
  g_assert_cmpint (i2,==, 0);
  g_free (s);
  s = NULL;

  g_settings_get (settings, "test-array", "ai", &iter);
  g_assert_cmpint (g_variant_iter_n_children (iter), ==, 6);
388
  g_assert_true (g_variant_iter_next (iter, "i", &i1));
Matthias Clasen's avatar
Matthias Clasen committed
389
  g_assert_cmpint (i1, ==, 0);
390
  g_assert_true (g_variant_iter_next (iter, "i", &i1));
Matthias Clasen's avatar
Matthias Clasen committed
391
  g_assert_cmpint (i1, ==, 1);
392
  g_assert_true (g_variant_iter_next (iter, "i", &i1));
Matthias Clasen's avatar
Matthias Clasen committed
393
  g_assert_cmpint (i1, ==, 2);
394
  g_assert_true (g_variant_iter_next (iter, "i", &i1));
Matthias Clasen's avatar
Matthias Clasen committed
395
  g_assert_cmpint (i1, ==, 3);
396
  g_assert_true (g_variant_iter_next (iter, "i", &i1));
Matthias Clasen's avatar
Matthias Clasen committed
397
  g_assert_cmpint (i1, ==, 4);
398
  g_assert_true (g_variant_iter_next (iter, "i", &i1));
Matthias Clasen's avatar
Matthias Clasen committed
399
  g_assert_cmpint (i1, ==, 5);
400
  g_assert_false (g_variant_iter_next (iter, "i", &i1));
Matthias Clasen's avatar
Matthias Clasen committed
401 402
  g_variant_iter_free (iter);

403 404
  g_settings_get (settings, "test-dict", "a{sau}", &iter);
  g_assert_cmpint (g_variant_iter_n_children (iter), ==, 2);
405
  g_assert_true (g_variant_iter_next (iter, "{&s@au}", &s, &v));
406 407 408
  g_assert_cmpstr (s, ==, "AC");
  g_assert_cmpstr ((char *)g_variant_get_type (v), ==, "au");
  g_variant_unref (v);
409
  g_assert_true (g_variant_iter_next (iter, "{&s@au}", &s, &v));
410 411 412 413 414 415 416 417 418
  g_assert_cmpstr (s, ==, "IV");
  g_assert_cmpstr ((char *)g_variant_get_type (v), ==, "au");
  g_variant_unref (v);
  g_variant_iter_free (iter);

  v = g_settings_get_value (settings, "test-dict");
  g_assert_cmpstr ((char *)g_variant_get_type (v), ==, "a{sau}");
  g_variant_unref (v);

Matthias Clasen's avatar
Matthias Clasen committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
  g_object_unref (settings);
}

static gboolean changed_cb_called;

static void
changed_cb (GSettings   *settings,
            const gchar *key,
            gpointer     data)
{
  changed_cb_called = TRUE;

  g_assert_cmpstr (key, ==, data);
}

Matthias Clasen's avatar
Matthias Clasen committed
434 435
/* Test that basic change notification with the changed signal works.
 */
436
static void
Matthias Clasen's avatar
Matthias Clasen committed
437 438 439 440 441 442 443 444 445 446 447 448 449
test_changes (void)
{
  GSettings *settings;
  GSettings *settings2;

  settings = g_settings_new ("org.gtk.test");

  g_signal_connect (settings, "changed",
                    G_CALLBACK (changed_cb), "greeting");

  changed_cb_called = FALSE;

  g_settings_set (settings, "greeting", "s", "new greeting");
450
  g_assert_true (changed_cb_called);
Matthias Clasen's avatar
Matthias Clasen committed
451 452 453 454 455 456

  settings2 = g_settings_new ("org.gtk.test");

  changed_cb_called = FALSE;

  g_settings_set (settings2, "greeting", "s", "hi");
457
  g_assert_true (changed_cb_called);
Matthias Clasen's avatar
Matthias Clasen committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

  g_object_unref (settings2);
  g_object_unref (settings);
}

static gboolean changed_cb_called2;

static void
changed_cb2 (GSettings   *settings,
             const gchar *key,
             gpointer     data)
{
  gboolean *p = data;

  *p = TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
475 476 477 478 479 480 481
/* Test that changes done to a delay-mode instance
 * don't appear to the outside world until apply. Also
 * check that we get change notification when they are
 * applied.
 * Also test that the has-unapplied property is properly
 * maintained.
 */
482
static void
Matthias Clasen's avatar
Matthias Clasen committed
483 484 485 486 487
test_delay_apply (void)
{
  GSettings *settings;
  GSettings *settings2;
  gchar *str;
488
  gboolean writable;
489 490
  GVariant *v;
  const gchar *s;
Matthias Clasen's avatar
Matthias Clasen committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508

  settings = g_settings_new ("org.gtk.test");
  settings2 = g_settings_new ("org.gtk.test");

  g_settings_set (settings2, "greeting", "s", "top o' the morning");

  changed_cb_called = FALSE;
  changed_cb_called2 = FALSE;

  g_signal_connect (settings, "changed",
                    G_CALLBACK (changed_cb2), &changed_cb_called);
  g_signal_connect (settings2, "changed",
                    G_CALLBACK (changed_cb2), &changed_cb_called2);

  g_settings_delay (settings);

  g_settings_set (settings, "greeting", "s", "greetings from test_delay_apply");

509 510
  g_assert_true (changed_cb_called);
  g_assert_false (changed_cb_called2);
Matthias Clasen's avatar
Matthias Clasen committed
511

512
  writable = g_settings_is_writable (settings, "greeting");
513
  g_assert_true (writable);
514

Matthias Clasen's avatar
Matthias Clasen committed
515 516 517 518 519
  g_settings_get (settings, "greeting", "s", &str);
  g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
  g_free (str);
  str = NULL;

520 521 522 523 524
  v = g_settings_get_user_value (settings, "greeting");
  s = g_variant_get_string (v, NULL);
  g_assert_cmpstr (s, ==, "greetings from test_delay_apply");
  g_variant_unref (v);

Matthias Clasen's avatar
Matthias Clasen committed
525 526 527 528 529
  g_settings_get (settings2, "greeting", "s", &str);
  g_assert_cmpstr (str, ==, "top o' the morning");
  g_free (str);
  str = NULL;

530 531
  g_assert_true (g_settings_get_has_unapplied (settings));
  g_assert_false (g_settings_get_has_unapplied (settings2));
Matthias Clasen's avatar
Matthias Clasen committed
532 533 534 535 536 537

  changed_cb_called = FALSE;
  changed_cb_called2 = FALSE;

  g_settings_apply (settings);

538 539
  g_assert_false (changed_cb_called);
  g_assert_true (changed_cb_called2);
Matthias Clasen's avatar
Matthias Clasen committed
540 541 542 543 544 545 546 547 548 549 550

  g_settings_get (settings, "greeting", "s", &str);
  g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
  g_free (str);
  str = NULL;

  g_settings_get (settings2, "greeting", "s", &str);
  g_assert_cmpstr (str, ==, "greetings from test_delay_apply");
  g_free (str);
  str = NULL;

551 552
  g_assert_false (g_settings_get_has_unapplied (settings));
  g_assert_false (g_settings_get_has_unapplied (settings2));
Matthias Clasen's avatar
Matthias Clasen committed
553

554 555 556 557 558 559 560
  g_settings_reset (settings, "greeting");
  g_settings_apply (settings);

  g_settings_get (settings, "greeting", "s", &str);
  g_assert_cmpstr (str, ==, "Hello, earthlings");
  g_free (str);

Matthias Clasen's avatar
Matthias Clasen committed
561 562 563 564
  g_object_unref (settings2);
  g_object_unref (settings);
}

Matthias Clasen's avatar
Matthias Clasen committed
565 566 567
/* Test that reverting unapplied changes in a delay-apply
 * settings instance works.
 */
Matthias Clasen's avatar
Matthias Clasen committed
568 569 570 571 572 573 574 575 576 577 578 579
static void
test_delay_revert (void)
{
  GSettings *settings;
  GSettings *settings2;
  gchar *str;

  settings = g_settings_new ("org.gtk.test");
  settings2 = g_settings_new ("org.gtk.test");

  g_settings_set (settings2, "greeting", "s", "top o' the morning");

580 581 582 583
  g_settings_get (settings, "greeting", "s", &str);
  g_assert_cmpstr (str, ==, "top o' the morning");
  g_free (str);

Matthias Clasen's avatar
Matthias Clasen committed
584 585 586 587 588 589 590 591 592 593 594 595 596 597
  g_settings_delay (settings);

  g_settings_set (settings, "greeting", "s", "greetings from test_delay_revert");

  g_settings_get (settings, "greeting", "s", &str);
  g_assert_cmpstr (str, ==, "greetings from test_delay_revert");
  g_free (str);
  str = NULL;

  g_settings_get (settings2, "greeting", "s", &str);
  g_assert_cmpstr (str, ==, "top o' the morning");
  g_free (str);
  str = NULL;

598
  g_assert_true (g_settings_get_has_unapplied (settings));
Matthias Clasen's avatar
Matthias Clasen committed
599

Matthias Clasen's avatar
Matthias Clasen committed
600 601
  g_settings_revert (settings);

602
  g_assert_false (g_settings_get_has_unapplied (settings));
Matthias Clasen's avatar
Matthias Clasen committed
603

Matthias Clasen's avatar
Matthias Clasen committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617
  g_settings_get (settings, "greeting", "s", &str);
  g_assert_cmpstr (str, ==, "top o' the morning");
  g_free (str);
  str = NULL;

  g_settings_get (settings2, "greeting", "s", &str);
  g_assert_cmpstr (str, ==, "top o' the morning");
  g_free (str);
  str = NULL;

  g_object_unref (settings2);
  g_object_unref (settings);
}

618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
static void
test_delay_child (void)
{
  GSettings *base;
  GSettings *settings;
  GSettings *child;
  guint8 byte;
  gboolean delay;

  base = g_settings_new ("org.gtk.test.basic-types");
  g_settings_set (base, "test-byte", "y", 36);

  settings = g_settings_new ("org.gtk.test");
  g_settings_delay (settings);
  g_object_get (settings, "delay-apply", &delay, NULL);
633
  g_assert_true (delay);
634 635

  child = g_settings_get_child (settings, "basic-types");
636
  g_assert_nonnull (child);
637 638

  g_object_get (child, "delay-apply", &delay, NULL);
639
  g_assert_false (delay);
640 641 642 643 644 645

  g_settings_get (child, "test-byte", "y", &byte);
  g_assert_cmpuint (byte, ==, 36);

  g_settings_set (child, "test-byte", "y", 42);

646
  /* make sure the child was delayed too */
647
  g_settings_get (base, "test-byte", "y", &byte);
648
  g_assert_cmpuint (byte, ==, 36);
649 650 651 652 653 654

  g_object_unref (child);
  g_object_unref (settings);
  g_object_unref (base);
}

Matthias Clasen's avatar
Matthias Clasen committed
655 656 657 658 659 660 661 662 663
static void
keys_changed_cb (GSettings    *settings,
                 const GQuark *keys,
                 gint          n_keys)
{
  gchar *str;

  g_assert_cmpint (n_keys, ==, 2);

664 665 666 667
  g_assert_true ((keys[0] == g_quark_from_static_string ("greeting") &&
                  keys[1] == g_quark_from_static_string ("farewell")) ||
                 (keys[1] == g_quark_from_static_string ("greeting") &&
                  keys[0] == g_quark_from_static_string ("farewell")));
Matthias Clasen's avatar
Matthias Clasen committed
668 669 670 671 672 673 674 675 676 677 678 679

  g_settings_get (settings, "greeting", "s", &str);
  g_assert_cmpstr (str, ==, "greetings from test_atomic");
  g_free (str);
  str = NULL;

  g_settings_get (settings, "farewell", "s", &str);
  g_assert_cmpstr (str, ==, "atomic bye-bye");
  g_free (str);
  str = NULL;
}

Matthias Clasen's avatar
Matthias Clasen committed
680 681 682 683
/* Check that delay-applied changes appear atomically.
 * More specifically, verify that all changed keys appear
 * with their new value while handling the change-event signal.
 */
Matthias Clasen's avatar
Matthias Clasen committed
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
static void
test_atomic (void)
{
  GSettings *settings;
  GSettings *settings2;
  gchar *str;

  settings = g_settings_new ("org.gtk.test");
  settings2 = g_settings_new ("org.gtk.test");

  g_settings_set (settings2, "greeting", "s", "top o' the morning");

  changed_cb_called = FALSE;
  changed_cb_called2 = FALSE;

  g_signal_connect (settings2, "change-event",
                    G_CALLBACK (keys_changed_cb), NULL);

  g_settings_delay (settings);

  g_settings_set (settings, "greeting", "s", "greetings from test_atomic");
  g_settings_set (settings, "farewell", "s", "atomic bye-bye");

  g_settings_apply (settings);

  g_settings_get (settings, "greeting", "s", &str);
  g_assert_cmpstr (str, ==, "greetings from test_atomic");
  g_free (str);
  str = NULL;

  g_settings_get (settings, "farewell", "s", &str);
  g_assert_cmpstr (str, ==, "atomic bye-bye");
  g_free (str);
  str = NULL;

  g_settings_get (settings2, "greeting", "s", &str);
  g_assert_cmpstr (str, ==, "greetings from test_atomic");
  g_free (str);
  str = NULL;

  g_settings_get (settings2, "farewell", "s", &str);
  g_assert_cmpstr (str, ==, "atomic bye-bye");
  g_free (str);
  str = NULL;

  g_object_unref (settings2);
  g_object_unref (settings);
}

733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
/* On Windows the interaction between the C library locale and libintl
 * (from GNU gettext) is not like on POSIX, so just skip these tests
 * for now.
 *
 * There are several issues:
 *
 * 1) The C library doesn't use LC_MESSAGES, that is implemented only
 * in libintl (defined in its <libintl.h>).
 *
 * 2) The locale names that setlocale() accepts and returns aren't in
 * the "de_DE" style, but like "German_Germany".
 *
 * 3) libintl looks at the Win32 thread locale and not the C library
 * locale. (And even if libintl would use the C library's locale, as
 * there are several alternative C library DLLs, libintl might be
 * linked to a different one than the application code, so they
 * wouldn't have the same C library locale anyway.)
 */

Matthias Clasen's avatar
Matthias Clasen committed
752 753 754 755 756
/* Test that translations work for schema defaults.
 *
 * This test relies on the de.po file in the same directory
 * to be compiled into ./de/LC_MESSAGES/test.mo
 */
Matthias Clasen's avatar
Matthias Clasen committed
757 758 759 760 761 762 763
static void
test_l10n (void)
{
  GSettings *settings;
  gchar *str;
  gchar *locale;

764
  bindtextdomain ("test", locale_dir);
Matthias Clasen's avatar
Matthias Clasen committed
765 766 767 768 769 770
  bind_textdomain_codeset ("test", "UTF-8");

  locale = g_strdup (setlocale (LC_MESSAGES, NULL));

  settings = g_settings_new ("org.gtk.test.localized");

771
  g_setenv ("LC_MESSAGES", "C", TRUE);
Matthias Clasen's avatar
Matthias Clasen committed
772 773
  setlocale (LC_MESSAGES, "C");
  str = g_settings_get_string (settings, "error-message");
774
  g_setenv ("LC_MESSAGES", locale, TRUE);
Matthias Clasen's avatar
Matthias Clasen committed
775 776 777 778 779 780
  setlocale (LC_MESSAGES, locale);

  g_assert_cmpstr (str, ==, "Unnamed");
  g_free (str);
  str = NULL;

781
  g_setenv ("LC_MESSAGES", "de_DE.UTF-8", TRUE);
782
  setlocale (LC_MESSAGES, "de_DE.UTF-8");
783 784 785 786
  /* Only do the test if translation is actually working... */
  if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
    {
      str = g_settings_get_string (settings, "error-message");
Matthias Clasen's avatar
Matthias Clasen committed
787

788 789 790 791 792 793
      g_assert_cmpstr (str, ==, "Unbenannt");
      g_free (str);
      str = NULL;
    }
  else
    g_printerr ("warning: translation is not working... skipping test. ");
Matthias Clasen's avatar
Matthias Clasen committed
794

795
  g_setenv ("LC_MESSAGES", locale, TRUE);
796
  setlocale (LC_MESSAGES, locale);
Matthias Clasen's avatar
Matthias Clasen committed
797
  g_free (locale);
798
  g_object_unref (settings);
Matthias Clasen's avatar
Matthias Clasen committed
799 800
}

Matthias Clasen's avatar
Matthias Clasen committed
801 802 803 804 805 806 807
/* Test that message context works as expected with translated
 * schema defaults. Also, verify that non-ASCII UTF-8 content
 * works.
 *
 * This test relies on the de.po file in the same directory
 * to be compiled into ./de/LC_MESSAGES/test.mo
 */
Matthias Clasen's avatar
Matthias Clasen committed
808 809 810 811 812 813 814
static void
test_l10n_context (void)
{
  GSettings *settings;
  gchar *str;
  gchar *locale;

815
  bindtextdomain ("test", locale_dir);
Matthias Clasen's avatar
Matthias Clasen committed
816 817 818 819 820 821
  bind_textdomain_codeset ("test", "UTF-8");

  locale = g_strdup (setlocale (LC_MESSAGES, NULL));

  settings = g_settings_new ("org.gtk.test.localized");

822
  g_setenv ("LC_MESSAGES", "C", TRUE);
Matthias Clasen's avatar
Matthias Clasen committed
823 824
  setlocale (LC_MESSAGES, "C");
  g_settings_get (settings, "backspace", "s", &str);
825
  g_setenv ("LC_MESSAGES", locale, TRUE);
Matthias Clasen's avatar
Matthias Clasen committed
826 827 828 829 830 831
  setlocale (LC_MESSAGES, locale);

  g_assert_cmpstr (str, ==, "BackSpace");
  g_free (str);
  str = NULL;

832
  g_setenv ("LC_MESSAGES", "de_DE.UTF-8", TRUE);
833
  setlocale (LC_MESSAGES, "de_DE.UTF-8");
834 835 836 837
  /* Only do the test if translation is actually working... */
  if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
    {
      g_settings_get (settings, "backspace", "s", &str);
Matthias Clasen's avatar
Matthias Clasen committed
838

839 840 841 842 843 844
      g_assert_cmpstr (str, ==, "Löschen");
      g_free (str);
      str = NULL;
    }
  else
    g_printerr ("warning: translation is not working... skipping test.  ");
Matthias Clasen's avatar
Matthias Clasen committed
845

846
  g_setenv ("LC_MESSAGES", locale, TRUE);
847
  setlocale (LC_MESSAGES, locale);
Matthias Clasen's avatar
Matthias Clasen committed
848
  g_free (locale);
849
  g_object_unref (settings);
Matthias Clasen's avatar
Matthias Clasen committed
850 851 852 853 854 855
}

enum
{
  PROP_0,
  PROP_BOOL,
856
  PROP_ANTI_BOOL,
857 858 859
  PROP_BYTE,
  PROP_INT16,
  PROP_UINT16,
Matthias Clasen's avatar
Matthias Clasen committed
860
  PROP_INT,
861
  PROP_UINT,
862 863
  PROP_INT64,
  PROP_UINT64,
Matthias Clasen's avatar
Matthias Clasen committed
864
  PROP_DOUBLE,
865 866
  PROP_STRING,
  PROP_NO_READ,
Matthias Clasen's avatar
Matthias Clasen committed
867
  PROP_NO_WRITE,
868
  PROP_STRV,
869 870
  PROP_ENUM,
  PROP_FLAGS
Matthias Clasen's avatar
Matthias Clasen committed
871 872 873 874 875 876 877
};

typedef struct
{
  GObject parent_instance;

  gboolean bool_prop;
878
  gboolean anti_bool_prop;
879
  gint8 byte_prop;
880 881
  gint int16_prop;
  guint16 uint16_prop;
Matthias Clasen's avatar
Matthias Clasen committed
882
  gint int_prop;
883
  guint uint_prop;
884 885
  gint64 int64_prop;
  guint64 uint64_prop;
Matthias Clasen's avatar
Matthias Clasen committed
886 887
  gdouble double_prop;
  gchar *string_prop;
888 889
  gchar *no_read_prop;
  gchar *no_write_prop;
890
  gchar **strv_prop;
Matthias Clasen's avatar
Matthias Clasen committed
891
  guint enum_prop;
892
  guint flags_prop;
Matthias Clasen's avatar
Matthias Clasen committed
893 894 895 896 897 898 899
} TestObject;

typedef struct
{
  GObjectClass parent_class;
} TestObjectClass;

900
static GType test_object_get_type (void);
Matthias Clasen's avatar
Matthias Clasen committed
901 902 903 904 905 906 907 908 909 910 911
G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)

static void
test_object_init (TestObject *object)
{
}

static void
test_object_finalize (GObject *object)
{
  TestObject *testo = (TestObject*)object;
912
  g_strfreev (testo->strv_prop);
Matthias Clasen's avatar
Matthias Clasen committed
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
  g_free (testo->string_prop);
  G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
}

static void
test_object_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  TestObject *test_object = (TestObject *)object;

  switch (prop_id)
    {
    case PROP_BOOL:
      g_value_set_boolean (value, test_object->bool_prop);
      break;
930 931 932
    case PROP_ANTI_BOOL:
      g_value_set_boolean (value, test_object->anti_bool_prop);
      break;
933
    case PROP_BYTE:
934
      g_value_set_schar (value, test_object->byte_prop);
935 936 937 938 939 940 941
      break;
    case PROP_UINT16:
      g_value_set_uint (value, test_object->uint16_prop);
      break;
    case PROP_INT16:
      g_value_set_int (value, test_object->int16_prop);
      break;
Matthias Clasen's avatar
Matthias Clasen committed
942 943 944
    case PROP_INT:
      g_value_set_int (value, test_object->int_prop);
      break;
945 946 947
    case PROP_UINT:
      g_value_set_uint (value, test_object->uint_prop);
      break;
948 949 950 951 952 953
    case PROP_INT64:
      g_value_set_int64 (value, test_object->int64_prop);
      break;
    case PROP_UINT64:
      g_value_set_uint64 (value, test_object->uint64_prop);
      break;
Matthias Clasen's avatar
Matthias Clasen committed
954 955 956 957 958 959
    case PROP_DOUBLE:
      g_value_set_double (value, test_object->double_prop);
      break;
    case PROP_STRING:
      g_value_set_string (value, test_object->string_prop);
      break;
960 961 962
    case PROP_NO_WRITE:
      g_value_set_string (value, test_object->no_write_prop);
      break;
963 964 965
    case PROP_STRV:
      g_value_set_boxed (value, test_object->strv_prop);
      break;
Matthias Clasen's avatar
Matthias Clasen committed
966 967 968
    case PROP_ENUM:
      g_value_set_enum (value, test_object->enum_prop);
      break;
969 970 971
    case PROP_FLAGS:
      g_value_set_flags (value, test_object->flags_prop);
      break;
Matthias Clasen's avatar
Matthias Clasen committed
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
test_object_set_property (GObject      *object,
                          guint         prop_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  TestObject *test_object = (TestObject *)object;

  switch (prop_id)
    {
    case PROP_BOOL:
      test_object->bool_prop = g_value_get_boolean (value);
      break;
991 992 993
    case PROP_ANTI_BOOL:
      test_object->anti_bool_prop = g_value_get_boolean (value);
      break;
994
    case PROP_BYTE:
995
      test_object->byte_prop = g_value_get_schar (value);
996 997 998 999 1000 1001 1002
      break;
    case PROP_INT16:
      test_object->int16_prop = g_value_get_int (value);
      break;
    case PROP_UINT16:
      test_object->uint16_prop = g_value_get_uint (value);
      break;
Matthias Clasen's avatar
Matthias Clasen committed
1003 1004 1005
    case PROP_INT:
      test_object->int_prop = g_value_get_int (value);
      break;
1006 1007 1008
    case PROP_UINT:
      test_object->uint_prop = g_value_get_uint (value);
      break;
1009 1010 1011 1012 1013 1014
    case PROP_INT64:
      test_object->int64_prop = g_value_get_int64 (value);
      break;
    case PROP_UINT64:
      test_object->uint64_prop = g_value_get_uint64 (value);
      break;
Matthias Clasen's avatar
Matthias Clasen committed
1015 1016 1017 1018 1019 1020 1021
    case PROP_DOUBLE:
      test_object->double_prop = g_value_get_double (value);
      break;
    case PROP_STRING:
      g_free (test_object->string_prop);
      test_object->string_prop = g_value_dup_string (value);
      break;
1022 1023 1024 1025
    case PROP_NO_READ:
      g_free (test_object->no_read_prop);
      test_object->no_read_prop = g_value_dup_string (value);
      break;
1026 1027 1028 1029
    case PROP_STRV:
      g_strfreev (test_object->strv_prop);
      test_object->strv_prop = g_value_dup_boxed (value);
      break;
Matthias Clasen's avatar
Matthias Clasen committed
1030 1031 1032
    case PROP_ENUM:
      test_object->enum_prop = g_value_get_enum (value);
      break;
1033 1034 1035
    case PROP_FLAGS:
      test_object->flags_prop = g_value_get_flags (value);
      break;
Matthias Clasen's avatar
Matthias Clasen committed
1036 1037 1038 1039 1040 1041
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
static GType
test_enum_get_type (void)
{
  static volatile gsize define_type_id = 0;

  if (g_once_init_enter (&define_type_id))
    {
      static const GEnumValue values[] = {
        { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
        { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
        { TEST_ENUM_BAZ, "TEST_ENUM_BAZ", "baz" },
        { TEST_ENUM_QUUX, "TEST_ENUM_QUUX", "quux" },
        { 0, NULL, NULL }
      };

      GType type_id = g_enum_register_static ("TestEnum", values);
      g_once_init_leave (&define_type_id, type_id);
    }

  return define_type_id;
}

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
static GType
test_flags_get_type (void)
{
  static volatile gsize define_type_id = 0;

  if (g_once_init_enter (&define_type_id))
    {
      static const GFlagsValue values[] = {
        { TEST_FLAGS_NONE, "TEST_FLAGS_NONE", "none" },
        { TEST_FLAGS_MOURNING, "TEST_FLAGS_MOURNING", "mourning" },
        { TEST_FLAGS_LAUGHING, "TEST_FLAGS_LAUGHING", "laughing" },
        { TEST_FLAGS_WALKING, "TEST_FLAGS_WALKING", "walking" },
        { 0, NULL, NULL }
      };

      GType type_id = g_flags_register_static ("TestFlags", values);
      g_once_init_leave (&define_type_id, type_id);
    }

  return define_type_id;
}

Matthias Clasen's avatar
Matthias Clasen committed
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
static void
test_object_class_init (TestObjectClass *class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);

  gobject_class->get_property = test_object_get_property;
  gobject_class->set_property = test_object_set_property;
  gobject_class->finalize = test_object_finalize;

  g_object_class_install_property (gobject_class, PROP_BOOL,
    g_param_spec_boolean ("bool", "", "", FALSE, G_PARAM_READWRITE));
1097 1098
  g_object_class_install_property (gobject_class, PROP_ANTI_BOOL,
    g_param_spec_boolean ("anti-bool", "", "", FALSE, G_PARAM_READWRITE));
1099 1100 1101 1102 1103 1104
  g_object_class_install_property (gobject_class, PROP_BYTE,
    g_param_spec_char ("byte", "", "", G_MININT8, G_MAXINT8, 0, G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class, PROP_INT16,
    g_param_spec_int ("int16", "", "", -G_MAXINT16, G_MAXINT16, 0, G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class, PROP_UINT16,
    g_param_spec_uint ("uint16", "", "", 0, G_MAXUINT16, 0, G_PARAM_READWRITE));
Matthias Clasen's avatar
Matthias Clasen committed
1105
  g_object_class_install_property (gobject_class, PROP_INT,
1106 1107 1108
    g_param_spec_int ("int", "", "", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class, PROP_UINT,
    g_param_spec_uint ("uint", "", "", 0, G_MAXUINT, 0, G_PARAM_READWRITE));
1109 1110 1111 1112
  g_object_class_install_property (gobject_class, PROP_INT64,
    g_param_spec_int64 ("int64", "", "", G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class, PROP_UINT64,
    g_param_spec_uint64 ("uint64", "", "", 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
Matthias Clasen's avatar
Matthias Clasen committed
1113 1114 1115 1116
  g_object_class_install_property (gobject_class, PROP_DOUBLE,
    g_param_spec_double ("double", "", "", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class, PROP_STRING,
    g_param_spec_string ("string", "", "", NULL, G_PARAM_READWRITE));
1117 1118 1119 1120
  g_object_class_install_property (gobject_class, PROP_NO_WRITE,
    g_param_spec_string ("no-write", "", "", NULL, G_PARAM_READABLE));
  g_object_class_install_property (gobject_class, PROP_NO_READ,
    g_param_spec_string ("no-read", "", "", NULL, G_PARAM_WRITABLE));
1121 1122
  g_object_class_install_property (gobject_class, PROP_STRV,
    g_param_spec_boxed ("strv", "", "", G_TYPE_STRV, G_PARAM_READWRITE));
Matthias Clasen's avatar
Matthias Clasen committed
1123 1124
  g_object_class_install_property (gobject_class, PROP_ENUM,
    g_param_spec_enum ("enum", "", "", test_enum_get_type (), TEST_ENUM_FOO, G_PARAM_READWRITE));
1125 1126
  g_object_class_install_property (gobject_class, PROP_FLAGS,
    g_param_spec_flags ("flags", "", "", test_flags_get_type (), TEST_FLAGS_NONE, G_PARAM_READWRITE));
Matthias Clasen's avatar
Matthias Clasen committed
1127 1128 1129 1130 1131 1132 1133 1134
}

static TestObject *
test_object_new (void)
{
  return (TestObject*)g_object_new (test_object_get_type (), NULL);
}

Matthias Clasen's avatar
Matthias Clasen committed
1135 1136 1137 1138
/* Test basic binding functionality for simple types.
 * Verify that with bidirectional bindings, changes on either side
 * are notified on the other end.
 */
Matthias Clasen's avatar
Matthias Clasen committed
1139 1140 1141 1142 1143 1144
static void
test_simple_binding (void)
{
  TestObject *obj;
  GSettings *settings;
  gboolean b;
1145
  gchar y;
Matthias Clasen's avatar
Matthias Clasen committed
1146
  gint i;
1147
  guint u;
Matthias Clasen's avatar
Matthias Clasen committed
1148 1149
  gint16 n;
  guint16 q;
1150 1151
  gint n2;
  guint q2;
1152 1153
  gint64 i64;
  guint64 u64;
Matthias Clasen's avatar
Matthias Clasen committed
1154 1155
  gdouble d;
  gchar *s;
Matthias Clasen's avatar
Matthias Clasen committed
1156
  GVariant *value;
1157
  gchar **strv;
Matthias Clasen's avatar
Matthias Clasen committed
1158 1159 1160 1161 1162 1163 1164 1165 1166

  settings = g_settings_new ("org.gtk.test.binding");
  obj = test_object_new ();

  g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_DEFAULT);
  g_object_set (obj, "bool", TRUE, NULL);
  g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);

  g_settings_set_boolean (settings, "bool", FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
1167
  b = TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
1168 1169 1170
  g_object_get (obj, "bool", &b, NULL);
  g_assert_cmpint (b, ==, FALSE);

1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
  g_settings_bind (settings, "anti-bool", obj, "anti-bool",
                   G_SETTINGS_BIND_INVERT_BOOLEAN);
  g_object_set (obj, "anti-bool", FALSE, NULL);
  g_assert_cmpint (g_settings_get_boolean (settings, "anti-bool"), ==, TRUE);

  g_settings_set_boolean (settings, "anti-bool", FALSE);
  b = FALSE;
  g_object_get (obj, "anti-bool", &b, NULL);
  g_assert_cmpint (b, ==, TRUE);

1181 1182 1183
  g_settings_bind (settings, "byte", obj, "byte", G_SETTINGS_BIND_DEFAULT);

  g_object_set (obj, "byte", 123, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1184
  y = 'c';
1185 1186 1187 1188
  g_settings_get (settings, "byte", "y", &y);
  g_assert_cmpint (y, ==, 123);

  g_settings_set (settings, "byte", "y", 54);
Matthias Clasen's avatar
Matthias Clasen committed
1189
  y = 'c';
1190 1191 1192 1193 1194 1195
  g_object_get (obj, "byte", &y, NULL);
  g_assert_cmpint (y, ==, 54);

  g_settings_bind (settings, "int16", obj, "int16", G_SETTINGS_BIND_DEFAULT);

  g_object_set (obj, "int16", 1234, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1196 1197 1198
  n = 4321;
  g_settings_get (settings, "int16", "n", &n);
  g_assert_cmpint (n, ==, 1234);
1199 1200

  g_settings_set (settings, "int16", "n", 4321);
1201 1202 1203
  n2 = 1111;
  g_object_get (obj, "int16", &n2, NULL);
  g_assert_cmpint (n2, ==, 4321);
1204 1205 1206 1207

  g_settings_bind (settings, "uint16", obj, "uint16", G_SETTINGS_BIND_DEFAULT);

  g_object_set (obj, "uint16", (guint16) G_MAXUINT16, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1208 1209 1210
  q = 1111;
  g_settings_get (settings, "uint16", "q", &q);
  g_assert_cmpuint (q, ==, G_MAXUINT16);
1211 1212

  g_settings_set (settings, "uint16", "q", (guint16) G_MAXINT16);
1213 1214 1215
  q2 = 1111;
  g_object_get (obj, "uint16", &q2, NULL);
  g_assert_cmpuint (q2, ==, (guint16) G_MAXINT16);
1216

Matthias Clasen's avatar
Matthias Clasen committed
1217 1218 1219 1220 1221 1222
  g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);

  g_object_set (obj, "int", 12345, NULL);
  g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);

  g_settings_set_int (settings, "int", 54321);
Matthias Clasen's avatar
Matthias Clasen committed
1223
  i = 1111;
Matthias Clasen's avatar
Matthias Clasen committed
1224 1225 1226
  g_object_get (obj, "int", &i, NULL);
  g_assert_cmpint (i, ==, 54321);

1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
  g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);

  g_object_set (obj, "uint", 12345, NULL);
  g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);

  g_settings_set_uint (settings, "uint", 54321);
  u = 1111;
  g_object_get (obj, "uint", &u, NULL);
  g_assert_cmpuint (u, ==, 54321);

1237 1238
  g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);

1239
  g_object_set (obj, "uint64", (guint64) 12345, NULL);
1240 1241 1242
  g_assert_cmpuint (g_settings_get_uint64 (settings, "uint64"), ==, 12345);

  g_settings_set_uint64 (settings, "uint64", 54321);
1243 1244 1245
  u64 = 1111;
  g_object_get (obj, "uint64", &u64, NULL);
  g_assert_cmpuint (u64, ==, 54321);
1246

1247 1248 1249
  g_settings_bind (settings, "int64", obj, "int64", G_SETTINGS_BIND_DEFAULT);

  g_object_set (obj, "int64", (gint64) G_MAXINT64, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1250
  i64 = 1111;
1251 1252 1253 1254
  g_settings_get (settings, "int64", "x", &i64);
  g_assert_cmpint (i64, ==, G_MAXINT64);

  g_settings_set (settings, "int64", "x", (gint64) G_MININT64);
Matthias Clasen's avatar
Matthias Clasen committed
1255
  i64 = 1111;
1256
  g_object_get (obj, "int64", &i64, NULL);
1257
  g_assert_cmpint (i64, ==, G_MININT64);
1258 1259 1260 1261

  g_settings_bind (settings, "uint64", obj, "uint64", G_SETTINGS_BIND_DEFAULT);

  g_object_set (obj, "uint64", (guint64) G_MAXUINT64, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1262
  u64 = 1111;
1263
  g_settings_get (settings, "uint64", "t", &u64);
1264
  g_assert_cmpuint (u64, ==, G_MAXUINT64);
1265

1266
  g_settings_set (settings, "uint64", "t", (guint64) G_MAXINT64);
Matthias Clasen's avatar
Matthias Clasen committed
1267
  u64 = 1111;
1268
  g_object_get (obj, "uint64", &u64, NULL);
1269
  g_assert_cmpuint (u64, ==, (guint64) G_MAXINT64);
1270

Matthias Clasen's avatar
Matthias Clasen committed
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
  g_settings_bind (settings, "string", obj, "string", G_SETTINGS_BIND_DEFAULT);

  g_object_set (obj, "string", "bu ba", NULL);
  s = g_settings_get_string (settings, "string");
  g_assert_cmpstr (s, ==, "bu ba");
  g_free (s);

  g_settings_set_string (settings, "string", "bla bla");
  g_object_get (obj, "string", &s, NULL);
  g_assert_cmpstr (s, ==, "bla bla");
  g_free (s);

Matthias Clasen's avatar
Matthias Clasen committed
1283 1284 1285 1286
  g_settings_bind (settings, "chararray", obj, "string", G_SETTINGS_BIND_DEFAULT);

  g_object_set (obj, "string", "non-unicode:\315", NULL);
  value = g_settings_get_value (settings, "chararray");
1287
  g_assert_cmpstr (g_variant_get_bytestring (value), ==, "non-unicode:\315");
Matthias Clasen's avatar
Matthias Clasen committed
1288 1289
  g_variant_unref (value);

Matthias Clasen's avatar
Matthias Clasen committed
1290 1291
  g_settings_bind (settings, "double", obj, "double", G_SETTINGS_BIND_DEFAULT);

1292 1293
  g_object_set (obj, "double", G_MAXFLOAT, NULL);
  g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXFLOAT);
Matthias Clasen's avatar
Matthias Clasen committed
1294

1295
  g_settings_set_double (settings, "double", G_MINFLOAT);
Matthias Clasen's avatar
Matthias Clasen committed
1296
  d = 1.0;
Matthias Clasen's avatar
Matthias Clasen committed
1297
  g_object_get (obj, "double", &d, NULL);
1298
  g_assert_cmpfloat (d, ==, G_MINFLOAT);
Matthias Clasen's avatar
Matthias Clasen committed
1299

1300 1301 1302 1303
  g_object_set (obj, "double", G_MAXDOUBLE, NULL);
  g_assert_cmpfloat (g_settings_get_double (settings, "double"), ==, G_MAXDOUBLE);

  g_settings_set_double (settings, "double", -G_MINDOUBLE);
Matthias Clasen's avatar
Matthias Clasen committed
1304
  d = 1.0;
1305 1306
  g_object_get (obj, "double", &d, NULL);
  g_assert_cmpfloat (d, ==, -G_MINDOUBLE);
Matthias Clasen's avatar
Matthias Clasen committed
1307

1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
  strv = g_strsplit ("plastic bag,middle class,polyethylene", ",", 0);
  g_settings_bind (settings, "strv", obj, "strv", G_SETTINGS_BIND_DEFAULT);
  g_object_set (obj, "strv", strv, NULL);
  g_strfreev (strv);
  strv = g_settings_get_strv (settings, "strv");
  s = g_strjoinv (",", strv);
  g_assert_cmpstr (s, ==, "plastic bag,middle class,polyethylene");
  g_strfreev (strv);
  g_free (s);
  strv = g_strsplit ("decaffeinate,unleaded,keep all surfaces clean", ",", 0);
  g_settings_set_strv (settings, "strv", (const gchar **) strv);
  g_strfreev (strv);
  g_object_get (obj, "strv", &strv, NULL);
  s = g_strjoinv (",", strv);
  g_assert_cmpstr (s, ==, "decaffeinate,unleaded,keep all surfaces clean");
  g_strfreev (strv);
  g_free (s);
1325 1326
  g_settings_set_strv (settings, "strv", NULL);
  g_object_get (obj, "strv", &strv, NULL);
1327
  g_assert_nonnull (strv);
1328 1329
  g_assert_cmpint (g_strv_length (strv), ==, 0);
  g_strfreev (strv);
1330

Matthias Clasen's avatar
Matthias Clasen committed
1331 1332
  g_settings_bind (settings, "enum", obj, "enum", G_SETTINGS_BIND_DEFAULT);
  g_object_set (obj, "enum", TEST_ENUM_BAZ, NULL);
1333 1334 1335
  s = g_settings_get_string (settings, "enum");
  g_assert_cmpstr (s, ==, "baz");
  g_free (s);
Matthias Clasen's avatar
Matthias Clasen committed
1336 1337 1338
  g_assert_cmpint (g_settings_get_enum (settings, "enum"), ==, TEST_ENUM_BAZ);

  g_settings_set_enum (settings, "enum", TEST_ENUM_QUUX);
Matthias Clasen's avatar
Matthias Clasen committed
1339
  i = 230;
Matthias Clasen's avatar
Matthias Clasen committed
1340 1341 1342 1343
  g_object_get (obj, "enum", &i, NULL);
  g_assert_cmpint (i, ==, TEST_ENUM_QUUX);

  g_settings_set_string (settings, "enum", "baz");
Matthias Clasen's avatar
Matthias Clasen committed
1344
  i = 230;
Matthias Clasen's avatar
Matthias Clasen committed
1345 1346 1347
  g_object_get (obj, "enum", &i, NULL);
  g_assert_cmpint (i, ==, TEST_ENUM_BAZ);

1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
  g_settings_bind (settings, "flags", obj, "flags", G_SETTINGS_BIND_DEFAULT);
  g_object_set (obj, "flags", TEST_FLAGS_MOURNING, NULL);
  strv = g_settings_get_strv (settings, "flags");
  g_assert_cmpint (g_strv_length (strv), ==, 1);
  g_assert_cmpstr (strv[0], ==, "mourning");
  g_strfreev (strv);

  g_assert_cmpint (g_settings_get_flags (settings, "flags"), ==, TEST_FLAGS_MOURNING);

  g_settings_set_flags (settings, "flags", TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);
  i = 230;
  g_object_get (obj, "flags", &i, NULL);
  g_assert_cmpint (i, ==, TEST_FLAGS_MOURNING | TEST_FLAGS_WALKING);

1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
  g_settings_bind (settings, "uint", obj, "uint", G_SETTINGS_BIND_DEFAULT);

  g_object_set (obj, "uint", 12345, NULL);
  g_assert_cmpuint (g_settings_get_uint (settings, "uint"), ==, 12345);

  g_settings_set_uint (settings, "uint", 54321);
  u = 1111;
  g_object_get (obj, "uint", &u, NULL);
  g_assert_cmpuint (u, ==, 54321);

  g_settings_bind (settings, "range", obj, "uint", G_SETTINGS_BIND_DEFAULT);
  g_object_set (obj, "uint", 22, NULL);
  u = 1111;
  g_assert_cmpuint (g_settings_get_uint (settings, "range"), ==, 22);
  g_object_get (obj, "uint", &u, NULL);
  g_assert_cmpuint (u, ==, 22);

  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
                         "* is out of schema-specified range for*");
  g_object_set (obj, "uint", 45, NULL);
  g_test_assert_expected_messages ();
  u = 1111;
  g_object_get (obj, "uint", &u, NULL);
  g_assert_cmpuint (g_settings_get_uint (settings, "range"), ==, 22);
  /* The value of the object is currently not reset back to its initial value
  g_assert_cmpuint (u, ==, 22); */

Matthias Clasen's avatar
Matthias Clasen committed
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
  g_object_unref (obj);
  g_object_unref (settings);
}

static void
test_unbind (void)
{
  TestObject *obj;
  GSettings *settings;

  settings = g_settings_new ("org.gtk.test.binding");
  obj = test_object_new ();

  g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_DEFAULT);

  g_object_set (obj, "int", 12345, NULL);
  g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);

  g_settings_unbind (obj, "int");

  g_object_set (obj, "int", 54321, NULL);
  g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 12345);

  g_object_unref (obj);
  g_object_unref (settings);
}

static void
test_bind_writable (void)
{
  TestObject *obj;
  GSettings *settings;
  gboolean b;

  settings = g_settings_new ("org.gtk.test.binding");
  obj = test_object_new ();

  g_object_set (obj, "bool", FALSE, NULL);

1428
  g_settings_bind_writable (settings, "int", obj, "bool", FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
1429 1430

  g_object_get (obj, "bool", &b, NULL);
1431
  g_assert_true (b);
Matthias Clasen's avatar
Matthias Clasen committed
1432

1433 1434 1435 1436 1437
  g_settings_unbind (obj, "bool");

  g_settings_bind_writable (settings, "int", obj, "bool", TRUE);

  g_object_get (obj, "bool", &b, NULL);
1438
  g_assert_false (b);
1439

Matthias Clasen's avatar
Matthias Clasen committed
1440 1441 1442 1443
  g_object_unref (obj);
  g_object_unref (settings);
}

Matthias Clasen's avatar
Matthias Clasen committed
1444 1445 1446 1447
/* Test one-way bindings.
 * Verify that changes on one side show up on the other,
 * but not vice versa
 */
Matthias Clasen's avatar
Matthias Clasen committed
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
static void
test_directional_binding (void)
{
  TestObject *obj;
  GSettings *settings;
  gboolean b;
  gint i;

  settings = g_settings_new ("org.gtk.test.binding");
  obj = test_object_new ();

  g_object_set (obj, "bool", FALSE, NULL);
  g_settings_set_boolean (settings, "bool", FALSE);

  g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET);

  g_settings_set_boolean (settings, "bool", TRUE);
  g_object_get (obj, "bool", &b, NULL);
  g_assert_cmpint (b, ==, TRUE);

  g_object_set (obj, "bool", FALSE, NULL);
  g_assert_cmpint (g_settings_get_boolean (settings, "bool"), ==, TRUE);

  g_object_set (obj, "int", 20, NULL);
  g_settings_set_int (settings, "int", 20);

  g_settings_bind (settings, "int", obj, "int", G_SETTINGS_BIND_SET);

  g_object_set (obj, "int", 32, NULL);
  g_assert_cmpint (g_settings_get_int (settings, "int"), ==, 32);

  g_settings_set_int (settings, "int", 20);
  g_object_get (obj, "int", &i, NULL);
  g_assert_cmpint (i, ==, 32);

  g_object_unref (obj);
  g_object_unref (settings);
}

1487
/* Test that type mismatch is caught when creating a binding */
1488 1489 1490 1491 1492 1493
static void
test_typesafe_binding (void)
{
  if (!g_test_undefined ())
    return;

1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
  if (g_test_subprocess ())
    {
      TestObject *obj;
      GSettings *settings;

      settings = g_settings_new ("org.gtk.test.binding");
      obj = test_object_new ();

      g_settings_bind (settings, "string", obj, "int", G_SETTINGS_BIND_DEFAULT);

      g_object_unref (obj);
      g_object_unref (settings);
      return;
    }
  g_test_trap_subprocess (NULL, 0, 0);
Matthias Clasen's avatar
Matthias Clasen committed
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
  g_test_trap_assert_failed ();
  g_test_trap_assert_stderr ("*not compatible*");
}

static gboolean
string_to_bool (GValue   *value,
                GVariant *variant,
                gpointer  user_data)
{
  const gchar *s;

  s = g_variant_get_string (variant, NULL);
  g_value_set_boolean (value, g_strcmp0 (s, "true") == 0);

  return TRUE;
}

static GVariant *
bool_to_string (const GValue       *value,
                const GVariantType *expected_type,
                gpointer            user_data)
{
  if (g_value_get_boolean (value))
    return g_variant_new_string ("true");
  else
    return g_variant_new_string ("false");
}

1537 1538 1539 1540 1541 1542 1543 1544
static GVariant *
bool_to_bool (const GValue       *value,
              const GVariantType *expected_type,
              gpointer            user_data)
{
  return g_variant_new_boolean (g_value_get_boolean (value));
}

Matthias Clasen's avatar
Matthias Clasen committed
1545 1546 1547
/* Test custom bindings.
 * Translate strings to booleans and back
 */
Matthias Clasen's avatar
Matthias Clasen committed
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
static void
test_custom_binding (void)
{
  TestObject *obj;
  GSettings *settings;
  gchar *s;
  gboolean b;

  settings = g_settings_new ("org.gtk.test.binding");
  obj = test_object_new ();

  g_settings_set_string (settings, "string", "true");

  g_settings_bind_with_mapping (settings, "string",
                                obj, "bool",
                                G_SETTINGS_BIND_DEFAULT,
                                string_to_bool,
                                bool_to_string,
                                NULL, NULL);

  g_settings_set_string (settings, "string", "false");
  g_object_get (obj, "bool", &b, NULL);
  g_assert_cmpint (b, ==, FALSE);

  g_settings_set_string (settings, "string", "not true");
  g_object_get (obj, "bool", &b, NULL);
  g_assert_cmpint (b, ==, FALSE);

  g_object_set (obj, "bool", TRUE, NULL);
  s = g_settings_get_string (settings, "string");
  g_assert_cmpstr (s, ==, "true");
1579
  g_free (s);
Matthias Clasen's avatar
Matthias Clasen committed
1580

1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
  g_settings_bind_with_mapping (settings, "string",
                                obj, "bool",
                                G_SETTINGS_BIND_DEFAULT,
                                string_to_bool, bool_to_bool,
                                NULL, NULL);
  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
                         "*binding mapping function for key 'string' returned"
                         " GVariant of type 'b' when type 's' was requested*");
  g_object_set (obj, "bool", FALSE, NULL);
  g_test_assert_expected_messages ();

Matthias Clasen's avatar
Matthias Clasen committed
1592 1593 1594 1595
  g_object_unref (obj);
  g_object_unref (settings);
}

Matthias Clasen's avatar
Matthias Clasen committed
1596 1597 1598 1599 1600
/* Test that with G_SETTINGS_BIND_NO_CHANGES, the
 * initial settings value is transported to the object
 * side, but later settings changes do not affect the
 * object
 */
Matthias Clasen's avatar
Matthias Clasen committed
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
static void
test_no_change_binding (void)
{
  TestObject *obj;
  GSettings *settings;
  gboolean b;

  settings = g_settings_new ("org.gtk.test.binding");
  obj = test_object_new ();

  g_object_set (obj, "bool", TRUE, NULL);
  g_settings_set_boolean (settings, "bool", FALSE);

  g_settings_bind (settings, "bool", obj, "bool", G_SETTINGS_BIND_GET_NO_CHANGES);

  g_object_get (obj, "bool", &b, NULL);
  g_assert_cmpint (b, ==, FALSE);

  g_settings_set_boolean (settings, "bool", TRUE);
  g_object_get (obj, "bool", &b, NULL);
  g_assert_cmpint (b, ==, FALSE);

  g_settings_set_boolean (settings, "bool", FALSE);
  g_object_set (obj, "bool", TRUE, NULL);
  b = g_settings_get_boolean (settings, "bool");
  g_assert_cmpint (b, ==, TRUE);

  g_object_unref (obj);
  g_object_unref (settings);
}

1632 1633 1634
/* Test that binding a non-readable property only
 * works in 'GET' mode.
 */
1635
static void
1636
test_no_read_binding_fail (void)
1637
{
1638 1639
  TestObject *obj;
  GSettings *settings;
1640

1641 1642
  settings = g_settings_new ("org.gtk.test.binding");
  obj = test_object_new ();
1643

1644 1645
  g_settings_bind (settings, "string", obj, "no-read", 0);
}
1646

1647 1648 1649 1650 1651 1652 1653 1654
static void
test_no_read_binding_pass (void)
{
  TestObject *obj;
  GSettings *settings;

  settings = g_settings_new ("org.gtk.test.binding");
  obj = test_object_new ();
1655

1656
  g_settings_bind (settings, "string", obj, "no-read", G_SETTINGS_BIND_GET);
1657

1658 1659
  exit (0);
}
1660

1661 1662 1663 1664 1665 1666 1667 1668
static void
test_no_read_binding (void)
{
  if (g_test_undefined ())
    {
      g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/fail", 0, 0);
      g_test_trap_assert_failed ();
      g_test_trap_assert_stderr ("*property*is not readable*");
1669
    }
1670 1671

  g_test_trap_subprocess ("/gsettings/no-read-binding/subprocess/pass", 0, 0);
1672 1673 1674 1675 1676 1677
  g_test_trap_assert_passed ();
}

/* Test that binding a non-writable property only
 * works in 'SET' mode.
 */
1678
static void
1679
test_no_write_binding_fail (void)
1680
{
1681 1682
  TestObject *obj;
  GSettings *settings;
1683

1684 1685
  settings = g_settings_new ("org.gtk.test.binding");
  obj = test_object_new ();
1686

1687 1688
  g_settings_bind (settings, "string", obj, "no-write", 0);
}
1689

1690 1691 1692 1693 1694
static void
test_no_write_binding_pass (void)
{
  TestObject *obj;
  GSettings *settings;
1695

1696 1697 1698 1699
  settings = g_settings_new ("org.gtk.test.binding");
  obj = test_object_new ();

  g_settings_bind (settings, "string", obj, "no-write", G_SETTINGS_BIND_SET);
1700

1701 1702
  exit (0);
}
1703

1704 1705 1706 1707 1708 1709 1710 1711
static void
test_no_write_binding (void)
{
  if (g_test_undefined ())
    {
      g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/fail", 0, 0);
      g_test_trap_assert_failed ();
      g_test_trap_assert_stderr ("*property*is not writable*");
1712
    }
1713 1714

  g_test_trap_subprocess ("/gsettings/no-write-binding/subprocess/pass", 0, 0);
1715 1716 1717
  g_test_trap_assert_passed ();
}

1718 1719 1720 1721 1722 1723 1724
static void
key_changed_cb (GSettings *settings, const gchar *key, gpointer data)
{
  gboolean *b = data;
  (*b) = TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
1725 1726 1727 1728
/*
 * Test that using a keyfile works
 */
static void
1729 1730
test_keyfile (Fixture       *fixture,
              gconstpointer  user_data)
Matthias Clasen's avatar
Matthias Clasen committed
1731
{
1732
  GSettingsBackend *kf_backend;
Matthias Clasen's avatar
Matthias Clasen committed
1733 1734 1735
  GSettings *settings;
  GKeyFile *keyfile;
  gchar *str;
1736 1737 1738 1739