1 #undef G_DISABLE_ASSERT
14 #define TEST_URI_0 "file:///abc/defgh/ijklmnopqrstuvwxyz"
15 #define TEST_URI_1 "file:///test/uri/1"
16 #define TEST_URI_2 "file:///test/uri/2"
18 #define TEST_MIME "text/plain"
20 #define TEST_APP_NAME "bookmarkfile-test"
21 #define TEST_APP_EXEC "bookmarkfile-test %f"
24 test_load (GBookmarkFile
*bookmark
,
25 const gchar
*filename
)
30 res
= g_bookmark_file_load_from_file (bookmark
, filename
, &error
);
31 if (error
&& g_test_verbose ())
33 g_print ("Load error: %s\n", error
->message
);
41 test_query (GBookmarkFile
*bookmark
)
49 size
= g_bookmark_file_get_size (bookmark
);
50 uris
= g_bookmark_file_get_uris (bookmark
, &uris_len
);
52 g_assert_cmpint (uris_len
, ==, size
);
54 for (i
= 0; i
< uris_len
; i
++)
56 g_assert (g_bookmark_file_has_item (bookmark
, uris
[i
]));
58 mime
= g_bookmark_file_get_mime_type (bookmark
, uris
[i
], &error
);
59 g_assert (mime
!= NULL
);
60 g_assert_no_error (error
);
65 g_assert (!g_bookmark_file_has_item (bookmark
, "file:///no/such/uri"));
67 mime
= g_bookmark_file_get_mime_type (bookmark
, "file:///no/such/uri", &error
);
68 g_assert (mime
== NULL
);
69 g_assert_error (error
, G_BOOKMARK_FILE_ERROR
, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND
);
75 test_modify (GBookmarkFile
*bookmark
)
88 if (g_test_verbose ())
89 g_print ("\t=> check global title/description...");
90 g_bookmark_file_set_title (bookmark
, NULL
, "a file");
91 g_bookmark_file_set_description (bookmark
, NULL
, "a bookmark file");
93 text
= g_bookmark_file_get_title (bookmark
, NULL
, &error
);
94 g_assert_no_error (error
);
95 g_assert_cmpstr (text
, ==, "a file");
98 text
= g_bookmark_file_get_description (bookmark
, NULL
, &error
);
99 g_assert_no_error (error
);
100 g_assert_cmpstr (text
, ==, "a bookmark file");
102 if (g_test_verbose ())
105 if (g_test_verbose ())
106 g_print ("\t=> check bookmark title/description...");
107 g_bookmark_file_set_title (bookmark
, TEST_URI_0
, "a title");
108 g_bookmark_file_set_description (bookmark
, TEST_URI_0
, "a description");
109 g_bookmark_file_set_is_private (bookmark
, TEST_URI_0
, TRUE
);
111 g_bookmark_file_set_added (bookmark
, TEST_URI_0
, now
);
112 g_bookmark_file_set_modified (bookmark
, TEST_URI_0
, now
);
113 g_bookmark_file_set_visited (bookmark
, TEST_URI_0
, now
);
114 g_bookmark_file_set_icon (bookmark
, TEST_URI_0
, "testicon", "image/png");
116 text
= g_bookmark_file_get_title (bookmark
, TEST_URI_0
, &error
);
117 g_assert_no_error (error
);
118 g_assert_cmpstr (text
, ==, "a title");
120 text
= g_bookmark_file_get_description (bookmark
, TEST_URI_0
, &error
);
121 g_assert_no_error (error
);
122 g_assert_cmpstr (text
, ==, "a description");
124 g_assert (g_bookmark_file_get_is_private (bookmark
, TEST_URI_0
, &error
));
125 g_assert_no_error (error
);
126 stamp
= g_bookmark_file_get_added (bookmark
, TEST_URI_0
, &error
);
127 g_assert_no_error (error
);
128 g_assert (stamp
== now
);
129 stamp
= g_bookmark_file_get_modified (bookmark
, TEST_URI_0
, &error
);
130 g_assert_no_error (error
);
131 g_assert (stamp
== now
);
132 stamp
= g_bookmark_file_get_visited (bookmark
, TEST_URI_0
, &error
);
133 g_assert_no_error (error
);
134 g_assert (stamp
== now
);
135 g_assert (g_bookmark_file_get_icon (bookmark
, TEST_URI_0
, &icon
, &mime
, &error
));
136 g_assert_no_error (error
);
137 g_assert_cmpstr (icon
, ==, "testicon");
138 g_assert_cmpstr (mime
, ==, "image/png");
141 if (g_test_verbose ())
144 if (g_test_verbose ())
145 g_print ("\t=> check non existing bookmark...");
146 g_bookmark_file_get_description (bookmark
, TEST_URI_1
, &error
);
147 g_assert_error (error
, G_BOOKMARK_FILE_ERROR
, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND
);
148 g_clear_error (&error
);
149 g_bookmark_file_get_is_private (bookmark
, TEST_URI_1
, &error
);
150 g_assert_error (error
, G_BOOKMARK_FILE_ERROR
, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND
);
151 g_clear_error (&error
);
152 g_bookmark_file_get_added (bookmark
, TEST_URI_1
, &error
);
153 g_assert_error (error
, G_BOOKMARK_FILE_ERROR
, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND
);
154 g_clear_error (&error
);
155 g_bookmark_file_get_modified (bookmark
, TEST_URI_1
, &error
);
156 g_assert_error (error
, G_BOOKMARK_FILE_ERROR
, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND
);
157 g_clear_error (&error
);
158 g_bookmark_file_get_visited (bookmark
, TEST_URI_1
, &error
);
159 g_assert_error (error
, G_BOOKMARK_FILE_ERROR
, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND
);
160 g_clear_error (&error
);
161 if (g_test_verbose ())
164 if (g_test_verbose ())
165 g_print ("\t=> check application...");
166 g_bookmark_file_set_mime_type (bookmark
, TEST_URI_0
, TEST_MIME
);
167 g_assert (!g_bookmark_file_has_application (bookmark
, TEST_URI_0
, TEST_APP_NAME
, NULL
));
168 g_bookmark_file_add_application (bookmark
, TEST_URI_0
,
171 g_assert (g_bookmark_file_has_application (bookmark
, TEST_URI_0
, TEST_APP_NAME
, NULL
));
172 g_bookmark_file_get_app_info (bookmark
, TEST_URI_0
, TEST_APP_NAME
,
177 g_assert_no_error (error
);
178 g_assert (count
== 1);
179 g_assert (stamp
== g_bookmark_file_get_modified (bookmark
, TEST_URI_0
, NULL
));
181 g_assert (g_bookmark_file_remove_application (bookmark
, TEST_URI_0
, TEST_APP_NAME
, &error
));
182 g_assert_no_error (error
);
183 g_bookmark_file_add_application (bookmark
, TEST_URI_0
, TEST_APP_NAME
, TEST_APP_EXEC
);
184 apps
= g_bookmark_file_get_applications (bookmark
, TEST_URI_0
, &length
, &error
);
185 g_assert_no_error (error
);
186 g_assert_cmpint (length
, ==, 1);
187 g_assert_cmpstr (apps
[0], ==, TEST_APP_NAME
);
190 g_bookmark_file_get_app_info (bookmark
, TEST_URI_0
, "fail",
195 g_assert_error (error
, G_BOOKMARK_FILE_ERROR
, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED
);
196 g_clear_error (&error
);
198 if (g_test_verbose ())
201 if (g_test_verbose ())
202 g_print ("\t=> check groups...");
203 g_assert (!g_bookmark_file_has_group (bookmark
, TEST_URI_1
, "Test", NULL
));
204 g_bookmark_file_add_group (bookmark
, TEST_URI_1
, "Test");
205 g_assert (g_bookmark_file_has_group (bookmark
, TEST_URI_1
, "Test", NULL
));
206 g_assert (!g_bookmark_file_has_group (bookmark
, TEST_URI_1
, "Fail", NULL
));
207 g_assert (g_bookmark_file_remove_group (bookmark
, TEST_URI_1
, "Test", &error
));
208 g_assert_no_error (error
);
209 groups
= g_bookmark_file_get_groups (bookmark
, TEST_URI_1
, NULL
, &error
);
210 g_assert_cmpint (g_strv_length (groups
), ==, 0);
212 groups
= g_new0 (gchar
*, 3);
213 groups
[0] = "Group1";
214 groups
[1] = "Group2";
216 g_bookmark_file_set_groups (bookmark
, TEST_URI_1
, (const gchar
**)groups
, 2);
218 groups
= g_bookmark_file_get_groups (bookmark
, TEST_URI_1
, &length
, &error
);
219 g_assert_cmpint (length
, ==, 2);
221 g_assert_no_error (error
);
223 if (g_test_verbose ())
226 if (g_test_verbose ())
227 g_print ("\t=> check remove...");
228 g_assert (g_bookmark_file_remove_item (bookmark
, TEST_URI_1
, &error
) == TRUE
);
229 g_assert_no_error (error
);
230 g_assert (g_bookmark_file_remove_item (bookmark
, TEST_URI_1
, &error
) == FALSE
);
231 g_assert_error (error
, G_BOOKMARK_FILE_ERROR
, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND
);
232 g_clear_error (&error
);
233 if (g_test_verbose ())
240 test_file (gconstpointer d
)
242 const gchar
*filename
= d
;
243 GBookmarkFile
*bookmark_file
;
248 bookmark_file
= g_bookmark_file_new ();
249 g_assert (bookmark_file
!= NULL
);
251 success
= test_load (bookmark_file
, filename
);
255 test_query (bookmark_file
);
256 test_modify (bookmark_file
);
259 data
= g_bookmark_file_to_data (bookmark_file
, NULL
, &error
);
260 g_assert_no_error (error
);
261 /* FIXME do some checks on data */
265 g_bookmark_file_free (bookmark_file
);
267 g_assert (success
== (strstr (filename
, "fail") == NULL
));
271 main (int argc
, char *argv
[])
278 g_test_init (&argc
, &argv
, NULL
);
287 dir
= g_dir_open (SRCDIR
"/bookmarks", 0, &error
);
288 g_assert_no_error (error
);
289 while ((name
= g_dir_read_name (dir
)) != NULL
)
291 path
= g_strdup_printf ("/bookmarks/parse/%s", name
);
292 g_test_add_data_func (path
, g_build_filename (SRCDIR
, "bookmarks", name
, NULL
), test_file
);
297 return g_test_run ();