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_assert_empty_error (GError **error)
24 g_warning ("Unexpected error (d: %s, c: %d): %s\n",
25 g_quark_to_string ((*error)->domain),
28 g_error_free (*error);
30 g_assert_not_reached ();
35 test_assert_not_empty_error (GError **error,
41 g_warning ("Unexpected success (%s domain expected)\n",
42 g_quark_to_string (domain));
44 g_assert_not_reached ();
47 if ((*error)->domain != domain)
49 g_warning ("Unexpected domain %s (%s domain expected)\n",
50 g_quark_to_string ((*error)->domain),
51 g_quark_to_string (domain));
53 g_assert_not_reached ();
56 if ((*error)->code != code)
58 g_warning ("Unexpected code %d (%d code expected)\n",
62 g_assert_not_reached ();
65 g_error_free (*error);
70 test_assert_str_equal (const gchar *str,
73 if (strcmp (str, cmp) != 0)
75 g_warning ("Unexpected string '%s' ('%s' expected)\n", str, cmp);
77 g_assert_not_reached ();
82 test_load (GBookmarkFile *bookmark,
83 const gchar *filename)
88 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
91 g_print ("Load error: %s\n", error->message);
99 test_query (GBookmarkFile *bookmark)
106 size = g_bookmark_file_get_size (bookmark);
107 uris = g_bookmark_file_get_uris (bookmark, &uris_len);
109 if (uris_len != size)
111 g_print ("URI/size mismatch: URI count is %d (should be %d)\n", uris_len, size);
116 for (i = 0; i < uris_len; i++)
117 if (!g_bookmark_file_has_item (bookmark, uris[i]))
119 g_print ("URI/bookmark mismatch: bookmark for '%s' does not exist\n", uris[i]);
130 test_modify (GBookmarkFile *bookmark)
135 GError *error = NULL;
137 g_print ("\t=> check global title/description...");
138 g_bookmark_file_set_title (bookmark, NULL, "a file");
139 g_bookmark_file_set_description (bookmark, NULL, "a bookmark file");
141 text = g_bookmark_file_get_title (bookmark, NULL, &error);
142 test_assert_empty_error (&error);
143 test_assert_str_equal (text, "a file");
146 text = g_bookmark_file_get_description (bookmark, NULL, &error);
147 test_assert_empty_error (&error);
148 test_assert_str_equal (text, "a bookmark file");
152 g_print ("\t=> check bookmark title/description...");
153 g_bookmark_file_set_title (bookmark, TEST_URI_0, "a title");
154 g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description");
156 text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error);
157 test_assert_empty_error (&error);
158 test_assert_str_equal (text, "a title");
162 g_print ("\t=> check non existing bookmark...");
163 g_bookmark_file_get_description (bookmark, TEST_URI_1, &error);
164 test_assert_not_empty_error (&error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
167 g_print ("\t=> check application...");
168 g_bookmark_file_set_mime_type (bookmark, TEST_URI_0, TEST_MIME);
169 g_bookmark_file_add_application (bookmark, TEST_URI_0,
172 g_assert (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL) == TRUE);
173 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, TEST_APP_NAME,
178 test_assert_empty_error (&error);
179 g_assert (count == 1);
180 g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL));
183 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail",
188 test_assert_not_empty_error (&error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
191 g_print ("\t=> check groups...");
192 g_bookmark_file_add_group (bookmark, TEST_URI_1, "Test");
193 g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL) == TRUE);
194 g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Fail", NULL) == FALSE);
197 g_print ("\t=> check remove...");
198 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == TRUE);
199 test_assert_empty_error (&error);
200 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == FALSE);
201 test_assert_not_empty_error (&error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
208 test_file (const gchar *filename)
210 GBookmarkFile *bookmark_file;
213 g_return_val_if_fail (filename != NULL, 1);
215 g_print ("checking GBookmarkFile...\n");
217 bookmark_file = g_bookmark_file_new ();
218 g_assert (bookmark_file != NULL);
220 success = test_load (bookmark_file, filename);
224 success = test_query (bookmark_file);
225 success = test_modify (bookmark_file);
228 g_bookmark_file_free (bookmark_file);
232 return (success == TRUE ? 0 : 1);
240 return test_file (argv[1]);
243 fprintf (stderr, "Usage: bookmarkfile-test <bookmarkfile>\n");