2 * Copyright (C) 2011 Red Hat Inc.
5 * Benjamin Otte <otte@redhat.com>
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.
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.
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.
26 #include <glib/gstdio.h>
30 test_get_reference_file (const char *css_file)
32 GString *file = g_string_new (NULL);
34 if (g_str_has_suffix (css_file, ".css"))
35 g_string_append_len (file, css_file, strlen (css_file) - 4);
37 g_string_append (file, css_file);
39 g_string_append (file, ".ref.css");
41 if (!g_file_test (file->str, G_FILE_TEST_EXISTS))
43 g_string_free (file, TRUE);
44 return g_strdup (css_file);
47 return g_string_free (file, FALSE);
51 test_get_errors_file (const char *css_file)
53 GString *file = g_string_new (NULL);
55 if (g_str_has_suffix (css_file, ".css"))
56 g_string_append_len (file, css_file, strlen (css_file) - 4);
58 g_string_append (file, css_file);
60 g_string_append (file, ".errors");
62 if (!g_file_test (file->str, G_FILE_TEST_EXISTS))
64 g_string_free (file, TRUE);
68 return g_string_free (file, FALSE);
72 diff_with_file (const char *file1,
77 const char *command[] = { "diff", "-u", file1, NULL, NULL };
86 /* write the text buffer to a temporary file */
87 fd = g_file_open_tmp (NULL, &tmpfile, error);
91 if (write (fd, text, len) != (int) len)
95 G_FILE_ERROR, G_FILE_ERROR_FAILED,
96 "Could not write data to temporary file '%s'", tmpfile);
100 command[3] = tmpfile;
102 /* run diff command */
120 append_error_value (GString *string,
124 GEnumClass *enum_class;
125 GEnumValue *enum_value;
127 enum_class = g_type_class_ref (enum_type);
128 enum_value = g_enum_get_value (enum_class, value);
130 g_string_append (string, enum_value->value_name);
132 g_type_class_unref (enum_class);
136 parsing_error_cb (GtkCssProvider *provider,
140 const GError * error,
148 basename = g_path_get_basename (path);
149 g_string_append_printf (errors,
154 if (error->domain == GTK_CSS_PROVIDER_ERROR)
155 append_error_value (errors, GTK_TYPE_CSS_PROVIDER_ERROR, error->code);
157 g_string_append_printf (errors,
159 g_quark_to_string (error->domain),
162 g_string_append_c (errors, '\n');
166 test_css_file (GFile *file)
168 GtkCssProvider *provider;
170 char *css_file, *reference_file, *errors_file;
172 GError *error = NULL;
174 css_file = g_file_get_path (file);
175 errors = g_string_new ("");
177 provider = gtk_css_provider_new ();
178 g_signal_connect (provider,
180 G_CALLBACK (parsing_error_cb),
182 gtk_css_provider_load_from_path (provider,
186 css = gtk_css_provider_to_string (provider);
188 reference_file = test_get_reference_file (css_file);
190 diff = diff_with_file (reference_file, css, -1, &error);
191 g_assert_no_error (error);
195 g_test_message ("%s", diff);
196 g_assert_not_reached ();
200 g_free (reference_file);
202 errors_file = test_get_errors_file (css_file);
206 diff = diff_with_file (errors_file, errors->str, errors->len, &error);
207 g_assert_no_error (error);
211 g_test_message ("%s", diff);
212 g_assert_not_reached ();
215 else if (errors->str[0])
217 g_test_message ("Unexpected errors:\n%s", errors->str);
218 g_assert_not_reached ();
221 g_free (errors_file);
222 g_string_free (errors, TRUE);
229 main (int argc, char **argv)
232 GError *error = NULL;
234 GFileEnumerator *enumerator;
237 gtk_test_init (&argc, &argv);
239 /* Add a bunch of properties so we can test that we parse them properly */
240 gtk_style_properties_register_property (NULL,
241 g_param_spec_boolean ("boolean-property",
243 "test boolean properties",
246 gtk_style_properties_register_property (NULL,
247 g_param_spec_int ("int-property",
249 "test int properties",
250 G_MININT, G_MAXINT, 0,
252 gtk_style_properties_register_property (NULL,
253 g_param_spec_uint ("uint-property",
255 "test uint properties",
258 gtk_style_properties_register_property (NULL,
259 g_param_spec_float ("float-property",
261 "test float properties",
262 -G_MAXFLOAT, G_MAXFLOAT, 0.0f,
264 gtk_style_properties_register_property (NULL,
265 g_param_spec_double ("double-property",
267 "test double properties",
268 -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
270 gtk_style_properties_register_property (NULL,
271 g_param_spec_string ("string-property",
273 "test string properties",
276 gtk_style_properties_register_property (NULL,
277 g_param_spec_boxed ("rgba-property",
279 "test rgba properties",
282 gtk_style_properties_register_property (NULL,
283 g_param_spec_boxed ("color-property",
285 "test color properties",
288 gtk_style_properties_register_property (NULL,
289 g_param_spec_boxed ("border-property",
291 "test border properties",
294 gtk_style_properties_register_property (NULL,
295 g_param_spec_boxed ("font-property",
297 "test font properties",
298 PANGO_TYPE_FONT_DESCRIPTION,
301 /* not public API, use transition instead */
302 gtk_style_properties_register_property (NULL,
303 g_param_spec_boxed ("animation-property",
304 "animation property",
305 "test animation properties",
306 GTK_TYPE_ANIMATION_DESCRIPTION,
309 gtk_style_properties_register_property (NULL,
310 g_param_spec_object ("engine-property",
312 "test theming engine properties",
313 GTK_TYPE_THEMING_ENGINE,
315 gtk_style_properties_register_property (NULL,
316 g_param_spec_enum ("enum-property",
318 "test enum properties",
319 GTK_TYPE_SHADOW_TYPE,
322 gtk_style_properties_register_property (NULL,
323 g_param_spec_flags ("flags-property",
325 "test flags properties",
326 GTK_TYPE_STATE_FLAGS,
327 GTK_STATE_FLAG_NORMAL,
330 if (g_getenv ("srcdir"))
331 basedir = g_getenv ("srcdir");
335 dir = g_file_new_for_path (basedir);
336 enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
337 g_assert_no_error (error);
339 while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
342 const char *filename;
344 filename = g_file_info_get_name (info);
346 if (!g_str_has_suffix (filename, ".css") ||
347 g_str_has_suffix (filename, ".out.css") ||
348 g_str_has_suffix (filename, ".ref.css"))
350 g_object_unref (info);
354 file = g_file_get_child (dir, filename);
356 g_test_add_vtable (g_file_get_path (file),
360 (GTestFixtureFunc) test_css_file,
361 (GTestFixtureFunc) g_object_unref);
363 g_object_unref (info);
366 g_assert_no_error (error);
367 g_object_unref (enumerator);
369 return g_test_run ();