1 #include <glib.h>
2 #include <glib/gstdio.h>
3
4 #define TEST_URI_0 "file:///abc/defgh/ijklmnopqrstuvwxyz"
5 #define TEST_URI_1 "file:///test/uri/1"
6 #define TEST_URI_2 "file:///test/uri/2"
7
8 #define TEST_MIME "text/plain"
9
10 #define TEST_APP_NAME "bookmarkfile-test"
11 #define TEST_APP_EXEC "bookmarkfile-test %f"
12
13 static void
14 test_load_from_data_dirs (void)
15 {
16 GBookmarkFile *bookmark;
17 gboolean res;
18 gchar *path = NULL;
19 GError *error = NULL;
20
21 bookmark = g_bookmark_file_new ();
22
23 res = g_bookmark_file_load_from_data_dirs (bookmark, "no-such-bookmark-file.xbel", &path, &error);
24
25 g_assert_false (res);
26 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
27 g_assert_null (path);
28 g_error_free (error);
29
30 g_bookmark_file_free (bookmark);
31 }
32
33 static void
34 test_to_file (void)
35 {
36 GBookmarkFile *bookmark;
37 const gchar *filename;
38 gboolean res;
39 GError *error = NULL;
40 char *in, *out;
41 gchar *tmp_filename = NULL;
42 gint fd;
43
44 fd = g_file_open_tmp ("bookmarkfile-test-XXXXXX.xbel", &tmp_filename, NULL);
45 g_assert_cmpint (fd, >, -1);
46 g_close (fd, NULL);
47
48 bookmark = g_bookmark_file_new ();
49
50 g_test_message ("Roundtrip from newly created bookmark file %s", tmp_filename);
51 g_bookmark_file_set_title (bookmark, "file:///tmp/schedule.ps", "schedule.ps");
52 g_bookmark_file_set_mime_type (bookmark, "file:///tmp/schedule.ps", "application/postscript");
53 g_bookmark_file_add_application (bookmark, "file:///tmp/schedule.ps", "ghostscript", "ghostscript %F");
54
55 res = g_bookmark_file_to_file (bookmark, tmp_filename, &error);
56 g_assert_no_error (error);
57 g_assert_true (res);
58
59 res = g_bookmark_file_load_from_file (bookmark, tmp_filename, &error);
60 g_assert_no_error (error);
61 g_assert_true (res);
62
63 out = g_bookmark_file_get_title (bookmark, "file:///tmp/schedule.ps", &error);
64 g_assert_no_error (error);
65 g_assert_cmpstr (out, ==, "schedule.ps");
66 g_free (out);
67
68 out = g_bookmark_file_get_mime_type (bookmark, "file:///tmp/schedule.ps", &error);
69 g_assert_no_error (error);
70 g_assert_cmpstr (out, ==, "application/postscript");
71 g_free (out);
72
73 remove (tmp_filename);
74
75 g_test_message ("Roundtrip from a valid bookmark file");
76 filename = g_test_get_filename (G_TEST_DIST, "bookmarks", "valid-01.xbel", NULL);
77 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
78 g_assert_no_error (error);
79 g_assert_true (res);
80
81 res = g_bookmark_file_to_file (bookmark, tmp_filename, &error);
82 g_assert_no_error (error);
83 g_assert_true (res);
84
85 res = g_file_get_contents (filename, &in, NULL, &error);
86 g_assert_no_error (error);
87 g_assert_true (res);
88
89 res = g_file_get_contents (tmp_filename, &out, NULL, &error);
90 g_assert_no_error (error);
91 g_assert_true (res);
92 remove (tmp_filename);
93
94 g_assert_cmpstr (in, ==, out);
95 g_free (in);
96 g_free (out);
97
98 g_bookmark_file_free (bookmark);
99 g_free (tmp_filename);
100 }
101
102 static void
103 test_move_item (void)
104 {
105 GBookmarkFile *bookmark;
106 const gchar *filename;
107 gboolean res;
108 GError *error = NULL;
109
110 bookmark = g_bookmark_file_new ();
111
112 filename = g_test_get_filename (G_TEST_DIST, "bookmarks", "valid-01.xbel", NULL);
113 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
114 g_assert_true (res);
115 g_assert_no_error (error);
116
117 res = g_bookmark_file_move_item (bookmark,
118 "file:///home/zefram/Documents/milan-stuttgart.ps",
119 "file:///tmp/schedule.ps",
120 &error);
121 g_assert_true (res);
122 g_assert_no_error (error);
123
124 res = g_bookmark_file_move_item (bookmark,
125 "file:///tmp/schedule.ps",
126 "file:///tmp/schedule.ps",
127 &error);
128 g_assert_true (res);
129 g_assert_no_error (error);
130
131 res = g_bookmark_file_move_item (bookmark,
132 "file:///no-such-file.xbel",
133 "file:///tmp/schedule.ps",
134 &error);
135 g_assert_false (res);
136 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
137 g_clear_error (&error);
138
139 res = g_bookmark_file_move_item (bookmark,
140 "file:///tmp/schedule.ps",
141 NULL,
142 &error);
143 g_assert_true (res);
144 g_assert_no_error (error);
145
146 g_bookmark_file_free (bookmark);
147 }
148
149 static void
150 test_corner_cases (void)
151 {
152 gsize size;
153 gchar *message, **messages;
154 GError *error = NULL;
155 GBookmarkFile *bookmark;
156
157 bookmark = g_bookmark_file_new ();
158
159 if (g_test_undefined ())
160 {
161 /* g_bookmark_file_load_from_data() */
162 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
163 "*assertion*!= NULL*");
164 g_assert_false (g_bookmark_file_load_from_data (NULL, NULL, -1, NULL));
165 g_test_assert_expected_messages ();
166
167 /* g_bookmark_file_load_from_file() */
168 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
169 "*assertion*!= NULL*");
170 g_assert_false (g_bookmark_file_load_from_file (NULL, NULL, NULL));
171 g_test_assert_expected_messages ();
172
173 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
174 "*assertion*!= NULL*");
175 g_assert_false (g_bookmark_file_load_from_file (bookmark, NULL, NULL));
176 g_test_assert_expected_messages ();
177
178 /* g_bookmark_file_load_from_data_dirs() */
179 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
180 "*assertion*!= NULL*");
181 g_assert_false (g_bookmark_file_load_from_data_dirs (NULL, NULL, NULL, NULL));
182 g_test_assert_expected_messages ();
183
184 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
185 "*assertion*!= NULL*");
186 g_assert_false (g_bookmark_file_load_from_data_dirs (bookmark, NULL, NULL, NULL));
187 g_test_assert_expected_messages ();
188
189 /* g_bookmark_file_to_data() */
190 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
191 "*assertion*!= NULL*");
192 g_assert_null (g_bookmark_file_to_data (NULL, NULL, NULL));
193 g_test_assert_expected_messages ();
194
195 /* g_bookmark_file_to_file() */
196 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
197 "*assertion*!= NULL*");
198 g_assert_false (g_bookmark_file_to_file (NULL, NULL, NULL));
199 g_test_assert_expected_messages ();
200
201 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
202 "*assertion*!= NULL*");
203 g_assert_false (g_bookmark_file_to_file (bookmark, NULL, NULL));
204 g_test_assert_expected_messages ();
205
206 /* g_bookmark_file_remove_item() */
207 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
208 "*assertion*!= NULL*");
209 g_assert_false (g_bookmark_file_remove_item (NULL, NULL, NULL));
210 g_test_assert_expected_messages ();
211
212 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
213 "*assertion*!= NULL*");
214 g_assert_false (g_bookmark_file_remove_item (bookmark, NULL, NULL));
215 g_test_assert_expected_messages ();
216
217 /* g_bookmark_file_has_item() */
218 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
219 "*assertion*!= NULL*");
220 g_assert_false (g_bookmark_file_has_item (NULL, NULL));
221 g_test_assert_expected_messages ();
222
223 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
224 "*assertion*!= NULL*");
225 g_assert_false (g_bookmark_file_has_item (bookmark, NULL));
226 g_test_assert_expected_messages ();
227
228 /* g_bookmark_file_get_uris() */
229 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
230 "*assertion*!= NULL*");
231 g_assert_null (g_bookmark_file_get_uris (NULL, NULL));
232 g_test_assert_expected_messages ();
233
234 /* g_bookmark_file_set_title() */
235 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
236 "*assertion*!= NULL*");
237 g_bookmark_file_set_title (NULL, NULL, NULL);
238 g_test_assert_expected_messages ();
239
240 /* g_bookmark_file_get_title() */
241 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
242 "*assertion*!= NULL*");
243 g_assert_null (g_bookmark_file_get_title (NULL, NULL, NULL));
244 g_test_assert_expected_messages ();
245
246 /* g_bookmark_file_set_description() */
247 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
248 "*assertion*!= NULL*");
249 g_bookmark_file_set_description (NULL, NULL, NULL);
250 g_test_assert_expected_messages ();
251
252 /* g_bookmark_file_get_description() */
253 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
254 "*assertion*!= NULL*");
255 g_bookmark_file_get_description (NULL, NULL, NULL);
256 g_test_assert_expected_messages ();
257
258 /* g_bookmark_file_set_mime_type() */
259 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
260 "*assertion*!= NULL*");
261 g_bookmark_file_set_mime_type (NULL, NULL, NULL);
262 g_test_assert_expected_messages ();
263
264 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
265 "*assertion*!= NULL*");
266 g_bookmark_file_set_mime_type (bookmark, NULL, NULL);
267 g_test_assert_expected_messages ();
268
269 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
270 "*assertion*!= NULL*");
271 g_bookmark_file_set_mime_type (bookmark, "uri", NULL);
272 g_test_assert_expected_messages ();
273
274 /* g_bookmark_file_get_mime_type() */
275 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
276 "*assertion*!= NULL*");
277 g_assert_null (g_bookmark_file_get_mime_type (NULL, NULL, NULL));
278 g_test_assert_expected_messages ();
279
280 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
281 "*assertion*!= NULL*");
282 g_assert_null (g_bookmark_file_get_mime_type (bookmark, NULL, NULL));
283 g_test_assert_expected_messages ();
284
285 /* g_bookmark_file_set_is_private() */
286 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
287 "*assertion*!= NULL*");
288 g_bookmark_file_set_is_private (NULL, NULL, TRUE);
289 g_test_assert_expected_messages ();
290
291 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
292 "*assertion*!= NULL*");
293 g_bookmark_file_set_is_private (bookmark, NULL, TRUE);
294 g_test_assert_expected_messages ();
295
296 /* g_bookmark_file_get_is_private() */
297 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
298 "*assertion*!= NULL*");
299 g_assert_false (g_bookmark_file_get_is_private (NULL, NULL, NULL));
300 g_test_assert_expected_messages ();
301
302 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
303 "*assertion*!= NULL*");
304 g_assert_false (g_bookmark_file_get_is_private (bookmark, NULL, NULL));
305 g_test_assert_expected_messages ();
306
307 /* g_bookmark_file_set_added_date_time() */
308 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
309 "*assertion*!= NULL*");
310 g_bookmark_file_set_added_date_time (NULL, NULL, NULL);
311 g_test_assert_expected_messages ();
312
313 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
314 "*assertion*!= NULL*");
315 g_bookmark_file_set_added_date_time (bookmark, NULL, NULL);
316 g_test_assert_expected_messages ();
317
318 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
319 "*assertion*!= NULL*");
320 g_bookmark_file_set_added_date_time (bookmark, "a", NULL);
321 g_test_assert_expected_messages ();
322
323 /* g_bookmark_file_get_added_date_time() */
324 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
325 "*assertion*!= NULL*");
326 g_assert_null (g_bookmark_file_get_added_date_time (NULL, NULL, NULL));
327 g_test_assert_expected_messages ();
328
329 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
330 "*assertion*!= NULL*");
331 g_assert_null (g_bookmark_file_get_added_date_time (bookmark, NULL, NULL));
332 g_test_assert_expected_messages ();
333
334 /* g_bookmark_file_set_modified_date_time() */
335 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
336 "*assertion*!= NULL*");
337 g_bookmark_file_set_modified_date_time (NULL, NULL, NULL);
338 g_test_assert_expected_messages ();
339
340 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
341 "*assertion*!= NULL*");
342 g_bookmark_file_set_modified_date_time (bookmark, NULL, NULL);
343 g_test_assert_expected_messages ();
344
345 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
346 "*assertion*!= NULL*");
347 g_bookmark_file_set_modified_date_time (bookmark, "a", NULL);
348 g_test_assert_expected_messages ();
349
350 /* g_bookmark_file_get_modified_date_time() */
351 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
352 "*assertion*!= NULL*");
353 g_assert_null (g_bookmark_file_get_modified_date_time (NULL, NULL, NULL));
354 g_test_assert_expected_messages ();
355
356 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
357 "*assertion*!= NULL*");
358 g_assert_null (g_bookmark_file_get_modified_date_time (bookmark, NULL, NULL));
359 g_test_assert_expected_messages ();
360
361 /* g_bookmark_file_set_visited_date_time() */
362 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
363 "*assertion*!= NULL*");
364 g_bookmark_file_set_visited_date_time (NULL, NULL, NULL);
365 g_test_assert_expected_messages ();
366
367 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
368 "*assertion*!= NULL*");
369 g_bookmark_file_set_visited_date_time (bookmark, NULL, NULL);
370 g_test_assert_expected_messages ();
371
372 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
373 "*assertion*!= NULL*");
374 g_bookmark_file_set_visited_date_time (bookmark, "a", NULL);
375 g_test_assert_expected_messages ();
376
377 /* g_bookmark_file_get_visited_date_time() */
378 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
379 "*assertion*!= NULL*");
380 g_assert_null (g_bookmark_file_get_visited_date_time (NULL, NULL, NULL));
381 g_test_assert_expected_messages ();
382
383 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
384 "*assertion*!= NULL*");
385 g_assert_null (g_bookmark_file_get_visited_date_time (bookmark, NULL, NULL));
386 g_test_assert_expected_messages ();
387
388 /* g_bookmark_file_load_from_data_dirs() */
389 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
390 "*assertion*!= NULL*");
391 g_assert_false (g_bookmark_file_load_from_data_dirs (NULL, NULL, NULL, NULL));
392 g_test_assert_expected_messages ();
393
394 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
395 "*assertion*!= NULL*");
396 g_assert_false (g_bookmark_file_load_from_data_dirs (bookmark, NULL, NULL, NULL));
397 g_test_assert_expected_messages ();
398
399 /* g_bookmark_file_has_group() */
400 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
401 "*assertion*!= NULL*");
402 g_assert_false (g_bookmark_file_has_group (NULL, NULL, NULL, NULL));
403 g_test_assert_expected_messages ();
404
405 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
406 "*assertion*!= NULL*");
407 g_assert_false (g_bookmark_file_has_group (bookmark, NULL, NULL, NULL));
408 g_test_assert_expected_messages ();
409
410 /* g_bookmark_file_add_group() */
411 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
412 "*assertion*!= NULL*");
413 g_bookmark_file_add_group (NULL, NULL, NULL);
414 g_test_assert_expected_messages ();
415
416 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
417 "*assertion*!= NULL*");
418 g_bookmark_file_add_group (bookmark, NULL, NULL);
419 g_test_assert_expected_messages ();
420
421 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
422 "*assertion*!= NULL*");
423 g_bookmark_file_add_group (bookmark, "a", NULL);
424 g_test_assert_expected_messages ();
425
426 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
427 "*assertion*!= NULL*");
428 g_bookmark_file_add_group (bookmark, "a", "");
429 g_test_assert_expected_messages ();
430
431 /* g_bookmark_file_remove_group() */
432 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
433 "*assertion*!= NULL*");
434 g_assert_false (g_bookmark_file_remove_group (NULL, NULL, NULL, NULL));
435 g_test_assert_expected_messages ();
436
437 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
438 "*assertion*!= NULL*");
439 g_assert_false (g_bookmark_file_remove_group (bookmark, NULL, NULL, NULL));
440 g_test_assert_expected_messages ();
441
442 /* g_bookmark_file_set_group() */
443 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
444 "*assertion*!= NULL*");
445 g_bookmark_file_set_groups (NULL, NULL, NULL, 0);
446 g_test_assert_expected_messages ();
447
448 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
449 "*assertion*!= NULL*");
450 g_bookmark_file_set_groups (bookmark, NULL, NULL, 0);
451 g_test_assert_expected_messages ();
452
453 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
454 "*assertion*!= NULL*");
455 g_bookmark_file_set_groups (bookmark, "a", NULL, 0);
456 g_test_assert_expected_messages ();
457
458 /* g_bookmark_file_get_group() */
459 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
460 "*assertion*!= NULL*");
461 g_assert_null (g_bookmark_file_get_groups (NULL, NULL, NULL, NULL));
462 g_test_assert_expected_messages ();
463
464 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
465 "*assertion*!= NULL*");
466 g_assert_null (g_bookmark_file_get_groups (bookmark, NULL, NULL, NULL));
467 g_test_assert_expected_messages ();
468
469 /* g_bookmark_file_to_file() */
470 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
471 "*assertion*!= NULL*");
472 g_assert_false (g_bookmark_file_to_file (NULL, NULL, NULL));
473 g_test_assert_expected_messages ();
474
475 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
476 "*assertion*!= NULL*");
477 g_assert_false (g_bookmark_file_to_file (bookmark, NULL, NULL));
478 g_test_assert_expected_messages ();
479
480 /* g_bookmark_file_add_application() */
481 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
482 "*assertion*!= NULL*");
483 g_bookmark_file_add_application (NULL, NULL, NULL, NULL);
484 g_test_assert_expected_messages ();
485
486 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
487 "*assertion*!= NULL*");
488 g_bookmark_file_add_application (bookmark, NULL, NULL, NULL);
489 g_test_assert_expected_messages ();
490
491 /* g_bookmark_file_remove_application() */
492 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
493 "*assertion*!= NULL*");
494 g_assert_false (g_bookmark_file_remove_application (NULL, NULL, NULL, NULL));
495 g_test_assert_expected_messages ();
496
497 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
498 "*assertion*!= NULL*");
499 g_assert_false (g_bookmark_file_remove_application (bookmark, NULL, NULL, NULL));
500 g_test_assert_expected_messages ();
501
502 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
503 "*assertion*!= NULL*");
504 g_assert_false (g_bookmark_file_remove_application (bookmark, "a", NULL, NULL));
505 g_test_assert_expected_messages ();
506
507 /* g_bookmark_file_has_application() */
508 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
509 "*assertion*!= NULL*");
510 g_assert_false (g_bookmark_file_has_application (NULL, NULL, NULL, NULL));
511 g_test_assert_expected_messages ();
512
513 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
514 "*assertion*!= NULL*");
515 g_assert_false (g_bookmark_file_has_application (bookmark, NULL, NULL, NULL));
516 g_test_assert_expected_messages ();
517
518 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
519 "*assertion*!= NULL*");
520 g_assert_false (g_bookmark_file_has_application (bookmark, "a", NULL, NULL));
521 g_test_assert_expected_messages ();
522
523 /* g_bookmark_file_set_application_info() */
524 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
525 "*assertion*!= NULL*");
526 g_assert_false (g_bookmark_file_set_application_info (NULL, NULL, NULL, NULL, 0, NULL, NULL));
527 g_test_assert_expected_messages ();
528
529 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
530 "*assertion*!= NULL*");
531 g_assert_false (g_bookmark_file_set_application_info (bookmark, NULL, NULL, NULL, 0, NULL, NULL));
532 g_test_assert_expected_messages ();
533
534 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
535 "*assertion*!= NULL*");
536 g_assert_false (g_bookmark_file_set_application_info (bookmark, "a", NULL, NULL, 0, NULL, NULL));
537 g_test_assert_expected_messages ();
538
539 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
540 "*assertion*!= NULL*");
541 g_assert_false (g_bookmark_file_set_application_info (bookmark, "a", "b", NULL, 0, NULL, NULL));
542 g_test_assert_expected_messages ();
543
544 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
545 "*assertion*!= NULL*");
546 g_assert_false (g_bookmark_file_set_application_info (bookmark, "a", "b", "c", 5, NULL, NULL));
547 g_test_assert_expected_messages ();
548
549 /* g_bookmark_file_get_application_info() */
550 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
551 "*assertion*!= NULL*");
552 g_assert_false (g_bookmark_file_get_application_info (NULL, NULL, NULL, NULL, NULL, NULL, NULL));
553 g_test_assert_expected_messages ();
554
555 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
556 "*assertion*!= NULL*");
557 g_assert_false (g_bookmark_file_get_application_info (bookmark, NULL, NULL, NULL, NULL, NULL, NULL));
558 g_test_assert_expected_messages ();
559
560 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
561 "*assertion*!= NULL*");
562 g_assert_false (g_bookmark_file_get_application_info (bookmark, "a", NULL, NULL, NULL, NULL, NULL));
563 g_test_assert_expected_messages ();
564
565 /* g_bookmark_file_get_applications() */
566 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
567 "*assertion*!= NULL*");
568 g_assert_null (g_bookmark_file_get_applications (NULL, NULL, NULL, NULL));
569 g_test_assert_expected_messages ();
570
571 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
572 "*assertion*!= NULL*");
573 g_assert_null (g_bookmark_file_get_applications (bookmark, NULL, NULL, NULL));
574 g_test_assert_expected_messages ();
575
576 /* g_bookmark_file_get_size() */
577 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
578 "*assertion*!= NULL*");
579 g_assert_cmpint (g_bookmark_file_get_size (NULL), ==, 0);
580 g_test_assert_expected_messages ();
581
582 /* g_bookmark_file_move_item() */
583 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
584 "*assertion*!= NULL*");
585 g_assert_false (g_bookmark_file_move_item (NULL, NULL, NULL, NULL));
586 g_test_assert_expected_messages ();
587
588 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
589 "*assertion*!= NULL*");
590 g_assert_false (g_bookmark_file_move_item (bookmark, NULL, NULL, NULL));
591 g_test_assert_expected_messages ();
592
593 /* g_bookmark_file_set_icon() */
594 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
595 "*assertion*!= NULL*");
596 g_bookmark_file_set_icon (NULL, NULL, NULL, NULL);
597 g_test_assert_expected_messages ();
598
599 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
600 "*assertion*!= NULL*");
601 g_bookmark_file_set_icon (bookmark, NULL, NULL, NULL);
602 g_test_assert_expected_messages ();
603
604 /* g_bookmark_file_get_icon() */
605 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
606 "*assertion*!= NULL*");
607 g_assert_false (g_bookmark_file_get_icon (NULL, NULL, NULL, NULL, NULL));
608 g_test_assert_expected_messages ();
609
610 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
611 "*assertion*!= NULL*");
612 g_assert_false (g_bookmark_file_get_icon (bookmark, NULL, NULL, NULL, NULL));
613 g_test_assert_expected_messages ();
614 }
615
616 /* g_file_bookmark_free() */
617 g_bookmark_file_free (NULL);
618
619 /* g_bookmark_file_load_from_data() */
620 g_assert_false (g_bookmark_file_load_from_data (bookmark, "data", -1, &error));
621 g_assert_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE);
622 g_clear_error (&error);
623
624 /* g_bookmark_file_load_from_data_dirs() */
625 g_assert_false (g_bookmark_file_load_from_data_dirs (bookmark, "a", NULL, NULL));
626 g_assert_false (g_bookmark_file_load_from_data_dirs (bookmark, "a", NULL, &error));
627 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
628 g_clear_error (&error);
629
630 /* g_bookmark_file_to_data() */
631 message = g_bookmark_file_to_data (bookmark, &size, &error);
632 g_assert_nonnull (message);
633 g_assert_cmpstr (message, ==,
634 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
635 "<xbel version=\"1.0\"\n"
636 " xmlns:bookmark=\"http://www.freedesktop.org/standards/desktop-bookmarks\"\n"
637 " xmlns:mime=\"http://www.freedesktop.org/standards/shared-mime-info\"\n"
638 "></xbel>");
639 g_free (message);
640
641 /* g_bookmark_file_get_uris() */
642 size = 10;
643 messages = g_bookmark_file_get_uris (bookmark, &size);
644 g_assert_nonnull (messages);
645 g_assert_null (messages[0]);
646 g_free (messages);
647
648 /* g_bookmark_file_get_added_date_time() */
649 g_assert_null (g_bookmark_file_get_added_date_time (bookmark, "a", NULL));
650 g_assert_null (g_bookmark_file_get_added_date_time (bookmark, "a", &error));
651 g_clear_error (&error);
652
653 /* g_bookmark_file_get_modified_date_time() */
654 g_assert_null (g_bookmark_file_get_modified_date_time (bookmark, "a", NULL));
655 g_assert_null (g_bookmark_file_get_modified_date_time (bookmark, "a", &error));
656 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
657 g_clear_error (&error);
658
659 /* g_bookmark_file_get_visited_date_time() */
660 g_assert_null (g_bookmark_file_get_visited_date_time (bookmark, "a", NULL));
661 g_assert_null (g_bookmark_file_get_visited_date_time (bookmark, "a", &error));
662 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
663 g_clear_error (&error);
664
665 /* g_bookmark_file_get_groups() */
666 g_assert_null (g_bookmark_file_get_groups (bookmark, "a", &size, NULL));
667 g_assert_null (g_bookmark_file_get_groups (bookmark, "a", &size, &error));
668 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
669 g_clear_error (&error);
670
671 /* g_bookmark_file_to_file() */
672 g_assert_true (g_bookmark_file_to_file (bookmark, "a", &error));
673 g_assert_no_error (error);
674
675 /* g_bookmark_file_remove_group() */
676 g_assert_false (g_bookmark_file_remove_group (bookmark, "a", NULL, NULL));
677 g_assert_false (g_bookmark_file_remove_group (bookmark, "a", NULL, &error));
678 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
679 g_clear_error (&error);
680
681 /* g_bookmark_file_get_title() */
682 g_assert_null (g_bookmark_file_get_title (bookmark, "a", &error));
683 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
684 g_clear_error (&error);
685
686 /* g_bookmark_file_add_application() */
687 g_bookmark_file_add_application (bookmark, "a", NULL, NULL);
688 g_bookmark_file_add_application (bookmark, "a", "b", NULL);
689 g_bookmark_file_add_application (bookmark, "a", "b", "c");
690
691 /* g_bookmark_file_remove_application() */
692 g_assert_true (g_bookmark_file_remove_application (bookmark, "a", "b", NULL));
693 g_assert_false (g_bookmark_file_remove_application (bookmark, "a", "b", &error));
694 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
695 g_clear_error (&error);
696
697 /* g_bookmark_file_get_application_info() */
698 g_assert_false (g_bookmark_file_get_application_info (bookmark, "a", "b", NULL, NULL, NULL, NULL));
699 g_assert_false (g_bookmark_file_get_application_info (bookmark, "a", "b", NULL, NULL, NULL, &error));
700 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
701 g_clear_error (&error);
702
703 /* g_bookmark_file_move_item() */
704 g_assert_true (g_bookmark_file_move_item (bookmark, "a", NULL, NULL));
705 g_assert_false (g_bookmark_file_move_item (bookmark, "a", NULL, &error));
706 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
707 g_clear_error (&error);
708 g_assert_false (g_bookmark_file_move_item (bookmark, "a", "b", &error));
709 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
710 g_clear_error (&error);
711
712 g_bookmark_file_free (bookmark);
713 g_unlink ("a");
714 }
715
716 static void
717 test_misc (void)
718 {
719 GBookmarkFile *bookmark;
720 const gchar *filename;
721 gboolean res;
722 GError *error = NULL;
723 gchar *s;
724 GDateTime *before, *after, *t;
725 gchar *cmd, *exec;
726 guint count;
727
728 bookmark = g_bookmark_file_new ();
729
730 filename = g_test_get_filename (G_TEST_DIST, "bookmarks", "valid-01.xbel", NULL);
731 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
732 g_assert_true (res);
733 g_assert_no_error (error);
734
735 res = g_bookmark_file_get_icon (bookmark,
736 "file:///home/zefram/Documents/milan-stuttgart.ps",
737 NULL,
738 NULL,
739 &error);
740 g_assert_false (res);
741 g_assert_no_error (error);
742
743 res = g_bookmark_file_get_icon (bookmark,
744 "file:///tmp/schedule.ps",
745 NULL,
746 NULL,
747 &error);
748 g_assert_false (res);
749 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
750 g_clear_error (&error);
751
752 g_bookmark_file_set_description (bookmark,
753 "file:///tmp/schedule0.ps",
754 "imaginary schedule");
755 s = g_bookmark_file_get_description (bookmark,
756 "file:///tmp/schedule0.ps",
757 &error);
758 g_assert_no_error (error);
759 g_assert_cmpstr (s, ==, "imaginary schedule");
760 g_free (s);
761 s = g_bookmark_file_get_mime_type (bookmark,
762 "file:///tmp/schedule0.ps",
763 &error);
764 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_INVALID_VALUE);
765 g_assert_null (s);
766 g_clear_error (&error);
767 res = g_bookmark_file_get_is_private (bookmark,
768 "file:///tmp/schedule0.ps",
769 &error);
770 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_INVALID_VALUE);
771 g_clear_error (&error);
772 g_assert_false (res);
773
774 g_bookmark_file_set_mime_type (bookmark,
775 "file:///tmp/schedule1.ps",
776 "image/png");
777 s = g_bookmark_file_get_mime_type (bookmark,
778 "file:///tmp/schedule1.ps",
779 &error);
780 g_assert_no_error (error);
781 g_assert_cmpstr (s, ==, "image/png");
782 g_free (s);
783
784 g_bookmark_file_set_is_private (bookmark,
785 "file:///tmp/schedule2.ps",
786 TRUE);
787 res = g_bookmark_file_get_is_private (bookmark,
788 "file:///tmp/schedule2.ps",
789 &error);
790 g_assert_no_error (error);
791 g_assert_true (res);
792
793 before = g_date_time_new_now_utc ();
794
795 g_bookmark_file_set_added_date_time (bookmark,
796 "file:///tmp/schedule3.ps",
797 before);
798 t = g_bookmark_file_get_added_date_time (bookmark,
799 "file:///tmp/schedule3.ps",
800 &error);
801 g_assert_no_error (error);
802
803 after = g_date_time_new_now_utc ();
804 g_assert_cmpint (g_date_time_compare (before, t), <=, 0);
805 g_assert_cmpint (g_date_time_compare (t, after), <=, 0);
806
807 g_date_time_unref (after);
808 g_date_time_unref (before);
809
810 before = g_date_time_new_now_utc ();
811
812 g_bookmark_file_set_modified_date_time (bookmark,
813 "file:///tmp/schedule4.ps",
814 before);
815 t = g_bookmark_file_get_modified_date_time (bookmark,
816 "file:///tmp/schedule4.ps",
817 &error);
818 g_assert_no_error (error);
819
820 after = g_date_time_new_now_utc ();
821 g_assert_cmpint (g_date_time_compare (before, t), <=, 0);
822 g_assert_cmpint (g_date_time_compare (t, after), <=, 0);
823
824 g_date_time_unref (after);
825 g_date_time_unref (before);
826
827 before = g_date_time_new_now_utc ();
828
829 g_bookmark_file_set_visited_date_time (bookmark,
830 "file:///tmp/schedule5.ps",
831 before);
832 t = g_bookmark_file_get_visited_date_time (bookmark,
833 "file:///tmp/schedule5.ps",
834 &error);
835 g_assert_no_error (error);
836
837 after = g_date_time_new_now_utc ();
838 g_assert_cmpint (g_date_time_compare (before, t), <=, 0);
839 g_assert_cmpint (g_date_time_compare (t, after), <=, 0);
840 g_date_time_unref (after);
841 g_date_time_unref (before);
842
843 g_bookmark_file_set_icon (bookmark,
844 "file:///tmp/schedule6.ps",
845 "application-x-postscript",
846 "image/png");
847 res = g_bookmark_file_get_icon (bookmark,
848 "file:///tmp/schedule6.ps",
849 &s,
850 NULL,
851 &error);
852 g_assert_no_error (error);
853 g_assert_true (res);
854 g_assert_cmpstr (s, ==, "application-x-postscript");
855 g_free (s);
856
857 g_bookmark_file_set_icon (bookmark,
858 "file:///tmp/schedule6.ps",
859 NULL, NULL);
860 res = g_bookmark_file_get_icon (bookmark,
861 "file:///tmp/schedule6.ps",
862 &s,
863 NULL,
864 &error);
865 g_assert_no_error (error);
866 g_assert_false (res);
867
868 res = g_bookmark_file_has_application (bookmark,
869 "file:///tmp/schedule7.ps",
870 "foo",
871 &error);
872 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
873 g_assert_false (res);
874 g_clear_error (&error);
875
876 before = g_date_time_new_now_utc ();
877
878 g_bookmark_file_add_application (bookmark,
879 "file:///tmp/schedule7.ps",
880 NULL, NULL);
881 res = g_bookmark_file_get_application_info (bookmark,
882 "file:///tmp/schedule7.ps",
883 g_get_application_name (),
884 &exec, &count, &t,
885 &error);
886 g_assert_no_error (error);
887 g_assert_true (res);
888 cmd = g_strconcat (g_get_prgname (), " file:///tmp/schedule7.ps", NULL);
889 g_assert_cmpstr (exec, ==, cmd);
890 g_free (cmd);
891 g_free (exec);
892 g_assert_cmpuint (count, ==, 1);
893
894 after = g_date_time_new_now_utc ();
895 g_assert_cmpint (g_date_time_compare (before, t), <=, 0);
896 g_assert_cmpint (g_date_time_compare (t, after), <=, 0);
897
898 g_date_time_unref (after);
899 g_date_time_unref (before);
900
901 g_bookmark_file_free (bookmark);
902 }
903
904 static void
905 test_deprecated (void)
906 {
907 GBookmarkFile *file = NULL;
908 GError *local_error = NULL;
909 time_t t, now;
910 gboolean retval;
911
912 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
913
914 now = g_get_real_time () / G_USEC_PER_SEC;
915 file = g_bookmark_file_new ();
916
917 /* added */
918 g_bookmark_file_set_added (file, "file://test", -1);
919 t = g_bookmark_file_get_added (file, "file://test", &local_error);
920 g_assert_no_error (local_error);
921 g_assert_cmpint (t, >=, now);
922
923 g_bookmark_file_set_added (file, "file://test", 1234);
924 t = g_bookmark_file_get_added (file, "file://test", &local_error);
925 g_assert_no_error (local_error);
926 g_assert_cmpint (t, ==, 1234);
927
928 t = g_bookmark_file_get_added (file, "file://not-exist", &local_error);
929 g_assert_error (local_error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
930 g_assert_cmpint (t, ==, (time_t) -1);
931 g_clear_error (&local_error);
932
933 /* modified */
934 g_bookmark_file_set_modified (file, "file://test", -1);
935 t = g_bookmark_file_get_modified (file, "file://test", &local_error);
936 g_assert_no_error (local_error);
937 g_assert_cmpint (t, >=, now);
938
939 g_bookmark_file_set_modified (file, "file://test", 1234);
940 t = g_bookmark_file_get_modified (file, "file://test", &local_error);
941 g_assert_no_error (local_error);
942 g_assert_cmpint (t, ==, 1234);
943
944 t = g_bookmark_file_get_modified (file, "file://not-exist", &local_error);
945 g_assert_error (local_error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
946 g_assert_cmpint (t, ==, (time_t) -1);
947 g_clear_error (&local_error);
948
949 /* visited */
950 g_bookmark_file_set_visited (file, "file://test", -1);
951 t = g_bookmark_file_get_visited (file, "file://test", &local_error);
952 g_assert_no_error (local_error);
953 g_assert_cmpint (t, >=, now);
954
955 g_bookmark_file_set_visited (file, "file://test", 1234);
956 t = g_bookmark_file_get_visited (file, "file://test", &local_error);
957 g_assert_no_error (local_error);
958 g_assert_cmpint (t, ==, 1234);
959
960 t = g_bookmark_file_get_visited (file, "file://not-exist", &local_error);
961 g_assert_error (local_error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
962 g_assert_cmpint (t, ==, (time_t) -1);
963 g_clear_error (&local_error);
964
965 /* set app info */
966 retval = g_bookmark_file_set_app_info (file, "file://test", "app", "/path/to/app", 1, -1, &local_error);
967 g_assert_no_error (local_error);
968 g_assert_true (retval);
969
970 retval = g_bookmark_file_get_app_info (file, "file://test", "app", NULL, NULL, &t, &local_error);
971 g_assert_no_error (local_error);
972 g_assert_true (retval);
973 g_assert_cmpint (t, >=, now);
974
975 retval = g_bookmark_file_set_app_info (file, "file://test", "app", "/path/to/app", 1, 1234, &local_error);
976 g_assert_no_error (local_error);
977 g_assert_true (retval);
978
979 retval = g_bookmark_file_get_app_info (file, "file://test", "app", NULL, NULL, &t, &local_error);
980 g_assert_no_error (local_error);
981 g_assert_true (retval);
982 g_assert_cmpint (t, ==, 1234);
983
984 retval = g_bookmark_file_get_app_info (file, "file://test", "app", NULL, NULL, NULL, &local_error);
985 g_assert_no_error (local_error);
986 g_assert_true (retval);
987
988 retval = g_bookmark_file_get_app_info (file, "file://not-exist", "app", NULL, NULL, &t, &local_error);
989 g_assert_error (local_error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
990 g_assert_false (retval);
991 g_clear_error (&local_error);
992
993 g_bookmark_file_free (file);
994
995 G_GNUC_END_IGNORE_DEPRECATIONS
996 }
997
998 static gboolean
999 test_load (GBookmarkFile *bookmark,
1000 const gchar *filename)
1001 {
1002 GError *error = NULL;
1003 gboolean res;
1004
1005 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
1006 if (error && g_test_verbose ())
1007 g_printerr ("Load error: %s\n", error->message);
1008
1009 g_clear_error (&error);
1010 return res;
1011 }
1012
1013 static void
1014 test_query (GBookmarkFile *bookmark)
1015 {
1016 gint size;
1017 gchar **uris;
1018 gsize uris_len, i;
1019 gchar *mime;
1020 GError *error;
1021
1022 size = g_bookmark_file_get_size (bookmark);
1023 uris = g_bookmark_file_get_uris (bookmark, &uris_len);
1024
1025 g_assert_cmpint (uris_len, ==, size);
1026
1027 for (i = 0; i < uris_len; i++)
1028 {
1029 g_assert_true (g_bookmark_file_has_item (bookmark, uris[i]));
1030 error = NULL;
1031 mime = g_bookmark_file_get_mime_type (bookmark, uris[i], &error);
1032 g_assert_nonnull (mime);
1033 g_assert_no_error (error);
1034 g_free (mime);
1035 }
1036 g_strfreev (uris);
1037
1038 g_assert_false (g_bookmark_file_has_item (bookmark, "file:///no/such/uri"));
1039 error = NULL;
1040 mime = g_bookmark_file_get_mime_type (bookmark, "file:///no/such/uri", &error);
1041 g_assert_null (mime);
1042 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
1043 g_error_free (error);
1044 g_free (mime);
1045 }
1046
1047 static gboolean
1048 test_modify (GBookmarkFile *bookmark)
1049 {
1050 gchar *text;
1051 guint count;
1052 GDateTime *stamp;
1053 GDateTime *now = NULL;
1054 GError *error = NULL;
1055 gchar **groups;
1056 gsize length;
1057 gchar **apps;
1058 gchar *icon;
1059 gchar *mime;
1060
1061 if (g_test_verbose ())
1062 g_printerr ("\t=> check global title/description...");
1063 g_bookmark_file_set_title (bookmark, NULL, "a file");
1064 g_bookmark_file_set_description (bookmark, NULL, "a bookmark file");
1065
1066 text = g_bookmark_file_get_title (bookmark, NULL, &error);
1067 g_assert_no_error (error);
1068 g_assert_cmpstr (text, ==, "a file");
1069 g_free (text);
1070
1071 text = g_bookmark_file_get_description (bookmark, NULL, &error);
1072 g_assert_no_error (error);
1073 g_assert_cmpstr (text, ==, "a bookmark file");
1074 g_free (text);
1075 if (g_test_verbose ())
1076 g_printerr ("ok\n");
1077
1078 if (g_test_verbose ())
1079 g_printerr ("\t=> check bookmark title/description...");
1080 g_bookmark_file_set_title (bookmark, TEST_URI_0, "a title");
1081 g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description");
1082 g_bookmark_file_set_is_private (bookmark, TEST_URI_0, TRUE);
1083 now = g_date_time_new_now_utc ();
1084 g_bookmark_file_set_added_date_time (bookmark, TEST_URI_0, now);
1085 g_bookmark_file_set_visited_date_time (bookmark, TEST_URI_0, now);
1086 g_bookmark_file_set_icon (bookmark, TEST_URI_0, "testicon", "image/png");
1087
1088 /* Check the modification date by itself, as it’s updated whenever we modify
1089 * other properties. */
1090 g_bookmark_file_set_modified_date_time (bookmark, TEST_URI_0, now);
1091 stamp = g_bookmark_file_get_modified_date_time (bookmark, TEST_URI_0, &error);
1092 g_assert_no_error (error);
1093 g_assert_cmpint (g_date_time_compare (stamp, now), ==, 0);
1094
1095 text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error);
1096 g_assert_no_error (error);
1097 g_assert_cmpstr (text, ==, "a title");
1098 g_free (text);
1099 text = g_bookmark_file_get_description (bookmark, TEST_URI_0, &error);
1100 g_assert_no_error (error);
1101 g_assert_cmpstr (text, ==, "a description");
1102 g_free (text);
1103 g_assert_true (g_bookmark_file_get_is_private (bookmark, TEST_URI_0, &error));
1104 g_assert_no_error (error);
1105 stamp = g_bookmark_file_get_added_date_time (bookmark, TEST_URI_0, &error);
1106 g_assert_no_error (error);
1107 g_assert_cmpint (g_date_time_compare (stamp, now), ==, 0);
1108 stamp = g_bookmark_file_get_visited_date_time (bookmark, TEST_URI_0, &error);
1109 g_assert_no_error (error);
1110 g_assert_cmpint (g_date_time_compare (stamp, now), ==, 0);
1111 g_assert_true (g_bookmark_file_get_icon (bookmark, TEST_URI_0, &icon, &mime, &error));
1112 g_assert_no_error (error);
1113 g_assert_cmpstr (icon, ==, "testicon");
1114 g_assert_cmpstr (mime, ==, "image/png");
1115 g_free (icon);
1116 g_free (mime);
1117 if (g_test_verbose ())
1118 g_printerr ("ok\n");
1119
1120 if (g_test_verbose ())
1121 g_printerr ("\t=> check non existing bookmark...");
1122 g_bookmark_file_get_description (bookmark, TEST_URI_1, &error);
1123 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
1124 g_clear_error (&error);
1125 g_bookmark_file_get_is_private (bookmark, TEST_URI_1, &error);
1126 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
1127 g_clear_error (&error);
1128 g_bookmark_file_get_added_date_time (bookmark, TEST_URI_1, &error);
1129 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
1130 g_clear_error (&error);
1131 g_bookmark_file_get_modified_date_time (bookmark, TEST_URI_1, &error);
1132 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
1133 g_clear_error (&error);
1134 g_bookmark_file_get_visited_date_time (bookmark, TEST_URI_1, &error);
1135 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
1136 g_clear_error (&error);
1137 if (g_test_verbose ())
1138 g_printerr ("ok\n");
1139
1140 if (g_test_verbose ())
1141 g_printerr ("\t=> check application...");
1142 g_bookmark_file_set_mime_type (bookmark, TEST_URI_0, TEST_MIME);
1143 g_assert_false (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL));
1144 g_bookmark_file_add_application (bookmark, TEST_URI_0,
1145 TEST_APP_NAME,
1146 TEST_APP_EXEC);
1147 g_assert_true (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL));
1148 g_bookmark_file_get_application_info (bookmark, TEST_URI_0, TEST_APP_NAME,
1149 &text,
1150 &count,
1151 &stamp,
1152 &error);
1153 g_assert_no_error (error);
1154 g_assert_cmpuint (count, ==, 1);
1155 g_assert_cmpint (g_date_time_compare (stamp, g_bookmark_file_get_modified_date_time (bookmark, TEST_URI_0, NULL)), <=, 0);
1156 g_free (text);
1157 g_assert_true (g_bookmark_file_remove_application (bookmark, TEST_URI_0, TEST_APP_NAME, &error));
1158 g_assert_no_error (error);
1159 g_bookmark_file_add_application (bookmark, TEST_URI_0, TEST_APP_NAME, TEST_APP_EXEC);
1160 apps = g_bookmark_file_get_applications (bookmark, TEST_URI_0, &length, &error);
1161 g_assert_no_error (error);
1162 g_assert_cmpint (length, ==, 1);
1163 g_assert_cmpstr (apps[0], ==, TEST_APP_NAME);
1164 g_strfreev (apps);
1165
1166 g_bookmark_file_get_application_info (bookmark, TEST_URI_0, "fail",
1167 &text,
1168 &count,
1169 &stamp,
1170 &error);
1171 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
1172 g_clear_error (&error);
1173
1174 if (g_test_verbose ())
1175 g_printerr ("ok\n");
1176
1177 if (g_test_verbose ())
1178 g_printerr ("\t=> check groups...");
1179 g_assert_false (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL));
1180 g_bookmark_file_add_group (bookmark, TEST_URI_1, "Test");
1181 g_assert_true (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL));
1182 g_assert_false (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Fail", NULL));
1183 g_assert_true (g_bookmark_file_remove_group (bookmark, TEST_URI_1, "Test", &error));
1184 g_assert_no_error (error);
1185 groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, NULL, &error);
1186 g_assert_cmpint (g_strv_length (groups), ==, 0);
1187 g_strfreev (groups);
1188 groups = g_new0 (gchar *, 3);
1189 groups[0] = "Group1";
1190 groups[1] = "Group2";
1191 groups[2] = NULL;
1192 g_bookmark_file_set_groups (bookmark, TEST_URI_1, (const gchar **)groups, 2);
1193 g_free (groups);
1194 groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, &length, &error);
1195 g_assert_cmpint (length, ==, 2);
1196 g_strfreev (groups);
1197 g_assert_no_error (error);
1198
1199 if (g_test_verbose ())
1200 g_printerr ("ok\n");
1201
1202 if (g_test_verbose ())
1203 g_printerr ("\t=> check remove...");
1204 g_assert_true (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error));
1205 g_assert_no_error (error);
1206 g_assert_false (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error));
1207 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
1208 g_clear_error (&error);
1209 if (g_test_verbose ())
1210 g_printerr ("ok\n");
1211
1212 g_date_time_unref (now);
1213
1214 return TRUE;
1215 }
1216
1217 static void
1218 test_file (gconstpointer d)
1219 {
1220 const gchar *filename = d;
1221 GBookmarkFile *bookmark_file;
1222 gboolean success;
1223 gchar *data;
1224 GError *error;
1225
1226 bookmark_file = g_bookmark_file_new ();
1227 g_assert_nonnull (bookmark_file);
1228
1229 success = test_load (bookmark_file, filename);
1230
1231 if (success)
1232 {
1233 test_query (bookmark_file);
1234 test_modify (bookmark_file);
1235
1236 error = NULL;
1237 data = g_bookmark_file_to_data (bookmark_file, NULL, &error);
1238 g_assert_no_error (error);
1239 /* FIXME do some checks on data */
1240 g_free (data);
1241 }
1242
1243 g_bookmark_file_free (bookmark_file);
1244
1245 g_assert_true (success == (strstr (filename, "fail") == NULL));
1246 }
1247
1248 static void
1249 test_file_copy (gconstpointer d)
1250 {
1251 const gchar *filename = d;
1252 GBookmarkFile *bookmark_file;
1253 GBookmarkFile *copy;
1254 gboolean success;
1255 gchar *data;
1256 gchar *copy_data;
1257 gsize length;
1258 gsize copy_length;
1259 GError *error = NULL;
1260
1261 bookmark_file = g_bookmark_file_new ();
1262 g_assert_nonnull (bookmark_file);
1263
1264 success = test_load (bookmark_file, filename);
1265 g_assert_true (success == (strstr (filename, "fail") == NULL));
1266
1267 copy = g_bookmark_file_copy (bookmark_file);
1268 g_assert_nonnull (copy);
1269
1270 if (g_str_has_suffix (filename, "fail-08.xbel") ||
1271 g_str_has_suffix (filename, "fail-06.xbel") ||
1272 g_str_has_suffix (filename, "fail-07.xbel") ||
1273 g_str_has_suffix (filename, "fail-09.xbel") ||
1274 g_str_has_suffix (filename, "fail-10.xbel") ||
1275 g_str_has_suffix (filename, "fail-11.xbel") ||
1276 g_str_has_suffix (filename, "fail-39.xbel"))
1277 {
1278 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
1279 "*no registered applications*skipping*");
1280 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
1281 "*no registered applications*skipping*");
1282 }
1283
1284 data = g_bookmark_file_to_data (bookmark_file, &length, &error);
1285 g_assert_no_error (error);
1286
1287 copy_data = g_bookmark_file_to_data (copy, ©_length, &error);
1288 g_assert_no_error (error);
1289
1290 g_test_assert_expected_messages ();
1291
1292 g_assert_cmpuint (length, ==, copy_length);
1293 g_assert_cmpstr (data, ==, copy_data);
1294
1295 if (success)
1296 {
1297 GBookmarkFile *modified_copy;
1298 gchar *modified_data;
1299 gchar *modified_copy_data;
1300 gsize modified_length;
1301 gsize modified_copy_length;
1302
1303 test_modify (bookmark_file);
1304 test_modify (copy);
1305
1306 modified_data = g_bookmark_file_to_data (bookmark_file,
1307 &modified_length,
1308 &error);
1309 g_assert_no_error (error);
1310
1311 modified_copy_data = g_bookmark_file_to_data (copy,
1312 &modified_copy_length,
1313 &error);
1314 g_assert_no_error (error);
1315
1316 g_assert_cmpstr (data, !=, modified_data);
1317 g_assert_cmpstr (copy_data, !=, modified_copy_data);
1318
1319 g_free (modified_copy_data);
1320 modified_copy = g_bookmark_file_copy (bookmark_file);
1321 modified_copy_data = g_bookmark_file_to_data (modified_copy,
1322 &modified_copy_length,
1323 &error);
1324 g_assert_no_error (error);
1325
1326 g_assert_cmpuint (modified_length, ==, modified_copy_length);
1327 g_assert_cmpstr (modified_data, ==, modified_copy_data);
1328
1329 g_free (modified_data);
1330 g_free (modified_copy_data);
1331 g_bookmark_file_free (modified_copy);
1332 }
1333
1334 g_bookmark_file_free (bookmark_file);
1335 g_bookmark_file_free (copy);
1336
1337 g_free (data);
1338 g_free (copy_data);
1339 }
1340
1341 int
1342 main (int argc, char *argv[])
1343 {
1344 GDir *dir;
1345 GError *error;
1346 const gchar *name;
1347 gchar *path;
1348
1349 g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
1350
1351 if (argc > 1)
1352 {
1353 test_file (argv[1]);
1354 return 0;
1355 }
1356
1357 g_test_add_func ("/bookmarks/load-from-data-dirs", test_load_from_data_dirs);
1358 g_test_add_func ("/bookmarks/to-file", test_to_file);
1359 g_test_add_func ("/bookmarks/move-item", test_move_item);
1360 g_test_add_func ("/bookmarks/corner-cases", test_corner_cases);
1361 g_test_add_func ("/bookmarks/misc", test_misc);
1362 g_test_add_func ("/bookmarks/deprecated", test_deprecated);
1363
1364 error = NULL;
1365 path = g_test_build_filename (G_TEST_DIST, "bookmarks", NULL);
1366 dir = g_dir_open (path, 0, &error);
1367 g_free (path);
1368 g_assert_no_error (error);
1369 while ((name = g_dir_read_name (dir)) != NULL)
1370 {
1371 gchar *filename;
1372 if (!g_str_has_suffix (name, ".xbel"))
1373 continue;
1374
1375 filename = g_test_build_filename (G_TEST_DIST, "bookmarks", name, NULL);
1376
1377 path = g_strdup_printf ("/bookmarks/parse/%s", name);
1378 g_test_add_data_func_full (path, filename, test_file, g_free);
1379 g_free (path);
1380 path = g_strdup_printf ("/bookmarks/copy/%s", name);
1381 g_test_add_data_func_full (path, g_strdup (filename), test_file_copy, g_free);
1382 g_free (path);
1383 }
1384 g_dir_close (dir);
1385
1386 return g_test_run ();
1387 }