1 #undef G_DISABLE_ASSERT
10 #define TEST_URI_0 "file:///abc/defgh/ijklmnopqrstuvwxyz"
11 #define TEST_URI_1 "file:///test/uri/1"
12 #define TEST_URI_2 "file:///test/uri/2"
14 #define TEST_MIME "text/plain"
16 #define TEST_APP_NAME "bookmarkfile-test"
17 #define TEST_APP_EXEC "bookmarkfile-test %f"
20 test_load (GBookmarkFile *bookmark,
21 const gchar *filename)
26 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
27 if (error && g_test_verbose ())
28 g_print ("Load error: %s\n", error->message);
30 g_clear_error (&error);
35 test_query (GBookmarkFile *bookmark)
43 size = g_bookmark_file_get_size (bookmark);
44 uris = g_bookmark_file_get_uris (bookmark, &uris_len);
46 g_assert_cmpint (uris_len, ==, size);
48 for (i = 0; i < uris_len; i++)
50 g_assert (g_bookmark_file_has_item (bookmark, uris[i]));
52 mime = g_bookmark_file_get_mime_type (bookmark, uris[i], &error);
53 g_assert (mime != NULL);
54 g_assert_no_error (error);
59 g_assert (!g_bookmark_file_has_item (bookmark, "file:///no/such/uri"));
61 mime = g_bookmark_file_get_mime_type (bookmark, "file:///no/such/uri", &error);
62 g_assert (mime == NULL);
63 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
69 test_modify (GBookmarkFile *bookmark)
82 if (g_test_verbose ())
83 g_print ("\t=> check global title/description...");
84 g_bookmark_file_set_title (bookmark, NULL, "a file");
85 g_bookmark_file_set_description (bookmark, NULL, "a bookmark file");
87 text = g_bookmark_file_get_title (bookmark, NULL, &error);
88 g_assert_no_error (error);
89 g_assert_cmpstr (text, ==, "a file");
92 text = g_bookmark_file_get_description (bookmark, NULL, &error);
93 g_assert_no_error (error);
94 g_assert_cmpstr (text, ==, "a bookmark file");
96 if (g_test_verbose ())
99 if (g_test_verbose ())
100 g_print ("\t=> check bookmark title/description...");
101 g_bookmark_file_set_title (bookmark, TEST_URI_0, "a title");
102 g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description");
103 g_bookmark_file_set_is_private (bookmark, TEST_URI_0, TRUE);
105 g_bookmark_file_set_added (bookmark, TEST_URI_0, now);
106 g_bookmark_file_set_modified (bookmark, TEST_URI_0, now);
107 g_bookmark_file_set_visited (bookmark, TEST_URI_0, now);
108 g_bookmark_file_set_icon (bookmark, TEST_URI_0, "testicon", "image/png");
110 text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error);
111 g_assert_no_error (error);
112 g_assert_cmpstr (text, ==, "a title");
114 text = g_bookmark_file_get_description (bookmark, TEST_URI_0, &error);
115 g_assert_no_error (error);
116 g_assert_cmpstr (text, ==, "a description");
118 g_assert (g_bookmark_file_get_is_private (bookmark, TEST_URI_0, &error));
119 g_assert_no_error (error);
120 stamp = g_bookmark_file_get_added (bookmark, TEST_URI_0, &error);
121 g_assert_no_error (error);
122 g_assert (stamp == now);
123 stamp = g_bookmark_file_get_modified (bookmark, TEST_URI_0, &error);
124 g_assert_no_error (error);
125 g_assert (stamp == now);
126 stamp = g_bookmark_file_get_visited (bookmark, TEST_URI_0, &error);
127 g_assert_no_error (error);
128 g_assert (stamp == now);
129 g_assert (g_bookmark_file_get_icon (bookmark, TEST_URI_0, &icon, &mime, &error));
130 g_assert_no_error (error);
131 g_assert_cmpstr (icon, ==, "testicon");
132 g_assert_cmpstr (mime, ==, "image/png");
135 if (g_test_verbose ())
138 if (g_test_verbose ())
139 g_print ("\t=> check non existing bookmark...");
140 g_bookmark_file_get_description (bookmark, TEST_URI_1, &error);
141 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
142 g_clear_error (&error);
143 g_bookmark_file_get_is_private (bookmark, TEST_URI_1, &error);
144 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
145 g_clear_error (&error);
146 g_bookmark_file_get_added (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_modified (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_visited (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 if (g_test_verbose ())
158 if (g_test_verbose ())
159 g_print ("\t=> check application...");
160 g_bookmark_file_set_mime_type (bookmark, TEST_URI_0, TEST_MIME);
161 g_assert (!g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL));
162 g_bookmark_file_add_application (bookmark, TEST_URI_0,
165 g_assert (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL));
166 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, TEST_APP_NAME,
171 g_assert_no_error (error);
172 g_assert (count == 1);
173 g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL));
175 g_assert (g_bookmark_file_remove_application (bookmark, TEST_URI_0, TEST_APP_NAME, &error));
176 g_assert_no_error (error);
177 g_bookmark_file_add_application (bookmark, TEST_URI_0, TEST_APP_NAME, TEST_APP_EXEC);
178 apps = g_bookmark_file_get_applications (bookmark, TEST_URI_0, &length, &error);
179 g_assert_no_error (error);
180 g_assert_cmpint (length, ==, 1);
181 g_assert_cmpstr (apps[0], ==, TEST_APP_NAME);
184 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail",
189 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
190 g_clear_error (&error);
192 if (g_test_verbose ())
195 if (g_test_verbose ())
196 g_print ("\t=> check groups...");
197 g_assert (!g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL));
198 g_bookmark_file_add_group (bookmark, TEST_URI_1, "Test");
199 g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL));
200 g_assert (!g_bookmark_file_has_group (bookmark, TEST_URI_1, "Fail", NULL));
201 g_assert (g_bookmark_file_remove_group (bookmark, TEST_URI_1, "Test", &error));
202 g_assert_no_error (error);
203 groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, NULL, &error);
204 g_assert_cmpint (g_strv_length (groups), ==, 0);
206 groups = g_new0 (gchar *, 3);
207 groups[0] = "Group1";
208 groups[1] = "Group2";
210 g_bookmark_file_set_groups (bookmark, TEST_URI_1, (const gchar **)groups, 2);
212 groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, &length, &error);
213 g_assert_cmpint (length, ==, 2);
215 g_assert_no_error (error);
217 if (g_test_verbose ())
220 if (g_test_verbose ())
221 g_print ("\t=> check remove...");
222 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == TRUE);
223 g_assert_no_error (error);
224 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == FALSE);
225 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
226 g_clear_error (&error);
227 if (g_test_verbose ())
234 test_file (gconstpointer d)
236 const gchar *filename = d;
237 GBookmarkFile *bookmark_file;
242 bookmark_file = g_bookmark_file_new ();
243 g_assert (bookmark_file != NULL);
245 success = test_load (bookmark_file, filename);
249 test_query (bookmark_file);
250 test_modify (bookmark_file);
253 data = g_bookmark_file_to_data (bookmark_file, NULL, &error);
254 g_assert_no_error (error);
255 /* FIXME do some checks on data */
259 g_bookmark_file_free (bookmark_file);
261 g_assert (success == (strstr (filename, "fail") == NULL));
265 main (int argc, char *argv[])
272 g_test_init (&argc, &argv, NULL);
281 path = g_test_build_filename (G_TEST_DIST, "bookmarks", NULL);
282 dir = g_dir_open (path, 0, &error);
284 g_assert_no_error (error);
285 while ((name = g_dir_read_name (dir)) != NULL)
287 path = g_strdup_printf ("/bookmarks/parse/%s", name);
288 g_test_add_data_func_full (path, g_test_build_filename (G_TEST_DIST, "bookmarks", name, NULL),
294 return g_test_run ();