~ubuntu-branches/ubuntu/edgy/gconf/edgy

« back to all changes in this revision

Viewing changes to tests/testschemas.c

  • Committer: Bazaar Package Importer
  • Author(s): Takuo KITAME
  • Date: 2002-03-17 01:51:39 UTC
  • Revision ID: james.westby@ubuntu.com-20020317015139-z4f8fdg1hoe049g0
Tags: upstream-1.0.9
Import upstream version 1.0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GConf
 
2
 * Copyright (C) 1999, 2000 Red Hat Inc.
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Library General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Library General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Library General Public
 
15
 * License along with this library; if not, write to the
 
16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA.
 
18
 */
 
19
 
 
20
#include <gconf/gconf.h>
 
21
#include <stdio.h>
 
22
#include <unistd.h>
 
23
#include <math.h>
 
24
#include <gconf/gconf-internals.h>
 
25
 
 
26
/* glibc printf() functions accept NULL for a %s format, but to be
 
27
   safe, check for null strings and return a printable value */
 
28
const char     *
 
29
NULL_SAFE(const char *x)
 
30
{
 
31
        return x ? x : "<NULL>";
 
32
}
 
33
 
 
34
static void
 
35
check(gboolean condition, const gchar* fmt, ...)
 
36
{
 
37
  va_list args;
 
38
  gchar* description;
 
39
  
 
40
  va_start (args, fmt);
 
41
  description = g_strdup_vprintf(fmt, args);
 
42
  va_end (args);
 
43
  
 
44
  if (condition)
 
45
    {
 
46
      printf(".");
 
47
      fflush(stdout);
 
48
    }
 
49
  else
 
50
    {
 
51
      fprintf(stderr, "\n*** FAILED: %s\n", description);
 
52
      exit(1);
 
53
    }
 
54
  
 
55
  g_free(description);
 
56
}
 
57
 
 
58
static const gchar*
 
59
keys[] = {
 
60
  "/testing/foo/tar",
 
61
  "/testing/foo/bar",
 
62
  "/testing/quad",
 
63
  "/testing/blah",
 
64
  "/testing/q/a/b/c/z/w/x/y/z",
 
65
  "/testing/foo/baz",
 
66
  "/testing/oops/bloo",
 
67
  "/testing/oops/snoo",
 
68
  "/testing/oops/kwoo",
 
69
  "/testing/foo/quaz",
 
70
  NULL
 
71
};
 
72
 
 
73
static const gchar*
 
74
locales[] = {
 
75
  "C",
 
76
  "es",
 
77
  "en",
 
78
  "no",
 
79
  NULL
 
80
};
 
81
 
 
82
static gint ints[] = { -1, -2, -3, 0, 1, 2, 3, 4000, 0xfffff, -0xfffff, G_MININT, G_MAXINT, 0, 0, 57, 83, 95 };
 
83
static const guint n_ints = sizeof(ints)/sizeof(ints[0]);
 
84
 
 
85
static void
 
86
check_unset(GConfEngine* conf)
 
87
{
 
88
  GError* err = NULL;
 
89
  const gchar** keyp = NULL;
 
90
 
 
91
  keyp = keys;
 
92
 
 
93
  while (*keyp)
 
94
    {
 
95
      gconf_engine_unset(conf, *keyp, &err);
 
96
 
 
97
      if (err != NULL)
 
98
        {
 
99
          fprintf(stderr, "unset of `%s' failed: %s\n", *keyp, err->message);
 
100
          g_error_free(err);
 
101
          err = NULL;
 
102
        }
 
103
      else
 
104
        {
 
105
          GConfValue* val;
 
106
          gchar* valstr;
 
107
          
 
108
          val = gconf_engine_get (conf, *keyp, &err);
 
109
 
 
110
 
 
111
          if (val)
 
112
            valstr = gconf_value_to_string(val);
 
113
          else
 
114
            valstr = g_strdup("(none)");
 
115
          
 
116
          check(val == NULL, "unsetting a previously-set value `%s' the value `%s' existed", *keyp, valstr);
 
117
 
 
118
          g_free(valstr);
 
119
        }
 
120
      
 
121
      ++keyp;
 
122
    }
 
123
}
 
124
 
 
125
void
 
126
check_int_storage(GConfEngine* conf)
 
127
{
 
128
  GError* err = NULL;
 
129
  const gchar** keyp = NULL;
 
130
  guint i; 
 
131
  
 
132
  /* Loop over keys, storing all values at each then retrieving them */
 
133
  
 
134
  keyp = keys;
 
135
 
 
136
  while (*keyp)
 
137
    {
 
138
      i = 0;
 
139
      while (i < n_ints)
 
140
        {
 
141
          gint gotten;
 
142
          
 
143
          if (!gconf_engine_set_int(conf, *keyp, ints[i], &err))
 
144
            {
 
145
              fprintf(stderr, "Failed to set key `%s' to `%d': %s\n",
 
146
                      *keyp, ints[i], err->message);
 
147
              g_error_free(err);
 
148
              err = NULL;
 
149
            }
 
150
          else
 
151
            {
 
152
              gotten = gconf_engine_get_int(conf, *keyp, &err);
 
153
 
 
154
              if (err != NULL)
 
155
                {
 
156
                  check(gotten == 0.0, "0.0 not returned though there was an error");
 
157
 
 
158
                  fprintf(stderr, "Failed to get key `%s': %s\n",
 
159
                          *keyp, err->message);
 
160
                  g_error_free(err);
 
161
                  err = NULL;
 
162
                }
 
163
              else
 
164
                {
 
165
                  check (ints[i] == gotten,
 
166
                         "int set/get pair: `%d' set, `%d' got",
 
167
                         ints[i], gotten);
 
168
 
 
169
                }
 
170
            }
 
171
          
 
172
          ++i;
 
173
        }
 
174
      
 
175
      ++keyp;
 
176
    }
 
177
 
 
178
  /* Now invert the loop and see if that causes problems */
 
179
 
 
180
  i = 0;
 
181
  while (i < n_ints)
 
182
    {
 
183
 
 
184
      keyp = keys;
 
185
 
 
186
      while (*keyp)
 
187
        {
 
188
          gint gotten;
 
189
          
 
190
          if (!gconf_engine_set_int(conf, *keyp, ints[i], &err))
 
191
            {
 
192
              fprintf(stderr, "Failed to set key `%s' to `%d': %s\n",
 
193
                      *keyp, ints[i], err->message);
 
194
              g_error_free(err);
 
195
              err = NULL;
 
196
            }
 
197
          else
 
198
            {
 
199
              gotten = gconf_engine_get_int(conf, *keyp, &err);
 
200
 
 
201
              if (err != NULL)
 
202
                {
 
203
                  check(gotten == 0, "0 not returned though there was an error");
 
204
 
 
205
                  fprintf(stderr, "Failed to get key `%s': %s\n",
 
206
                          *keyp, err->message);
 
207
                  g_error_free(err);
 
208
                  err = NULL;
 
209
                }
 
210
              else
 
211
                {
 
212
                  check (ints[i] == gotten,
 
213
                         "int set/get pair: `%d' set, `%d' got",
 
214
                         ints[i], gotten);
 
215
 
 
216
                }
 
217
            }
 
218
          
 
219
      
 
220
          ++keyp;
 
221
        }
 
222
 
 
223
      ++i;
 
224
    }
 
225
          
 
226
  check_unset(conf);
 
227
}
 
228
 
 
229
static int
 
230
null_safe_strcmp(const char* lhs, const char* rhs)
 
231
{
 
232
  if (lhs == NULL && rhs == NULL)
 
233
    return 0;
 
234
  else if (lhs == NULL)
 
235
    return 1;
 
236
  else if (rhs == NULL)
 
237
    return -1;
 
238
  else
 
239
    return strcmp(lhs, rhs);
 
240
}
 
241
 
 
242
void
 
243
check_one_schema(GConfEngine* conf, const gchar** keyp, GConfSchema* schema)
 
244
{
 
245
  GError* err = NULL;
 
246
  
 
247
  if (!gconf_engine_set_schema(conf, *keyp, schema, &err))
 
248
    {
 
249
      fprintf(stderr, "Failed to set key `%s' to schema: %s\n",
 
250
              *keyp, err->message);
 
251
      g_error_free(err);
 
252
      err = NULL;
 
253
    }
 
254
  else
 
255
    {
 
256
      GConfSchema* gotten;
 
257
      
 
258
      gotten = gconf_engine_get_schema(conf, *keyp, &err);
 
259
 
 
260
      if (err != NULL)
 
261
        {
 
262
          check(gotten == NULL, "NULL not returned though there was an error");
 
263
 
 
264
          fprintf(stderr, "Failed to get key `%s': %s\n",
 
265
                  *keyp, err->message);
 
266
          g_error_free(err);
 
267
          err = NULL;
 
268
        }
 
269
      else
 
270
        {
 
271
          check (gconf_schema_get_type(schema) == gconf_schema_get_type(gotten),
 
272
                 "schema set/get pair: type `%s' set, `%s' got",
 
273
                 gconf_value_type_to_string(gconf_schema_get_type(schema)),
 
274
                 gconf_value_type_to_string(gconf_schema_get_type(gotten)));
 
275
 
 
276
 
 
277
          /* If we set the schema for the current locale be sure we get it back */
 
278
          if (null_safe_strcmp(gconf_current_locale(), gconf_schema_get_locale(schema)) == 0)
 
279
            {
 
280
              check (null_safe_strcmp(gconf_current_locale(), gconf_schema_get_locale(gotten)) == 0,
 
281
                     "schema set/get pair: locale `%s' set, `%s' got",
 
282
                     gconf_current_locale(),
 
283
                     NULL_SAFE(gconf_schema_get_locale(gotten)));
 
284
            }
 
285
              
 
286
          check (null_safe_strcmp(gconf_schema_get_short_desc(schema), gconf_schema_get_short_desc(gotten)) == 0,
 
287
                 "schema set/get pair: short_desc `%s' set, `%s' got",
 
288
                 NULL_SAFE(gconf_schema_get_short_desc(schema)),
 
289
                 NULL_SAFE(gconf_schema_get_short_desc(gotten)));
 
290
 
 
291
          check (null_safe_strcmp(gconf_schema_get_long_desc(schema), gconf_schema_get_long_desc(gotten)) == 0,
 
292
                 "schema set/get pair: long_desc `%s' set, `%s' got",
 
293
                 NULL_SAFE(gconf_schema_get_long_desc(schema)),
 
294
                 NULL_SAFE(gconf_schema_get_long_desc(gotten)));
 
295
 
 
296
          check (null_safe_strcmp(gconf_schema_get_owner(schema), gconf_schema_get_owner(gotten)) == 0,
 
297
                 "schema set/get pair: owner `%s' set, `%s' got",
 
298
                 NULL_SAFE(gconf_schema_get_owner(schema)),
 
299
                 NULL_SAFE(gconf_schema_get_owner(gotten)));
 
300
 
 
301
          {
 
302
            GConfValue* set_default;
 
303
            GConfValue* got_default;
 
304
 
 
305
            set_default = gconf_schema_get_default_value(schema);
 
306
            got_default = gconf_schema_get_default_value(gotten);
 
307
 
 
308
            if (set_default && got_default)
 
309
              {
 
310
                check(set_default->type == GCONF_VALUE_INT,
 
311
                      "set default type is INT");
 
312
                
 
313
                check(set_default->type == got_default->type,
 
314
                      "schema set/get pair: default value type `%s' set, `%s' got",
 
315
                      gconf_value_type_to_string(set_default->type),
 
316
                      gconf_value_type_to_string(got_default->type));
 
317
                
 
318
                check(set_default->type == got_default->type,
 
319
                      "schema set/get pair: default value type `%s' set, `%s' got",
 
320
                      gconf_value_type_to_string(set_default->type),
 
321
                      gconf_value_type_to_string(got_default->type));
 
322
                
 
323
                check(gconf_value_get_int(set_default) == gconf_value_get_int(got_default),
 
324
                      "schema set/get pair: default value (int) `%d' set, `%d' got",
 
325
                      gconf_value_get_int(set_default), gconf_value_get_int(got_default));
 
326
              }
 
327
            else
 
328
              {
 
329
                /* mem leak */
 
330
                check (set_default == NULL && got_default == NULL,
 
331
                       "set and got default value aren't both NULL (`%s' and `%s')",
 
332
                       set_default ? gconf_value_to_string(set_default) : "NULL",
 
333
                       got_default ? gconf_value_to_string(got_default) : "NULL");
 
334
              }
 
335
          }
 
336
          
 
337
          gconf_schema_free(gotten);
 
338
        }
 
339
    }
 
340
}
 
341
      
 
342
void
 
343
check_schema_storage(GConfEngine* conf)
 
344
{
 
345
  const gchar** keyp = NULL;
 
346
  guint i; 
 
347
  const gchar** localep = NULL;
 
348
  
 
349
  /* Loop over keys, storing all values at each then retrieving them */
 
350
  
 
351
  keyp = keys;
 
352
  localep = locales;
 
353
 
 
354
  while (*keyp)
 
355
    {
 
356
      i = 0;
 
357
      while (i < n_ints)
 
358
        {
 
359
          GConfSchema* schema;
 
360
          gchar* short_desc;
 
361
          gchar* long_desc;
 
362
          GConfValue* default_value;
 
363
          const int default_value_int = 97992;
 
364
 
 
365
          if (*localep == NULL)
 
366
            localep = locales;
 
367
          
 
368
          schema = gconf_schema_new();
 
369
 
 
370
          gconf_schema_set_type(schema, GCONF_VALUE_INT);
 
371
          gconf_schema_set_locale(schema, *localep);
 
372
          short_desc = g_strdup_printf("Schema for key `%s' storing value %d",
 
373
                                       *keyp, ints[i]);
 
374
          gconf_schema_set_short_desc(schema, short_desc);
 
375
 
 
376
          long_desc = g_strdup_printf("Long description for schema with short description `%s' is really really long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long "
 
377
                                      "UTF-8: German (Deutsch Süd) Grüß Gott Greek (Ελληνικά) Γειά σας Hebrew(שלום) Hebrew punctuation(\xd6\xbfש\xd6\xbb\xd6\xbc\xd6\xbb\xd6\xbfל\xd6\xbcו\xd6\xbc\xd6\xbb\xd6\xbb\xd6\xbfם\xd6\xbc\xd6\xbb\xd6\xbf) Japanese (日本語) Thai (สวัสดีครับ) Thai wrong spelling (คำต่อไปนื่สะกดผิด พัั้ัั่งโกะ)\n", short_desc);
 
378
          
 
379
          gconf_schema_set_long_desc(schema, long_desc);
 
380
 
 
381
          gconf_schema_set_owner(schema, "testschemas");
 
382
 
 
383
          default_value = gconf_value_new(GCONF_VALUE_INT);
 
384
          gconf_value_set_int(default_value, default_value_int);
 
385
          
 
386
          gconf_schema_set_default_value_nocopy(schema, default_value);
 
387
 
 
388
          check(gconf_value_get_int(gconf_schema_get_default_value(schema)) == default_value_int,
 
389
                "Properly stored default int value in the schema");
 
390
          
 
391
          check_one_schema(conf, keyp, schema);
 
392
 
 
393
          gconf_schema_free(schema);
 
394
          g_free(long_desc);
 
395
          g_free(short_desc);
 
396
          
 
397
          ++i;
 
398
        }
 
399
      
 
400
      ++keyp;
 
401
      ++localep;
 
402
    }
 
403
 
 
404
  /* Check setting/getting "empty" schemas */
 
405
  
 
406
  keyp = keys;
 
407
 
 
408
  while (*keyp)
 
409
    {
 
410
      i = 0;
 
411
      while (i < n_ints)
 
412
        {
 
413
          GConfSchema* schema;
 
414
          
 
415
          schema = gconf_schema_new();
 
416
 
 
417
          /* this isn't guaranteed to be the same on get/set */
 
418
          gconf_schema_set_locale(schema, "C");
 
419
          
 
420
          gconf_schema_set_type(schema, GCONF_VALUE_INT);
 
421
 
 
422
          check_one_schema(conf, keyp, schema);
 
423
 
 
424
          gconf_schema_free(schema);
 
425
          
 
426
          ++i;
 
427
        }
 
428
      
 
429
      ++keyp;
 
430
    }
 
431
  
 
432
  check_unset(conf);
 
433
}
 
434
 
 
435
void 
 
436
check_schema_use(GConfEngine * conf)
 
437
{
 
438
  GError     *err = NULL;
 
439
  const gchar   **keyp = NULL;
 
440
  GConfSchema    *schema = gconf_schema_new();
 
441
  GConfValue     *value = gconf_value_new(GCONF_VALUE_STRING);
 
442
  char           *schema_key = "/schemas/desktop/test-schema";
 
443
  char           *s;
 
444
  char           *schema_value = "string value from schema";
 
445
  char           *non_schema_value = "a string value *not* from schema";
 
446
 
 
447
  gconf_schema_set_type(schema, GCONF_VALUE_STRING);
 
448
  gconf_schema_set_locale(schema, "C");
 
449
  gconf_schema_set_short_desc(schema, "short desc");
 
450
  gconf_schema_set_long_desc(schema, "long desc");
 
451
  gconf_schema_set_owner(schema, "testschemas");
 
452
  gconf_value_set_string(value, schema_value);
 
453
  gconf_schema_set_default_value(schema, value);
 
454
 
 
455
  if (gconf_engine_set_schema(conf, schema_key, schema, &err))
 
456
    {
 
457
      if (err)
 
458
        {
 
459
          fprintf(stderr, "gconf_engine_set_schema -> %s\n", err->message);
 
460
          g_error_free (err);
 
461
        }
 
462
      
 
463
      err = NULL;
 
464
    }
 
465
  keyp = keys;
 
466
  while (*keyp)
 
467
    {
 
468
      if (gconf_engine_associate_schema(conf, *keyp, schema_key, &err))
 
469
        {
 
470
          if (err)
 
471
            {
 
472
              fprintf(stderr, "gconf_engine_associate_schema -> %s\n", err->message);
 
473
              g_error_free (err);
 
474
            }
 
475
          
 
476
          err = NULL;
 
477
 
 
478
        }
 
479
      ++keyp;
 
480
    }
 
481
 
 
482
  /* FIXME we need to actually exit with error if these checks fail */
 
483
  
 
484
  keyp = keys;
 
485
  while (*keyp)
 
486
    {
 
487
      gconf_engine_unset(conf, *keyp, &err);
 
488
      s = gconf_engine_get_string(conf, *keyp, &err);
 
489
      if (s)
 
490
        {
 
491
          if (strcmp(s, schema_value) != 0)
 
492
            {
 
493
              fprintf(stderr, "ERROR: Failed to get schema value (got '%s', not '%s')\n", s, schema_value);
 
494
            }
 
495
          else
 
496
            {
 
497
              printf(".");
 
498
            }
 
499
        }
 
500
      else
 
501
        {
 
502
          fprintf(stderr, "ERROR: Failed to get a value when expecting schema value\n");
 
503
        }
 
504
 
 
505
      /* associate_schema should accept NULL to unset schemas */
 
506
      gconf_engine_associate_schema(conf, *keyp, "/bogus-nonschema", &err);
 
507
      s = gconf_engine_get_string(conf, *keyp, &err);
 
508
      if (s != NULL)
 
509
        {
 
510
          fprintf(stderr, "Failed to disassociate schema, found '%s'\n", s);
 
511
        }
 
512
 
 
513
      gconf_engine_set_string(conf, *keyp, non_schema_value, &err);
 
514
      s = gconf_engine_get_string(conf, *keyp, &err);
 
515
      if (s)
 
516
        {
 
517
          if (strcmp(s, non_schema_value) != 0)
 
518
            {
 
519
              fprintf(stderr, "ERROR: Failed to get non-schema value (got '%s', not '%s')\n", s, non_schema_value);
 
520
            }
 
521
          else
 
522
            {
 
523
              printf(".");
 
524
            }
 
525
        }
 
526
      else
 
527
        {
 
528
          fprintf(stderr, "ERROR: Failed to get a value when expecting non-schema value\n");
 
529
        }
 
530
      
 
531
      fflush(stdout);
 
532
      ++keyp;
 
533
    }
 
534
}
 
535
 
 
536
int 
 
537
main (int argc, char** argv)
 
538
{
 
539
  GConfEngine* conf;
 
540
  GError* err = NULL;
 
541
  
 
542
  if (!gconf_init(argc, argv, &err))
 
543
    {
 
544
      fprintf(stderr, "Failed to init GConf: %s\n", err->message);
 
545
      g_error_free(err);
 
546
      err = NULL;
 
547
      return 1;
 
548
    }
 
549
  
 
550
  conf = gconf_engine_get_default();
 
551
 
 
552
  check(conf != NULL, "create the default conf engine");
 
553
  
 
554
  printf("\nChecking integer storage:");
 
555
  
 
556
  check_int_storage(conf);
 
557
 
 
558
  printf("\nChecking schema storage:");
 
559
 
 
560
  check_schema_storage(conf);
 
561
  
 
562
  printf("\nChecking schema use:");
 
563
 
 
564
  check_schema_use(conf);
 
565
  
 
566
  gconf_engine_unref(conf);
 
567
 
 
568
  printf("\n\n");
 
569
  
 
570
  return 0;
 
571
}