2
* Copyright (C) 1999, 2000 Red Hat Inc.
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.
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.
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.
20
#include <gconf/gconf-listeners.h>
28
check(gboolean condition, const gchar* fmt, ...)
34
description = g_strdup_vprintf(fmt, args);
44
fprintf(stderr, "\n*** FAILED: %s\n", description);
57
"/testing/q/a/b/c/z/w/x/y/z",
88
test_destroy_notify(gpointer data)
90
struct stuff* s = data;
92
*s->id_retloc = s->id;
94
*s->key_retloc = s->key;
100
check_add_remove(GConfListeners* listeners)
108
memset(ids, 0, sizeof(ids[0]) * 128);
113
while (i < 128 && *keyp)
117
s = g_new0(struct stuff, 1);
119
s->id_retloc = &id_retloc;
120
s->key_retloc = &key_retloc;
122
s->id = ids[i] = gconf_listeners_add(listeners,
125
test_destroy_notify);
127
s->key = g_strdup(*keyp);
129
check(ids[i] != 0, "invalid connection ID returned for added listener");
135
check(gconf_listeners_count(listeners) == i,
136
"number of listeners added (%u) don't now exist in the GConfListeners (%u exist)", i, gconf_listeners_count(listeners));
141
while (i < 128 && *keyp)
146
gconf_listeners_remove(listeners, ids[i]);
148
check(strcmp(key_retloc, *keyp) == 0,
149
"listener removed has different key from listener added (`%s' vs. `%s')", *keyp, key_retloc);
153
check(ids[i] == id_retloc, "listener removed had different id from that added (%u vs. %u)", ids[i], id_retloc);
159
check(gconf_listeners_count(listeners) == 0,
160
"listener count isn't 0 after removing all the listeners");
164
check_immediate_remove_after_add(GConfListeners* listeners)
172
memset(ids, 0, sizeof(ids[0]) * 128);
177
while (i < 128 && *keyp)
181
s = g_new0(struct stuff, 1);
183
s->id_retloc = &id_retloc;
184
s->key_retloc = &key_retloc;
186
s->id = ids[i] = gconf_listeners_add(listeners,
189
test_destroy_notify);
191
s->key = g_strdup(*keyp);
193
check(ids[i] != 0, "invalid connection ID returned for added listener");
197
check((ids[i] & 0xFFFFFF) == (ids[i-1] & 0xFFFFFF), "connection ID was not properly recycled");
198
check(ids[i] != ids[i-1], "connection ID was not properly uniqueized");
201
check(gconf_listeners_count(listeners) == 1,
202
"didn't have 1 listener as expected");
207
gconf_listeners_remove(listeners, ids[i]);
209
check(strcmp(key_retloc, *keyp) == 0,
210
"listener removed has different key from listener added (`%s' vs. `%s')", *keyp, key_retloc);
214
check(ids[i] == id_retloc, "listener removed had different id from that added (%u vs. %u)", ids[i], id_retloc);
216
check(gconf_listeners_count(listeners) == 0,
217
"didn't have 0 listeners as expected");
223
check(gconf_listeners_count(listeners) == 0,
224
"listener count isn't 0 after removing all the listeners");
228
check_double_add_remove(GConfListeners* listeners)
236
memset(ids, 0, sizeof(ids[0]) * 128);
241
while (i < 128 && *keyp)
245
s = g_new0(struct stuff, 1);
247
s->id_retloc = &id_retloc;
248
s->key_retloc = &key_retloc;
250
s->id = ids[i] = gconf_listeners_add(listeners,
253
test_destroy_notify);
255
s->key = g_strdup(*keyp);
257
check(ids[i] != 0, "invalid connection ID returned for added listener");
264
check(gconf_listeners_count(listeners) == i,
265
"number of listeners added (%u) don't now exist in the GConfListeners (%u exist)", i, gconf_listeners_count(listeners));
270
while (i < 128 && *keyp)
275
gconf_listeners_remove(listeners, ids[i]);
277
check(strcmp(key_retloc, *keyp) == 0,
278
"listener removed has different key from listener added (`%s' vs. `%s')", *keyp, key_retloc);
282
check(ids[i] == id_retloc, "listener removed had different id from that added (%u vs. %u)", ids[i], id_retloc);
289
check(gconf_listeners_count(listeners) == 0,
290
"listener count isn't 0 after removing all the listeners");
294
should_be_notified(const gchar* changed_key,
295
const gchar* listener_watchpoint)
297
return strncmp(changed_key, listener_watchpoint, strlen(listener_watchpoint)) == 0;
301
const gchar* notify_key;
306
notify_callback(GConfListeners* listeners,
307
const gchar* all_above_key,
309
gpointer listener_data,
312
struct notify_data* nd = user_data;
313
struct stuff* s = listener_data;
315
check(strcmp(all_above_key, nd->notify_key) == 0, "notify key `%s' is not the notify key received in callback `%s'",
316
nd->notify_key, all_above_key);
318
check(cnxn_id == s->id,
319
"listener ID is wrong in callback (%u vs. %u)",
322
check(should_be_notified(all_above_key, s->key),
323
"listener %u at `%s' shouldn't have been notified of change to `%s'",
324
s->id, s->key, all_above_key);
326
nd->notified = g_slist_prepend(nd->notified, s);
330
check_notification(GConfListeners* listeners)
338
memset(ids, 0, sizeof(ids[0]) * 128);
343
while (i < 128 && *keyp)
347
s = g_new0(struct stuff, 1);
349
s->id_retloc = &id_retloc;
350
s->key_retloc = &key_retloc;
352
s->id = ids[i] = gconf_listeners_add(listeners,
355
test_destroy_notify);
357
s->key = g_strdup(*keyp);
359
check(ids[i] != 0, "invalid connection ID returned for added listener");
361
/* printf("%u added at `%s'\n", s->id, s->key); */
367
check(gconf_listeners_count(listeners) == i,
368
"number of listeners added (%u) don't now exist in the GConfListeners (%u exist)", i, gconf_listeners_count(listeners));
375
const gchar** sub_keyp;
376
struct notify_data nd = { NULL, NULL };
378
nd.notify_key = *keyp;
380
gconf_listeners_notify(listeners, *keyp,
384
/* Check that the list that was notified matches
385
the list that should have been */
389
struct stuff* s = NULL;
390
gboolean should_be = should_be_notified(*keyp, *sub_keyp);
397
if (strcmp(s->key, *sub_keyp) == 0)
400
tmp = g_slist_next(tmp);
405
check (tmp != NULL, "listener at `%s' should have been notified of change to `%s' and was not", *sub_keyp, *keyp);
407
/* remove so we can handle duplicate keys */
408
nd.notified = g_slist_remove(nd.notified, tmp->data);
411
g_assert(strcmp(s->key, *sub_keyp) == 0);
412
printf("%u at `%s' notified properly of `%s'\n",
413
s->id, *sub_keyp, *keyp);
418
check(tmp == NULL, "listener at `%s' should not have been notified of change to `%s' but it was", *sub_keyp, *keyp);
420
printf("`%s' properly not notified of `%s'\n",
428
if (nd.notified != NULL)
430
GSList* tmp = nd.notified;
433
struct stuff* s = tmp->data;
434
fprintf(stderr, "leftover: %u at `%s' notified of `%s'\n",
435
s->id, s->key, *keyp);
436
tmp = g_slist_next(tmp);
439
check (nd.notified == NULL, "superfluous listeners were notified of `%s'; perhaps listeners were notified twice?", *keyp);
447
while (i < 128 && *keyp)
452
gconf_listeners_remove(listeners, ids[i]);
454
check(strcmp(key_retloc, *keyp) == 0,
455
"listener removed has different key from listener added (`%s' vs. `%s')", *keyp, key_retloc);
459
check(ids[i] == id_retloc, "listener removed had different id from that added (%u vs. %u)", ids[i], id_retloc);
465
check(gconf_listeners_count(listeners) == 0,
466
"listener count isn't 0 after removing all the listeners");
469
struct destroy_data {
471
guint* destroy_count_loc;
475
destroy_test_destroy_notify(gpointer data)
477
struct destroy_data* d = data;
479
*d->destroy_count_loc += 1;
486
/* This is mostly for use with memory leak detection tools */
490
GConfListeners* listeners;
494
guint destroy_count = 0;
496
listeners = gconf_listeners_new();
498
memset(ids, 0, sizeof(ids[0]) * 128);
503
while (i < 128 && *keyp)
505
struct destroy_data* d;
507
d = g_new0(struct destroy_data, 1);
509
d->destroy_count_loc = &destroy_count;
511
ids[i] = gconf_listeners_add(listeners,
514
destroy_test_destroy_notify);
516
d->key = g_strdup(*keyp);
518
check(ids[i] != 0, "invalid connection ID returned for added listener");
524
check(gconf_listeners_count(listeners) == i,
525
"number of listeners added (%u) don't now exist in the GConfListeners (%u exist)", i, gconf_listeners_count(listeners));
527
gconf_listeners_free(listeners);
529
check(destroy_count == i,
530
"number of listeners added (%u) doesn't match number destroyed (%u) on GConfListeners destruction", i, destroy_count);
534
main (int argc, char** argv)
536
GConfListeners* listeners;
538
listeners = gconf_listeners_new();
540
check_add_remove(listeners);
542
g_assert(gconf_listeners_count(listeners) == 0);
544
check_immediate_remove_after_add(listeners);
546
g_assert(gconf_listeners_count(listeners) == 0);
548
check_double_add_remove(listeners);
550
g_assert(gconf_listeners_count(listeners) == 0);
552
check_notification(listeners);
554
g_assert(gconf_listeners_count(listeners) == 0);
556
gconf_listeners_free(listeners);