Simplify subprocesses in tests
[platform/upstream/glib.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 #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
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 && g_test_verbose ())
28     g_print ("Load error: %s\n", error->message);
29
30   g_clear_error (&error);
31   return res;
32 }
33
34 static void
35 test_query (GBookmarkFile *bookmark)
36 {
37   gint size;
38   gchar **uris;
39   gsize uris_len, i;
40   gchar *mime;
41   GError *error;
42
43   size = g_bookmark_file_get_size (bookmark);
44   uris = g_bookmark_file_get_uris (bookmark, &uris_len);
45
46   g_assert_cmpint (uris_len, ==, size);
47
48   for (i = 0; i < uris_len; i++)
49     {
50       g_assert (g_bookmark_file_has_item (bookmark, uris[i]));
51       error = NULL;
52       mime = g_bookmark_file_get_mime_type (bookmark, uris[i], &error);
53       g_assert (mime != NULL);
54       g_assert_no_error (error);
55       g_free (mime);
56     }
57   g_strfreev (uris);
58
59   g_assert (!g_bookmark_file_has_item (bookmark, "file:///no/such/uri"));
60   error = NULL;
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);
64   g_error_free (error);
65   g_free (mime);
66 }
67
68 static gboolean
69 test_modify (GBookmarkFile *bookmark)
70 {
71   gchar *text;
72   guint count;
73   time_t stamp;
74   time_t now;
75   GError *error = NULL;
76   gchar **groups;
77   gsize length;
78   gchar **apps;
79   gchar *icon;
80   gchar *mime;
81
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");
86
87   text = g_bookmark_file_get_title (bookmark, NULL, &error);
88   g_assert_no_error (error);
89   g_assert_cmpstr (text, ==, "a file");
90   g_free (text);
91
92   text = g_bookmark_file_get_description (bookmark, NULL, &error);
93   g_assert_no_error (error);
94   g_assert_cmpstr (text, ==, "a bookmark file");
95   g_free (text);
96   if (g_test_verbose ())
97     g_print ("ok\n");
98
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);
104   time (&now);
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");
109
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");
113   g_free (text);
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");
117   g_free (text);
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");
133   g_free (icon);
134   g_free (mime);
135   if (g_test_verbose ())
136     g_print ("ok\n");
137
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 ())
156     g_print ("ok\n");
157
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,
163                                    TEST_APP_NAME,
164                                    TEST_APP_EXEC);
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,
167                                 &text,
168                                 &count,
169                                 &stamp,
170                                 &error);
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));
174   g_free (text);
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);
182   g_strfreev (apps);
183
184   g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail",
185                                 &text,
186                                 &count,
187                                 &stamp,
188                                 &error);
189   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
190   g_clear_error (&error);
191
192   if (g_test_verbose ())
193     g_print ("ok\n"); 
194
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);
205   g_strfreev (groups);
206   groups = g_new0 (gchar *, 3);
207   groups[0] = "Group1";
208   groups[1] = "Group2";
209   groups[2] = NULL;
210   g_bookmark_file_set_groups (bookmark, TEST_URI_1, (const gchar **)groups, 2);
211   g_free (groups);
212   groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, &length, &error);
213   g_assert_cmpint (length, ==, 2);
214   g_strfreev (groups);
215   g_assert_no_error (error);
216
217   if (g_test_verbose ())
218     g_print ("ok\n");
219
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 ())
228     g_print ("ok\n");
229   
230   return TRUE;
231 }
232
233 static void
234 test_file (gconstpointer d)
235 {
236   const gchar *filename = d;
237   GBookmarkFile *bookmark_file;
238   gboolean success;
239   gchar *data;
240   GError *error;
241
242   bookmark_file = g_bookmark_file_new ();
243   g_assert (bookmark_file != NULL);
244
245   success = test_load (bookmark_file, filename);
246
247   if (success)
248     {
249       test_query (bookmark_file);
250       test_modify (bookmark_file);
251
252       error = NULL;
253       data = g_bookmark_file_to_data (bookmark_file, NULL, &error);
254       g_assert_no_error (error);
255       /* FIXME do some checks on data */
256       g_free (data);
257     }
258
259   g_bookmark_file_free (bookmark_file);
260
261   g_assert (success == (strstr (filename, "fail") == NULL));
262 }
263
264 int
265 main (int argc, char *argv[])
266 {
267   GDir *dir;
268   GError *error;
269   const gchar *name;
270   gchar *path;
271
272   g_test_init (&argc, &argv, NULL);
273
274   if (argc > 1)
275     {
276       test_file (argv[1]);
277       return 0;
278     }
279
280   error = NULL;
281   path = g_test_build_filename (G_TEST_DIST, "bookmarks", NULL);
282   dir = g_dir_open (path, 0, &error);
283   g_free (path);
284   g_assert_no_error (error);
285   while ((name = g_dir_read_name (dir)) != NULL)
286     {
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),
289                                  test_file, g_free);
290       g_free (path);
291     }
292   g_dir_close (dir);
293
294   return g_test_run ();
295 }