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, see <http://www.gnu.org/licenses/>.
24 #include <glib/gstdio.h>
32 /* This is exactly the style information you've been looking for */
33 #define GTK_STYLE_PROVIDER_PRIORITY_FORCE G_MAXUINT
35 static char *arg_output_dir = NULL;
37 static const GOptionEntry test_args[] = {
38 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &arg_output_dir,
39 "Directory to save image files to", "DIR" },
44 parse_command_line (int *argc, char ***argv)
47 GOptionContext *context;
49 context = g_option_context_new ("- run GTK reftests");
50 g_option_context_add_main_entries (context, test_args, NULL);
51 g_option_context_set_ignore_unknown_options (context, TRUE);
53 if (!g_option_context_parse (context, argc, argv, &error))
55 g_print ("option parsing failed: %s\n", error->message);
59 gtk_test_init (argc, argv);
67 static const char *output_dir = NULL;
75 GFile *file = g_file_new_for_commandline_arg (arg_output_dir);
76 output_dir = g_file_get_path (file);
77 g_object_unref (file);
81 output_dir = g_get_tmp_dir ();
84 if (!g_file_test (output_dir, G_FILE_TEST_EXISTS))
88 file = g_file_new_for_path (output_dir);
89 g_assert (g_file_make_directory_with_parents (file, NULL, &error));
90 g_assert_no_error (error);
91 g_object_unref (file);
98 get_output_file (const char *test_file,
99 const char *extension)
101 const char *output_dir = get_output_dir ();
104 base = g_path_get_basename (test_file);
105 if (g_str_has_suffix (base, ".ui"))
106 base[strlen (base) - strlen (".ui")] = '\0';
108 result = g_strconcat (output_dir, G_DIR_SEPARATOR_S, base, extension, NULL);
115 get_test_file (const char *test_file,
116 const char *extension,
119 GString *file = g_string_new (NULL);
121 if (g_str_has_suffix (test_file, ".ui"))
122 g_string_append_len (file, test_file, strlen (test_file) - strlen (".ui"));
124 g_string_append (file, test_file);
126 g_string_append (file, extension);
129 !g_file_test (file->str, G_FILE_TEST_EXISTS))
131 g_string_free (file, TRUE);
135 return g_string_free (file, FALSE);
138 static GtkStyleProvider *
139 add_extra_css (const char *testname,
140 const char *extension)
142 GtkStyleProvider *provider = NULL;
145 css_file = get_test_file (testname, extension, TRUE);
146 if (css_file == NULL)
149 provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ());
150 gtk_css_provider_load_from_path (GTK_CSS_PROVIDER (provider),
153 gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
155 GTK_STYLE_PROVIDER_PRIORITY_FORCE);
163 remove_extra_css (GtkStyleProvider *provider)
165 if (provider == NULL)
168 gtk_style_context_remove_provider_for_screen (gdk_screen_get_default (),
173 builder_get_toplevel (GtkBuilder *builder)
176 GtkWidget *window = NULL;
178 list = gtk_builder_get_objects (builder);
179 for (walk = list; walk; walk = walk->next)
181 if (GTK_IS_WINDOW (walk->data) &&
182 gtk_widget_get_parent (walk->data) == NULL)
195 quit_when_idle (gpointer loop)
197 g_main_loop_quit (loop);
199 return G_SOURCE_REMOVE;
203 check_for_draw (GdkEvent *event, gpointer loop)
205 if (event->type == GDK_EXPOSE)
207 g_idle_add (quit_when_idle, loop);
208 gdk_event_handler_set ((GdkEventFunc) gtk_main_do_event, NULL, NULL);
211 gtk_main_do_event (event);
214 static cairo_surface_t *
215 snapshot_widget (GtkWidget *widget, SnapshotMode mode)
217 cairo_surface_t *surface;
222 g_assert (gtk_widget_get_realized (widget));
224 surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
226 gtk_widget_get_allocated_width (widget),
227 gtk_widget_get_allocated_height (widget));
229 loop = g_main_loop_new (NULL, FALSE);
230 /* We wait until the widget is drawn for the first time.
231 * We can not wait for a GtkWidget::draw event, because that might not
232 * happen if the window is fully obscured by windowed child widgets.
233 * Alternatively, we could wait for an expose event on widget's window.
234 * Both of these are rather hairy, not sure what's best. */
235 gdk_event_handler_set (check_for_draw, loop, NULL);
236 g_main_loop_run (loop);
238 cr = cairo_create (surface);
242 case SNAPSHOT_WINDOW:
244 GdkWindow *window = gtk_widget_get_window (widget);
245 if (gdk_window_get_window_type (window) == GDK_WINDOW_TOPLEVEL ||
246 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
248 /* give the WM/server some time to sync. They need it.
249 * Also, do use popups instead of toplevls in your tests
250 * whenever you can. */
251 gdk_display_sync (gdk_window_get_display (window));
252 g_timeout_add (500, quit_when_idle, loop);
253 g_main_loop_run (loop);
255 gdk_cairo_set_source_window (cr, window, 0, 0);
260 bg = gdk_window_get_background_pattern (gtk_widget_get_window (widget));
263 cairo_set_source (cr, bg);
266 gtk_widget_draw (widget, cr);
269 g_assert_not_reached();
274 g_main_loop_unref (loop);
275 gtk_widget_destroy (widget);
280 static cairo_surface_t *
281 snapshot_ui_file (const char *ui_file)
285 GError *error = NULL;
287 builder = gtk_builder_new ();
288 gtk_builder_add_from_file (builder, ui_file, &error);
289 g_assert_no_error (error);
290 window = builder_get_toplevel (builder);
291 g_object_unref (builder);
294 gtk_widget_show (window);
296 return snapshot_widget (window, SNAPSHOT_WINDOW);
300 save_image (cairo_surface_t *surface,
301 const char *test_name,
302 const char *extension)
304 char *filename = get_output_file (test_name, extension);
306 g_test_message ("Storing test result image at %s", filename);
307 g_assert (cairo_surface_write_to_png (surface, filename) == CAIRO_STATUS_SUCCESS);
313 get_surface_size (cairo_surface_t *surface,
320 cr = cairo_create (surface);
321 if (!gdk_cairo_get_clip_rectangle (cr, &area))
323 g_assert_not_reached ();
326 g_assert (area.x == 0 && area.y == 0);
327 g_assert (area.width > 0 && area.height > 0);
330 *height = area.height;
333 static cairo_surface_t *
334 coerce_surface_for_comparison (cairo_surface_t *surface,
338 cairo_surface_t *coerced;
341 coerced = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
344 cr = cairo_create (coerced);
346 cairo_set_source_surface (cr, surface, 0, 0);
347 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
351 cairo_surface_destroy (surface);
353 g_assert (cairo_surface_status (coerced) == CAIRO_STATUS_SUCCESS);
358 /* Compares two CAIRO_FORMAT_ARGB32 buffers, returning NULL if the
359 * buffers are equal or a surface containing a diff between the two
362 * This function should be rewritten to compare all formats supported by
363 * cairo_format_t instead of taking a mask as a parameter.
365 * This function is originally from cairo:test/buffer-diff.c.
366 * Copyright © 2004 Richard D. Worth
368 static cairo_surface_t *
369 buffer_diff_core (const guchar *buf_a,
377 guchar *buf_diff = NULL;
379 cairo_surface_t *diff = NULL;
381 for (y = 0; y < height; y++)
383 const guint32 *row_a = (const guint32 *) (buf_a + y * stride_a);
384 const guint32 *row_b = (const guint32 *) (buf_b + y * stride_b);
385 guint32 *row = (guint32 *) (buf_diff + y * stride_diff);
387 for (x = 0; x < width; x++)
390 guint32 diff_pixel = 0;
392 /* check if the pixels are the same */
393 if (row_a[x] == row_b[x])
398 diff = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
401 g_assert (cairo_surface_status (diff) == CAIRO_STATUS_SUCCESS);
402 buf_diff = cairo_image_surface_get_data (diff);
403 stride_diff = cairo_image_surface_get_stride (diff);
404 row = (guint32 *) (buf_diff + y * stride_diff);
407 /* calculate a difference value for all 4 channels */
408 for (channel = 0; channel < 4; channel++)
410 int value_a = (row_a[x] >> (channel*8)) & 0xff;
411 int value_b = (row_b[x] >> (channel*8)) & 0xff;
414 diff = ABS (value_a - value_b);
415 diff *= 4; /* emphasize */
417 diff += 128; /* make sure it's visible */
420 diff_pixel |= diff << (channel*8);
423 if ((diff_pixel & 0x00ffffff) == 0)
425 /* alpha only difference, convert to luminance */
426 guint8 alpha = diff_pixel >> 24;
427 diff_pixel = alpha * 0x010101;
437 static cairo_surface_t *
438 compare_surfaces (const char *test_file,
439 cairo_surface_t *surface1,
440 cairo_surface_t *surface2)
442 int w1, h1, w2, h2, w, h;
443 cairo_surface_t *diff;
445 get_surface_size (surface1, &w1, &h1);
446 get_surface_size (surface2, &w2, &h2);
449 surface1 = coerce_surface_for_comparison (surface1, w, h);
450 surface2 = coerce_surface_for_comparison (surface2, w, h);
452 diff = buffer_diff_core (cairo_image_surface_get_data (surface1),
453 cairo_image_surface_get_stride (surface1),
454 cairo_image_surface_get_data (surface2),
455 cairo_image_surface_get_stride (surface2),
462 test_ui_file (GFile *file)
464 char *ui_file, *reference_file;
465 cairo_surface_t *ui_image, *reference_image, *diff_image;
466 GtkStyleProvider *provider;
468 ui_file = g_file_get_path (file);
470 provider = add_extra_css (ui_file, ".css");
472 ui_image = snapshot_ui_file (ui_file);
474 reference_file = get_test_file (ui_file, ".ref.ui", TRUE);
476 reference_image = snapshot_ui_file (reference_file);
479 reference_image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
480 g_test_message ("No reference image.");
483 g_free (reference_file);
485 diff_image = compare_surfaces (ui_file, ui_image, reference_image);
487 save_image (ui_image, ui_file, ".out.png");
488 save_image (reference_image, ui_file, ".ref.png");
491 save_image (diff_image, ui_file, ".diff.png");
495 remove_extra_css (provider);
499 add_test_for_file (GFile *file)
501 g_test_add_vtable (g_file_get_path (file),
505 (GTestFixtureFunc) test_ui_file,
506 (GTestFixtureFunc) g_object_unref);
510 compare_files (gconstpointer a, gconstpointer b)
512 GFile *file1 = G_FILE (a);
513 GFile *file2 = G_FILE (b);
517 path1 = g_file_get_path (file1);
518 path2 = g_file_get_path (file2);
520 result = strcmp (path1, path2);
529 add_tests_for_files_in_directory (GFile *dir)
531 GFileEnumerator *enumerator;
534 GError *error = NULL;
536 enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
537 g_assert_no_error (error);
540 while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
542 const char *filename;
544 filename = g_file_info_get_name (info);
546 if (!g_str_has_suffix (filename, ".ui") ||
547 g_str_has_suffix (filename, ".ref.ui"))
549 g_object_unref (info);
553 files = g_list_prepend (files, g_file_get_child (dir, filename));
555 g_object_unref (info);
558 g_assert_no_error (error);
559 g_object_unref (enumerator);
561 files = g_list_sort (files, compare_files);
562 g_list_foreach (files, (GFunc) add_test_for_file, NULL);
563 g_list_free_full (files, g_object_unref);
567 main (int argc, char **argv)
569 /* I don't want to fight fuzzy scaling algorithms in GPUs,
570 * so unles you explicitly set it to something else, we
571 * will use Cairo's image surface.
573 g_setenv ("GDK_RENDERING", "image", FALSE);
575 if (!parse_command_line (&argc, &argv))
583 if (g_getenv ("srcdir"))
584 basedir = g_getenv ("srcdir");
588 dir = g_file_new_for_path (basedir);
590 add_tests_for_files_in_directory (dir);
592 g_object_unref (dir);
598 for (i = 1; i < argc; i++)
600 GFile *file = g_file_new_for_commandline_arg (argv[i]);
602 add_test_for_file (file);
604 g_object_unref (file);
608 return g_test_run ();