Debian Codegraph @

testsuite/css/style/test-css-style.c

raw
1 /*
2 * Copyright (C) 2015 Red Hat Inc.
3 *
4 * Author:
5 * Matthias Clasen <[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 <string.h>
22 #include <glib/gstdio.h>
23 #include <gtk/gtk.h>
24
25 #ifdef G_OS_WIN32
26 # include <io.h>
27 #endif
28
29 /* There shall be no other styles */
30 #define GTK_STYLE_PROVIDER_PRIORITY_FORCE G_MAXUINT
31
32 char *
33 test_get_other_file (const char *ui_file, const char *extension)
34 {
35 GString *file = g_string_new (NULL);
36
37 if (g_str_has_suffix (ui_file, ".ui"))
38 g_string_append_len (file, ui_file, strlen (ui_file) - strlen (".ui"));
39 else
40 g_string_append (file, ui_file);
41
42 g_string_append (file, extension);
43
44 if (!g_file_test (file->str, G_FILE_TEST_EXISTS))
45 {
46 g_string_free (file, TRUE);
47 return NULL;
48 }
49
50 return g_string_free (file, FALSE);
51 }
52
53 static char *
54 diff_with_file (const char *file1,
55 char *text,
56 gssize len,
57 GError **error)
58 {
59 const char *command[] = { "diff", "-u", file1, NULL, NULL };
60 char *diff, *tmpfile;
61 int fd;
62
63 diff = NULL;
64
65 if (len < 0)
66 len = strlen (text);
67
68 /* write the text buffer to a temporary file */
69 fd = g_file_open_tmp (NULL, &tmpfile, error);
70 if (fd < 0)
71 return NULL;
72
73 if (write (fd, text, len) != (int) len)
74 {
75 close (fd);
76 g_set_error (error,
77 G_FILE_ERROR, G_FILE_ERROR_FAILED,
78 "Could not write data to temporary file '%s'", tmpfile);
79 goto done;
80 }
81 close (fd);
82 command[3] = tmpfile;
83
84 /* run diff command */
85 g_spawn_sync (NULL,
86 (char **) command,
87 NULL,
88 G_SPAWN_SEARCH_PATH,
89 NULL, NULL,
90 &diff,
91 NULL, NULL,
92 error);
93
94 done:
95 g_unlink (tmpfile);
96 g_free (tmpfile);
97
98 return diff;
99 }
100
101 static void
102 load_ui_file (GFile *file, gboolean generate)
103 {
104 GtkBuilder *builder;
105 GtkWidget *window;
106 GtkStyleContext *context;
107 char *output, *diff;
108 char *ui_file, *css_file, *reference_file;
109 GtkCssProvider *provider;
110 GError *error = NULL;
111
112 ui_file = g_file_get_path (file);
113
114 css_file = test_get_other_file (ui_file, ".css");
115 g_assert (css_file != NULL);
116
117 provider = gtk_css_provider_new ();
118 gtk_css_provider_load_from_path (provider, css_file, &error);
119 g_assert_no_error (error);
120 gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
121 GTK_STYLE_PROVIDER (provider),
122 GTK_STYLE_PROVIDER_PRIORITY_FORCE);
123
124 builder = gtk_builder_new_from_file (ui_file);
125 window = GTK_WIDGET (gtk_builder_get_object (builder, "window1"));
126
127 g_assert (window != NULL);
128
129 context = gtk_widget_get_style_context (window);
130
131 output = gtk_style_context_to_string (context, GTK_STYLE_CONTEXT_PRINT_RECURSE |
132 GTK_STYLE_CONTEXT_PRINT_SHOW_STYLE);
133
134 if (generate)
135 {
136 g_print ("%s", output);
137 goto out;
138 }
139
140 reference_file = test_get_other_file (ui_file, ".nodes");
141
142 diff = diff_with_file (reference_file, output, -1, &error);
143 g_assert_no_error (error);
144
145 if (diff && diff[0])
146 {
147 g_test_message ("Resulting output doesn't match reference:\n%s", diff);
148 g_test_fail ();
149 }
150 g_free (reference_file);
151 g_free (diff);
152
153 out:
154 gtk_style_context_remove_provider_for_screen (gdk_screen_get_default (),
155 GTK_STYLE_PROVIDER (provider));
156 g_object_unref (provider);
157
158 g_free (output);
159 g_free (ui_file);
160 g_free (css_file);
161 }
162
163 static void
164 test_ui_file (GFile *file)
165 {
166 load_ui_file (file, FALSE);
167 }
168
169 static void
170 add_test_for_file (GFile *file)
171 {
172 char *path;
173
174 path = g_file_get_path (file);
175
176 g_test_add_vtable (path,
177 0,
178 g_object_ref (file),
179 NULL,
180 (GTestFixtureFunc) test_ui_file,
181 (GTestFixtureFunc) g_object_unref);
182
183 g_free (path);
184 }
185
186 static int
187 compare_files (gconstpointer a, gconstpointer b)
188 {
189 GFile *file1 = G_FILE (a);
190 GFile *file2 = G_FILE (b);
191 char *path1, *path2;
192 int result;
193
194 path1 = g_file_get_path (file1);
195 path2 = g_file_get_path (file2);
196
197 result = strcmp (path1, path2);
198
199 g_free (path1);
200 g_free (path2);
201
202 return result;
203 }
204
205 static void
206 add_tests_for_files_in_directory (GFile *dir)
207 {
208 GFileEnumerator *enumerator;
209 GFileInfo *info;
210 GList *files;
211 GError *error = NULL;
212
213 enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
214 g_assert_no_error (error);
215 files = NULL;
216
217 while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
218 {
219 const char *filename;
220
221 filename = g_file_info_get_name (info);
222
223 if (!g_str_has_suffix (filename, ".ui") ||
224 g_str_has_suffix (filename, ".nodes"))
225 {
226 g_object_unref (info);
227 continue;
228 }
229
230 files = g_list_prepend (files, g_file_get_child (dir, filename));
231
232 g_object_unref (info);
233 }
234
235 g_assert_no_error (error);
236 g_object_unref (enumerator);
237
238 files = g_list_sort (files, compare_files);
239 g_list_foreach (files, (GFunc) add_test_for_file, NULL);
240 g_list_free_full (files, g_object_unref);
241 }
242
243 int
244 main (int argc, char **argv)
245 {
246 g_setenv ("GTK_CSS_DEBUG", "1", TRUE);
247
248 gtk_test_init (&argc, &argv);
249
250 g_object_set (gtk_settings_get_default (),
251 "gtk-font-name", "Sans",
252 "gtk-theme-name", "Empty",
253 NULL);
254 if (argc < 2)
255 {
256 const char *basedir;
257 GFile *dir;
258
259 basedir = g_test_get_dir (G_TEST_DIST);
260 dir = g_file_new_for_path (basedir);
261 add_tests_for_files_in_directory (dir);
262
263 g_object_unref (dir);
264 }
265 else if (strcmp (argv[1], "--generate") == 0)
266 {
267 if (argc >= 3)
268 {
269 GFile *file = g_file_new_for_commandline_arg (argv[2]);
270
271 load_ui_file (file, TRUE);
272
273 g_object_unref (file);
274 }
275 }
276 else
277 {
278 guint i;
279
280 for (i = 1; i < argc; i++)
281 {
282 GFile *file = g_file_new_for_commandline_arg (argv[i]);
283
284 add_test_for_file (file);
285
286 g_object_unref (file);
287 }
288 }
289
290 return g_test_run ();
291 }
292