Add some more cases to the app-id unit tests
[glib.git] / glib / tests / bookmarkfile.c
blobc81561ea286f2bfc6b42766e13504d8aa2ec9dee
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_load_from_data_dirs (void)
22 GBookmarkFile *bookmark;
23 gboolean res;
24 gchar *path = NULL;
25 GError *error = NULL;
27 bookmark = g_bookmark_file_new ();
29 res = g_bookmark_file_load_from_data_dirs (bookmark, "no-such-bookmark-file.xbel", &path, &error);
31 g_assert (!res);
32 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
33 g_assert_null (path);
34 g_error_free (error);
36 g_bookmark_file_free (bookmark);
39 static void
40 test_to_file (void)
42 GBookmarkFile *bookmark;
43 const gchar *filename;
44 gboolean res;
45 GError *error = NULL;
46 gchar *in, *out;
48 bookmark = g_bookmark_file_new ();
50 filename = g_test_get_filename (G_TEST_DIST, "bookmarks", "valid-01.xbel", NULL);
51 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
52 g_assert (res);
53 g_assert_no_error (error);
55 res = g_bookmark_file_to_file (bookmark, "out.xbel", &error);
56 g_assert (res);
57 g_assert_no_error (error);
59 res = g_file_get_contents (filename, &in, NULL, &error);
60 g_assert (res);
61 g_assert_no_error (error);
63 res = g_file_get_contents ("out.xbel", &out, NULL, &error);
64 g_assert (res);
65 g_assert_no_error (error);
66 remove ("out.xbel");
68 g_assert_cmpstr (in, ==, out);
69 g_free (in);
70 g_free (out);
72 g_bookmark_file_free (bookmark);
75 static void
76 test_move_item (void)
78 GBookmarkFile *bookmark;
79 const gchar *filename;
80 gboolean res;
81 GError *error = NULL;
83 bookmark = g_bookmark_file_new ();
85 filename = g_test_get_filename (G_TEST_DIST, "bookmarks", "valid-01.xbel", NULL);
86 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
87 g_assert (res);
88 g_assert_no_error (error);
90 res = g_bookmark_file_move_item (bookmark,
91 "file:///home/zefram/Documents/milan-stuttgart.ps",
92 "file:///tmp/schedule.ps",
93 &error);
94 g_assert (res);
95 g_assert_no_error (error);
97 res = g_bookmark_file_move_item (bookmark,
98 "file:///no-such-file.xbel",
99 "file:///tmp/schedule.ps",
100 &error);
101 g_assert (!res);
102 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
103 g_clear_error (&error);
105 res = g_bookmark_file_move_item (bookmark,
106 "file:///tmp/schedule.ps",
107 NULL,
108 &error);
109 g_assert (res);
110 g_assert_no_error (error);
112 g_bookmark_file_free (bookmark);
115 static void
116 test_misc (void)
118 GBookmarkFile *bookmark;
119 const gchar *filename;
120 gboolean res;
121 GError *error = NULL;
122 gchar *s;
123 time_t now, t;
124 gchar *cmd, *exec;
125 guint count;
127 bookmark = g_bookmark_file_new ();
129 filename = g_test_get_filename (G_TEST_DIST, "bookmarks", "valid-01.xbel", NULL);
130 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
131 g_assert (res);
132 g_assert_no_error (error);
134 res = g_bookmark_file_get_icon (bookmark,
135 "file:///home/zefram/Documents/milan-stuttgart.ps",
136 NULL,
137 NULL,
138 &error);
139 g_assert (!res);
140 g_assert_no_error (error);
142 res = g_bookmark_file_get_icon (bookmark,
143 "file:///tmp/schedule.ps",
144 NULL,
145 NULL,
146 &error);
147 g_assert (!res);
148 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
149 g_clear_error (&error);
151 g_bookmark_file_set_description (bookmark,
152 "file:///tmp/schedule0.ps",
153 "imaginary schedule");
154 s = g_bookmark_file_get_description (bookmark,
155 "file:///tmp/schedule0.ps",
156 &error);
157 g_assert_no_error (error);
158 g_assert_cmpstr (s, ==, "imaginary schedule");
159 g_free (s);
160 s = g_bookmark_file_get_mime_type (bookmark,
161 "file:///tmp/schedule0.ps",
162 &error);
163 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_INVALID_VALUE);
164 g_assert_null (s);
165 g_clear_error (&error);
166 res = g_bookmark_file_get_is_private (bookmark,
167 "file:///tmp/schedule0.ps",
168 &error);
169 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_INVALID_VALUE);
170 g_clear_error (&error);
172 g_bookmark_file_set_mime_type (bookmark,
173 "file:///tmp/schedule1.ps",
174 "image/png");
175 s = g_bookmark_file_get_mime_type (bookmark,
176 "file:///tmp/schedule1.ps",
177 &error);
178 g_assert_no_error (error);
179 g_assert_cmpstr (s, ==, "image/png");
180 g_free (s);
182 g_bookmark_file_set_is_private (bookmark,
183 "file:///tmp/schedule2.ps",
184 TRUE);
185 res = g_bookmark_file_get_is_private (bookmark,
186 "file:///tmp/schedule2.ps",
187 &error);
188 g_assert_no_error (error);
189 g_assert (res);
191 time (&now);
192 g_bookmark_file_set_added (bookmark,
193 "file:///tmp/schedule3.ps",
194 (time_t)-1);
195 t = g_bookmark_file_get_added (bookmark,
196 "file:///tmp/schedule3.ps",
197 &error);
198 g_assert_no_error (error);
199 g_assert (t == now);
201 g_bookmark_file_set_modified (bookmark,
202 "file:///tmp/schedule4.ps",
203 (time_t)-1);
204 t = g_bookmark_file_get_modified (bookmark,
205 "file:///tmp/schedule4.ps",
206 &error);
207 g_assert_no_error (error);
208 g_assert (t == now);
210 g_bookmark_file_set_visited (bookmark,
211 "file:///tmp/schedule5.ps",
212 (time_t)-1);
213 t = g_bookmark_file_get_visited (bookmark,
214 "file:///tmp/schedule5.ps",
215 &error);
216 g_assert_no_error (error);
217 g_assert (t == now);
219 g_bookmark_file_set_icon (bookmark,
220 "file:///tmp/schedule6.ps",
221 "application-x-postscript",
222 "image/png");
223 res = g_bookmark_file_get_icon (bookmark,
224 "file:///tmp/schedule6.ps",
226 NULL,
227 &error);
228 g_assert_no_error (error);
229 g_assert (res);
230 g_assert_cmpstr (s, ==, "application-x-postscript");
231 g_free (s);
233 g_bookmark_file_set_icon (bookmark,
234 "file:///tmp/schedule6.ps",
235 NULL, NULL);
236 res = g_bookmark_file_get_icon (bookmark,
237 "file:///tmp/schedule6.ps",
239 NULL,
240 &error);
241 g_assert_no_error (error);
242 g_assert (!res);
244 res = g_bookmark_file_has_application (bookmark,
245 "file:///tmp/schedule7.ps",
246 "foo",
247 &error);
248 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
249 g_assert (!res);
250 g_clear_error (&error);
252 g_bookmark_file_add_application (bookmark,
253 "file:///tmp/schedule7.ps",
254 NULL, NULL);
255 res = g_bookmark_file_get_app_info (bookmark,
256 "file:///tmp/schedule7.ps",
257 g_get_application_name (),
258 &exec, &count, &t,
259 &error);
260 g_assert_no_error (error);
261 g_assert (res);
262 cmd = g_strconcat (g_get_prgname (), " file:///tmp/schedule7.ps", NULL);
263 g_assert_cmpstr (exec, ==, cmd);
264 g_free (cmd);
265 g_free (exec);
266 g_assert_cmpuint (count, ==, 1);
267 g_assert (t == now);
269 g_bookmark_file_free (bookmark);
272 static gboolean
273 test_load (GBookmarkFile *bookmark,
274 const gchar *filename)
276 GError *error = NULL;
277 gboolean res;
279 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
280 if (error && g_test_verbose ())
281 g_printerr ("Load error: %s\n", error->message);
283 g_clear_error (&error);
284 return res;
287 static void
288 test_query (GBookmarkFile *bookmark)
290 gint size;
291 gchar **uris;
292 gsize uris_len, i;
293 gchar *mime;
294 GError *error;
296 size = g_bookmark_file_get_size (bookmark);
297 uris = g_bookmark_file_get_uris (bookmark, &uris_len);
299 g_assert_cmpint (uris_len, ==, size);
301 for (i = 0; i < uris_len; i++)
303 g_assert (g_bookmark_file_has_item (bookmark, uris[i]));
304 error = NULL;
305 mime = g_bookmark_file_get_mime_type (bookmark, uris[i], &error);
306 g_assert (mime != NULL);
307 g_assert_no_error (error);
308 g_free (mime);
310 g_strfreev (uris);
312 g_assert (!g_bookmark_file_has_item (bookmark, "file:///no/such/uri"));
313 error = NULL;
314 mime = g_bookmark_file_get_mime_type (bookmark, "file:///no/such/uri", &error);
315 g_assert (mime == NULL);
316 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
317 g_error_free (error);
318 g_free (mime);
321 static gboolean
322 test_modify (GBookmarkFile *bookmark)
324 gchar *text;
325 guint count;
326 time_t stamp;
327 time_t now;
328 GError *error = NULL;
329 gchar **groups;
330 gsize length;
331 gchar **apps;
332 gchar *icon;
333 gchar *mime;
335 if (g_test_verbose ())
336 g_printerr ("\t=> check global title/description...");
337 g_bookmark_file_set_title (bookmark, NULL, "a file");
338 g_bookmark_file_set_description (bookmark, NULL, "a bookmark file");
340 text = g_bookmark_file_get_title (bookmark, NULL, &error);
341 g_assert_no_error (error);
342 g_assert_cmpstr (text, ==, "a file");
343 g_free (text);
345 text = g_bookmark_file_get_description (bookmark, NULL, &error);
346 g_assert_no_error (error);
347 g_assert_cmpstr (text, ==, "a bookmark file");
348 g_free (text);
349 if (g_test_verbose ())
350 g_printerr ("ok\n");
352 if (g_test_verbose ())
353 g_printerr ("\t=> check bookmark title/description...");
354 g_bookmark_file_set_title (bookmark, TEST_URI_0, "a title");
355 g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description");
356 g_bookmark_file_set_is_private (bookmark, TEST_URI_0, TRUE);
357 time (&now);
358 g_bookmark_file_set_added (bookmark, TEST_URI_0, now);
359 g_bookmark_file_set_modified (bookmark, TEST_URI_0, now);
360 g_bookmark_file_set_visited (bookmark, TEST_URI_0, now);
361 g_bookmark_file_set_icon (bookmark, TEST_URI_0, "testicon", "image/png");
363 text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error);
364 g_assert_no_error (error);
365 g_assert_cmpstr (text, ==, "a title");
366 g_free (text);
367 text = g_bookmark_file_get_description (bookmark, TEST_URI_0, &error);
368 g_assert_no_error (error);
369 g_assert_cmpstr (text, ==, "a description");
370 g_free (text);
371 g_assert (g_bookmark_file_get_is_private (bookmark, TEST_URI_0, &error));
372 g_assert_no_error (error);
373 stamp = g_bookmark_file_get_added (bookmark, TEST_URI_0, &error);
374 g_assert_no_error (error);
375 g_assert (stamp == now);
376 stamp = g_bookmark_file_get_modified (bookmark, TEST_URI_0, &error);
377 g_assert_no_error (error);
378 g_assert (stamp == now);
379 stamp = g_bookmark_file_get_visited (bookmark, TEST_URI_0, &error);
380 g_assert_no_error (error);
381 g_assert (stamp == now);
382 g_assert (g_bookmark_file_get_icon (bookmark, TEST_URI_0, &icon, &mime, &error));
383 g_assert_no_error (error);
384 g_assert_cmpstr (icon, ==, "testicon");
385 g_assert_cmpstr (mime, ==, "image/png");
386 g_free (icon);
387 g_free (mime);
388 if (g_test_verbose ())
389 g_printerr ("ok\n");
391 if (g_test_verbose ())
392 g_printerr ("\t=> check non existing bookmark...");
393 g_bookmark_file_get_description (bookmark, TEST_URI_1, &error);
394 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
395 g_clear_error (&error);
396 g_bookmark_file_get_is_private (bookmark, TEST_URI_1, &error);
397 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
398 g_clear_error (&error);
399 g_bookmark_file_get_added (bookmark, TEST_URI_1, &error);
400 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
401 g_clear_error (&error);
402 g_bookmark_file_get_modified (bookmark, TEST_URI_1, &error);
403 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
404 g_clear_error (&error);
405 g_bookmark_file_get_visited (bookmark, TEST_URI_1, &error);
406 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
407 g_clear_error (&error);
408 if (g_test_verbose ())
409 g_printerr ("ok\n");
411 if (g_test_verbose ())
412 g_printerr ("\t=> check application...");
413 g_bookmark_file_set_mime_type (bookmark, TEST_URI_0, TEST_MIME);
414 g_assert (!g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL));
415 g_bookmark_file_add_application (bookmark, TEST_URI_0,
416 TEST_APP_NAME,
417 TEST_APP_EXEC);
418 g_assert (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL));
419 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, TEST_APP_NAME,
420 &text,
421 &count,
422 &stamp,
423 &error);
424 g_assert_no_error (error);
425 g_assert (count == 1);
426 g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL));
427 g_free (text);
428 g_assert (g_bookmark_file_remove_application (bookmark, TEST_URI_0, TEST_APP_NAME, &error));
429 g_assert_no_error (error);
430 g_bookmark_file_add_application (bookmark, TEST_URI_0, TEST_APP_NAME, TEST_APP_EXEC);
431 apps = g_bookmark_file_get_applications (bookmark, TEST_URI_0, &length, &error);
432 g_assert_no_error (error);
433 g_assert_cmpint (length, ==, 1);
434 g_assert_cmpstr (apps[0], ==, TEST_APP_NAME);
435 g_strfreev (apps);
437 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail",
438 &text,
439 &count,
440 &stamp,
441 &error);
442 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
443 g_clear_error (&error);
445 if (g_test_verbose ())
446 g_printerr ("ok\n");
448 if (g_test_verbose ())
449 g_printerr ("\t=> check groups...");
450 g_assert (!g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL));
451 g_bookmark_file_add_group (bookmark, TEST_URI_1, "Test");
452 g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL));
453 g_assert (!g_bookmark_file_has_group (bookmark, TEST_URI_1, "Fail", NULL));
454 g_assert (g_bookmark_file_remove_group (bookmark, TEST_URI_1, "Test", &error));
455 g_assert_no_error (error);
456 groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, NULL, &error);
457 g_assert_cmpint (g_strv_length (groups), ==, 0);
458 g_strfreev (groups);
459 groups = g_new0 (gchar *, 3);
460 groups[0] = "Group1";
461 groups[1] = "Group2";
462 groups[2] = NULL;
463 g_bookmark_file_set_groups (bookmark, TEST_URI_1, (const gchar **)groups, 2);
464 g_free (groups);
465 groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, &length, &error);
466 g_assert_cmpint (length, ==, 2);
467 g_strfreev (groups);
468 g_assert_no_error (error);
470 if (g_test_verbose ())
471 g_printerr ("ok\n");
473 if (g_test_verbose ())
474 g_printerr ("\t=> check remove...");
475 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == TRUE);
476 g_assert_no_error (error);
477 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == FALSE);
478 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
479 g_clear_error (&error);
480 if (g_test_verbose ())
481 g_printerr ("ok\n");
483 return TRUE;
486 static void
487 test_file (gconstpointer d)
489 const gchar *filename = d;
490 GBookmarkFile *bookmark_file;
491 gboolean success;
492 gchar *data;
493 GError *error;
495 bookmark_file = g_bookmark_file_new ();
496 g_assert (bookmark_file != NULL);
498 success = test_load (bookmark_file, filename);
500 if (success)
502 test_query (bookmark_file);
503 test_modify (bookmark_file);
505 error = NULL;
506 data = g_bookmark_file_to_data (bookmark_file, NULL, &error);
507 g_assert_no_error (error);
508 /* FIXME do some checks on data */
509 g_free (data);
512 g_bookmark_file_free (bookmark_file);
514 g_assert (success == (strstr (filename, "fail") == NULL));
518 main (int argc, char *argv[])
520 GDir *dir;
521 GError *error;
522 const gchar *name;
523 gchar *path;
525 g_test_init (&argc, &argv, NULL);
527 if (argc > 1)
529 test_file (argv[1]);
530 return 0;
533 g_test_add_func ("/bookmarks/load-from-data-dirs", test_load_from_data_dirs);
534 g_test_add_func ("/bookmarks/to-file", test_to_file);
535 g_test_add_func ("/bookmarks/move-item", test_move_item);
536 g_test_add_func ("/bookmarks/misc", test_misc);
538 error = NULL;
539 path = g_test_build_filename (G_TEST_DIST, "bookmarks", NULL);
540 dir = g_dir_open (path, 0, &error);
541 g_free (path);
542 g_assert_no_error (error);
543 while ((name = g_dir_read_name (dir)) != NULL)
545 if (!g_str_has_suffix (name, ".xbel"))
546 continue;
548 path = g_strdup_printf ("/bookmarks/parse/%s", name);
549 g_test_add_data_func_full (path, g_test_build_filename (G_TEST_DIST, "bookmarks", name, NULL),
550 test_file, g_free);
551 g_free (path);
553 g_dir_close (dir);
555 return g_test_run ();