Debian Codegraph @

testsuite/reftests/gtk-reftest.c

raw ◀ ▶
Source reconstructed at patch state 0 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
270 ui_file = g_file_get_path (file);
271
272 provider = add_extra_css (ui_file, ".css");
273
274 ui_image = reftest_snapshot_ui_file (ui_file);
275
276 if ((reference_file = get_reference_image (ui_file)) != NULL)
277 reference_image = cairo_image_surface_create_from_png (reference_file);
278 else if ((reference_file = get_test_file (ui_file, ".ref.ui", TRUE)) != NULL)
279 reference_image = reftest_snapshot_ui_file (reference_file);
280 else
281 {
282 reference_image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
283 g_test_message ("No reference image.");
284 g_test_fail ();
285 }
286 g_free (reference_file);
287
288 diff_image = reftest_compare_surfaces (ui_image, reference_image);
289
290 save_image (ui_image, ui_file, ".out.png");
291 save_image (reference_image, ui_file, ".ref.png");
292 if (diff_image)
293 {
294 save_image (diff_image, ui_file, ".diff.png");
295 g_test_fail ();
296 }
297
298 remove_extra_css (provider);
299 }
300
301 static int
302 compare_files (gconstpointer a, gconstpointer b)
303 {
304 GFile *file1 = G_FILE (a);
305 GFile *file2 = G_FILE (b);
306 char *path1, *path2;
307 int result;
308
309 path1 = g_file_get_path (file1);
310 path2 = g_file_get_path (file2);
311
312 result = strcmp (path1, path2);
313
314 g_free (path1);
315 g_free (path2);
316
317 return result;
318 }
319
320 static void
321 add_test_for_file (GFile *file)
322 {
323 GFileEnumerator *enumerator;
324 GFileInfo *info;
325 GList *files;
326 GError *error = NULL;
327
328
329 if (g_file_query_file_type (file, 0, NULL) != G_FILE_TYPE_DIRECTORY)
330 {
331 g_test_add_vtable (g_file_get_path (file),
332 0,
333 g_object_ref (file),
334 NULL,
335 (GTestFixtureFunc) test_ui_file,
336 (GTestFixtureFunc) g_object_unref);
337 return;
338 }
339
340
341 enumerator = g_file_enumerate_children (file, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
342 g_assert_no_error (error);
343 files = NULL;
344
345 while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
346 {
347 const char *filename;
348
349 filename = g_file_info_get_name (info);
350
351 if (!g_str_has_suffix (filename, ".ui") ||
352 g_str_has_suffix (filename, ".ref.ui"))
353 {
354 g_object_unref (info);
355 continue;
356 }
357
358 files = g_list_prepend (files, g_file_get_child (file, filename));
359
360 g_object_unref (info);
361 }
362
363 g_assert_no_error (error);
364 g_object_unref (enumerator);
365
366 files = g_list_sort (files, compare_files);
367 g_list_foreach (files, (GFunc) add_test_for_file, NULL);
368 g_list_free_full (files, g_object_unref);
369 }
370
371 static void
372 enforce_default_settings (void)
373 {
374 GtkSettings *settings;
375 GTypeClass *klass;
376 GParamSpec **pspecs;
377 guint n_pspecs;
378 int i;
379
380 settings = gtk_settings_get_default ();
381
382 klass = g_type_class_ref (G_OBJECT_TYPE (settings));
383
384 pspecs = g_object_class_list_properties (G_OBJECT_CLASS (klass), &n_pspecs);
385 for (i = 0; i < n_pspecs; i++)
386 {
387 GParamSpec *pspec = pspecs[i];
388 const GValue *value;
389
390 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
391 continue;
392
393 if (pspec->value_type == G_TYPE_HASH_TABLE)
394 continue;
395
396 value = g_param_spec_get_default_value (pspec);
397 g_object_set_property (G_OBJECT (settings), pspec->name, value);
398 }
399
400 g_free (pspecs);
401
402 g_type_class_unref (klass);
403 }
404
405 int
406 main (int argc, char **argv)
407 {
408 const char *basedir;
409
410 /* I don't want to fight fuzzy scaling algorithms in GPUs,
411 * so unless you explicitly set it to something else, we
412 * will use Cairo's image surface.
413 */
414 g_setenv ("GDK_RENDERING", "image", FALSE);
415
416 if (!parse_command_line (&argc, &argv))
417 return 1;
418
419 enforce_default_settings ();
420
421 if (arg_base_dir)
422 basedir = arg_base_dir;
423 else
424 basedir = g_test_get_dir (G_TEST_DIST);
425
426 if (argc < 2)
427 {
428 GFile *dir;
429
430 dir = g_file_new_for_path (basedir);
431
432 add_test_for_file (dir);
433
434 g_object_unref (dir);
435 }
436 else
437 {
438 guint i;
439
440 for (i = 1; i < argc; i++)
441 {
442 GFile *file = g_file_new_for_commandline_arg (argv[i]);
443
444 add_test_for_file (file);
445
446 g_object_unref (file);
447 }
448 }
449
450 /* We need to ensure the process' current working directory
451 * is the same as the reftest data, because we're using the
452 * "file" property of GtkImage as a relative path in builder files.
453 */
454 chdir (basedir);
455
456 return g_test_run ();
457 }
458