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 ())
32 g_print ("Load error: %s\n", error->message);
34 g_clear_error (&error);
39 test_query (GBookmarkFile *bookmark)
47 size = g_bookmark_file_get_size (bookmark);
48 uris = g_bookmark_file_get_uris (bookmark, &uris_len);
50 g_assert_cmpint (uris_len, ==, size);
52 for (i = 0; i < uris_len; i++)
54 g_assert (g_bookmark_file_has_item (bookmark, uris[i]));
56 mime = g_bookmark_file_get_mime_type (bookmark, uris[i], &error);
57 g_assert (mime != NULL);
58 g_assert_no_error (error);
63 g_assert (!g_bookmark_file_has_item (bookmark, "file:///no/such/uri"));
65 mime = g_bookmark_file_get_mime_type (bookmark, "file:///no/such/uri", &error);
66 g_assert (mime == NULL);
67 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
73 test_modify (GBookmarkFile *bookmark)
86 if (g_test_verbose ())
87 g_print ("\t=> check global title/description...");
88 g_bookmark_file_set_title (bookmark, NULL, "a file");
89 g_bookmark_file_set_description (bookmark, NULL, "a bookmark file");
91 text = g_bookmark_file_get_title (bookmark, NULL, &error);
92 g_assert_no_error (error);
93 g_assert_cmpstr (text, ==, "a file");
96 text = g_bookmark_file_get_description (bookmark, NULL, &error);
97 g_assert_no_error (error);
98 g_assert_cmpstr (text, ==, "a bookmark file");
100 if (g_test_verbose ())
103 if (g_test_verbose ())
104 g_print ("\t=> check bookmark title/description...");
105 g_bookmark_file_set_title (bookmark, TEST_URI_0, "a title");
106 g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description");
107 g_bookmark_file_set_is_private (bookmark, TEST_URI_0, TRUE);
109 g_bookmark_file_set_added (bookmark, TEST_URI_0, now);
110 g_bookmark_file_set_modified (bookmark, TEST_URI_0, now);
111 g_bookmark_file_set_visited (bookmark, TEST_URI_0, now);
112 g_bookmark_file_set_icon (bookmark, TEST_URI_0, "testicon", "image/png");
114 text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error);
115 g_assert_no_error (error);
116 g_assert_cmpstr (text, ==, "a title");
118 text = g_bookmark_file_get_description (bookmark, TEST_URI_0, &error);
119 g_assert_no_error (error);
120 g_assert_cmpstr (text, ==, "a description");
122 g_assert (g_bookmark_file_get_is_private (bookmark, TEST_URI_0, &error));
123 g_assert_no_error (error);
124 stamp = g_bookmark_file_get_added (bookmark, TEST_URI_0, &error);
125 g_assert_no_error (error);
126 g_assert (stamp == now);
127 stamp = g_bookmark_file_get_modified (bookmark, TEST_URI_0, &error);
128 g_assert_no_error (error);
129 g_assert (stamp == now);
130 stamp = g_bookmark_file_get_visited (bookmark, TEST_URI_0, &error);
131 g_assert_no_error (error);
132 g_assert (stamp == now);
133 g_assert (g_bookmark_file_get_icon (bookmark, TEST_URI_0, &icon, &mime, &error));
134 g_assert_no_error (error);
135 g_assert_cmpstr (icon, ==, "testicon");
136 g_assert_cmpstr (mime, ==, "image/png");
139 if (g_test_verbose ())
142 if (g_test_verbose ())
143 g_print ("\t=> check non existing bookmark...");
144 g_bookmark_file_get_description (bookmark, TEST_URI_1, &error);
145 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
146 g_clear_error (&error);
147 g_bookmark_file_get_is_private (bookmark, TEST_URI_1, &error);
148 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
149 g_clear_error (&error);
150 g_bookmark_file_get_added (bookmark, TEST_URI_1, &error);
151 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
152 g_clear_error (&error);
153 g_bookmark_file_get_modified (bookmark, TEST_URI_1, &error);
154 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
155 g_clear_error (&error);
156 g_bookmark_file_get_visited (bookmark, TEST_URI_1, &error);
157 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
158 g_clear_error (&error);
159 if (g_test_verbose ())
162 if (g_test_verbose ())
163 g_print ("\t=> check application...");
164 g_bookmark_file_set_mime_type (bookmark, TEST_URI_0, TEST_MIME);
165 g_assert (!g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL));
166 g_bookmark_file_add_application (bookmark, TEST_URI_0,
169 g_assert (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL));
170 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, TEST_APP_NAME,
175 g_assert_no_error (error);
176 g_assert (count == 1);
177 g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL));
179 g_assert (g_bookmark_file_remove_application (bookmark, TEST_URI_0, TEST_APP_NAME, &error));
180 g_assert_no_error (error);
181 g_bookmark_file_add_application (bookmark, TEST_URI_0, TEST_APP_NAME, TEST_APP_EXEC);
182 apps = g_bookmark_file_get_applications (bookmark, TEST_URI_0, &length, &error);
183 g_assert_no_error (error);
184 g_assert_cmpint (length, ==, 1);
185 g_assert_cmpstr (apps[0], ==, TEST_APP_NAME);
188 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail",
193 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
194 g_clear_error (&error);
196 if (g_test_verbose ())
199 if (g_test_verbose ())
200 g_print ("\t=> check groups...");
201 g_assert (!g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL));
202 g_bookmark_file_add_group (bookmark, TEST_URI_1, "Test");
203 g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL));
204 g_assert (!g_bookmark_file_has_group (bookmark, TEST_URI_1, "Fail", NULL));
205 g_assert (g_bookmark_file_remove_group (bookmark, TEST_URI_1, "Test", &error));
206 g_assert_no_error (error);
207 groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, NULL, &error);
208 g_assert_cmpint (g_strv_length (groups), ==, 0);
210 groups = g_new0 (gchar *, 3);
211 groups[0] = "Group1";
212 groups[1] = "Group2";
214 g_bookmark_file_set_groups (bookmark, TEST_URI_1, (const gchar **)groups, 2);
216 groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, &length, &error);
217 g_assert_cmpint (length, ==, 2);
219 g_assert_no_error (error);
221 if (g_test_verbose ())
224 if (g_test_verbose ())
225 g_print ("\t=> check remove...");
226 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == TRUE);
227 g_assert_no_error (error);
228 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == FALSE);
229 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
230 g_clear_error (&error);
231 if (g_test_verbose ())
238 test_file (gconstpointer d)
240 const gchar *filename = d;
241 GBookmarkFile *bookmark_file;
246 bookmark_file = g_bookmark_file_new ();
247 g_assert (bookmark_file != NULL);
249 success = test_load (bookmark_file, filename);
253 test_query (bookmark_file);
254 test_modify (bookmark_file);
257 data = g_bookmark_file_to_data (bookmark_file, NULL, &error);
258 g_assert_no_error (error);
259 /* FIXME do some checks on data */
263 g_bookmark_file_free (bookmark_file);
265 g_assert (success == (strstr (filename, "fail") == NULL));
269 main (int argc, char *argv[])
276 g_test_init (&argc, &argv, NULL);
285 dir = g_dir_open (SRCDIR "/bookmarks", 0, &error);
286 g_assert_no_error (error);
287 while ((name = g_dir_read_name (dir)) != NULL)
289 path = g_strdup_printf ("/bookmarks/parse/%s", name);
290 g_test_add_data_func (path, g_build_filename (SRCDIR, "bookmarks", name, NULL), test_file);
295 return g_test_run ();