]> Pileus Git - ~andy/gtk/blob - tests/css/parser/test-css-parser.c
test: Add a bunch of default properties to the CSS parser
[~andy/gtk] / tests / css / parser / test-css-parser.c
1 /*
2  * Copyright (C) 2011 Red Hat Inc.
3  *
4  * Author:
5  *      Benjamin Otte <otte@redhat.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26 #include <glib/gstdio.h>
27 #include <gtk/gtk.h>
28
29 static char *
30 test_get_reference_file (const char *css_file)
31 {
32   GString *file = g_string_new (NULL);
33
34   if (g_str_has_suffix (css_file, ".css"))
35     g_string_append_len (file, css_file, strlen (css_file) - 4);
36   else
37     g_string_append (file, css_file);
38   
39   g_string_append (file, ".ref.css");
40
41   if (!g_file_test (file->str, G_FILE_TEST_EXISTS))
42     {
43       g_string_free (file, TRUE);
44       return g_strdup (css_file);
45     }
46
47   return g_string_free (file, FALSE);
48 }
49
50 static char *
51 test_get_errors_file (const char *css_file)
52 {
53   GString *file = g_string_new (NULL);
54
55   if (g_str_has_suffix (css_file, ".css"))
56     g_string_append_len (file, css_file, strlen (css_file) - 4);
57   else
58     g_string_append (file, css_file);
59   
60   g_string_append (file, ".errors");
61
62   if (!g_file_test (file->str, G_FILE_TEST_EXISTS))
63     {
64       g_string_free (file, TRUE);
65       return NULL;
66     }
67
68   return g_string_free (file, FALSE);
69 }
70
71 static char *
72 diff_with_file (const char  *file1,
73                 char        *text,
74                 gssize       len,
75                 GError     **error)
76 {
77   const char *command[] = { "diff", "-u", file1, NULL, NULL };
78   char *diff, *tmpfile;
79   int fd;
80
81   diff = NULL;
82
83   if (len < 0)
84     len = strlen (text);
85   
86   /* write the text buffer to a temporary file */
87   fd = g_file_open_tmp (NULL, &tmpfile, error);
88   if (fd < 0)
89     return NULL;
90
91   if (write (fd, text, len) != (int) len)
92     {
93       close (fd);
94       g_set_error (error,
95                    G_FILE_ERROR, G_FILE_ERROR_FAILED,
96                    "Could not write data to temporary file '%s'", tmpfile);
97       goto done;
98     }
99   close (fd);
100   command[3] = tmpfile;
101
102   /* run diff command */
103   g_spawn_sync (NULL, 
104                 (char **) command,
105                 NULL,
106                 G_SPAWN_SEARCH_PATH,
107                 NULL, NULL,
108                 &diff,
109                 NULL, NULL,
110                 error);
111
112 done:
113   g_unlink (tmpfile);
114   g_free (tmpfile);
115
116   return diff;
117 }
118
119 static void
120 parsing_error_cb (GtkCssProvider *provider,
121                   const gchar     *path,
122                   guint            line,
123                   guint            position,
124                   const GError *   error,
125                   GString *        errors)
126 {
127   char *basename;
128
129   g_assert (path);
130   g_assert (line > 0);
131
132   basename = g_path_get_basename (path);
133
134   g_string_append_printf (errors,
135                           "%s:%u: error: %s %u\n",
136                           basename, line,
137                           g_quark_to_string (error->domain),
138                           error->code);
139
140   g_free (basename);
141 }
142
143 static void
144 test_css_file (GFile *file)
145 {
146   GtkCssProvider *provider;
147   char *css, *diff;
148   char *css_file, *reference_file, *errors_file;
149   GString *errors;
150   GError *error = NULL;
151
152   css_file = g_file_get_path (file);
153   errors = g_string_new ("");
154
155   provider = gtk_css_provider_new ();
156   g_signal_connect (provider, 
157                     "parsing-error",
158                     G_CALLBACK (parsing_error_cb),
159                     errors);
160   gtk_css_provider_load_from_path (provider,
161                                    css_file,
162                                    NULL);
163
164   css = gtk_css_provider_to_string (provider);
165
166   reference_file = test_get_reference_file (css_file);
167
168   diff = diff_with_file (reference_file, css, -1, &error);
169   g_assert_no_error (error);
170
171   if (diff && diff[0])
172     {
173       g_test_message ("%s", diff);
174       g_assert_not_reached ();
175     }
176
177   g_free (css);
178   g_free (reference_file);
179
180   errors_file = test_get_errors_file (css_file);
181
182   if (errors_file)
183     {
184       diff = diff_with_file (errors_file, errors->str, errors->len, &error);
185       g_assert_no_error (error);
186
187       if (diff && diff[0])
188         {
189           g_test_message ("%s", diff);
190           g_assert_not_reached ();
191         }
192     }
193   else if (errors->str[0])
194     {
195       g_test_message ("Unexpected errors:\n%s", errors->str);
196       g_assert_not_reached ();
197     }
198
199   g_free (errors_file);
200   g_string_free (errors, TRUE);
201
202   g_free (diff);
203   g_free (css_file);
204 }
205
206 int
207 main (int argc, char **argv)
208 {
209   const char *basedir;
210   GError *error = NULL;
211   GFile *dir;
212   GFileEnumerator *enumerator;
213   GFileInfo *info;
214
215   gtk_test_init (&argc, &argv);
216
217   /* Add a bunch of properties so we can test that we parse them properly */
218   gtk_style_properties_register_property (NULL,
219                                           g_param_spec_boolean ("boolean-property",
220                                                                 "boolean property",
221                                                                 "test boolean properties",
222                                                                 TRUE,
223                                                                 G_PARAM_READABLE));
224   gtk_style_properties_register_property (NULL,
225                                           g_param_spec_int ("int-property",
226                                                             "int property",
227                                                             "test int properties",
228                                                             G_MININT, G_MAXINT, 0,
229                                                             G_PARAM_READABLE));
230   gtk_style_properties_register_property (NULL,
231                                           g_param_spec_uint ("uint-property",
232                                                              "uint property",
233                                                              "test uint properties",
234                                                              0, G_MAXUINT, 0,
235                                                              G_PARAM_READABLE));
236   gtk_style_properties_register_property (NULL,
237                                           g_param_spec_float ("float-property",
238                                                               "float property",
239                                                               "test float properties",
240                                                               -G_MAXFLOAT, G_MAXFLOAT, 0.0f,
241                                                               G_PARAM_READABLE));
242   gtk_style_properties_register_property (NULL,
243                                           g_param_spec_double ("double-property",
244                                                                "double property",
245                                                                "test double properties",
246                                                                -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
247                                                                G_PARAM_READABLE));
248   gtk_style_properties_register_property (NULL,
249                                           g_param_spec_string ("string-property",
250                                                                "string property",
251                                                                "test string properties",
252                                                                NULL,
253                                                                G_PARAM_READABLE));
254   gtk_style_properties_register_property (NULL,
255                                           g_param_spec_boxed ("rgba-property",
256                                                               "rgba property",
257                                                               "test rgba properties",
258                                                               GDK_TYPE_RGBA,
259                                                               G_PARAM_READABLE));
260   gtk_style_properties_register_property (NULL,
261                                           g_param_spec_boxed ("color-property",
262                                                               "color property",
263                                                               "test color properties",
264                                                               GDK_TYPE_COLOR,
265                                                               G_PARAM_READABLE));
266   gtk_style_properties_register_property (NULL,
267                                           g_param_spec_boxed ("border-property",
268                                                               "border property",
269                                                               "test border properties",
270                                                               GTK_TYPE_BORDER,
271                                                               G_PARAM_READABLE));
272   gtk_style_properties_register_property (NULL,
273                                           g_param_spec_boxed ("font-property",
274                                                               "font property",
275                                                               "test font properties",
276                                                               PANGO_TYPE_FONT_DESCRIPTION,
277                                                               G_PARAM_READABLE));
278 #if 0
279   /* not public API, use transition instead */
280   gtk_style_properties_register_property (NULL,
281                                           g_param_spec_boxed ("animation-property",
282                                                               "animation property",
283                                                               "test animation properties",
284                                                               GTK_TYPE_ANIMATION_DESCRIPTION,
285                                                               G_PARAM_READABLE));
286 #endif
287   gtk_style_properties_register_property (NULL,
288                                           g_param_spec_object ("engine-property",
289                                                                "engine property",
290                                                                "test theming engine properties",
291                                                                GTK_TYPE_THEMING_ENGINE,
292                                                                G_PARAM_READABLE));
293   gtk_style_properties_register_property (NULL,
294                                           g_param_spec_enum ("enum-property",
295                                                              "enum property",
296                                                              "test enum properties",
297                                                              GTK_TYPE_SHADOW_TYPE,
298                                                              0,
299                                                              G_PARAM_READABLE));
300   gtk_style_properties_register_property (NULL,
301                                           g_param_spec_flags ("flags-property",
302                                                               "flags property",
303                                                               "test flags properties",
304                                                               GTK_TYPE_STATE_FLAGS,
305                                                               GTK_STATE_FLAG_NORMAL,
306                                                               G_PARAM_READABLE));
307
308   if (g_getenv ("srcdir"))
309     basedir = g_getenv ("srcdir");
310   else
311     basedir = ".";
312     
313   dir = g_file_new_for_path (basedir);
314   enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
315   g_assert_no_error (error);
316
317   while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
318     {
319       GFile *file;
320       const char *filename;
321
322       filename = g_file_info_get_name (info);
323
324       if (!g_str_has_suffix (filename, ".css") ||
325           g_str_has_suffix (filename, ".out.css") ||
326           g_str_has_suffix (filename, ".ref.css"))
327         {
328           g_object_unref (info);
329           continue;
330         }
331
332       file = g_file_get_child (dir, filename);
333
334       g_test_add_vtable (g_file_get_path (file),
335                          0,
336                          file,
337                          NULL,
338                          (GTestFixtureFunc) test_css_file,
339                          (GTestFixtureFunc) g_object_unref);
340
341       g_object_unref (info);
342     }
343   
344   g_assert_no_error (error);
345   g_object_unref (enumerator);
346
347   return g_test_run ();
348 }
349