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
);
68 g_assert_cmpstr (in
, ==, out
);
72 g_bookmark_file_free (bookmark
);
78 GBookmarkFile
*bookmark
;
79 const gchar
*filename
;
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
);
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",
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",
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",
110 g_assert_no_error (error
);
112 g_bookmark_file_free (bookmark
);
118 GBookmarkFile
*bookmark
;
119 const gchar
*filename
;
121 GError
*error
= NULL
;
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
);
132 g_assert_no_error (error
);
134 res
= g_bookmark_file_get_icon (bookmark
,
135 "file:///home/zefram/Documents/milan-stuttgart.ps",
140 g_assert_no_error (error
);
142 res
= g_bookmark_file_get_icon (bookmark
,
143 "file:///tmp/schedule.ps",
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",
157 g_assert_no_error (error
);
158 g_assert_cmpstr (s
, ==, "imaginary schedule");
160 s
= g_bookmark_file_get_mime_type (bookmark
,
161 "file:///tmp/schedule0.ps",
163 g_assert_error (error
, G_BOOKMARK_FILE_ERROR
, G_BOOKMARK_FILE_ERROR_INVALID_VALUE
);
165 g_clear_error (&error
);
166 res
= g_bookmark_file_get_is_private (bookmark
,
167 "file:///tmp/schedule0.ps",
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",
175 s
= g_bookmark_file_get_mime_type (bookmark
,
176 "file:///tmp/schedule1.ps",
178 g_assert_no_error (error
);
179 g_assert_cmpstr (s
, ==, "image/png");
182 g_bookmark_file_set_is_private (bookmark
,
183 "file:///tmp/schedule2.ps",
185 res
= g_bookmark_file_get_is_private (bookmark
,
186 "file:///tmp/schedule2.ps",
188 g_assert_no_error (error
);
192 g_bookmark_file_set_added (bookmark
,
193 "file:///tmp/schedule3.ps",
195 t
= g_bookmark_file_get_added (bookmark
,
196 "file:///tmp/schedule3.ps",
198 g_assert_no_error (error
);
201 g_bookmark_file_set_modified (bookmark
,
202 "file:///tmp/schedule4.ps",
204 t
= g_bookmark_file_get_modified (bookmark
,
205 "file:///tmp/schedule4.ps",
207 g_assert_no_error (error
);
210 g_bookmark_file_set_visited (bookmark
,
211 "file:///tmp/schedule5.ps",
213 t
= g_bookmark_file_get_visited (bookmark
,
214 "file:///tmp/schedule5.ps",
216 g_assert_no_error (error
);
219 g_bookmark_file_set_icon (bookmark
,
220 "file:///tmp/schedule6.ps",
221 "application-x-postscript",
223 res
= g_bookmark_file_get_icon (bookmark
,
224 "file:///tmp/schedule6.ps",
228 g_assert_no_error (error
);
230 g_assert_cmpstr (s
, ==, "application-x-postscript");
233 g_bookmark_file_set_icon (bookmark
,
234 "file:///tmp/schedule6.ps",
236 res
= g_bookmark_file_get_icon (bookmark
,
237 "file:///tmp/schedule6.ps",
241 g_assert_no_error (error
);
244 res
= g_bookmark_file_has_application (bookmark
,
245 "file:///tmp/schedule7.ps",
248 g_assert_error (error
, G_BOOKMARK_FILE_ERROR
, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND
);
250 g_clear_error (&error
);
252 g_bookmark_file_add_application (bookmark
,
253 "file:///tmp/schedule7.ps",
255 res
= g_bookmark_file_get_app_info (bookmark
,
256 "file:///tmp/schedule7.ps",
257 g_get_application_name (),
260 g_assert_no_error (error
);
262 cmd
= g_strconcat (g_get_prgname (), " file:///tmp/schedule7.ps", NULL
);
263 g_assert_cmpstr (exec
, ==, cmd
);
266 g_assert_cmpuint (count
, ==, 1);
269 g_bookmark_file_free (bookmark
);
273 test_load (GBookmarkFile
*bookmark
,
274 const gchar
*filename
)
276 GError
*error
= NULL
;
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
);
288 test_query (GBookmarkFile
*bookmark
)
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
]));
305 mime
= g_bookmark_file_get_mime_type (bookmark
, uris
[i
], &error
);
306 g_assert (mime
!= NULL
);
307 g_assert_no_error (error
);
312 g_assert (!g_bookmark_file_has_item (bookmark
, "file:///no/such/uri"));
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
);
322 test_modify (GBookmarkFile
*bookmark
)
328 GError
*error
= NULL
;
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");
345 text
= g_bookmark_file_get_description (bookmark
, NULL
, &error
);
346 g_assert_no_error (error
);
347 g_assert_cmpstr (text
, ==, "a bookmark file");
349 if (g_test_verbose ())
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
);
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");
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");
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");
388 if (g_test_verbose ())
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 ())
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
,
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
,
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
));
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
);
437 g_bookmark_file_get_app_info (bookmark
, TEST_URI_0
, "fail",
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 ())
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);
459 groups
= g_new0 (gchar
*, 3);
460 groups
[0] = "Group1";
461 groups
[1] = "Group2";
463 g_bookmark_file_set_groups (bookmark
, TEST_URI_1
, (const gchar
**)groups
, 2);
465 groups
= g_bookmark_file_get_groups (bookmark
, TEST_URI_1
, &length
, &error
);
466 g_assert_cmpint (length
, ==, 2);
468 g_assert_no_error (error
);
470 if (g_test_verbose ())
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 ())
487 test_file (gconstpointer d
)
489 const gchar
*filename
= d
;
490 GBookmarkFile
*bookmark_file
;
495 bookmark_file
= g_bookmark_file_new ();
496 g_assert (bookmark_file
!= NULL
);
498 success
= test_load (bookmark_file
, filename
);
502 test_query (bookmark_file
);
503 test_modify (bookmark_file
);
506 data
= g_bookmark_file_to_data (bookmark_file
, NULL
, &error
);
507 g_assert_no_error (error
);
508 /* FIXME do some checks on data */
512 g_bookmark_file_free (bookmark_file
);
514 g_assert (success
== (strstr (filename
, "fail") == NULL
));
518 main (int argc
, char *argv
[])
525 g_test_init (&argc
, &argv
, NULL
);
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
);
539 path
= g_test_build_filename (G_TEST_DIST
, "bookmarks", NULL
);
540 dir
= g_dir_open (path
, 0, &error
);
542 g_assert_no_error (error
);
543 while ((name
= g_dir_read_name (dir
)) != NULL
)
545 if (!g_str_has_suffix (name
, ".xbel"))
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
),
555 return g_test_run ();