Remove redundant header inclusions
[glib.git] / glib / tests / bookmarkfile.c
blob5e1eac74a2c02e5722f76fe6657a86fdc9a7bec0
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 #ifndef SRCDIR
11 #define SRCDIR "."
12 #endif
14 #define TEST_URI_0 "file:///abc/defgh/ijklmnopqrstuvwxyz"
15 #define TEST_URI_1 "file:///test/uri/1"
16 #define TEST_URI_2 "file:///test/uri/2"
18 #define TEST_MIME "text/plain"
20 #define TEST_APP_NAME "bookmarkfile-test"
21 #define TEST_APP_EXEC "bookmarkfile-test %f"
23 static gboolean
24 test_load (GBookmarkFile *bookmark,
25 const gchar *filename)
27 GError *error = NULL;
28 gboolean res;
30 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
31 if (error && g_test_verbose ())
33 g_print ("Load error: %s\n", error->message);
34 g_error_free (error);
37 return res;
40 static void
41 test_query (GBookmarkFile *bookmark)
43 gint size;
44 gchar **uris;
45 gsize uris_len, i;
46 gchar *mime;
47 GError *error;
49 size = g_bookmark_file_get_size (bookmark);
50 uris = g_bookmark_file_get_uris (bookmark, &uris_len);
52 g_assert_cmpint (uris_len, ==, size);
54 for (i = 0; i < uris_len; i++)
56 g_assert (g_bookmark_file_has_item (bookmark, uris[i]));
57 error = NULL;
58 mime = g_bookmark_file_get_mime_type (bookmark, uris[i], &error);
59 g_assert (mime != NULL);
60 g_assert_no_error (error);
61 g_free (mime);
63 g_strfreev (uris);
65 g_assert (!g_bookmark_file_has_item (bookmark, "file:///no/such/uri"));
66 error = NULL;
67 mime = g_bookmark_file_get_mime_type (bookmark, "file:///no/such/uri", &error);
68 g_assert (mime == NULL);
69 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
70 g_error_free (error);
71 g_free (mime);
74 static gboolean
75 test_modify (GBookmarkFile *bookmark)
77 gchar *text;
78 guint count;
79 time_t stamp;
80 time_t now;
81 GError *error = NULL;
82 gchar **groups;
83 gsize length;
84 gchar **apps;
85 gchar *icon;
86 gchar *mime;
88 if (g_test_verbose ())
89 g_print ("\t=> check global title/description...");
90 g_bookmark_file_set_title (bookmark, NULL, "a file");
91 g_bookmark_file_set_description (bookmark, NULL, "a bookmark file");
93 text = g_bookmark_file_get_title (bookmark, NULL, &error);
94 g_assert_no_error (error);
95 g_assert_cmpstr (text, ==, "a file");
96 g_free (text);
98 text = g_bookmark_file_get_description (bookmark, NULL, &error);
99 g_assert_no_error (error);
100 g_assert_cmpstr (text, ==, "a bookmark file");
101 g_free (text);
102 if (g_test_verbose ())
103 g_print ("ok\n");
105 if (g_test_verbose ())
106 g_print ("\t=> check bookmark title/description...");
107 g_bookmark_file_set_title (bookmark, TEST_URI_0, "a title");
108 g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description");
109 g_bookmark_file_set_is_private (bookmark, TEST_URI_0, TRUE);
110 time (&now);
111 g_bookmark_file_set_added (bookmark, TEST_URI_0, now);
112 g_bookmark_file_set_modified (bookmark, TEST_URI_0, now);
113 g_bookmark_file_set_visited (bookmark, TEST_URI_0, now);
114 g_bookmark_file_set_icon (bookmark, TEST_URI_0, "testicon", "image/png");
116 text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error);
117 g_assert_no_error (error);
118 g_assert_cmpstr (text, ==, "a title");
119 g_free (text);
120 text = g_bookmark_file_get_description (bookmark, TEST_URI_0, &error);
121 g_assert_no_error (error);
122 g_assert_cmpstr (text, ==, "a description");
123 g_free (text);
124 g_assert (g_bookmark_file_get_is_private (bookmark, TEST_URI_0, &error));
125 g_assert_no_error (error);
126 stamp = g_bookmark_file_get_added (bookmark, TEST_URI_0, &error);
127 g_assert_no_error (error);
128 g_assert (stamp == now);
129 stamp = g_bookmark_file_get_modified (bookmark, TEST_URI_0, &error);
130 g_assert_no_error (error);
131 g_assert (stamp == now);
132 stamp = g_bookmark_file_get_visited (bookmark, TEST_URI_0, &error);
133 g_assert_no_error (error);
134 g_assert (stamp == now);
135 g_assert (g_bookmark_file_get_icon (bookmark, TEST_URI_0, &icon, &mime, &error));
136 g_assert_no_error (error);
137 g_assert_cmpstr (icon, ==, "testicon");
138 g_assert_cmpstr (mime, ==, "image/png");
139 g_free (icon);
140 g_free (mime);
141 if (g_test_verbose ())
142 g_print ("ok\n");
144 if (g_test_verbose ())
145 g_print ("\t=> check non existing bookmark...");
146 g_bookmark_file_get_description (bookmark, TEST_URI_1, &error);
147 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
148 g_clear_error (&error);
149 g_bookmark_file_get_is_private (bookmark, TEST_URI_1, &error);
150 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
151 g_clear_error (&error);
152 g_bookmark_file_get_added (bookmark, TEST_URI_1, &error);
153 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
154 g_clear_error (&error);
155 g_bookmark_file_get_modified (bookmark, TEST_URI_1, &error);
156 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
157 g_clear_error (&error);
158 g_bookmark_file_get_visited (bookmark, TEST_URI_1, &error);
159 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
160 g_clear_error (&error);
161 if (g_test_verbose ())
162 g_print ("ok\n");
164 if (g_test_verbose ())
165 g_print ("\t=> check application...");
166 g_bookmark_file_set_mime_type (bookmark, TEST_URI_0, TEST_MIME);
167 g_assert (!g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL));
168 g_bookmark_file_add_application (bookmark, TEST_URI_0,
169 TEST_APP_NAME,
170 TEST_APP_EXEC);
171 g_assert (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL));
172 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, TEST_APP_NAME,
173 &text,
174 &count,
175 &stamp,
176 &error);
177 g_assert_no_error (error);
178 g_assert (count == 1);
179 g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL));
180 g_free (text);
181 g_assert (g_bookmark_file_remove_application (bookmark, TEST_URI_0, TEST_APP_NAME, &error));
182 g_assert_no_error (error);
183 g_bookmark_file_add_application (bookmark, TEST_URI_0, TEST_APP_NAME, TEST_APP_EXEC);
184 apps = g_bookmark_file_get_applications (bookmark, TEST_URI_0, &length, &error);
185 g_assert_no_error (error);
186 g_assert_cmpint (length, ==, 1);
187 g_assert_cmpstr (apps[0], ==, TEST_APP_NAME);
188 g_strfreev (apps);
190 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail",
191 &text,
192 &count,
193 &stamp,
194 &error);
195 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
196 g_clear_error (&error);
198 if (g_test_verbose ())
199 g_print ("ok\n");
201 if (g_test_verbose ())
202 g_print ("\t=> check groups...");
203 g_assert (!g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL));
204 g_bookmark_file_add_group (bookmark, TEST_URI_1, "Test");
205 g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL));
206 g_assert (!g_bookmark_file_has_group (bookmark, TEST_URI_1, "Fail", NULL));
207 g_assert (g_bookmark_file_remove_group (bookmark, TEST_URI_1, "Test", &error));
208 g_assert_no_error (error);
209 groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, NULL, &error);
210 g_assert_cmpint (g_strv_length (groups), ==, 0);
211 g_strfreev (groups);
212 groups = g_new0 (gchar *, 3);
213 groups[0] = "Group1";
214 groups[1] = "Group2";
215 groups[2] = NULL;
216 g_bookmark_file_set_groups (bookmark, TEST_URI_1, (const gchar **)groups, 2);
217 g_free (groups);
218 groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, &length, &error);
219 g_assert_cmpint (length, ==, 2);
220 g_strfreev (groups);
221 g_assert_no_error (error);
223 if (g_test_verbose ())
224 g_print ("ok\n");
226 if (g_test_verbose ())
227 g_print ("\t=> check remove...");
228 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == TRUE);
229 g_assert_no_error (error);
230 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == FALSE);
231 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
232 g_clear_error (&error);
233 if (g_test_verbose ())
234 g_print ("ok\n");
236 return TRUE;
239 static void
240 test_file (gconstpointer d)
242 const gchar *filename = d;
243 GBookmarkFile *bookmark_file;
244 gboolean success;
245 gchar *data;
246 GError *error;
248 bookmark_file = g_bookmark_file_new ();
249 g_assert (bookmark_file != NULL);
251 success = test_load (bookmark_file, filename);
253 if (success)
255 test_query (bookmark_file);
256 test_modify (bookmark_file);
258 error = NULL;
259 data = g_bookmark_file_to_data (bookmark_file, NULL, &error);
260 g_assert_no_error (error);
261 /* FIXME do some checks on data */
262 g_free (data);
265 g_bookmark_file_free (bookmark_file);
267 g_assert (success == (strstr (filename, "fail") == NULL));
271 main (int argc, char *argv[])
273 GDir *dir;
274 GError *error;
275 const gchar *name;
276 gchar *path;
278 g_test_init (&argc, &argv, NULL);
280 if (argc > 1)
282 test_file (argv[1]);
283 return 0;
286 error = NULL;
287 dir = g_dir_open (SRCDIR "/bookmarks", 0, &error);
288 g_assert_no_error (error);
289 while ((name = g_dir_read_name (dir)) != NULL)
291 path = g_strdup_printf ("/bookmarks/parse/%s", name);
292 g_test_add_data_func (path, g_build_filename (SRCDIR, "bookmarks", name, NULL), test_file);
293 g_free (path);
295 g_dir_close (dir);
297 return g_test_run ();