Tizen 2.1 base
[platform/upstream/glib2.0.git] / glib / tests / bookmarkfile.c
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 #ifndef SRCDIR
11 #define SRCDIR "."
12 #endif
13
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"
17
18 #define TEST_MIME       "text/plain"
19
20 #define TEST_APP_NAME   "bookmarkfile-test"
21 #define TEST_APP_EXEC   "bookmarkfile-test %f"
22
23 static gboolean
24 test_load (GBookmarkFile *bookmark,
25            const gchar   *filename)
26 {
27   GError *error = NULL;
28   gboolean res;
29   
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);
33
34   g_clear_error (&error);
35   return res;
36 }
37
38 static void
39 test_query (GBookmarkFile *bookmark)
40 {
41   gint size;
42   gchar **uris;
43   gsize uris_len, i;
44   gchar *mime;
45   GError *error;
46
47   size = g_bookmark_file_get_size (bookmark);
48   uris = g_bookmark_file_get_uris (bookmark, &uris_len);
49
50   g_assert_cmpint (uris_len, ==, size);
51
52   for (i = 0; i < uris_len; i++)
53     {
54       g_assert (g_bookmark_file_has_item (bookmark, uris[i]));
55       error = NULL;
56       mime = g_bookmark_file_get_mime_type (bookmark, uris[i], &error);
57       g_assert (mime != NULL);
58       g_assert_no_error (error);
59       g_free (mime);
60     }
61   g_strfreev (uris);
62
63   g_assert (!g_bookmark_file_has_item (bookmark, "file:///no/such/uri"));
64   error = NULL;
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);
68   g_error_free (error);
69   g_free (mime);
70 }
71
72 static gboolean
73 test_modify (GBookmarkFile *bookmark)
74 {
75   gchar *text;
76   guint count;
77   time_t stamp;
78   time_t now;
79   GError *error = NULL;
80   gchar **groups;
81   gsize length;
82   gchar **apps;
83   gchar *icon;
84   gchar *mime;
85
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");
90
91   text = g_bookmark_file_get_title (bookmark, NULL, &error);
92   g_assert_no_error (error);
93   g_assert_cmpstr (text, ==, "a file");
94   g_free (text);
95
96   text = g_bookmark_file_get_description (bookmark, NULL, &error);
97   g_assert_no_error (error);
98   g_assert_cmpstr (text, ==, "a bookmark file");
99   g_free (text);
100   if (g_test_verbose ())
101     g_print ("ok\n");
102
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);
108   time (&now);
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");
113
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");
117   g_free (text);
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");
121   g_free (text);
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");
137   g_free (icon);
138   g_free (mime);
139   if (g_test_verbose ())
140     g_print ("ok\n");
141
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 ())
160     g_print ("ok\n");
161
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,
167                                    TEST_APP_NAME,
168                                    TEST_APP_EXEC);
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,
171                                 &text,
172                                 &count,
173                                 &stamp,
174                                 &error);
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));
178   g_free (text);
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);
186   g_strfreev (apps);
187
188   g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail",
189                                 &text,
190                                 &count,
191                                 &stamp,
192                                 &error);
193   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
194   g_clear_error (&error);
195
196   if (g_test_verbose ())
197     g_print ("ok\n"); 
198
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);
209   g_strfreev (groups);
210   groups = g_new0 (gchar *, 3);
211   groups[0] = "Group1";
212   groups[1] = "Group2";
213   groups[2] = NULL;
214   g_bookmark_file_set_groups (bookmark, TEST_URI_1, (const gchar **)groups, 2);
215   g_free (groups);
216   groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, &length, &error);
217   g_assert_cmpint (length, ==, 2);
218   g_strfreev (groups);
219   g_assert_no_error (error);
220
221   if (g_test_verbose ())
222     g_print ("ok\n");
223
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 ())
232     g_print ("ok\n");
233   
234   return TRUE;
235 }
236
237 static void
238 test_file (gconstpointer d)
239 {
240   const gchar *filename = d;
241   GBookmarkFile *bookmark_file;
242   gboolean success;
243   gchar *data;
244   GError *error;
245
246   bookmark_file = g_bookmark_file_new ();
247   g_assert (bookmark_file != NULL);
248
249   success = test_load (bookmark_file, filename);
250
251   if (success)
252     {
253       test_query (bookmark_file);
254       test_modify (bookmark_file);
255
256       error = NULL;
257       data = g_bookmark_file_to_data (bookmark_file, NULL, &error);
258       g_assert_no_error (error);
259       /* FIXME do some checks on data */
260       g_free (data);
261     }
262
263   g_bookmark_file_free (bookmark_file);
264
265   g_assert (success == (strstr (filename, "fail") == NULL));
266 }
267
268 int
269 main (int argc, char *argv[])
270 {
271   GDir *dir;
272   GError *error;
273   const gchar *name;
274   gchar *path;
275
276   g_test_init (&argc, &argv, NULL);
277
278   if (argc > 1)
279     {
280       test_file (argv[1]);
281       return 0;
282     }
283
284   error = 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)
288     {
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);
291       g_free (path);
292     }
293   g_dir_close (dir);
294
295   return g_test_run ();
296 }