1 #undef G_DISABLE_ASSERT
2
3 #include <glib.h>
4 #include <time.h>
5 #include <locale.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9
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"
13
14 #define TEST_MIME "text/plain"
15
16 #define TEST_APP_NAME "bookmarkfile-test"
17 #define TEST_APP_EXEC "bookmarkfile-test %f"
18
19 static gboolean
test_load(GBookmarkFile * bookmark,const gchar * filename)20 test_load (GBookmarkFile *bookmark,
21 const gchar *filename)
22 {
23 GError *error = NULL;
24 gboolean res;
25
26 res = g_bookmark_file_load_from_file (bookmark, filename, &error);
27 if (error)
28 {
29 g_print ("Load error: %s\n", error->message);
30 g_error_free (error);
31 }
32
33 return res;
34 }
35
36 static gboolean
test_query(GBookmarkFile * bookmark)37 test_query (GBookmarkFile *bookmark)
38 {
39 gint size;
40 gchar **uris;
41 gsize uris_len, i;
42 gboolean res = TRUE;
43
44 size = g_bookmark_file_get_size (bookmark);
45 uris = g_bookmark_file_get_uris (bookmark, &uris_len);
46
47 if (uris_len != size)
48 {
49 g_print ("URI/size mismatch: URI count is %d (should be %d)\n", uris_len, size);
50
51 res = FALSE;
52 }
53
54 for (i = 0; i < uris_len; i++)
55 if (!g_bookmark_file_has_item (bookmark, uris[i]))
56 {
57 g_print ("URI/bookmark mismatch: bookmark for '%s' does not exist\n", uris[i]);
58
59 res = FALSE;
60 }
61
62 g_strfreev (uris);
63
64 return res;
65 }
66
67 static gboolean
test_modify(GBookmarkFile * bookmark)68 test_modify (GBookmarkFile *bookmark)
69 {
70 gchar *text;
71 guint count;
72 time_t stamp;
73 GError *error = NULL;
74
75 g_print ("\t=> check global title/description...");
76 g_bookmark_file_set_title (bookmark, NULL, "a file");
77 g_bookmark_file_set_description (bookmark, NULL, "a bookmark file");
78
79 text = g_bookmark_file_get_title (bookmark, NULL, &error);
80 g_assert_no_error (error);
81 g_assert_cmpstr (text, ==, "a file");
82 g_free (text);
83
84 text = g_bookmark_file_get_description (bookmark, NULL, &error);
85 g_assert_no_error (error);
86 g_assert_cmpstr (text, ==, "a bookmark file");
87 g_free (text);
88 g_print ("ok\n");
89
90 g_print ("\t=> check bookmark title/description...");
91 g_bookmark_file_set_title (bookmark, TEST_URI_0, "a title");
92 g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description");
93
94 text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error);
95 g_assert_no_error (error);
96 g_assert_cmpstr (text, ==, "a title");
97 g_free (text);
98 g_print ("ok\n");
99
100 g_print ("\t=> check non existing bookmark...");
101 g_bookmark_file_get_description (bookmark, TEST_URI_1, &error);
102 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
103 g_clear_error (&error);
104 g_print ("ok\n");
105
106 g_print ("\t=> check application...");
107 g_bookmark_file_set_mime_type (bookmark, TEST_URI_0, TEST_MIME);
108 g_bookmark_file_add_application (bookmark, TEST_URI_0,
109 TEST_APP_NAME,
110 TEST_APP_EXEC);
111 g_assert (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL) == TRUE);
112 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, TEST_APP_NAME,
113 &text,
114 &count,
115 &stamp,
116 &error);
117 g_assert_no_error (error);
118 g_assert (count == 1);
119 g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL));
120 g_free (text);
121
122 g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail",
123 &text,
124 &count,
125 &stamp,
126 &error);
127 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
128 g_clear_error (&error);
129 g_print ("ok\n");
130
131 g_print ("\t=> check groups...");
132 g_bookmark_file_add_group (bookmark, TEST_URI_1, "Test");
133 g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL) == TRUE);
134 g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Fail", NULL) == FALSE);
135 g_print ("ok\n");
136
137 g_print ("\t=> check remove...");
138 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == TRUE);
139 g_assert_no_error (error);
140 g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == FALSE);
141 g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
142 g_clear_error (&error);
143 g_print ("ok\n");
144
145 return TRUE;
146 }
147
148 static gint
test_file(const gchar * filename)149 test_file (const gchar *filename)
150 {
151 GBookmarkFile *bookmark_file;
152 gboolean success;
153
154 g_return_val_if_fail (filename != NULL, 1);
155
156 g_print ("checking GBookmarkFile...\n");
157
158 bookmark_file = g_bookmark_file_new ();
159 g_assert (bookmark_file != NULL);
160
161 success = test_load (bookmark_file, filename);
162
163 if (success)
164 {
165 success = test_query (bookmark_file);
166 success = test_modify (bookmark_file);
167 }
168
169 g_bookmark_file_free (bookmark_file);
170
171 g_print ("ok\n");
172
173 return (success == TRUE ? 0 : 1);
174 }
175
176 int
main(int argc,char * argv[])177 main (int argc,
178 char *argv[])
179 {
180 if (argc > 1)
181 return test_file (argv[1]);
182 else
183 {
184 fprintf (stderr, "Usage: bookmarkfile-test <bookmarkfile>\n");
185
186 return 1;
187 }
188 }
189