1 | #undef G_DISABLE_ASSERT |
2 | |
3 | #include <glib.h> |
4 | #include <time.h> |
5 | #include <locale.h> |
6 | #include <string.h> |
7 | #include <stdio.h> |
8 | #include <stdlib.h> |
9 | |
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" |
13 | |
14 | #define TEST_MIME "text/plain" |
15 | |
16 | #define TEST_APP_NAME "bookmarkfile-test" |
17 | #define TEST_APP_EXEC "bookmarkfile-test %f" |
18 | |
19 | static void |
20 | test_load_from_data_dirs (void) |
21 | { |
22 | GBookmarkFile *bookmark; |
23 | gboolean res; |
24 | gchar *path = NULL; |
25 | GError *error = NULL; |
26 | |
27 | bookmark = g_bookmark_file_new (); |
28 | |
29 | res = g_bookmark_file_load_from_data_dirs (bookmark, file: "no-such-bookmark-file.xbel" , full_path: &path, error: &error); |
30 | |
31 | g_assert_false (res); |
32 | g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT); |
33 | g_assert_null (path); |
34 | g_error_free (error); |
35 | |
36 | g_bookmark_file_free (bookmark); |
37 | } |
38 | |
39 | static void |
40 | test_to_file (void) |
41 | { |
42 | GBookmarkFile *bookmark; |
43 | const gchar *filename; |
44 | gboolean res; |
45 | GError *error = NULL; |
46 | char *in, *out; |
47 | |
48 | bookmark = g_bookmark_file_new (); |
49 | |
50 | g_test_message (format: "Roundtrip from newly created bookmark file" ); |
51 | g_bookmark_file_set_title (bookmark, uri: "file:///tmp/schedule.ps" , title: "schedule.ps" ); |
52 | g_bookmark_file_set_mime_type (bookmark, uri: "file:///tmp/schedule.ps" , mime_type: "application/postscript" ); |
53 | g_bookmark_file_add_application (bookmark, uri: "file:///tmp/schedule.ps" , name: "ghostscript" , exec: "ghostscript %F" ); |
54 | |
55 | res = g_bookmark_file_to_file (bookmark, filename: "out.xbel" , error: &error); |
56 | g_assert_no_error (error); |
57 | g_assert_true (res); |
58 | |
59 | res = g_bookmark_file_load_from_file (bookmark, filename: "out.xbel" , error: &error); |
60 | g_assert_no_error (error); |
61 | g_assert_true (res); |
62 | |
63 | out = g_bookmark_file_get_title (bookmark, uri: "file:///tmp/schedule.ps" , error: &error); |
64 | g_assert_no_error (error); |
65 | g_assert_cmpstr (out, ==, "schedule.ps" ); |
66 | g_free (mem: out); |
67 | |
68 | out = g_bookmark_file_get_mime_type (bookmark, uri: "file:///tmp/schedule.ps" , error: &error); |
69 | g_assert_no_error (error); |
70 | g_assert_cmpstr (out, ==, "application/postscript" ); |
71 | g_free (mem: out); |
72 | |
73 | remove (filename: "out.xbel" ); |
74 | |
75 | g_test_message (format: "Roundtrip from a valid bookmark file" ); |
76 | filename = g_test_get_filename (file_type: G_TEST_DIST, first_path: "bookmarks" , "valid-01.xbel" , NULL); |
77 | res = g_bookmark_file_load_from_file (bookmark, filename, error: &error); |
78 | g_assert_no_error (error); |
79 | g_assert_true (res); |
80 | |
81 | res = g_bookmark_file_to_file (bookmark, filename: "out.xbel" , error: &error); |
82 | g_assert_no_error (error); |
83 | g_assert_true (res); |
84 | |
85 | res = g_file_get_contents (filename, contents: &in, NULL, error: &error); |
86 | g_assert_no_error (error); |
87 | g_assert_true (res); |
88 | |
89 | res = g_file_get_contents (filename: "out.xbel" , contents: &out, NULL, error: &error); |
90 | g_assert_no_error (error); |
91 | g_assert_true (res); |
92 | remove (filename: "out.xbel" ); |
93 | |
94 | g_assert_cmpstr (in, ==, out); |
95 | g_free (mem: in); |
96 | g_free (mem: out); |
97 | |
98 | g_bookmark_file_free (bookmark); |
99 | } |
100 | |
101 | static void |
102 | test_move_item (void) |
103 | { |
104 | GBookmarkFile *bookmark; |
105 | const gchar *filename; |
106 | gboolean res; |
107 | GError *error = NULL; |
108 | |
109 | bookmark = g_bookmark_file_new (); |
110 | |
111 | filename = g_test_get_filename (file_type: G_TEST_DIST, first_path: "bookmarks" , "valid-01.xbel" , NULL); |
112 | res = g_bookmark_file_load_from_file (bookmark, filename, error: &error); |
113 | g_assert_true (res); |
114 | g_assert_no_error (error); |
115 | |
116 | res = g_bookmark_file_move_item (bookmark, |
117 | old_uri: "file:///home/zefram/Documents/milan-stuttgart.ps" , |
118 | new_uri: "file:///tmp/schedule.ps" , |
119 | error: &error); |
120 | g_assert_true (res); |
121 | g_assert_no_error (error); |
122 | |
123 | res = g_bookmark_file_move_item (bookmark, |
124 | old_uri: "file:///tmp/schedule.ps" , |
125 | new_uri: "file:///tmp/schedule.ps" , |
126 | error: &error); |
127 | g_assert_true (res); |
128 | g_assert_no_error (error); |
129 | |
130 | res = g_bookmark_file_move_item (bookmark, |
131 | old_uri: "file:///no-such-file.xbel" , |
132 | new_uri: "file:///tmp/schedule.ps" , |
133 | error: &error); |
134 | g_assert_false (res); |
135 | g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); |
136 | g_clear_error (err: &error); |
137 | |
138 | res = g_bookmark_file_move_item (bookmark, |
139 | old_uri: "file:///tmp/schedule.ps" , |
140 | NULL, |
141 | error: &error); |
142 | g_assert_true (res); |
143 | g_assert_no_error (error); |
144 | |
145 | g_bookmark_file_free (bookmark); |
146 | } |
147 | |
148 | static void |
149 | test_misc (void) |
150 | { |
151 | GBookmarkFile *bookmark; |
152 | const gchar *filename; |
153 | gboolean res; |
154 | GError *error = NULL; |
155 | gchar *s; |
156 | GDateTime *before, *after, *t; |
157 | gchar *cmd, *exec; |
158 | guint count; |
159 | |
160 | bookmark = g_bookmark_file_new (); |
161 | |
162 | filename = g_test_get_filename (file_type: G_TEST_DIST, first_path: "bookmarks" , "valid-01.xbel" , NULL); |
163 | res = g_bookmark_file_load_from_file (bookmark, filename, error: &error); |
164 | g_assert_true (res); |
165 | g_assert_no_error (error); |
166 | |
167 | res = g_bookmark_file_get_icon (bookmark, |
168 | uri: "file:///home/zefram/Documents/milan-stuttgart.ps" , |
169 | NULL, |
170 | NULL, |
171 | error: &error); |
172 | g_assert_false (res); |
173 | g_assert_no_error (error); |
174 | |
175 | res = g_bookmark_file_get_icon (bookmark, |
176 | uri: "file:///tmp/schedule.ps" , |
177 | NULL, |
178 | NULL, |
179 | error: &error); |
180 | g_assert_false (res); |
181 | g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); |
182 | g_clear_error (err: &error); |
183 | |
184 | g_bookmark_file_set_description (bookmark, |
185 | uri: "file:///tmp/schedule0.ps" , |
186 | description: "imaginary schedule" ); |
187 | s = g_bookmark_file_get_description (bookmark, |
188 | uri: "file:///tmp/schedule0.ps" , |
189 | error: &error); |
190 | g_assert_no_error (error); |
191 | g_assert_cmpstr (s, ==, "imaginary schedule" ); |
192 | g_free (mem: s); |
193 | s = g_bookmark_file_get_mime_type (bookmark, |
194 | uri: "file:///tmp/schedule0.ps" , |
195 | error: &error); |
196 | g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_INVALID_VALUE); |
197 | g_assert_null (s); |
198 | g_clear_error (err: &error); |
199 | res = g_bookmark_file_get_is_private (bookmark, |
200 | uri: "file:///tmp/schedule0.ps" , |
201 | error: &error); |
202 | g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_INVALID_VALUE); |
203 | g_clear_error (err: &error); |
204 | g_assert_false (res); |
205 | |
206 | g_bookmark_file_set_mime_type (bookmark, |
207 | uri: "file:///tmp/schedule1.ps" , |
208 | mime_type: "image/png" ); |
209 | s = g_bookmark_file_get_mime_type (bookmark, |
210 | uri: "file:///tmp/schedule1.ps" , |
211 | error: &error); |
212 | g_assert_no_error (error); |
213 | g_assert_cmpstr (s, ==, "image/png" ); |
214 | g_free (mem: s); |
215 | |
216 | g_bookmark_file_set_is_private (bookmark, |
217 | uri: "file:///tmp/schedule2.ps" , |
218 | TRUE); |
219 | res = g_bookmark_file_get_is_private (bookmark, |
220 | uri: "file:///tmp/schedule2.ps" , |
221 | error: &error); |
222 | g_assert_no_error (error); |
223 | g_assert_true (res); |
224 | |
225 | before = g_date_time_new_now_utc (); |
226 | |
227 | g_bookmark_file_set_added_date_time (bookmark, |
228 | uri: "file:///tmp/schedule3.ps" , |
229 | added: before); |
230 | t = g_bookmark_file_get_added_date_time (bookmark, |
231 | uri: "file:///tmp/schedule3.ps" , |
232 | error: &error); |
233 | g_assert_no_error (error); |
234 | |
235 | after = g_date_time_new_now_utc (); |
236 | g_assert_cmpint (g_date_time_compare (before, t), <=, 0); |
237 | g_assert_cmpint (g_date_time_compare (t, after), <=, 0); |
238 | |
239 | g_date_time_unref (datetime: after); |
240 | g_date_time_unref (datetime: before); |
241 | |
242 | before = g_date_time_new_now_utc (); |
243 | |
244 | g_bookmark_file_set_modified_date_time (bookmark, |
245 | uri: "file:///tmp/schedule4.ps" , |
246 | modified: before); |
247 | t = g_bookmark_file_get_modified_date_time (bookmark, |
248 | uri: "file:///tmp/schedule4.ps" , |
249 | error: &error); |
250 | g_assert_no_error (error); |
251 | |
252 | after = g_date_time_new_now_utc (); |
253 | g_assert_cmpint (g_date_time_compare (before, t), <=, 0); |
254 | g_assert_cmpint (g_date_time_compare (t, after), <=, 0); |
255 | |
256 | g_date_time_unref (datetime: after); |
257 | g_date_time_unref (datetime: before); |
258 | |
259 | before = g_date_time_new_now_utc (); |
260 | |
261 | g_bookmark_file_set_visited_date_time (bookmark, |
262 | uri: "file:///tmp/schedule5.ps" , |
263 | visited: before); |
264 | t = g_bookmark_file_get_visited_date_time (bookmark, |
265 | uri: "file:///tmp/schedule5.ps" , |
266 | error: &error); |
267 | g_assert_no_error (error); |
268 | |
269 | after = g_date_time_new_now_utc (); |
270 | g_assert_cmpint (g_date_time_compare (before, t), <=, 0); |
271 | g_assert_cmpint (g_date_time_compare (t, after), <=, 0); |
272 | g_date_time_unref (datetime: after); |
273 | g_date_time_unref (datetime: before); |
274 | |
275 | g_bookmark_file_set_icon (bookmark, |
276 | uri: "file:///tmp/schedule6.ps" , |
277 | href: "application-x-postscript" , |
278 | mime_type: "image/png" ); |
279 | res = g_bookmark_file_get_icon (bookmark, |
280 | uri: "file:///tmp/schedule6.ps" , |
281 | href: &s, |
282 | NULL, |
283 | error: &error); |
284 | g_assert_no_error (error); |
285 | g_assert_true (res); |
286 | g_assert_cmpstr (s, ==, "application-x-postscript" ); |
287 | g_free (mem: s); |
288 | |
289 | g_bookmark_file_set_icon (bookmark, |
290 | uri: "file:///tmp/schedule6.ps" , |
291 | NULL, NULL); |
292 | res = g_bookmark_file_get_icon (bookmark, |
293 | uri: "file:///tmp/schedule6.ps" , |
294 | href: &s, |
295 | NULL, |
296 | error: &error); |
297 | g_assert_no_error (error); |
298 | g_assert_false (res); |
299 | |
300 | res = g_bookmark_file_has_application (bookmark, |
301 | uri: "file:///tmp/schedule7.ps" , |
302 | name: "foo" , |
303 | error: &error); |
304 | g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); |
305 | g_assert_false (res); |
306 | g_clear_error (err: &error); |
307 | |
308 | before = g_date_time_new_now_utc (); |
309 | |
310 | g_bookmark_file_add_application (bookmark, |
311 | uri: "file:///tmp/schedule7.ps" , |
312 | NULL, NULL); |
313 | res = g_bookmark_file_get_application_info (bookmark, |
314 | uri: "file:///tmp/schedule7.ps" , |
315 | name: g_get_application_name (), |
316 | exec: &exec, count: &count, stamp: &t, |
317 | error: &error); |
318 | g_assert_no_error (error); |
319 | g_assert_true (res); |
320 | cmd = g_strconcat (string1: g_get_prgname (), " file:///tmp/schedule7.ps" , NULL); |
321 | g_assert_cmpstr (exec, ==, cmd); |
322 | g_free (mem: cmd); |
323 | g_free (mem: exec); |
324 | g_assert_cmpuint (count, ==, 1); |
325 | |
326 | after = g_date_time_new_now_utc (); |
327 | g_assert_cmpint (g_date_time_compare (before, t), <=, 0); |
328 | g_assert_cmpint (g_date_time_compare (t, after), <=, 0); |
329 | |
330 | g_date_time_unref (datetime: after); |
331 | g_date_time_unref (datetime: before); |
332 | |
333 | g_bookmark_file_free (bookmark); |
334 | } |
335 | |
336 | static void |
337 | test_deprecated (void) |
338 | { |
339 | GBookmarkFile *file = NULL; |
340 | GError *local_error = NULL; |
341 | time_t t, now; |
342 | gboolean retval; |
343 | |
344 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
345 | |
346 | now = time (NULL); |
347 | file = g_bookmark_file_new (); |
348 | |
349 | /* added */ |
350 | g_bookmark_file_set_added (bookmark: file, uri: "file://test" , added: -1); |
351 | t = g_bookmark_file_get_added (bookmark: file, uri: "file://test" , error: &local_error); |
352 | g_assert_no_error (local_error); |
353 | g_assert_cmpint (t, >=, now); |
354 | |
355 | g_bookmark_file_set_added (bookmark: file, uri: "file://test" , added: 1234); |
356 | t = g_bookmark_file_get_added (bookmark: file, uri: "file://test" , error: &local_error); |
357 | g_assert_no_error (local_error); |
358 | g_assert_cmpint (t, ==, 1234); |
359 | |
360 | t = g_bookmark_file_get_added (bookmark: file, uri: "file://not-exist" , error: &local_error); |
361 | g_assert_error (local_error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); |
362 | g_assert_cmpint (t, ==, (time_t) -1); |
363 | g_clear_error (err: &local_error); |
364 | |
365 | /* modified */ |
366 | g_bookmark_file_set_modified (bookmark: file, uri: "file://test" , modified: -1); |
367 | t = g_bookmark_file_get_modified (bookmark: file, uri: "file://test" , error: &local_error); |
368 | g_assert_no_error (local_error); |
369 | g_assert_cmpint (t, >=, now); |
370 | |
371 | g_bookmark_file_set_modified (bookmark: file, uri: "file://test" , modified: 1234); |
372 | t = g_bookmark_file_get_modified (bookmark: file, uri: "file://test" , error: &local_error); |
373 | g_assert_no_error (local_error); |
374 | g_assert_cmpint (t, ==, 1234); |
375 | |
376 | t = g_bookmark_file_get_modified (bookmark: file, uri: "file://not-exist" , error: &local_error); |
377 | g_assert_error (local_error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); |
378 | g_assert_cmpint (t, ==, (time_t) -1); |
379 | g_clear_error (err: &local_error); |
380 | |
381 | /* visited */ |
382 | g_bookmark_file_set_visited (bookmark: file, uri: "file://test" , visited: -1); |
383 | t = g_bookmark_file_get_visited (bookmark: file, uri: "file://test" , error: &local_error); |
384 | g_assert_no_error (local_error); |
385 | g_assert_cmpint (t, >=, now); |
386 | |
387 | g_bookmark_file_set_visited (bookmark: file, uri: "file://test" , visited: 1234); |
388 | t = g_bookmark_file_get_visited (bookmark: file, uri: "file://test" , error: &local_error); |
389 | g_assert_no_error (local_error); |
390 | g_assert_cmpint (t, ==, 1234); |
391 | |
392 | t = g_bookmark_file_get_visited (bookmark: file, uri: "file://not-exist" , error: &local_error); |
393 | g_assert_error (local_error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); |
394 | g_assert_cmpint (t, ==, (time_t) -1); |
395 | g_clear_error (err: &local_error); |
396 | |
397 | /* set app info */ |
398 | retval = g_bookmark_file_set_app_info (bookmark: file, uri: "file://test" , name: "app" , exec: "/path/to/app" , count: 1, stamp: -1, error: &local_error); |
399 | g_assert_no_error (local_error); |
400 | g_assert_true (retval); |
401 | |
402 | retval = g_bookmark_file_get_app_info (bookmark: file, uri: "file://test" , name: "app" , NULL, NULL, stamp: &t, error: &local_error); |
403 | g_assert_no_error (local_error); |
404 | g_assert_true (retval); |
405 | g_assert_cmpint (t, >=, now); |
406 | |
407 | retval = g_bookmark_file_set_app_info (bookmark: file, uri: "file://test" , name: "app" , exec: "/path/to/app" , count: 1, stamp: 1234, error: &local_error); |
408 | g_assert_no_error (local_error); |
409 | g_assert_true (retval); |
410 | |
411 | retval = g_bookmark_file_get_app_info (bookmark: file, uri: "file://test" , name: "app" , NULL, NULL, stamp: &t, error: &local_error); |
412 | g_assert_no_error (local_error); |
413 | g_assert_true (retval); |
414 | g_assert_cmpint (t, ==, 1234); |
415 | |
416 | retval = g_bookmark_file_get_app_info (bookmark: file, uri: "file://test" , name: "app" , NULL, NULL, NULL, error: &local_error); |
417 | g_assert_no_error (local_error); |
418 | g_assert_true (retval); |
419 | |
420 | retval = g_bookmark_file_get_app_info (bookmark: file, uri: "file://not-exist" , name: "app" , NULL, NULL, stamp: &t, error: &local_error); |
421 | g_assert_error (local_error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); |
422 | g_assert_false (retval); |
423 | g_clear_error (err: &local_error); |
424 | |
425 | g_bookmark_file_free (bookmark: file); |
426 | |
427 | G_GNUC_END_IGNORE_DEPRECATIONS |
428 | } |
429 | |
430 | static gboolean |
431 | test_load (GBookmarkFile *bookmark, |
432 | const gchar *filename) |
433 | { |
434 | GError *error = NULL; |
435 | gboolean res; |
436 | |
437 | res = g_bookmark_file_load_from_file (bookmark, filename, error: &error); |
438 | if (error && g_test_verbose ()) |
439 | g_printerr (format: "Load error: %s\n" , error->message); |
440 | |
441 | g_clear_error (err: &error); |
442 | return res; |
443 | } |
444 | |
445 | static void |
446 | test_query (GBookmarkFile *bookmark) |
447 | { |
448 | gint size; |
449 | gchar **uris; |
450 | gsize uris_len, i; |
451 | gchar *mime; |
452 | GError *error; |
453 | |
454 | size = g_bookmark_file_get_size (bookmark); |
455 | uris = g_bookmark_file_get_uris (bookmark, length: &uris_len); |
456 | |
457 | g_assert_cmpint (uris_len, ==, size); |
458 | |
459 | for (i = 0; i < uris_len; i++) |
460 | { |
461 | g_assert_true (g_bookmark_file_has_item (bookmark, uris[i])); |
462 | error = NULL; |
463 | mime = g_bookmark_file_get_mime_type (bookmark, uri: uris[i], error: &error); |
464 | g_assert_nonnull (mime); |
465 | g_assert_no_error (error); |
466 | g_free (mem: mime); |
467 | } |
468 | g_strfreev (str_array: uris); |
469 | |
470 | g_assert_false (g_bookmark_file_has_item (bookmark, "file:///no/such/uri" )); |
471 | error = NULL; |
472 | mime = g_bookmark_file_get_mime_type (bookmark, uri: "file:///no/such/uri" , error: &error); |
473 | g_assert_null (mime); |
474 | g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); |
475 | g_error_free (error); |
476 | g_free (mem: mime); |
477 | } |
478 | |
479 | static gboolean |
480 | test_modify (GBookmarkFile *bookmark) |
481 | { |
482 | gchar *text; |
483 | guint count; |
484 | GDateTime *stamp; |
485 | GDateTime *now = NULL; |
486 | GError *error = NULL; |
487 | gchar **groups; |
488 | gsize length; |
489 | gchar **apps; |
490 | gchar *icon; |
491 | gchar *mime; |
492 | |
493 | if (g_test_verbose ()) |
494 | g_printerr (format: "\t=> check global title/description..." ); |
495 | g_bookmark_file_set_title (bookmark, NULL, title: "a file" ); |
496 | g_bookmark_file_set_description (bookmark, NULL, description: "a bookmark file" ); |
497 | |
498 | text = g_bookmark_file_get_title (bookmark, NULL, error: &error); |
499 | g_assert_no_error (error); |
500 | g_assert_cmpstr (text, ==, "a file" ); |
501 | g_free (mem: text); |
502 | |
503 | text = g_bookmark_file_get_description (bookmark, NULL, error: &error); |
504 | g_assert_no_error (error); |
505 | g_assert_cmpstr (text, ==, "a bookmark file" ); |
506 | g_free (mem: text); |
507 | if (g_test_verbose ()) |
508 | g_printerr (format: "ok\n" ); |
509 | |
510 | if (g_test_verbose ()) |
511 | g_printerr (format: "\t=> check bookmark title/description..." ); |
512 | g_bookmark_file_set_title (bookmark, TEST_URI_0, title: "a title" ); |
513 | g_bookmark_file_set_description (bookmark, TEST_URI_0, description: "a description" ); |
514 | g_bookmark_file_set_is_private (bookmark, TEST_URI_0, TRUE); |
515 | now = g_date_time_new_now_utc (); |
516 | g_bookmark_file_set_added_date_time (bookmark, TEST_URI_0, added: now); |
517 | g_bookmark_file_set_visited_date_time (bookmark, TEST_URI_0, visited: now); |
518 | g_bookmark_file_set_icon (bookmark, TEST_URI_0, href: "testicon" , mime_type: "image/png" ); |
519 | |
520 | /* Check the modification date by itself, as it’s updated whenever we modify |
521 | * other properties. */ |
522 | g_bookmark_file_set_modified_date_time (bookmark, TEST_URI_0, modified: now); |
523 | stamp = g_bookmark_file_get_modified_date_time (bookmark, TEST_URI_0, error: &error); |
524 | g_assert_no_error (error); |
525 | g_assert_cmpint (g_date_time_compare (stamp, now), ==, 0); |
526 | |
527 | text = g_bookmark_file_get_title (bookmark, TEST_URI_0, error: &error); |
528 | g_assert_no_error (error); |
529 | g_assert_cmpstr (text, ==, "a title" ); |
530 | g_free (mem: text); |
531 | text = g_bookmark_file_get_description (bookmark, TEST_URI_0, error: &error); |
532 | g_assert_no_error (error); |
533 | g_assert_cmpstr (text, ==, "a description" ); |
534 | g_free (mem: text); |
535 | g_assert_true (g_bookmark_file_get_is_private (bookmark, TEST_URI_0, &error)); |
536 | g_assert_no_error (error); |
537 | stamp = g_bookmark_file_get_added_date_time (bookmark, TEST_URI_0, error: &error); |
538 | g_assert_no_error (error); |
539 | g_assert_cmpint (g_date_time_compare (stamp, now), ==, 0); |
540 | stamp = g_bookmark_file_get_visited_date_time (bookmark, TEST_URI_0, error: &error); |
541 | g_assert_no_error (error); |
542 | g_assert_cmpint (g_date_time_compare (stamp, now), ==, 0); |
543 | g_assert_true (g_bookmark_file_get_icon (bookmark, TEST_URI_0, &icon, &mime, &error)); |
544 | g_assert_no_error (error); |
545 | g_assert_cmpstr (icon, ==, "testicon" ); |
546 | g_assert_cmpstr (mime, ==, "image/png" ); |
547 | g_free (mem: icon); |
548 | g_free (mem: mime); |
549 | if (g_test_verbose ()) |
550 | g_printerr (format: "ok\n" ); |
551 | |
552 | if (g_test_verbose ()) |
553 | g_printerr (format: "\t=> check non existing bookmark..." ); |
554 | g_bookmark_file_get_description (bookmark, TEST_URI_1, error: &error); |
555 | g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); |
556 | g_clear_error (err: &error); |
557 | g_bookmark_file_get_is_private (bookmark, TEST_URI_1, error: &error); |
558 | g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); |
559 | g_clear_error (err: &error); |
560 | g_bookmark_file_get_added_date_time (bookmark, TEST_URI_1, error: &error); |
561 | g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); |
562 | g_clear_error (err: &error); |
563 | g_bookmark_file_get_modified_date_time (bookmark, TEST_URI_1, error: &error); |
564 | g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); |
565 | g_clear_error (err: &error); |
566 | g_bookmark_file_get_visited_date_time (bookmark, TEST_URI_1, error: &error); |
567 | g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); |
568 | g_clear_error (err: &error); |
569 | if (g_test_verbose ()) |
570 | g_printerr (format: "ok\n" ); |
571 | |
572 | if (g_test_verbose ()) |
573 | g_printerr (format: "\t=> check application..." ); |
574 | g_bookmark_file_set_mime_type (bookmark, TEST_URI_0, TEST_MIME); |
575 | g_assert_false (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL)); |
576 | g_bookmark_file_add_application (bookmark, TEST_URI_0, |
577 | TEST_APP_NAME, |
578 | TEST_APP_EXEC); |
579 | g_assert_true (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL)); |
580 | g_bookmark_file_get_application_info (bookmark, TEST_URI_0, TEST_APP_NAME, |
581 | exec: &text, |
582 | count: &count, |
583 | stamp: &stamp, |
584 | error: &error); |
585 | g_assert_no_error (error); |
586 | g_assert_cmpuint (count, ==, 1); |
587 | g_assert_cmpint (g_date_time_compare (stamp, g_bookmark_file_get_modified_date_time (bookmark, TEST_URI_0, NULL)), <=, 0); |
588 | g_free (mem: text); |
589 | g_assert_true (g_bookmark_file_remove_application (bookmark, TEST_URI_0, TEST_APP_NAME, &error)); |
590 | g_assert_no_error (error); |
591 | g_bookmark_file_add_application (bookmark, TEST_URI_0, TEST_APP_NAME, TEST_APP_EXEC); |
592 | apps = g_bookmark_file_get_applications (bookmark, TEST_URI_0, length: &length, error: &error); |
593 | g_assert_no_error (error); |
594 | g_assert_cmpint (length, ==, 1); |
595 | g_assert_cmpstr (apps[0], ==, TEST_APP_NAME); |
596 | g_strfreev (str_array: apps); |
597 | |
598 | g_bookmark_file_get_application_info (bookmark, TEST_URI_0, name: "fail" , |
599 | exec: &text, |
600 | count: &count, |
601 | stamp: &stamp, |
602 | error: &error); |
603 | g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED); |
604 | g_clear_error (err: &error); |
605 | |
606 | if (g_test_verbose ()) |
607 | g_printerr (format: "ok\n" ); |
608 | |
609 | if (g_test_verbose ()) |
610 | g_printerr (format: "\t=> check groups..." ); |
611 | g_assert_false (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test" , NULL)); |
612 | g_bookmark_file_add_group (bookmark, TEST_URI_1, group: "Test" ); |
613 | g_assert_true (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test" , NULL)); |
614 | g_assert_false (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Fail" , NULL)); |
615 | g_assert_true (g_bookmark_file_remove_group (bookmark, TEST_URI_1, "Test" , &error)); |
616 | g_assert_no_error (error); |
617 | groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, NULL, error: &error); |
618 | g_assert_cmpint (g_strv_length (groups), ==, 0); |
619 | g_strfreev (str_array: groups); |
620 | groups = g_new0 (gchar *, 3); |
621 | groups[0] = "Group1" ; |
622 | groups[1] = "Group2" ; |
623 | groups[2] = NULL; |
624 | g_bookmark_file_set_groups (bookmark, TEST_URI_1, groups: (const gchar **)groups, length: 2); |
625 | g_free (mem: groups); |
626 | groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, length: &length, error: &error); |
627 | g_assert_cmpint (length, ==, 2); |
628 | g_strfreev (str_array: groups); |
629 | g_assert_no_error (error); |
630 | |
631 | if (g_test_verbose ()) |
632 | g_printerr (format: "ok\n" ); |
633 | |
634 | if (g_test_verbose ()) |
635 | g_printerr (format: "\t=> check remove..." ); |
636 | g_assert_true (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error)); |
637 | g_assert_no_error (error); |
638 | g_assert_false (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error)); |
639 | g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); |
640 | g_clear_error (err: &error); |
641 | if (g_test_verbose ()) |
642 | g_printerr (format: "ok\n" ); |
643 | |
644 | g_date_time_unref (datetime: now); |
645 | |
646 | return TRUE; |
647 | } |
648 | |
649 | static void |
650 | test_file (gconstpointer d) |
651 | { |
652 | const gchar *filename = d; |
653 | GBookmarkFile *bookmark_file; |
654 | gboolean success; |
655 | gchar *data; |
656 | GError *error; |
657 | |
658 | bookmark_file = g_bookmark_file_new (); |
659 | g_assert_nonnull (bookmark_file); |
660 | |
661 | success = test_load (bookmark: bookmark_file, filename); |
662 | |
663 | if (success) |
664 | { |
665 | test_query (bookmark: bookmark_file); |
666 | test_modify (bookmark: bookmark_file); |
667 | |
668 | error = NULL; |
669 | data = g_bookmark_file_to_data (bookmark: bookmark_file, NULL, error: &error); |
670 | g_assert_no_error (error); |
671 | /* FIXME do some checks on data */ |
672 | g_free (mem: data); |
673 | } |
674 | |
675 | g_bookmark_file_free (bookmark: bookmark_file); |
676 | |
677 | g_assert_true (success == (strstr (filename, "fail" ) == NULL)); |
678 | } |
679 | |
680 | int |
681 | main (int argc, char *argv[]) |
682 | { |
683 | GDir *dir; |
684 | GError *error; |
685 | const gchar *name; |
686 | gchar *path; |
687 | |
688 | g_test_init (argc: &argc, argv: &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL); |
689 | |
690 | if (argc > 1) |
691 | { |
692 | test_file (d: argv[1]); |
693 | return 0; |
694 | } |
695 | |
696 | g_test_add_func (testpath: "/bookmarks/load-from-data-dirs" , test_func: test_load_from_data_dirs); |
697 | g_test_add_func (testpath: "/bookmarks/to-file" , test_func: test_to_file); |
698 | g_test_add_func (testpath: "/bookmarks/move-item" , test_func: test_move_item); |
699 | g_test_add_func (testpath: "/bookmarks/misc" , test_func: test_misc); |
700 | g_test_add_func (testpath: "/bookmarks/deprecated" , test_func: test_deprecated); |
701 | |
702 | error = NULL; |
703 | path = g_test_build_filename (file_type: G_TEST_DIST, first_path: "bookmarks" , NULL); |
704 | dir = g_dir_open (path, flags: 0, error: &error); |
705 | g_free (mem: path); |
706 | g_assert_no_error (error); |
707 | while ((name = g_dir_read_name (dir)) != NULL) |
708 | { |
709 | if (!g_str_has_suffix (str: name, suffix: ".xbel" )) |
710 | continue; |
711 | |
712 | path = g_strdup_printf (format: "/bookmarks/parse/%s" , name); |
713 | g_test_add_data_func_full (testpath: path, test_data: g_test_build_filename (file_type: G_TEST_DIST, first_path: "bookmarks" , name, NULL), |
714 | test_func: test_file, data_free_func: g_free); |
715 | g_free (mem: path); |
716 | } |
717 | g_dir_close (dir); |
718 | |
719 | return g_test_run (); |
720 | } |
721 | |