1 #undef G_DISABLE_ASSERT
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"
20 test_load_from_data_dirs (void)
22 GBookmarkFile *bookmark;
27 bookmark = g_bookmark_file_new ();
29 res = g_bookmark_file_load_from_data_dirs (bookmark, "no-such-bookmark-file.xbel", &path, &error);
32 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
36 g_bookmark_file_free (bookmark);
42 GBookmarkFile *bookmark;
43 const gchar *filename;
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);
53 g_assert_no_error (error);
55 res = g_bookmark_file_to_file (bookmark, "out.xbel", &error);
57 g_assert_no_error (error);
59 res = g_file_get_contents (filename, &in, NULL, &error);
61 g_assert_no_error (error);
63 res = g_file_get_contents ("out.xbel", &out, NULL, &error);
65 g_assert_no_error (error);
67 g_assert_cmpstr (in, ==, out);
71 g_bookmark_file_free (bookmark);
77 GBookmarkFile *bookmark;
78 const gchar *filename;
82 bookmark = g_bookmark_file_new ();
84 filename = g_test_get_filename (G_TEST_DIST, "bookmarks", "valid-01.xbel", NULL);
85 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
87 g_assert_no_error (error);
89 res = g_bookmark_file_move_item (bookmark,
90 "file:///home/zefram/Documents/milan-stuttgart.ps",
91 "file:///tmp/schedule.ps",
94 g_assert_no_error (error);
96 res = g_bookmark_file_move_item (bookmark,
97 "file:///no-such-file.xbel",
98 "file:///tmp/schedule.ps",
101 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
102 g_clear_error (&error);
104 res = g_bookmark_file_move_item (bookmark,
105 "file:///tmp/schedule.ps",
109 g_assert_no_error (error);
111 g_bookmark_file_free (bookmark);
117 GBookmarkFile *bookmark;
118 const gchar *filename;
120 GError *error = NULL;
126 bookmark = g_bookmark_file_new ();
128 filename = g_test_get_filename (G_TEST_DIST, "bookmarks", "valid-01.xbel", NULL);
129 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
131 g_assert_no_error (error);
133 res = g_bookmark_file_get_icon (bookmark,
134 "file:///home/zefram/Documents/milan-stuttgart.ps",
139 g_assert_no_error (error);
141 res = g_bookmark_file_get_icon (bookmark,
142 "file:///tmp/schedule.ps",
147 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
148 g_clear_error (&error);
150 g_bookmark_file_set_description (bookmark,
151 "file:///tmp/schedule0.ps",
152 "imaginary schedule");
153 s = g_bookmark_file_get_description (bookmark,
154 "file:///tmp/schedule0.ps",
156 g_assert_no_error (error);
157 g_assert_cmpstr (s, ==, "imaginary schedule");
159 s = g_bookmark_file_get_mime_type (bookmark,
160 "file:///tmp/schedule0.ps",
162 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_INVALID_VALUE);
164 g_clear_error (&error);
165 res = g_bookmark_file_get_is_private (bookmark,
166 "file:///tmp/schedule0.ps",
168 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_INVALID_VALUE);
169 g_clear_error (&error);
171 g_bookmark_file_set_mime_type (bookmark,
172 "file:///tmp/schedule1.ps",
174 s = g_bookmark_file_get_mime_type (bookmark,
175 "file:///tmp/schedule1.ps",
177 g_assert_no_error (error);
178 g_assert_cmpstr (s, ==, "image/png");
181 g_bookmark_file_set_is_private (bookmark,
182 "file:///tmp/schedule2.ps",
184 res = g_bookmark_file_get_is_private (bookmark,
185 "file:///tmp/schedule2.ps",
187 g_assert_no_error (error);
191 g_bookmark_file_set_added (bookmark,
192 "file:///tmp/schedule3.ps",
194 t = g_bookmark_file_get_added (bookmark,
195 "file:///tmp/schedule3.ps",
197 g_assert_no_error (error);
200 g_bookmark_file_set_modified (bookmark,
201 "file:///tmp/schedule4.ps",
203 t = g_bookmark_file_get_modified (bookmark,
204 "file:///tmp/schedule4.ps",
206 g_assert_no_error (error);
209 g_bookmark_file_set_visited (bookmark,
210 "file:///tmp/schedule5.ps",
212 t = g_bookmark_file_get_visited (bookmark,
213 "file:///tmp/schedule5.ps",
215 g_assert_no_error (error);
218 g_bookmark_file_set_icon (bookmark,
219 "file:///tmp/schedule6.ps",
220 "application-x-postscript",
222 res = g_bookmark_file_get_icon (bookmark,
223 "file:///tmp/schedule6.ps",
227 g_assert_no_error (error);
229 g_assert_cmpstr (s, ==, "application-x-postscript");
232 g_bookmark_file_set_icon (bookmark,
233 "file:///tmp/schedule6.ps",
235 res = g_bookmark_file_get_icon (bookmark,
236 "file:///tmp/schedule6.ps",
240 g_assert_no_error (error);
243 res = g_bookmark_file_has_application (bookmark,
244 "file:///tmp/schedule7.ps",
247 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
249 g_clear_error (&error);
251 g_bookmark_file_add_application (bookmark,
252 "file:///tmp/schedule7.ps",
254 res = g_bookmark_file_get_app_info (bookmark,
255 "file:///tmp/schedule7.ps",
256 g_get_application_name (),
259 g_assert_no_error (error);
261 cmd = g_strconcat (g_get_prgname (), " file:///tmp/schedule7.ps", NULL);
262 g_assert_cmpstr (exec, ==, cmd);
265 g_assert_cmpuint (count, ==, 1);
268 g_bookmark_file_free (bookmark);
272 test_load (GBookmarkFile *bookmark,
273 const gchar *filename)
275 GError *error = NULL;
278 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
279 if (error && g_test_verbose ())
280 g_print ("Load error: %s\n", error->message);
282 g_clear_error (&error);
287 test_query (GBookmarkFile *bookmark)
295 size = g_bookmark_file_get_size (bookmark);
296 uris = g_bookmark_file_get_uris (bookmark, &uris_len);
298 g_assert_cmpint (uris_len, ==, size);
300 for (i = 0; i < uris_len; i++)
302 g_assert (g_bookmark_file_has_item (bookmark, uris[i]));
304 mime = g_bookmark_file_get_mime_type (bookmark, uris[i], &error);
305 g_assert (mime != NULL);
306 g_assert_no_error (error);
311 g_assert (!g_bookmark_file_has_item (bookmark, "file:///no/such/uri"));
313 mime = g_bookmark_file_get_mime_type (bookmark, "file:///no/such/uri", &error);
314 g_assert (mime == NULL);
315 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
316 g_error_free (error);
321 test_modify (GBookmarkFile *bookmark)
327 GError *error = NULL;
334 if (g_test_verbose ())
335 g_print ("\t=> check global title/description...");
336 g_bookmark_file_set_title (bookmark, NULL, "a file");
337 g_bookmark_file_set_description (bookmark, NULL, "a bookmark file");
339 text = g_bookmark_file_get_title (bookmark, NULL, &error);
340 g_assert_no_error (error);
341 g_assert_cmpstr (text, ==, "a file");
344 text = g_bookmark_file_get_description (bookmark, NULL, &error);
345 g_assert_no_error (error);
346 g_assert_cmpstr (text, ==, "a bookmark file");
348 if (g_test_verbose ())
351 if (g_test_verbose ())
352 g_print ("\t=> check bookmark title/description...");
353 g_bookmark_file_set_title (bookmark, TEST_URI_0, "a title");
354 g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description");
355 g_bookmark_file_set_is_private (bookmark, TEST_URI_0, TRUE);
357 g_bookmark_file_set_added (bookmark, TEST_URI_0, now);
358 g_bookmark_file_set_modified (bookmark, TEST_URI_0, now);
359 g_bookmark_file_set_visited (bookmark, TEST_URI_0, now);
360 g_bookmark_file_set_icon (bookmark, TEST_URI_0, "testicon", "image/png");
362 text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error);
363 g_assert_no_error (error);
364 g_assert_cmpstr (text, ==, "a title");
366 text = g_bookmark_file_get_description (bookmark, TEST_URI_0, &error);
367 g_assert_no_error (error);
368 g_assert_cmpstr (text, ==, "a description");
370 g_assert (g_bookmark_file_get_is_private (bookmark, TEST_URI_0, &error));
371 g_assert_no_error (error);
372 stamp = g_bookmark_file_get_added (bookmark, TEST_URI_0, &error);
373 g_assert_no_error (error);
374 g_assert (stamp == now);
375 stamp = g_bookmark_file_get_modified (bookmark, TEST_URI_0, &error);
376 g_assert_no_error (error);
377 g_assert (stamp == now);
378 stamp = g_bookmark_file_get_visited (bookmark, TEST_URI_0, &error);
379 g_assert_no_error (error);
380 g_assert (stamp == now);
381 g_assert (g_bookmark_file_get_icon (bookmark, TEST_URI_0, &icon, &mime, &error));
382 g_assert_no_error (error);
383 g_assert_cmpstr (icon, ==, "testicon");
384 g_assert_cmpstr (mime, ==, "image/png");
387 if (g_test_verbose ())
390 if (g_test_verbose ())
391 g_print ("\t=> check non existing bookmark...");
392 g_bookmark_file_get_description (bookmark, TEST_URI_1, &error);
393 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
394 g_clear_error (&error);
395 g_bookmark_file_get_is_private (bookmark, TEST_URI_1, &error);
396 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
397 g_clear_error (&error);
398 g_bookmark_file_get_added (bookmark, TEST_URI_1, &error);
399 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
400 g_clear_error (&error);
401 g_bookmark_file_get_modified (bookmark, TEST_URI_1, &error);
402 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
403 g_clear_error (&error);
404 g_bookmark_file_get_visited (bookmark, TEST_URI_1, &error);
405 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
406 g_clear_error (&error);
407 if (g_test_verbose ())
410 if (g_test_verbose ())
411 g_print ("\t=> check application...");
412 g_bookmark_file_set_mime_type (bookmark, TEST_URI_0, TEST_MIME);
413 g_assert (!g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL));
414 g_bookmark_file_add_application (bookmark, TEST_URI_0,
417 g_assert (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL));
418 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, TEST_APP_NAME,
423 g_assert_no_error (error);
424 g_assert (count == 1);
425 g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL));
427 g_assert (g_bookmark_file_remove_application (bookmark, TEST_URI_0, TEST_APP_NAME, &error));
428 g_assert_no_error (error);
429 g_bookmark_file_add_application (bookmark, TEST_URI_0, TEST_APP_NAME, TEST_APP_EXEC);
430 apps = g_bookmark_file_get_applications (bookmark, TEST_URI_0, &length, &error);
431 g_assert_no_error (error);
432 g_assert_cmpint (length, ==, 1);
433 g_assert_cmpstr (apps[0], ==, TEST_APP_NAME);
436 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail",
441 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
442 g_clear_error (&error);
444 if (g_test_verbose ())
447 if (g_test_verbose ())
448 g_print ("\t=> check groups...");
449 g_assert (!g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL));
450 g_bookmark_file_add_group (bookmark, TEST_URI_1, "Test");
451 g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL));
452 g_assert (!g_bookmark_file_has_group (bookmark, TEST_URI_1, "Fail", NULL));
453 g_assert (g_bookmark_file_remove_group (bookmark, TEST_URI_1, "Test", &error));
454 g_assert_no_error (error);
455 groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, NULL, &error);
456 g_assert_cmpint (g_strv_length (groups), ==, 0);
458 groups = g_new0 (gchar *, 3);
459 groups[0] = "Group1";
460 groups[1] = "Group2";
462 g_bookmark_file_set_groups (bookmark, TEST_URI_1, (const gchar **)groups, 2);
464 groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, &length, &error);
465 g_assert_cmpint (length, ==, 2);
467 g_assert_no_error (error);
469 if (g_test_verbose ())
472 if (g_test_verbose ())
473 g_print ("\t=> check remove...");
474 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == TRUE);
475 g_assert_no_error (error);
476 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == FALSE);
477 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
478 g_clear_error (&error);
479 if (g_test_verbose ())
486 test_file (gconstpointer d)
488 const gchar *filename = d;
489 GBookmarkFile *bookmark_file;
494 bookmark_file = g_bookmark_file_new ();
495 g_assert (bookmark_file != NULL);
497 success = test_load (bookmark_file, filename);
501 test_query (bookmark_file);
502 test_modify (bookmark_file);
505 data = g_bookmark_file_to_data (bookmark_file, NULL, &error);
506 g_assert_no_error (error);
507 /* FIXME do some checks on data */
511 g_bookmark_file_free (bookmark_file);
513 g_assert (success == (strstr (filename, "fail") == NULL));
517 main (int argc, char *argv[])
524 g_test_init (&argc, &argv, NULL);
532 g_test_add_func ("/bookmarks/load-from-data-dirs", test_load_from_data_dirs);
533 g_test_add_func ("/bookmarks/to-file", test_to_file);
534 g_test_add_func ("/bookmarks/move-item", test_move_item);
535 g_test_add_func ("/bookmarks/misc", test_misc);
538 path = g_test_build_filename (G_TEST_DIST, "bookmarks", NULL);
539 dir = g_dir_open (path, 0, &error);
541 g_assert_no_error (error);
542 while ((name = g_dir_read_name (dir)) != NULL)
544 if (!g_str_has_suffix (name, ".xbel"))
547 path = g_strdup_printf ("/bookmarks/parse/%s", name);
548 g_test_add_data_func_full (path, g_test_build_filename (G_TEST_DIST, "bookmarks", name, NULL),
554 return g_test_run ();