1 /* Tests for a bug that made blablabla... FIXME: summary */
6 #include <gconf/gconf-client.h>
8 #define KEY_ROOT "/foo"
9 #define KEY_UNCACHED KEY_ROOT "/baz_primitive"
10 #define KEY_PRIMITIVE KEY_ROOT "/bar/baz_primitive"
11 #define KEY_PAIR KEY_ROOT "/bar/baz_pair"
12 #define KEY_LIST KEY_ROOT "/bar/baz_list"
16 static GConfClient *client;
17 static GMainLoop *loop;
20 check_string (const gchar *key, const gchar *expected)
26 str = gconf_client_get_string (client, key, &error);
33 g_warning ("String value is NULL\n");
37 ret = strcmp (str, expected) == 0;
45 check_int (const gchar *key, gint expected)
50 i = gconf_client_get_int (client, key, &error);
61 check_float (const gchar *key, gdouble expected)
66 f = gconf_client_get_float (client, key, &error);
72 return fabs (f - expected) < DELTA;
76 check_pair (const gchar *key, gint expected_i, gdouble expected_f)
82 gconf_client_get_pair (client, key,
83 GCONF_VALUE_INT, GCONF_VALUE_FLOAT,
91 return (i == expected_i && fabs (f - expected_f) < DELTA);
95 check_list (const gchar *key, gint expected_1, gint expected_2, gint expected_3)
101 list = gconf_client_get_list (client, key, GCONF_VALUE_INT, &error);
103 g_error_free (error);
106 if (g_slist_length (list) != 3) {
110 /* ye ye, efficiency ;) */
111 i1 = GPOINTER_TO_INT (g_slist_nth_data (list, 0));
112 i2 = GPOINTER_TO_INT (g_slist_nth_data (list, 1));
113 i3 = GPOINTER_TO_INT (g_slist_nth_data (list, 2));
118 return (i1 == expected_1 && i2 == expected_2 && i3 == expected_3);
121 /* Checks that everything below "key" is gone. */
123 check_recursive_unset (void)
125 GError *error = NULL;
129 exists = gconf_client_dir_exists (client, KEY_ROOT, &error);
131 g_print ("Error when checking if root exists: %s\n", error->message);
132 g_error_free (error);
136 /* Note that this might exist or might not. Upstream behaves
137 * like this too for directories. The values are unset but the
138 * directory structure still exists, until the next time the
139 * database is synced to disk.
141 /*g_print ("Root exists: %s\n", KEY_ROOT);
145 exists = gconf_client_dir_exists (client, KEY_ROOT "/bar", &error);
147 g_print ("Error when checking if dir exists: %s\n", error->message);
148 g_error_free (error);
153 /*g_print ("Subdirectory exists: %s\n", KEY_ROOT "/bar");
157 value = gconf_client_get (client, KEY_PRIMITIVE, &error);
159 g_print ("Error when checking primitie key: %s\n", error->message);
160 g_error_free (error);
164 g_print ("Primitive key exists: %s\n", gconf_value_to_string (value));
165 gconf_value_free (value);
169 value = gconf_client_get (client, KEY_PAIR, &error);
171 g_print ("Error when checking pair key: %s\n", error->message);
172 g_error_free (error);
176 g_print ("Pair key exists: %s\n", gconf_value_to_string (value));
177 gconf_value_free (value);
181 value = gconf_client_get (client, KEY_LIST, &error);
183 g_print ("Error when checking list key: %s\n", error->message);
184 g_error_free (error);
188 g_print ("List key exists: %s\n", gconf_value_to_string (value));
189 gconf_value_free (value);
197 change_timeout_func (gpointer data)
199 static gint count = 0;
205 /* String, uncached. */
206 s = g_strdup_printf ("test-%d", g_random_int_range (1, 100));
207 gconf_client_set_string (client, KEY_UNCACHED, s, NULL);
208 if (!check_string (KEY_UNCACHED, s)) {
209 g_print ("Uncached string FAILED\n");
215 s = g_strdup_printf ("test-%d", g_random_int_range (1, 100));
216 gconf_client_set_string (client, KEY_PRIMITIVE, s, NULL);
217 if (!check_string (KEY_PRIMITIVE, s)) {
218 g_print ("String FAILED\n");
224 i = g_random_int_range (1, 100);
225 gconf_client_set_int (client, KEY_PRIMITIVE, i, NULL);
226 if (!check_int (KEY_PRIMITIVE, i)) {
227 g_print ("Int FAILED\n");
232 f = g_random_int_range (1, 1000) / 10.0;
233 gconf_client_set_float (client, KEY_PRIMITIVE, f, NULL);
234 if (!check_float (KEY_PRIMITIVE, f)) {
235 g_print ("Float FAILED\n");
239 /* Pair (int, float). */
240 i = g_random_int_range (1, 1000);
241 f = g_random_int_range (1, 10000) / 37.2;
243 gconf_client_set_pair (client, KEY_PAIR,
247 if (!check_pair (KEY_PAIR, i, f)) {
248 g_print ("Pair FAILED\n");
253 i1 = g_random_int_range (1, 1000);
254 i2 = g_random_int_range (1, 1000);
255 i3 = g_random_int_range (1, 1000);
257 list = g_slist_append (NULL, GINT_TO_POINTER (i1));
258 list = g_slist_append (list, GINT_TO_POINTER (i2));
259 list = g_slist_append (list, GINT_TO_POINTER (i3));
260 gconf_client_set_list (client, KEY_LIST,
263 if (!check_list (KEY_LIST, i1, i2, i3)) {
264 g_print ("List FAILED\n");
271 i = g_random_int_range (1, 100);
272 gconf_client_set_int (client, KEY_PRIMITIVE, i, NULL);
273 if (!check_int (KEY_PRIMITIVE, i)) {
274 g_print ("Unset FAILED\n");
277 gconf_client_unset (client, KEY_PRIMITIVE, NULL);
278 if (!check_int (KEY_PRIMITIVE, 0)) { /* We get 0 as unset... */
279 g_print ("Unset FAILED\n");
283 /* Recursive unset. Unset the entire subtree and check that all the keys
286 g_assert (gconf_client_recursive_unset (client, KEY_ROOT, 0, NULL) == TRUE);
287 if (!check_recursive_unset ()) {
288 g_print ("Recursive unset FAILED\n");
292 g_print ("Run %d completed.\n", ++count);
294 if (count == 3 || count == 6 || count == 9) {
295 g_main_loop_quit (loop);
303 main (int argc, char **argv)
307 client = gconf_client_get_default ();
309 /* Start out clean in case anything is left from earlier runs. */
310 gconf_client_recursive_unset (client, KEY_ROOT, 0, NULL);
312 /* We add "/foo/bar" so that "/foo" is uncached to test both those
315 gconf_client_add_dir (client,
317 GCONF_CLIENT_PRELOAD_RECURSIVE,
320 loop = g_main_loop_new (NULL, FALSE);
322 g_idle_add (change_timeout_func, NULL);
323 g_main_loop_run (loop);
325 g_timeout_add (500, change_timeout_func, NULL);
326 g_main_loop_run (loop);
328 g_idle_add (change_timeout_func, NULL);
329 g_main_loop_run (loop);
331 g_object_unref (client);