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);
35 g_bookmark_file_free (bookmark);
41 GBookmarkFile *bookmark;
42 const gchar *filename;
47 bookmark = g_bookmark_file_new ();
49 filename = g_test_get_filename (G_TEST_DIST, "bookmarks", "valid-01.xbel", NULL);
50 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
52 g_assert_no_error (error);
54 res = g_bookmark_file_to_file (bookmark, "out.xbel", &error);
56 g_assert_no_error (error);
58 res = g_file_get_contents (filename, &in, NULL, &error);
60 g_assert_no_error (error);
62 res = g_file_get_contents ("out.xbel", &out, NULL, &error);
64 g_assert_no_error (error);
66 g_assert_cmpstr (in, ==, out);
70 g_bookmark_file_free (bookmark);
76 GBookmarkFile *bookmark;
77 const gchar *filename;
81 bookmark = g_bookmark_file_new ();
83 filename = g_test_get_filename (G_TEST_DIST, "bookmarks", "valid-01.xbel", NULL);
84 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
86 g_assert_no_error (error);
88 res = g_bookmark_file_move_item (bookmark,
89 "file:///home/zefram/Documents/milan-stuttgart.ps",
90 "file:///tmp/schedule.ps",
93 g_assert_no_error (error);
95 res = g_bookmark_file_move_item (bookmark,
96 "file:///no-such-file.xbel",
97 "file:///tmp/schedule.ps",
100 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
101 g_clear_error (&error);
103 res = g_bookmark_file_move_item (bookmark,
104 "file:///tmp/schedule.ps",
108 g_assert_no_error (error);
110 g_bookmark_file_free (bookmark);
116 GBookmarkFile *bookmark;
117 const gchar *filename;
119 GError *error = NULL;
125 bookmark = g_bookmark_file_new ();
127 filename = g_test_get_filename (G_TEST_DIST, "bookmarks", "valid-01.xbel", NULL);
128 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
130 g_assert_no_error (error);
132 res = g_bookmark_file_get_icon (bookmark,
133 "file:///home/zefram/Documents/milan-stuttgart.ps",
138 g_assert_no_error (error);
140 res = g_bookmark_file_get_icon (bookmark,
141 "file:///tmp/schedule.ps",
146 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
147 g_clear_error (&error);
149 g_bookmark_file_set_description (bookmark,
150 "file:///tmp/schedule0.ps",
151 "imaginary schedule");
152 s = g_bookmark_file_get_description (bookmark,
153 "file:///tmp/schedule0.ps",
155 g_assert_no_error (error);
156 g_assert_cmpstr (s, ==, "imaginary schedule");
158 s = g_bookmark_file_get_mime_type (bookmark,
159 "file:///tmp/schedule0.ps",
161 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_INVALID_VALUE);
163 g_clear_error (&error);
164 res = g_bookmark_file_get_is_private (bookmark,
165 "file:///tmp/schedule0.ps",
167 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_INVALID_VALUE);
168 g_clear_error (&error);
170 g_bookmark_file_set_mime_type (bookmark,
171 "file:///tmp/schedule1.ps",
173 s = g_bookmark_file_get_mime_type (bookmark,
174 "file:///tmp/schedule1.ps",
176 g_assert_no_error (error);
177 g_assert_cmpstr (s, ==, "image/png");
180 g_bookmark_file_set_is_private (bookmark,
181 "file:///tmp/schedule2.ps",
183 res = g_bookmark_file_get_is_private (bookmark,
184 "file:///tmp/schedule2.ps",
186 g_assert_no_error (error);
190 g_bookmark_file_set_added (bookmark,
191 "file:///tmp/schedule3.ps",
193 t = g_bookmark_file_get_added (bookmark,
194 "file:///tmp/schedule3.ps",
196 g_assert_no_error (error);
199 g_bookmark_file_set_modified (bookmark,
200 "file:///tmp/schedule4.ps",
202 t = g_bookmark_file_get_modified (bookmark,
203 "file:///tmp/schedule4.ps",
205 g_assert_no_error (error);
208 g_bookmark_file_set_visited (bookmark,
209 "file:///tmp/schedule5.ps",
211 t = g_bookmark_file_get_visited (bookmark,
212 "file:///tmp/schedule5.ps",
214 g_assert_no_error (error);
217 g_bookmark_file_set_icon (bookmark,
218 "file:///tmp/schedule6.ps",
219 "application-x-postscript",
221 res = g_bookmark_file_get_icon (bookmark,
222 "file:///tmp/schedule6.ps",
226 g_assert_no_error (error);
228 g_assert_cmpstr (s, ==, "application-x-postscript");
231 g_bookmark_file_set_icon (bookmark,
232 "file:///tmp/schedule6.ps",
234 res = g_bookmark_file_get_icon (bookmark,
235 "file:///tmp/schedule6.ps",
239 g_assert_no_error (error);
242 res = g_bookmark_file_has_application (bookmark,
243 "file:///tmp/schedule7.ps",
246 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
248 g_clear_error (&error);
250 g_bookmark_file_add_application (bookmark,
251 "file:///tmp/schedule7.ps",
253 res = g_bookmark_file_get_app_info (bookmark,
254 "file:///tmp/schedule7.ps",
255 g_get_application_name (),
258 g_assert_no_error (error);
260 cmd = g_strconcat (g_get_prgname (), " file:///tmp/schedule7.ps", NULL);
261 g_assert_cmpstr (exec, ==, cmd);
264 g_assert_cmpuint (count, ==, 1);
267 g_bookmark_file_free (bookmark);
271 test_load (GBookmarkFile *bookmark,
272 const gchar *filename)
274 GError *error = NULL;
277 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
278 if (error && g_test_verbose ())
279 g_print ("Load error: %s\n", error->message);
281 g_clear_error (&error);
286 test_query (GBookmarkFile *bookmark)
294 size = g_bookmark_file_get_size (bookmark);
295 uris = g_bookmark_file_get_uris (bookmark, &uris_len);
297 g_assert_cmpint (uris_len, ==, size);
299 for (i = 0; i < uris_len; i++)
301 g_assert (g_bookmark_file_has_item (bookmark, uris[i]));
303 mime = g_bookmark_file_get_mime_type (bookmark, uris[i], &error);
304 g_assert (mime != NULL);
305 g_assert_no_error (error);
310 g_assert (!g_bookmark_file_has_item (bookmark, "file:///no/such/uri"));
312 mime = g_bookmark_file_get_mime_type (bookmark, "file:///no/such/uri", &error);
313 g_assert (mime == NULL);
314 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
315 g_error_free (error);
320 test_modify (GBookmarkFile *bookmark)
326 GError *error = NULL;
333 if (g_test_verbose ())
334 g_print ("\t=> check global title/description...");
335 g_bookmark_file_set_title (bookmark, NULL, "a file");
336 g_bookmark_file_set_description (bookmark, NULL, "a bookmark file");
338 text = g_bookmark_file_get_title (bookmark, NULL, &error);
339 g_assert_no_error (error);
340 g_assert_cmpstr (text, ==, "a file");
343 text = g_bookmark_file_get_description (bookmark, NULL, &error);
344 g_assert_no_error (error);
345 g_assert_cmpstr (text, ==, "a bookmark file");
347 if (g_test_verbose ())
350 if (g_test_verbose ())
351 g_print ("\t=> check bookmark title/description...");
352 g_bookmark_file_set_title (bookmark, TEST_URI_0, "a title");
353 g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description");
354 g_bookmark_file_set_is_private (bookmark, TEST_URI_0, TRUE);
356 g_bookmark_file_set_added (bookmark, TEST_URI_0, now);
357 g_bookmark_file_set_modified (bookmark, TEST_URI_0, now);
358 g_bookmark_file_set_visited (bookmark, TEST_URI_0, now);
359 g_bookmark_file_set_icon (bookmark, TEST_URI_0, "testicon", "image/png");
361 text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error);
362 g_assert_no_error (error);
363 g_assert_cmpstr (text, ==, "a title");
365 text = g_bookmark_file_get_description (bookmark, TEST_URI_0, &error);
366 g_assert_no_error (error);
367 g_assert_cmpstr (text, ==, "a description");
369 g_assert (g_bookmark_file_get_is_private (bookmark, TEST_URI_0, &error));
370 g_assert_no_error (error);
371 stamp = g_bookmark_file_get_added (bookmark, TEST_URI_0, &error);
372 g_assert_no_error (error);
373 g_assert (stamp == now);
374 stamp = g_bookmark_file_get_modified (bookmark, TEST_URI_0, &error);
375 g_assert_no_error (error);
376 g_assert (stamp == now);
377 stamp = g_bookmark_file_get_visited (bookmark, TEST_URI_0, &error);
378 g_assert_no_error (error);
379 g_assert (stamp == now);
380 g_assert (g_bookmark_file_get_icon (bookmark, TEST_URI_0, &icon, &mime, &error));
381 g_assert_no_error (error);
382 g_assert_cmpstr (icon, ==, "testicon");
383 g_assert_cmpstr (mime, ==, "image/png");
386 if (g_test_verbose ())
389 if (g_test_verbose ())
390 g_print ("\t=> check non existing bookmark...");
391 g_bookmark_file_get_description (bookmark, TEST_URI_1, &error);
392 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
393 g_clear_error (&error);
394 g_bookmark_file_get_is_private (bookmark, TEST_URI_1, &error);
395 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
396 g_clear_error (&error);
397 g_bookmark_file_get_added (bookmark, TEST_URI_1, &error);
398 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
399 g_clear_error (&error);
400 g_bookmark_file_get_modified (bookmark, TEST_URI_1, &error);
401 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
402 g_clear_error (&error);
403 g_bookmark_file_get_visited (bookmark, TEST_URI_1, &error);
404 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
405 g_clear_error (&error);
406 if (g_test_verbose ())
409 if (g_test_verbose ())
410 g_print ("\t=> check application...");
411 g_bookmark_file_set_mime_type (bookmark, TEST_URI_0, TEST_MIME);
412 g_assert (!g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL));
413 g_bookmark_file_add_application (bookmark, TEST_URI_0,
416 g_assert (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL));
417 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, TEST_APP_NAME,
422 g_assert_no_error (error);
423 g_assert (count == 1);
424 g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL));
426 g_assert (g_bookmark_file_remove_application (bookmark, TEST_URI_0, TEST_APP_NAME, &error));
427 g_assert_no_error (error);
428 g_bookmark_file_add_application (bookmark, TEST_URI_0, TEST_APP_NAME, TEST_APP_EXEC);
429 apps = g_bookmark_file_get_applications (bookmark, TEST_URI_0, &length, &error);
430 g_assert_no_error (error);
431 g_assert_cmpint (length, ==, 1);
432 g_assert_cmpstr (apps[0], ==, TEST_APP_NAME);
435 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail",
440 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
441 g_clear_error (&error);
443 if (g_test_verbose ())
446 if (g_test_verbose ())
447 g_print ("\t=> check groups...");
448 g_assert (!g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL));
449 g_bookmark_file_add_group (bookmark, TEST_URI_1, "Test");
450 g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL));
451 g_assert (!g_bookmark_file_has_group (bookmark, TEST_URI_1, "Fail", NULL));
452 g_assert (g_bookmark_file_remove_group (bookmark, TEST_URI_1, "Test", &error));
453 g_assert_no_error (error);
454 groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, NULL, &error);
455 g_assert_cmpint (g_strv_length (groups), ==, 0);
457 groups = g_new0 (gchar *, 3);
458 groups[0] = "Group1";
459 groups[1] = "Group2";
461 g_bookmark_file_set_groups (bookmark, TEST_URI_1, (const gchar **)groups, 2);
463 groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, &length, &error);
464 g_assert_cmpint (length, ==, 2);
466 g_assert_no_error (error);
468 if (g_test_verbose ())
471 if (g_test_verbose ())
472 g_print ("\t=> check remove...");
473 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == TRUE);
474 g_assert_no_error (error);
475 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == FALSE);
476 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
477 g_clear_error (&error);
478 if (g_test_verbose ())
485 test_file (gconstpointer d)
487 const gchar *filename = d;
488 GBookmarkFile *bookmark_file;
493 bookmark_file = g_bookmark_file_new ();
494 g_assert (bookmark_file != NULL);
496 success = test_load (bookmark_file, filename);
500 test_query (bookmark_file);
501 test_modify (bookmark_file);
504 data = g_bookmark_file_to_data (bookmark_file, NULL, &error);
505 g_assert_no_error (error);
506 /* FIXME do some checks on data */
510 g_bookmark_file_free (bookmark_file);
512 g_assert (success == (strstr (filename, "fail") == NULL));
516 main (int argc, char *argv[])
523 g_test_init (&argc, &argv, NULL);
531 g_test_add_func ("/bookmarks/load-from-data-dirs", test_load_from_data_dirs);
532 g_test_add_func ("/bookmarks/to-file", test_to_file);
533 g_test_add_func ("/bookmarks/move-item", test_move_item);
534 g_test_add_func ("/bookmarks/misc", test_misc);
537 path = g_test_build_filename (G_TEST_DIST, "bookmarks", NULL);
538 dir = g_dir_open (path, 0, &error);
540 g_assert_no_error (error);
541 while ((name = g_dir_read_name (dir)) != NULL)
543 if (!g_str_has_suffix (name, ".xbel"))
546 path = g_strdup_printf ("/bookmarks/parse/%s", name);
547 g_test_add_data_func_full (path, g_test_build_filename (G_TEST_DIST, "bookmarks", name, NULL),
553 return g_test_run ();