Debian Codegraph @

testsuite/reftests/gtk-reftest.c

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * Copyright (C) 2011 Red Hat Inc.
3 *
4 * Author:
5 * Benjamin Otte <[email protected]>
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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include "reftest-compare.h"
24 #include "reftest-module.h"
25 #include "reftest-snapshot.h"
26
27 #include <string.h>
28 #include <glib/gstdio.h>
29 #include <gtk/gtk.h>
30
31 #ifdef G_OS_WIN32
32 # include <direct.h>
33 #endif
34
35 typedef enum {
36 SNAPSHOT_WINDOW,
37 SNAPSHOT_DRAW
38 } SnapshotMode;
39
40 /* This is exactly the style information you've been looking for */
41 #define GTK_STYLE_PROVIDER_PRIORITY_FORCE G_MAXUINT
42
43 static char *arg_output_dir = NULL;
44 static char *arg_base_dir = NULL;
45 static char *arg_direction = NULL;
46 static char *arg_compare_dir = NULL;
47
48 static const GOptionEntry test_args[] = {
49 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &arg_output_dir,
50 "Directory to save image files to", "DIR" },
51 { "directory", 'd', 0, G_OPTION_ARG_FILENAME, &arg_base_dir,
52 "Directory to run tests from", "DIR" },
53 { "direction", 0, 0, G_OPTION_ARG_STRING, &arg_direction,
54 "Set text direction", "ltr|rtl" },
55 { "compare-with", 0, 0, G_OPTION_ARG_FILENAME, &arg_compare_dir,
56 "Directory to compare with", "DIR" },
57 { NULL }
58 };
59
60 static gboolean
61 parse_command_line (int *argc, char ***argv)
62 {
63 GError *error = NULL;
64 GOptionContext *context;
65 char *schema_dir;
66
67 context = g_option_context_new ("- run GTK reftests");
68 g_option_context_add_main_entries (context, test_args, NULL);
69 g_option_context_set_ignore_unknown_options (context, TRUE);
70
71 if (!g_option_context_parse (context, argc, argv, &error))
72 {
73 g_print ("option parsing failed: %s\n", error->message);
74 return FALSE;
75 }
76
77 gtk_test_init (argc, argv);
78
79 /* g_test_build_filename must be called after gtk_test_init */
80 schema_dir = g_test_build_filename (G_TEST_BUILT, "", NULL);
81 if (g_getenv ("GTK_TEST_MESON") == NULL)
82 g_setenv ("GSETTINGS_SCHEMA_DIR", schema_dir, TRUE);
83 g_free (schema_dir);
84
85 if (g_strcmp0 (arg_direction, "rtl") == 0)
86 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
87 else if (g_strcmp0 (arg_direction, "ltr") == 0)
88 gtk_widget_set_default_direction (GTK_TEXT_DIR_LTR);
89 else if (arg_direction != NULL)
90 g_printerr ("Invalid argument passed to --direction argument. Valid arguments are 'ltr' and 'rtl'\n");
91
92 g_type_ensure (G_TYPE_THEMED_ICON);
93
94 return TRUE;
95 }
96
97 static const char *
98 get_output_dir (void)
99 {
100 static const char *output_dir = NULL;
101 GError *error = NULL;
102
103 if (output_dir)
104 return output_dir;
105
106 if (arg_output_dir)
107 {
108 GFile *file = g_file_new_for_commandline_arg (arg_output_dir);
109 output_dir = g_file_get_path (file);
110 g_object_unref (file);
111 }
112 else
113 {
114 output_dir = g_get_tmp_dir ();
115 }
116
117 if (!g_file_test (output_dir, G_FILE_TEST_EXISTS))
118 {
119 GFile *file;
120
121 file = g_file_new_for_path (output_dir);
122 g_assert (g_file_make_directory_with_parents (file, NULL, &error));
123 g_assert_no_error (error);
124 g_object_unref (file);
125 }
126
127 return output_dir;
128 }
129
130 static void
131 get_components_of_test_file (const char *test_file,
132 char **directory,
133 char **basename)
134 {
135 if (directory)
136 {
137 *directory = g_path_get_dirname (test_file);
138 }
139
140 if (basename)
141 {
142 char *base = g_path_get_basename (test_file);
143
144 if (g_str_has_suffix (base, ".ui"))
145 base[strlen (base) - strlen (".ui")] = '\0';
146
147 *basename = base;
148 }
149 }
150
151 static char *
152 get_output_file (const char *test_file,
153 const char *extension)
154 {
155 const char *output_dir = get_output_dir ();
156 char *result, *base;
157
158 get_components_of_test_file (test_file, NULL, &base);
159
160 result = g_strconcat (output_dir, G_DIR_SEPARATOR_S, base, extension, NULL);
161 g_free (base);
162
163 return result;
164 }
165
166 static char *
167 get_test_file (const char *test_file,
168 const char *extension,
169 gboolean must_exist)
170 {
171 GString *file = g_string_new (NULL);
172 char *dir, *base;
173
174 get_components_of_test_file (test_file, &dir, &base);
175
176 file = g_string_new (dir);
177 g_string_append (file, G_DIR_SEPARATOR_S);
178 g_string_append (file, base);
179 g_string_append (file, extension);
180
181 g_free (dir);
182 g_free (base);
183
184 if (must_exist &&
185 !g_file_test (file->str, G_FILE_TEST_EXISTS))
186 {
187 g_string_free (file, TRUE);
188 return NULL;
189 }
190
191 return g_string_free (file, FALSE);
192 }
193
194 static char *
195 get_reference_image (const char *ui_file)
196 {
197 char *base;
198 char *reference_image;
199
200 if (!arg_compare_dir)
201 return NULL;
202
203 get_components_of_test_file (ui_file, NULL, &base);
204 reference_image = g_strconcat (arg_compare_dir, G_DIR_SEPARATOR_S, base, ".out.png", NULL);
205 g_free (base);
206
207 if (!g_file_test (reference_image, G_FILE_TEST_EXISTS))
208 {
209 g_free (reference_image);
210 return NULL;
211 }
212
213 return reference_image;
214 }
215
216 static GtkStyleProvider *
217 add_extra_css (const char *testname,
218 const char *extension)
219 {
220 GtkStyleProvider *provider = NULL;
221 char *css_file;
222
223 css_file = get_test_file (testname, extension, TRUE);
224 if (css_file == NULL)
225 return NULL;
226
227 provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ());
228 gtk_css_provider_load_from_path (GTK_CSS_PROVIDER (provider),
229 css_file,
230 NULL);
231 gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
232 provider,
233 GTK_STYLE_PROVIDER_PRIORITY_FORCE);
234
235 g_free (css_file);
236
237 return provider;
238 }
239
240 static void
241 remove_extra_css (GtkStyleProvider *provider)
242 {
243 if (provider == NULL)
244 return;
245
246 gtk_style_context_remove_provider_for_screen (gdk_screen_get_default (),
247 provider);
248 }
249
250 static void
251 save_image (cairo_surface_t *surface,
252 const char *test_name,
253 const char *extension)
254 {
255 char *filename = get_output_file (test_name, extension);
256
257 g_test_message ("Storing test result image at %s", filename);
258 g_assert (cairo_surface_write_to_png (surface, filename) == CAIRO_STATUS_SUCCESS);
259
260 g_free (filename);
261 }
262
263 static void
264 test_ui_file (GFile *file)
265 {
266 char *ui_file, *reference_file;
267 cairo_surface_t *ui_image, *reference_image, *diff_image;
268 GtkStyleProvider *provider;
269 guint max_diff = 0;
270 guint pixels_changed = 0;
271 guint pixels = 0;
272
273 ui_file = g_file_get_path (file);
274
275 provider = add_extra_css (ui_file, ".css");
276
277 ui_image = reftest_snapshot_ui_file (ui_file);
278
279 if ((reference_file = get_reference_image (ui_file)) != NULL)
280 reference_image = cairo_image_surface_create_from_png (reference_file);
281 else if ((reference_file = get_test_file (ui_file, ".ref.ui", TRUE)) != NULL)
282 reference_image = reftest_snapshot_ui_file (reference_file);
283 else
284 {
285 reference_image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
286 g_test_message ("No reference image.");
287 g_test_fail ();
288 }
289 g_free (reference_file);
290
291 diff_image = reftest_compare_surfaces (ui_image, reference_image,
292 &max_diff, &pixels_changed, &pixels);
293
294 save_image (ui_image, ui_file, ".out.png");
295 save_image (reference_image, ui_file, ".ref.png");
296
297 if (diff_image)
298 {
299 g_test_message ("%u (out of %u) pixels differ from reference by up to %u levels",
300 pixels_changed, pixels, max_diff);
301 save_image (diff_image, ui_file, ".diff.png");
302 g_test_fail ();
303 }
304
305 remove_extra_css (provider);
306 }
307
308 static int
309 compare_files (gconstpointer a, gconstpointer b)
310 {
311 GFile *file1 = G_FILE (a);
312 GFile *file2 = G_FILE (b);
313 char *path1, *path2;
314 int result;
315
316 path1 = g_file_get_path (file1);
317 path2 = g_file_get_path (file2);
318
319 result = strcmp (path1, path2);
320
321 g_free (path1);
322 g_free (path2);
323
324 return result;
325 }
326
327 static void
328 add_test_for_file (GFile *file)
329 {
330 GFileEnumerator *enumerator;
331 GFileInfo *info;
332 GList *files;
333 GError *error = NULL;
334
335
336 if (g_file_query_file_type (file, 0, NULL) != G_FILE_TYPE_DIRECTORY)
337 {
338 g_test_add_vtable (g_file_get_path (file),
339 0,
340 g_object_ref (file),
341 NULL,
342 (GTestFixtureFunc) test_ui_file,
343 (GTestFixtureFunc) g_object_unref);
344 return;
345 }
346
347
348 enumerator = g_file_enumerate_children (file, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
349 g_assert_no_error (error);
350 files = NULL;
351
352 while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
353 {
354 const char *filename;
355
356 filename = g_file_info_get_name (info);
357
358 if (!g_str_has_suffix (filename, ".ui") ||
359 g_str_has_suffix (filename, ".ref.ui"))
360 {
361 g_object_unref (info);
362 continue;
363 }
364
365 files = g_list_prepend (files, g_file_get_child (file, filename));
366
367 g_object_unref (info);
368 }
369
370 g_assert_no_error (error);
371 g_object_unref (enumerator);
372
373 files = g_list_sort (files, compare_files);
374 g_list_foreach (files, (GFunc) add_test_for_file, NULL);
375 g_list_free_full (files, g_object_unref);
376 }
377
378 static void
379 enforce_default_settings (void)
380 {
381 GtkSettings *settings;
382 GTypeClass *klass;
383 GParamSpec **pspecs;
384 guint n_pspecs;
385 int i;
386
387 settings = gtk_settings_get_default ();
388
389 klass = g_type_class_ref (G_OBJECT_TYPE (settings));
390
391 pspecs = g_object_class_list_properties (G_OBJECT_CLASS (klass), &n_pspecs);
392 for (i = 0; i < n_pspecs; i++)
393 {
394 GParamSpec *pspec = pspecs[i];
395 const GValue *value;
396
397 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
398 continue;
399
400 if (pspec->value_type == G_TYPE_HASH_TABLE)
401 continue;
402
403 value = g_param_spec_get_default_value (pspec);
404 g_object_set_property (G_OBJECT (settings), pspec->name, value);
405 }
406
407 g_free (pspecs);
408
409 g_type_class_unref (klass);
410 }
411
412 int
413 main (int argc, char **argv)
414 {
415 const char *basedir;
416
417 /* I don't want to fight fuzzy scaling algorithms in GPUs,
418 * so unless you explicitly set it to something else, we
419 * will use Cairo's image surface.
420 */
421 g_setenv ("GDK_RENDERING", "image", FALSE);
422
423 if (!parse_command_line (&argc, &argv))
424 return 1;
425
426 enforce_default_settings ();
427
428 if (arg_base_dir)
429 basedir = arg_base_dir;
430 else
431 basedir = g_test_get_dir (G_TEST_DIST);
432
433 if (argc < 2)
434 {
435 GFile *dir;
436
437 dir = g_file_new_for_path (basedir);
438
439 add_test_for_file (dir);
440
441 g_object_unref (dir);
442 }
443 else
444 {
445 guint i;
446
447 for (i = 1; i < argc; i++)
448 {
449 GFile *file = g_file_new_for_commandline_arg (argv[i]);
450
451 add_test_for_file (file);
452
453 g_object_unref (file);
454 }
455 }
456
457 /* We need to ensure the process' current working directory
458 * is the same as the reftest data, because we're using the
459 * "file" property of GtkImage as a relative path in builder files.
460 */
461 chdir (basedir);
462
463 return g_test_run ();
464 }
465