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);
201 return G_SOURCE_REMOVE;
205 check_for_draw (GdkEvent *event, gpointer loop)
207 if (event->type == GDK_EXPOSE)
209 g_idle_add (quit_when_idle, loop);
210 gdk_event_handler_set ((GdkEventFunc) gtk_main_do_event, NULL, NULL);
213 gtk_main_do_event (event);
216 static cairo_surface_t *
217 snapshot_widget (GtkWidget *widget, SnapshotMode mode)
219 cairo_surface_t *surface;
224 g_assert (gtk_widget_get_realized (widget));
226 surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
228 gtk_widget_get_allocated_width (widget),
229 gtk_widget_get_allocated_height (widget));
231 loop = g_main_loop_new (NULL, FALSE);
232 /* We wait until the widget is drawn for the first time.
233 * We can not wait for a GtkWidget::draw event, because that might not
234 * happen if the window is fully obscured by windowed child widgets.
235 * Alternatively, we could wait for an expose event on widget's window.
236 * Both of these are rather hairy, not sure what's best. */
237 gdk_event_handler_set (check_for_draw, loop, NULL);
238 g_main_loop_run (loop);
240 cr = cairo_create (surface);
244 case SNAPSHOT_WINDOW:
246 GdkWindow *window = gtk_widget_get_window (widget);
247 if (gdk_window_get_window_type (window) == GDK_WINDOW_TOPLEVEL ||
248 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
250 /* give the WM/server some time to sync. They need it.
251 * Also, do use popups instead of toplevls in your tests
252 * whenever you can. */
253 gdk_display_sync (gdk_window_get_display (window));
254 g_timeout_add (500, quit_when_idle, loop);
255 g_main_loop_run (loop);
257 gdk_cairo_set_source_window (cr, window, 0, 0);
262 bg = gdk_window_get_background_pattern (gtk_widget_get_window (widget));
265 cairo_set_source (cr, bg);
268 gtk_widget_draw (widget, cr);
271 g_assert_not_reached();
276 g_main_loop_unref (loop);
277 gtk_widget_destroy (widget);
282 static cairo_surface_t *
283 snapshot_ui_file (const char *ui_file)
287 GError *error = NULL;
289 builder = gtk_builder_new ();
290 gtk_builder_add_from_file (builder, ui_file, &error);
291 g_assert_no_error (error);
292 window = builder_get_toplevel (builder);
293 g_object_unref (builder);
296 gtk_widget_show (window);
298 return snapshot_widget (window, SNAPSHOT_WINDOW);
302 save_image (cairo_surface_t *surface,
303 const char *test_name,
304 const char *extension)
306 char *filename = get_output_file (test_name, extension);
308 g_test_message ("Storing test result image at %s", filename);
309 g_assert (cairo_surface_write_to_png (surface, filename) == CAIRO_STATUS_SUCCESS);
315 get_surface_size (cairo_surface_t *surface,
322 cr = cairo_create (surface);
323 if (!gdk_cairo_get_clip_rectangle (cr, &area))
325 g_assert_not_reached ();
328 g_assert (area.x == 0 && area.y == 0);
329 g_assert (area.width > 0 && area.height > 0);
332 *height = area.height;
335 static cairo_surface_t *
336 coerce_surface_for_comparison (cairo_surface_t *surface,
340 cairo_surface_t *coerced;
343 coerced = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
346 cr = cairo_create (coerced);
348 cairo_set_source_surface (cr, surface, 0, 0);
349 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
353 cairo_surface_destroy (surface);
355 g_assert (cairo_surface_status (coerced) == CAIRO_STATUS_SUCCESS);
360 /* Compares two CAIRO_FORMAT_ARGB32 buffers, returning NULL if the
361 * buffers are equal or a surface containing a diff between the two
364 * This function should be rewritten to compare all formats supported by
365 * cairo_format_t instead of taking a mask as a parameter.
367 * This function is originally from cairo:test/buffer-diff.c.
368 * Copyright © 2004 Richard D. Worth
370 static cairo_surface_t *
371 buffer_diff_core (const guchar *buf_a,
379 guchar *buf_diff = NULL;
381 cairo_surface_t *diff = NULL;
383 for (y = 0; y < height; y++)
385 const guint32 *row_a = (const guint32 *) (buf_a + y * stride_a);
386 const guint32 *row_b = (const guint32 *) (buf_b + y * stride_b);
387 guint32 *row = (guint32 *) (buf_diff + y * stride_diff);
389 for (x = 0; x < width; x++)
392 guint32 diff_pixel = 0;
394 /* check if the pixels are the same */
395 if (row_a[x] == row_b[x])
400 diff = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
403 g_assert (cairo_surface_status (diff) == CAIRO_STATUS_SUCCESS);
404 buf_diff = cairo_image_surface_get_data (diff);
405 stride_diff = cairo_image_surface_get_stride (diff);
406 row = (guint32 *) (buf_diff + y * stride_diff);
409 /* calculate a difference value for all 4 channels */
410 for (channel = 0; channel < 4; channel++)
412 int value_a = (row_a[x] >> (channel*8)) & 0xff;
413 int value_b = (row_b[x] >> (channel*8)) & 0xff;
416 diff = ABS (value_a - value_b);
417 diff *= 4; /* emphasize */
419 diff += 128; /* make sure it's visible */
422 diff_pixel |= diff << (channel*8);
425 if ((diff_pixel & 0x00ffffff) == 0)
427 /* alpha only difference, convert to luminance */
428 guint8 alpha = diff_pixel >> 24;
429 diff_pixel = alpha * 0x010101;
439 static cairo_surface_t *
440 compare_surfaces (const char *test_file,
441 cairo_surface_t *surface1,
442 cairo_surface_t *surface2)
444 int w1, h1, w2, h2, w, h;
445 cairo_surface_t *diff;
447 get_surface_size (surface1, &w1, &h1);
448 get_surface_size (surface2, &w2, &h2);
451 surface1 = coerce_surface_for_comparison (surface1, w, h);
452 surface2 = coerce_surface_for_comparison (surface2, w, h);
454 diff = buffer_diff_core (cairo_image_surface_get_data (surface1),
455 cairo_image_surface_get_stride (surface1),
456 cairo_image_surface_get_data (surface2),
457 cairo_image_surface_get_stride (surface2),
464 test_ui_file (GFile *file)
466 char *ui_file, *reference_file;
467 cairo_surface_t *ui_image, *reference_image, *diff_image;
468 GtkStyleProvider *provider;
470 ui_file = g_file_get_path (file);
472 provider = add_extra_css (ui_file, ".css");
474 ui_image = snapshot_ui_file (ui_file);
476 reference_file = get_test_file (ui_file, ".ref.ui", TRUE);
478 reference_image = snapshot_ui_file (reference_file);
481 reference_image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
482 g_test_message ("No reference image.");
485 g_free (reference_file);
487 diff_image = compare_surfaces (ui_file, ui_image, reference_image);
489 save_image (ui_image, ui_file, ".out.png");
490 save_image (reference_image, ui_file, ".ref.png");
493 save_image (diff_image, ui_file, ".diff.png");
497 remove_extra_css (provider);
501 add_test_for_file (GFile *file)
503 g_test_add_vtable (g_file_get_path (file),
507 (GTestFixtureFunc) test_ui_file,
508 (GTestFixtureFunc) g_object_unref);
512 compare_files (gconstpointer a, gconstpointer b)
514 GFile *file1 = G_FILE (a);
515 GFile *file2 = G_FILE (b);
519 path1 = g_file_get_path (file1);
520 path2 = g_file_get_path (file2);
522 result = strcmp (path1, path2);
531 add_tests_for_files_in_directory (GFile *dir)
533 GFileEnumerator *enumerator;
536 GError *error = NULL;
538 enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
539 g_assert_no_error (error);
542 while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
544 const char *filename;
546 filename = g_file_info_get_name (info);
548 if (!g_str_has_suffix (filename, ".ui") ||
549 g_str_has_suffix (filename, ".ref.ui"))
551 g_object_unref (info);
555 files = g_list_prepend (files, g_file_get_child (dir, filename));
557 g_object_unref (info);
560 g_assert_no_error (error);
561 g_object_unref (enumerator);
563 files = g_list_sort (files, compare_files);
564 g_list_foreach (files, (GFunc) add_test_for_file, NULL);
565 g_list_free_full (files, g_object_unref);
569 main (int argc, char **argv)
571 if (!parse_command_line (&argc, &argv))
579 if (g_getenv ("srcdir"))
580 basedir = g_getenv ("srcdir");
584 dir = g_file_new_for_path (basedir);
586 add_tests_for_files_in_directory (dir);
588 g_object_unref (dir);
594 for (i = 1; i < argc; i++)
596 GFile *file = g_file_new_for_commandline_arg (argv[i]);
598 add_test_for_file (file);
600 g_object_unref (file);
604 return g_test_run ();