2 * Copyright (C) 2011 Red Hat Inc.
5 * Benjamin Otte <otte@gnome.org>
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>
34 /* This is exactly the style information you've been looking for */
35 #define GTK_STYLE_PROVIDER_PRIORITY_FORCE G_MAXUINT
37 static char *arg_output_dir = NULL;
39 static const GOptionEntry test_args[] = {
40 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &arg_output_dir,
41 "Directory to save image files to", "DIR" },
46 parse_command_line (int *argc, char ***argv)
49 GOptionContext *context;
51 context = g_option_context_new ("- run GTK reftests");
52 g_option_context_add_main_entries (context, test_args, NULL);
53 g_option_context_set_ignore_unknown_options (context, TRUE);
55 if (!g_option_context_parse (context, argc, argv, &error))
57 g_print ("option parsing failed: %s\n", error->message);
61 gtk_test_init (argc, argv);
69 static const char *output_dir = NULL;
77 GFile *file = g_file_new_for_commandline_arg (arg_output_dir);
78 output_dir = g_file_get_path (file);
79 g_object_unref (file);
83 output_dir = g_get_tmp_dir ();
86 if (!g_file_test (output_dir, G_FILE_TEST_EXISTS))
90 file = g_file_new_for_path (output_dir);
91 g_assert (g_file_make_directory_with_parents (file, NULL, &error));
92 g_assert_no_error (error);
93 g_object_unref (file);
100 get_output_file (const char *test_file,
101 const char *extension)
103 const char *output_dir = get_output_dir ();
106 base = g_path_get_basename (test_file);
107 if (g_str_has_suffix (base, ".ui"))
108 base[strlen (base) - strlen (".ui")] = '\0';
110 result = g_strconcat (output_dir, G_DIR_SEPARATOR_S, base, extension, NULL);
117 get_test_file (const char *test_file,
118 const char *extension,
121 GString *file = g_string_new (NULL);
123 if (g_str_has_suffix (test_file, ".ui"))
124 g_string_append_len (file, test_file, strlen (test_file) - strlen (".ui"));
126 g_string_append (file, test_file);
128 g_string_append (file, extension);
131 !g_file_test (file->str, G_FILE_TEST_EXISTS))
133 g_string_free (file, TRUE);
137 return g_string_free (file, FALSE);
140 static GtkStyleProvider *
141 add_extra_css (const char *testname,
142 const char *extension)
144 GtkStyleProvider *provider = NULL;
147 css_file = get_test_file (testname, extension, TRUE);
148 if (css_file == NULL)
151 provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ());
152 gtk_css_provider_load_from_path (GTK_CSS_PROVIDER (provider),
155 gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
157 GTK_STYLE_PROVIDER_PRIORITY_FORCE);
165 remove_extra_css (GtkStyleProvider *provider)
167 if (provider == NULL)
170 gtk_style_context_remove_provider_for_screen (gdk_screen_get_default (),
175 builder_get_toplevel (GtkBuilder *builder)
178 GtkWidget *window = NULL;
180 list = gtk_builder_get_objects (builder);
181 for (walk = list; walk; walk = walk->next)
183 if (GTK_IS_WINDOW (walk->data) &&
184 gtk_widget_get_parent (walk->data) == NULL)
197 quit_when_idle (gpointer loop)
199 g_main_loop_quit (loop);
204 static cairo_surface_t *
205 snapshot_widget (GtkWidget *widget, SnapshotMode mode)
207 cairo_surface_t *surface;
212 g_assert (gtk_widget_get_realized (widget));
214 surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
216 gtk_widget_get_allocated_width (widget),
217 gtk_widget_get_allocated_height (widget));
219 loop = g_main_loop_new (NULL, FALSE);
220 g_idle_add (quit_when_idle, loop);
221 g_main_loop_run (loop);
223 cr = cairo_create (surface);
227 case SNAPSHOT_WINDOW:
228 gdk_cairo_set_source_window (cr, gtk_widget_get_window (widget), 0, 0);
232 bg = gdk_window_get_background_pattern (gtk_widget_get_window (widget));
235 cairo_set_source (cr, bg);
238 gtk_widget_draw (widget, cr);
241 g_assert_not_reached();
246 g_main_loop_unref (loop);
247 gtk_widget_destroy (widget);
252 static cairo_surface_t *
253 snapshot_ui_file (const char *ui_file)
257 GError *error = NULL;
259 builder = gtk_builder_new ();
260 gtk_builder_add_from_file (builder, ui_file, &error);
261 g_assert_no_error (error);
262 window = builder_get_toplevel (builder);
263 g_object_unref (builder);
266 gtk_widget_show (window);
268 return snapshot_widget (window, SNAPSHOT_WINDOW);
272 save_image (cairo_surface_t *surface,
273 const char *test_name,
274 const char *extension)
276 char *filename = get_output_file (test_name, extension);
278 g_test_message ("Storing test result image at %s", filename);
279 g_assert (cairo_surface_write_to_png (surface, filename) == CAIRO_STATUS_SUCCESS);
285 get_surface_size (cairo_surface_t *surface,
292 cr = cairo_create (surface);
293 if (!gdk_cairo_get_clip_rectangle (cr, &area))
295 g_assert_not_reached ();
298 g_assert (area.x == 0 && area.y == 0);
299 g_assert (area.width > 0 && area.height > 0);
302 *height = area.height;
305 static cairo_surface_t *
306 coerce_surface_for_comparison (cairo_surface_t *surface,
310 cairo_surface_t *coerced;
313 coerced = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
316 cr = cairo_create (coerced);
318 cairo_set_source_surface (cr, surface, 0, 0);
319 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
323 cairo_surface_destroy (surface);
325 g_assert (cairo_surface_status (coerced) == CAIRO_STATUS_SUCCESS);
330 /* Compares two CAIRO_FORMAT_ARGB32 buffers, returning NULL if the
331 * buffers are equal or a surface containing a diff between the two
334 * This function should be rewritten to compare all formats supported by
335 * cairo_format_t instead of taking a mask as a parameter.
337 * This function is originally from cairo:test/buffer-diff.c.
338 * Copyright © 2004 Richard D. Worth
340 static cairo_surface_t *
341 buffer_diff_core (const guchar *buf_a,
349 guchar *buf_diff = NULL;
351 cairo_surface_t *diff = NULL;
353 for (y = 0; y < height; y++)
355 const guint32 *row_a = (const guint32 *) (buf_a + y * stride_a);
356 const guint32 *row_b = (const guint32 *) (buf_b + y * stride_b);
357 guint32 *row = (guint32 *) (buf_diff + y * stride_diff);
359 for (x = 0; x < width; x++)
362 guint32 diff_pixel = 0;
364 /* check if the pixels are the same */
365 if (row_a[x] == row_b[x])
370 diff = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
373 g_assert (cairo_surface_status (diff) == CAIRO_STATUS_SUCCESS);
374 buf_diff = cairo_image_surface_get_data (diff);
375 stride_diff = cairo_image_surface_get_stride (diff);
376 row = (guint32 *) (buf_diff + y * stride_diff);
379 /* calculate a difference value for all 4 channels */
380 for (channel = 0; channel < 4; channel++)
382 int value_a = (row_a[x] >> (channel*8)) & 0xff;
383 int value_b = (row_b[x] >> (channel*8)) & 0xff;
386 diff = ABS (value_a - value_b);
387 diff *= 4; /* emphasize */
389 diff += 128; /* make sure it's visible */
392 diff_pixel |= diff << (channel*8);
395 if ((diff_pixel & 0x00ffffff) == 0)
397 /* alpha only difference, convert to luminance */
398 guint8 alpha = diff_pixel >> 24;
399 diff_pixel = alpha * 0x010101;
409 static cairo_surface_t *
410 compare_surfaces (const char *test_file,
411 cairo_surface_t *surface1,
412 cairo_surface_t *surface2)
414 int w1, h1, w2, h2, w, h;
415 cairo_surface_t *diff;
417 get_surface_size (surface1, &w1, &h1);
418 get_surface_size (surface2, &w2, &h2);
421 surface1 = coerce_surface_for_comparison (surface1, w, h);
422 surface2 = coerce_surface_for_comparison (surface2, w, h);
424 diff = buffer_diff_core (cairo_image_surface_get_data (surface1),
425 cairo_image_surface_get_stride (surface1),
426 cairo_image_surface_get_data (surface2),
427 cairo_image_surface_get_stride (surface2),
434 test_ui_file (GFile *file)
436 char *ui_file, *reference_file;
437 cairo_surface_t *ui_image, *reference_image, *diff_image;
438 GtkStyleProvider *provider;
440 ui_file = g_file_get_path (file);
442 provider = add_extra_css (ui_file, ".css");
444 ui_image = snapshot_ui_file (ui_file);
446 reference_file = get_test_file (ui_file, ".ref.ui", TRUE);
448 reference_image = snapshot_ui_file (reference_file);
451 reference_image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
452 g_test_message ("No reference image.");
455 g_free (reference_file);
457 diff_image = compare_surfaces (ui_file, ui_image, reference_image);
459 save_image (ui_image, ui_file, ".out.png");
460 save_image (reference_image, ui_file, ".ref.png");
463 save_image (diff_image, ui_file, ".diff.png");
467 remove_extra_css (provider);
471 add_test_for_file (GFile *file)
473 g_test_add_vtable (g_file_get_path (file),
477 (GTestFixtureFunc) test_ui_file,
478 (GTestFixtureFunc) g_object_unref);
482 compare_files (gconstpointer a, gconstpointer b)
484 GFile *file1 = G_FILE (a);
485 GFile *file2 = G_FILE (b);
489 path1 = g_file_get_path (file1);
490 path2 = g_file_get_path (file2);
492 result = strcmp (path1, path2);
501 add_tests_for_files_in_directory (GFile *dir)
503 GFileEnumerator *enumerator;
506 GError *error = NULL;
508 enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
509 g_assert_no_error (error);
512 while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
514 const char *filename;
516 filename = g_file_info_get_name (info);
518 if (!g_str_has_suffix (filename, ".ui") ||
519 g_str_has_suffix (filename, ".ref.ui"))
521 g_object_unref (info);
525 files = g_list_prepend (files, g_file_get_child (dir, filename));
527 g_object_unref (info);
530 g_assert_no_error (error);
531 g_object_unref (enumerator);
533 files = g_list_sort (files, compare_files);
534 g_list_foreach (files, (GFunc) add_test_for_file, NULL);
535 g_list_free_full (files, g_object_unref);
539 main (int argc, char **argv)
541 if (!parse_command_line (&argc, &argv))
549 if (g_getenv ("srcdir"))
550 basedir = g_getenv ("srcdir");
554 dir = g_file_new_for_path (basedir);
556 add_tests_for_files_in_directory (dir);
558 g_object_unref (dir);
564 for (i = 1; i < argc; i++)
566 GFile *file = g_file_new_for_commandline_arg (argv[i]);
568 add_test_for_file (file);
570 g_object_unref (file);
574 return g_test_run ();