2 * Copyright (C) 2011 Red Hat Inc.
5 * Benjamin Otte <otte@redhat.com>
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>
29 #define DEPTH_INCREMENT 2
32 get_test_file (const char *test_file,
33 const char *extension,
36 GString *file = g_string_new (NULL);
38 if (g_str_has_suffix (test_file, ".ui"))
39 g_string_append_len (file, test_file, strlen (test_file) - strlen (".ui"));
41 g_string_append (file, test_file);
43 g_string_append (file, extension);
46 !g_file_test (file->str, G_FILE_TEST_EXISTS))
48 g_string_free (file, TRUE);
52 return g_string_free (file, FALSE);
56 diff_with_file (const char *file1,
61 const char *command[] = { "diff", "-u", file1, NULL, NULL };
70 /* write the text buffer to a temporary file */
71 fd = g_file_open_tmp (NULL, &tmpfile, error);
75 if (write (fd, text, len) != (int) len)
79 G_FILE_ERROR, G_FILE_ERROR_FAILED,
80 "Could not write data to temporary file '%s'", tmpfile);
86 /* run diff command */
104 get_name (AtkObject *accessible)
108 name = g_object_get_data (G_OBJECT (accessible), "gtk-accessibility-dump-name");
112 if (GTK_IS_ACCESSIBLE (accessible))
114 GtkWidget *widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
116 name = g_strdup (gtk_buildable_get_name (GTK_BUILDABLE (widget)));
118 else if (ATK_IS_TEXT (accessible))
120 name = atk_text_get_text (ATK_TEXT (accessible), 0, -1);
124 g_warning ("get_name called on a %s\n", g_type_name_from_instance ((GTypeInstance *)accessible));
130 /* XXX: Generate a unique, repeatable name */
131 g_assert_not_reached ();
134 g_object_set_data_full (G_OBJECT (accessible), "gtk-accessibility-dump-name", name, g_free);
139 dump_relation (GString *string,
141 AtkRelation *relation)
147 targets = atk_relation_get_target (relation);
148 if (targets == NULL || targets->len == 0)
151 name = atk_relation_type_get_name (atk_relation_get_relation_type (relation));
152 g_string_append_printf (string, "%*s%s: %s\n", depth, "", name, get_name (g_ptr_array_index (targets, 0)));
153 depth += strlen (name) + 2;
155 for (i = 1; i < targets->len; i++)
157 g_string_append_printf (string, "%*s%s\n", depth, "", get_name (g_ptr_array_index (targets, i)));
162 dump_relation_set (GString *string,
171 for (i = 0; i < atk_relation_set_get_n_relations (set); i++)
173 AtkRelation *relation;
175 relation = atk_relation_set_get_relation (set, i);
177 dump_relation (string, depth, relation);
180 g_object_unref (set);
184 dump_state_set (GString *string,
193 if (!atk_state_set_is_empty (set))
195 g_string_append_printf (string, "%*sstate:", depth, "");
196 for (i = 0; i < ATK_STATE_LAST_DEFINED; i++)
198 if (atk_state_set_contains_state (set, i))
199 g_string_append_printf (string, " %s", atk_state_type_get_name (i));
201 g_string_append_c (string, '\n');
204 g_object_unref (set);
208 dump_attribute (GString *string,
210 AtkAttribute *attribute)
212 g_string_append_printf (string, "%*s%s: %s\n", depth, "", attribute->name, attribute->value);
216 dump_attribute_set (GString *string,
218 AtkAttributeSet *set)
221 AtkAttribute *attribute;
223 for (l = set; l; l = l->next)
227 dump_attribute (string, depth, attribute);
232 dump_text_attributes (GString *string,
235 AtkAttributeSet *attributes)
241 if (attributes == NULL)
244 g_string_append_printf (string, "%*s%s:", depth, "", name);
245 for (l = attributes; l; l = l->next)
248 /* don't dump values that depend on the environment */
249 if (strcmp (attr->name, "family-name") == 0 ||
250 strcmp (attr->name, "size") == 0 ||
251 strcmp (attr->name, "weight") == 0 ||
252 strcmp (attr->name, "stretch") == 0 ||
253 strcmp (attr->name, "variant") == 0 ||
254 strcmp (attr->name, "style") == 0 ||
255 strcmp (attr->name, "language") == 0 ||
256 strcmp (attr->name, "direction") == 0)
260 g_string_append_printf (string, " %s:%s", attr->name, value);
262 g_string_append_c (string, '\n');
264 atk_attribute_set_free (attributes);
268 dump_atk_text (AtkText *atk_text,
275 text = atk_text_get_text (atk_text, 0, -1);
276 g_string_append_printf (string, "%*stext: %s\n", depth, "", text);
279 g_string_append_printf (string, "%*scharacter count: %d\n", depth, "", atk_text_get_character_count (atk_text));
281 g_string_append_printf (string, "%*scaret offset: %d\n", depth, "", atk_text_get_caret_offset (atk_text));
283 for (i = 0; i < atk_text_get_n_selections (atk_text); i++)
285 text = atk_text_get_selection (atk_text, i, &start, &end);
287 g_string_append_printf (string, "%*sselection %d: (%d, %d) %s\n", depth, "", i, start, end, text);
291 dump_text_attributes (string, depth, "default attributes", atk_text_get_default_attributes (atk_text));
295 dump_atk_image (AtkImage *atk_image,
301 atk_image_get_image_size (atk_image, &width, &height);
302 g_string_append_printf (string, "%*simage size: %d x %d\n", depth, "", width, height);
304 g_string_append_printf (string, "%*simage description: %s\n", depth, "", atk_image_get_image_description (atk_image));
308 dump_atk_action (AtkAction *atk_action,
314 for (i = 0; i < atk_action_get_n_actions (atk_action); i++)
316 if (atk_action_get_name (atk_action, i))
317 g_string_append_printf (string, "%*saction %d name: %s\n", depth, "", i, atk_action_get_name (atk_action, i));
318 if (atk_action_get_description (atk_action, i))
319 g_string_append_printf (string, "%*saction %d description: %s\n", depth, "", i, atk_action_get_description (atk_action, i));
320 if (atk_action_get_keybinding (atk_action, i))
321 g_string_append_printf (string, "%*saction %d keybinding: %s\n", depth, "", i, atk_action_get_keybinding (atk_action, i));
326 dump_atk_selection (AtkSelection *atk_selection,
332 g_string_append_printf (string, "%*sselection count: %d\n", depth, "", atk_selection_get_selection_count (atk_selection));
334 if (atk_selection_get_selection_count (atk_selection) > 0)
336 g_string_append_printf (string, "%*sselected children:", depth, "");
337 for (i = 0; i < atk_object_get_n_accessible_children (ATK_OBJECT (atk_selection)); i++)
339 if (atk_selection_is_child_selected (atk_selection, i))
340 g_string_append_printf (string, " %d", i);
342 g_string_append_c (string, '\n');
347 dump_atk_value (AtkValue *atk_value,
351 GValue value = { 0, };
353 atk_value_get_minimum_value (atk_value, &value);
354 g_string_append_printf (string, "%*sminimum value: %g\n", depth, "", g_value_get_double (&value));
355 atk_value_get_maximum_value (atk_value, &value);
356 g_string_append_printf (string, "%*smaximum value: %g\n", depth, "", g_value_get_double (&value));
357 atk_value_get_current_value (atk_value, &value);
358 g_string_append_printf (string, "%*scurrent value: %g\n", depth, "", g_value_get_double (&value));
359 atk_value_get_minimum_increment (atk_value, &value);
360 g_string_append_printf (string, "%*sminimum increment: %g\n", depth, "", g_value_get_double (&value));
364 dump_atk_hyperlink_impl (AtkHyperlinkImpl *impl,
368 AtkHyperlink *atk_link;
371 atk_link = atk_hyperlink_impl_get_hyperlink (impl);
373 g_string_append_printf (string, "%*sanchors: %d\n", depth, "", atk_hyperlink_get_n_anchors (atk_link));
375 for (i = 0; i < atk_hyperlink_get_n_anchors (atk_link); i++)
379 uri = atk_hyperlink_get_uri (atk_link, i);
380 g_string_append_printf (string, "%*suri %d: %s\n", depth, "", i, uri);
384 g_object_unref (atk_link);
388 dump_accessible (AtkObject *accessible,
394 g_string_append_printf (string, "%*s%s\n", depth, "", get_name (accessible));
395 depth += DEPTH_INCREMENT;
397 g_string_append_printf (string, "%*s\"%s\"\n", depth, "", atk_role_get_name (atk_object_get_role (accessible)));
398 if (GTK_IS_ACCESSIBLE (atk_object_get_parent (accessible)))
399 g_string_append_printf (string, "%*sparent: %s\n", depth, "", get_name (atk_object_get_parent (accessible)));
400 if (atk_object_get_index_in_parent (accessible) != -1)
401 g_string_append_printf (string, "%*sindex: %d\n", depth, "", atk_object_get_index_in_parent (accessible));
402 if (atk_object_get_name (accessible))
403 g_string_append_printf (string, "%*sname: %s\n", depth, "", atk_object_get_name (accessible));
404 if (atk_object_get_description (accessible))
405 g_string_append_printf (string, "%*sdescription: %s\n", depth, "", atk_object_get_description (accessible));
406 dump_relation_set (string, depth, atk_object_ref_relation_set (accessible));
407 dump_state_set (string, depth, atk_object_ref_state_set (accessible));
408 dump_attribute_set (string, depth, atk_object_get_attributes (accessible));
410 if (ATK_IS_TEXT (accessible))
411 dump_atk_text (ATK_TEXT (accessible), depth, string);
413 if (ATK_IS_IMAGE (accessible))
414 dump_atk_image (ATK_IMAGE (accessible), depth, string);
416 if (ATK_IS_ACTION (accessible))
417 dump_atk_action (ATK_ACTION (accessible), depth, string);
419 if (ATK_IS_SELECTION (accessible))
420 dump_atk_selection (ATK_SELECTION (accessible), depth, string);
422 if (ATK_IS_VALUE (accessible))
423 dump_atk_value (ATK_VALUE (accessible), depth, string);
425 if (ATK_IS_HYPERLINK_IMPL (accessible))
426 dump_atk_hyperlink_impl (ATK_HYPERLINK_IMPL (accessible), depth, string);
428 for (i = 0; i < atk_object_get_n_accessible_children (accessible); i++)
430 AtkObject *child = atk_object_ref_accessible_child (accessible, i);
431 dump_accessible (child, depth, string);
432 g_object_unref (child);
437 builder_get_toplevel (GtkBuilder *builder)
440 GtkWidget *window = NULL;
442 list = gtk_builder_get_objects (builder);
443 for (walk = list; walk; walk = walk->next)
445 if (GTK_IS_WINDOW (walk->data) &&
446 gtk_widget_get_parent (walk->data) == NULL)
459 dump_ui_file (const char *ui_file,
464 GError *error = NULL;
466 builder = gtk_builder_new ();
467 gtk_builder_add_from_file (builder, ui_file, &error);
468 g_assert_no_error (error);
469 window = builder_get_toplevel (builder);
470 g_object_unref (builder);
473 gtk_widget_show (window);
475 dump_accessible (gtk_widget_get_accessible (window), 0, string);
476 gtk_widget_destroy (window);
480 dump_to_stdout (GFile *file)
485 ui_file = g_file_get_path (file);
486 dump = g_string_new ("");
488 dump_ui_file (ui_file, dump);
489 g_print ("%s", dump->str);
491 g_string_free (dump, TRUE);
496 test_ui_file (GFile *file)
499 char *ui_file, *a11y_file;
501 GError *error = NULL;
503 ui_file = g_file_get_path (file);
504 a11y_file = get_test_file (ui_file, ".txt", TRUE);
505 dump = g_string_new ("");
507 dump_ui_file (ui_file, dump);
511 diff = diff_with_file (a11y_file, dump->str, dump->len, &error);
512 g_assert_no_error (error);
516 g_test_message ("Contents don't match expected contents:\n%s", diff);
520 else if (dump->str[0])
522 g_test_message ("Expected a reference file:\n%s", dump->str);
526 g_string_free (dump, TRUE);
533 add_test_for_file (GFile *file)
535 g_test_add_vtable (g_file_get_path (file),
539 (GTestFixtureFunc) test_ui_file,
540 (GTestFixtureFunc) g_object_unref);
544 compare_files (gconstpointer a, gconstpointer b)
546 GFile *file1 = G_FILE (a);
547 GFile *file2 = G_FILE (b);
551 path1 = g_file_get_path (file1);
552 path2 = g_file_get_path (file2);
554 result = strcmp (path1, path2);
563 add_tests_for_files_in_directory (GFile *dir)
565 GFileEnumerator *enumerator;
568 GError *error = NULL;
570 enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
571 g_assert_no_error (error);
574 while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
576 const char *filename;
578 filename = g_file_info_get_name (info);
580 if (!g_str_has_suffix (filename, ".ui"))
582 g_object_unref (info);
586 files = g_list_prepend (files, g_file_get_child (dir, filename));
588 g_object_unref (info);
591 g_assert_no_error (error);
592 g_object_unref (enumerator);
594 files = g_list_sort (files, compare_files);
595 g_list_foreach (files, (GFunc) add_test_for_file, NULL);
596 g_list_free_full (files, g_object_unref);
600 main (int argc, char **argv)
602 gtk_test_init (&argc, &argv);
609 if (g_getenv ("srcdir"))
610 basedir = g_getenv ("srcdir");
614 dir = g_file_new_for_path (basedir);
616 add_tests_for_files_in_directory (dir);
618 g_object_unref (dir);
620 else if (argc == 3 && strcmp (argv[1], "--generate") == 0)
622 GFile *file = g_file_new_for_commandline_arg (argv[2]);
624 dump_to_stdout (file);
626 g_object_unref (file);
634 for (i = 1; i < argc; i++)
636 GFile *file = g_file_new_for_commandline_arg (argv[i]);
638 add_test_for_file (file);
640 g_object_unref (file);
644 return g_test_run ();