This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
/
bookmarkfile-test.c
247 lines (198 loc) · 5.95 KB
/
bookmarkfile-test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#undef G_DISABLE_ASSERT
#include <glib.h>
#include <time.h>
#include <locale.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define TEST_URI_0 "file:///abc/defgh/ijklmnopqrstuvwxyz"
#define TEST_URI_1 "file:///test/uri/1"
#define TEST_URI_2 "file:///test/uri/2"
#define TEST_MIME "text/plain"
#define TEST_APP_NAME "bookmarkfile-test"
#define TEST_APP_EXEC "bookmarkfile-test %f"
static void
test_assert_empty_error (GError **error)
{
if (*error != NULL)
{
g_warning ("Unexpected error (d: %s, c: %d): %s\n",
g_quark_to_string ((*error)->domain),
(*error)->code,
(*error)->message);
g_error_free (*error);
g_assert_not_reached ();
}
}
static void
test_assert_not_empty_error (GError **error,
GQuark domain,
gint code)
{
if (*error == NULL)
{
g_warning ("Unexpected success (%s domain expected)\n",
g_quark_to_string (domain));
g_assert_not_reached ();
}
if ((*error)->domain != domain)
{
g_warning ("Unexpected domain %s (%s domain expected)\n",
g_quark_to_string ((*error)->domain),
g_quark_to_string (domain));
g_assert_not_reached ();
}
if ((*error)->code != code)
{
g_warning ("Unexpected code %d (%d code expected)\n",
(*error)->code,
code);
g_assert_not_reached ();
}
g_error_free (*error);
*error = NULL;
}
static void
test_assert_str_equal (const gchar *str,
const gchar *cmp)
{
if (strcmp (str, cmp) != 0)
{
g_warning ("Unexpected string '%s' ('%s' expected)\n", str, cmp);
g_assert_not_reached ();
}
}
static gboolean
test_load (GBookmarkFile *bookmark,
const gchar *filename)
{
GError *error = NULL;
gboolean res;
res = g_bookmark_file_load_from_file (bookmark, filename, &error);
if (error)
{
g_print ("Load error: %s\n", error->message);
g_error_free (error);
}
return res;
}
static gboolean
test_query (GBookmarkFile *bookmark)
{
gint size;
gchar **uris;
gsize uris_len, i;
gboolean res = TRUE;
size = g_bookmark_file_get_size (bookmark);
uris = g_bookmark_file_get_uris (bookmark, &uris_len);
if (uris_len != size)
{
g_print ("URI/size mismatch: URI count is %d (should be %d)\n", uris_len, size);
res = FALSE;
}
for (i = 0; i < uris_len; i++)
if (!g_bookmark_file_has_item (bookmark, uris[i]))
{
g_print ("URI/bookmark mismatch: bookmark for '%s' does not exist\n", uris[i]);
res = FALSE;
}
g_strfreev (uris);
return res;
}
static gboolean
test_modify (GBookmarkFile *bookmark)
{
gchar *text;
guint count;
time_t stamp;
GError *error = NULL;
g_print ("\t=> check global title/description...");
g_bookmark_file_set_title (bookmark, NULL, "a file");
g_bookmark_file_set_description (bookmark, NULL, "a bookmark file");
text = g_bookmark_file_get_title (bookmark, NULL, &error);
test_assert_empty_error (&error);
test_assert_str_equal (text, "a file");
g_free (text);
text = g_bookmark_file_get_description (bookmark, NULL, &error);
test_assert_empty_error (&error);
test_assert_str_equal (text, "a bookmark file");
g_free (text);
g_print ("ok\n");
g_print ("\t=> check bookmark title/description...");
g_bookmark_file_set_title (bookmark, TEST_URI_0, "a title");
g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description");
text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error);
test_assert_empty_error (&error);
test_assert_str_equal (text, "a title");
g_free (text);
g_print ("ok\n");
g_print ("\t=> check non existing bookmark...");
g_bookmark_file_get_description (bookmark, TEST_URI_1, &error);
test_assert_not_empty_error (&error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
g_print ("ok\n");
g_print ("\t=> check application...");
g_bookmark_file_set_mime_type (bookmark, TEST_URI_0, TEST_MIME);
g_bookmark_file_add_application (bookmark, TEST_URI_0,
TEST_APP_NAME,
TEST_APP_EXEC);
g_assert (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL) == TRUE);
g_bookmark_file_get_app_info (bookmark, TEST_URI_0, TEST_APP_NAME,
&text,
&count,
&stamp,
&error);
test_assert_empty_error (&error);
g_assert (count == 1);
g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL));
g_free (text);
g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail",
&text,
&count,
&stamp,
&error);
test_assert_not_empty_error (&error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
g_print ("ok\n");
g_print ("\t=> check groups...");
g_bookmark_file_add_group (bookmark, TEST_URI_1, "Test");
g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL) == TRUE);
g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Fail", NULL) == FALSE);
g_print ("ok\n");
g_print ("\t=> check remove...");
g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == TRUE);
test_assert_empty_error (&error);
g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == FALSE);
test_assert_not_empty_error (&error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
g_print ("ok\n");
return TRUE;
}
static gint
test_file (const gchar *filename)
{
GBookmarkFile *bookmark_file;
gboolean success;
g_return_val_if_fail (filename != NULL, 1);
g_print ("checking GBookmarkFile...\n");
bookmark_file = g_bookmark_file_new ();
g_assert (bookmark_file != NULL);
success = test_load (bookmark_file, filename);
if (success)
{
success = test_query (bookmark_file);
success = test_modify (bookmark_file);
}
g_bookmark_file_free (bookmark_file);
g_print ("ok\n");
return (success == TRUE ? 0 : 1);
}
int
main (int argc,
char *argv[])
{
if (argc > 1)
return test_file (argv[1]);
else
{
fprintf (stderr, "Usage: bookmarkfile-test <bookmarkfile>\n");
return 1;
}
}