Add GRegex for regular expression matching. (#50075)
[platform/upstream/glib.git] / tests / bookmarkfile-test.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 void
20 test_assert_empty_error (GError **error)
21 {
22   if (*error != NULL)
23     {
24       g_warning ("Unexpected error (d: %s, c: %d): %s\n",
25                  g_quark_to_string ((*error)->domain),
26                  (*error)->code,
27                  (*error)->message);
28       g_error_free (*error);
29
30       g_assert_not_reached ();
31     }
32 }
33
34 static void
35 test_assert_not_empty_error (GError **error,
36                              GQuark   domain,
37                              gint     code)
38 {
39   if (*error == NULL)
40     {
41       g_warning ("Unexpected success (%s domain expected)\n",
42                  g_quark_to_string (domain));
43
44       g_assert_not_reached ();
45     }
46
47   if ((*error)->domain != domain)
48     {
49       g_warning ("Unexpected domain %s (%s domain expected)\n",
50                  g_quark_to_string ((*error)->domain),
51                  g_quark_to_string (domain));
52
53       g_assert_not_reached ();
54     }
55
56   if ((*error)->code != code)
57     {
58       g_warning ("Unexpected code %d (%d code expected)\n",
59                  (*error)->code,
60                  code);
61
62       g_assert_not_reached ();
63     }
64
65   g_error_free (*error);
66   *error = NULL;
67 }
68
69 static void
70 test_assert_str_equal (const gchar *str,
71                        const gchar *cmp)
72 {
73   if (strcmp (str, cmp) != 0)
74     {
75       g_warning ("Unexpected string '%s' ('%s' expected)\n", str, cmp);
76       
77       g_assert_not_reached ();
78     }
79 }
80
81 static gboolean
82 test_load (GBookmarkFile *bookmark,
83            const gchar   *filename)
84 {
85   GError *error = NULL;
86   gboolean res;
87   
88   res = g_bookmark_file_load_from_file (bookmark, filename, &error);
89   if (error)
90     {
91       g_print ("Load error: %s\n", error->message);
92       g_error_free (error);
93     }
94
95   return res;
96 }
97
98 static gboolean
99 test_query (GBookmarkFile *bookmark)
100 {
101   gint size;
102   gchar **uris;
103   gsize uris_len, i;
104   gboolean res = TRUE;
105
106   size = g_bookmark_file_get_size (bookmark);
107   uris = g_bookmark_file_get_uris (bookmark, &uris_len);
108  
109   if (uris_len != size)
110     {
111       g_print ("URI/size mismatch: URI count is %d (should be %d)\n", uris_len, size);
112
113       res = FALSE;
114     }
115
116   for (i = 0; i < uris_len; i++)
117     if (!g_bookmark_file_has_item (bookmark, uris[i]))
118       {
119         g_print ("URI/bookmark mismatch: bookmark for '%s' does not exist\n", uris[i]);
120
121         res = FALSE;
122       }
123
124   g_strfreev (uris);
125   
126   return res;
127 }
128
129 static gboolean
130 test_modify (GBookmarkFile *bookmark)
131 {
132   gchar *text;
133   guint count;
134   time_t stamp;
135   GError *error = NULL;
136   
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");
140   
141   text = g_bookmark_file_get_title (bookmark, NULL, &error);
142   test_assert_empty_error (&error);
143   test_assert_str_equal (text, "a file");
144   g_free (text);
145
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");
149   g_free (text);
150   g_print ("ok\n");
151
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");
155
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");
159   g_free (text);
160   g_print ("ok\n");
161
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);
165   g_print ("ok\n");
166   
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,
170                                    TEST_APP_NAME,
171                                    TEST_APP_EXEC);
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,
174                                 &text,
175                                 &count,
176                                 &stamp,
177                                 &error);
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));
181   g_free (text);
182   
183   g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail",
184                                 &text,
185                                 &count,
186                                 &stamp,
187                                 &error);
188   test_assert_not_empty_error (&error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
189   g_print ("ok\n"); 
190
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);
195   g_print ("ok\n");
196
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);
202   g_print ("ok\n");
203   
204   return TRUE;
205 }
206
207 static gint
208 test_file (const gchar *filename)
209 {
210   GBookmarkFile *bookmark_file;
211   gboolean success;
212
213   g_return_val_if_fail (filename != NULL, 1);
214
215   g_print ("checking GBookmarkFile...\n");
216
217   bookmark_file = g_bookmark_file_new ();
218   g_assert (bookmark_file != NULL);
219
220   success = test_load (bookmark_file, filename);
221   
222   if (success)
223     {
224       success = test_query (bookmark_file);
225       success = test_modify (bookmark_file);
226     }
227
228   g_bookmark_file_free (bookmark_file);
229
230   g_print ("ok\n");
231
232   return (success == TRUE ? 0 : 1);
233 }
234
235 int
236 main (int   argc,
237       char *argv[])
238 {
239   if (argc > 1)
240     return test_file (argv[1]);
241   else
242     {
243       fprintf (stderr, "Usage: bookmarkfile-test <bookmarkfile>\n");
244
245       return 1;
246     }
247 }