Add docs
[glib.git] / tests / bookmarkfile-test.c
blob06019ca1fb672de4962c360905e749c0b82698a4
1 #undef G_DISABLE_ASSERT
3 #include <glib.h>
4 #include <time.h>
5 #include <locale.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <stdlib.h>
10 #define TEST_URI_0 "file:///abc/defgh/ijklmnopqrstuvwxyz"
11 #define TEST_URI_1 "file:///test/uri/1"
12 #define TEST_URI_2 "file:///test/uri/2"
14 #define TEST_MIME "text/plain"
16 #define TEST_APP_NAME "bookmarkfile-test"
17 #define TEST_APP_EXEC "bookmarkfile-test %f"
19 static void
20 test_assert_empty_error (GError **error)
22 if (*error != NULL)
24 g_warning ("Unexpected error (d: %s, c: %d): %s\n",
25 g_quark_to_string ((*error)->domain),
26 (*error)->code,
27 (*error)->message);
28 g_error_free (*error);
30 g_assert_not_reached ();
34 static void
35 test_assert_not_empty_error (GError **error,
36 GQuark domain,
37 gint code)
39 if (*error == NULL)
41 g_warning ("Unexpected success (%s domain expected)\n",
42 g_quark_to_string (domain));
44 g_assert_not_reached ();
47 if ((*error)->domain != domain)
49 g_warning ("Unexpected domain %s (%s domain expected)\n",
50 g_quark_to_string ((*error)->domain),
51 g_quark_to_string (domain));
53 g_assert_not_reached ();
56 if ((*error)->code != code)
58 g_warning ("Unexpected code %d (%d code expected)\n",
59 (*error)->code,
60 code);
62 g_assert_not_reached ();
65 g_error_free (*error);
66 *error = NULL;
69 static void
70 test_assert_str_equal (const gchar *str,
71 const gchar *cmp)
73 if (strcmp (str, cmp) != 0)
75 g_warning ("Unexpected string '%s' ('%s' expected)\n", str, cmp);
77 g_assert_not_reached ();
81 static gboolean
82 test_load (GBookmarkFile *bookmark,
83 const gchar *filename)
85 GError *error = NULL;
86 gboolean res;
88 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
89 if (error)
91 g_print ("Load error: %s\n", error->message);
92 g_error_free (error);
95 return res;
98 static gboolean
99 test_query (GBookmarkFile *bookmark)
101 gint size;
102 gchar **uris;
103 gsize uris_len, i;
104 gboolean res = TRUE;
106 size = g_bookmark_file_get_size (bookmark);
107 uris = g_bookmark_file_get_uris (bookmark, &uris_len);
109 if (uris_len != size)
111 g_print ("URI/size mismatch: URI count is %d (should be %d)\n", uris_len, size);
113 res = FALSE;
116 for (i = 0; i < uris_len; i++)
117 if (!g_bookmark_file_has_item (bookmark, uris[i]))
119 g_print ("URI/bookmark mismatch: bookmark for '%s' does not exist\n", uris[i]);
121 res = FALSE;
124 g_strfreev (uris);
126 return res;
129 static gboolean
130 test_modify (GBookmarkFile *bookmark)
132 gchar *text;
133 guint count;
134 time_t stamp;
135 GError *error = NULL;
137 g_print ("\t=> check global title/description...");
138 g_bookmark_file_set_title (bookmark, NULL, "a file");
139 g_bookmark_file_set_description (bookmark, NULL, "a bookmark file");
141 text = g_bookmark_file_get_title (bookmark, NULL, &error);
142 test_assert_empty_error (&error);
143 test_assert_str_equal (text, "a file");
144 g_free (text);
146 text = g_bookmark_file_get_description (bookmark, NULL, &error);
147 test_assert_empty_error (&error);
148 test_assert_str_equal (text, "a bookmark file");
149 g_free (text);
150 g_print ("ok\n");
152 g_print ("\t=> check bookmark title/description...");
153 g_bookmark_file_set_title (bookmark, TEST_URI_0, "a title");
154 g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description");
156 text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error);
157 test_assert_empty_error (&error);
158 test_assert_str_equal (text, "a title");
159 g_free (text);
160 g_print ("ok\n");
162 g_print ("\t=> check non existing bookmark...");
163 g_bookmark_file_get_description (bookmark, TEST_URI_1, &error);
164 test_assert_not_empty_error (&error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
165 g_print ("ok\n");
167 g_print ("\t=> check application...");
168 g_bookmark_file_set_mime_type (bookmark, TEST_URI_0, TEST_MIME);
169 g_bookmark_file_add_application (bookmark, TEST_URI_0,
170 TEST_APP_NAME,
171 TEST_APP_EXEC);
172 g_assert (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL) == TRUE);
173 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, TEST_APP_NAME,
174 &text,
175 &count,
176 &stamp,
177 &error);
178 test_assert_empty_error (&error);
179 g_assert (count == 1);
180 g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL));
181 g_free (text);
183 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail",
184 &text,
185 &count,
186 &stamp,
187 &error);
188 test_assert_not_empty_error (&error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
189 g_print ("ok\n");
191 g_print ("\t=> check groups...");
192 g_bookmark_file_add_group (bookmark, TEST_URI_1, "Test");
193 g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL) == TRUE);
194 g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Fail", NULL) == FALSE);
195 g_print ("ok\n");
197 g_print ("\t=> check remove...");
198 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == TRUE);
199 test_assert_empty_error (&error);
200 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == FALSE);
201 test_assert_not_empty_error (&error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
202 g_print ("ok\n");
204 return TRUE;
207 static gint
208 test_file (const gchar *filename)
210 GBookmarkFile *bookmark_file;
211 gboolean success;
213 g_return_val_if_fail (filename != NULL, 1);
215 g_print ("checking GBookmarkFile...\n");
217 bookmark_file = g_bookmark_file_new ();
218 g_assert (bookmark_file != NULL);
220 success = test_load (bookmark_file, filename);
222 if (success)
224 success = test_query (bookmark_file);
225 success = test_modify (bookmark_file);
228 g_bookmark_file_free (bookmark_file);
230 g_print ("ok\n");
232 return (success == TRUE ? 0 : 1);
236 main (int argc,
237 char *argv[])
239 if (argc > 1)
240 return test_file (argv[1]);
241 else
243 fprintf (stderr, "Usage: bookmarkfile-test <bookmarkfile>\n");
245 return 1;