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 */
103 static int unnamed_object_count;
108 unnamed_object_count = 0;
112 get_name (AtkObject *accessible)
116 name = g_object_get_data (G_OBJECT (accessible), "gtk-accessibility-dump-name");
120 if (GTK_IS_ACCESSIBLE (accessible))
122 GtkWidget *widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
124 name = g_strdup (gtk_buildable_get_name (GTK_BUILDABLE (widget)));
127 if (name == NULL && ATK_IS_TEXT (accessible))
129 name = atk_text_get_text (ATK_TEXT (accessible), 0, -1);
134 /* Generate a unique, repeatable name */
135 name = g_strdup_printf ("unnamed-%s-%d", G_OBJECT_TYPE_NAME (accessible), unnamed_object_count++);
138 g_object_set_data_full (G_OBJECT (accessible), "gtk-accessibility-dump-name", name, g_free);
143 dump_relation (GString *string,
145 AtkRelation *relation)
151 targets = atk_relation_get_target (relation);
152 if (targets == NULL || targets->len == 0)
155 name = atk_relation_type_get_name (atk_relation_get_relation_type (relation));
156 g_string_append_printf (string, "%*s%s: %s\n", depth, "", name, get_name (g_ptr_array_index (targets, 0)));
157 depth += strlen (name) + 2;
159 for (i = 1; i < targets->len; i++)
161 g_string_append_printf (string, "%*s%s\n", depth, "", get_name (g_ptr_array_index (targets, i)));
166 dump_relation_set (GString *string,
175 for (i = 0; i < atk_relation_set_get_n_relations (set); i++)
177 AtkRelation *relation;
179 relation = atk_relation_set_get_relation (set, i);
181 dump_relation (string, depth, relation);
184 g_object_unref (set);
188 dump_state_set (GString *string,
197 if (!atk_state_set_is_empty (set))
199 g_string_append_printf (string, "%*sstate:", depth, "");
200 for (i = 0; i < ATK_STATE_LAST_DEFINED; i++)
202 if (atk_state_set_contains_state (set, i))
203 g_string_append_printf (string, " %s", atk_state_type_get_name (i));
205 g_string_append_c (string, '\n');
208 g_object_unref (set);
212 dump_attribute (GString *string,
214 AtkAttribute *attribute)
216 g_string_append_printf (string, "%*s%s: %s\n", depth, "", attribute->name, attribute->value);
220 dump_attribute_set (GString *string,
222 AtkAttributeSet *set)
225 AtkAttribute *attribute;
227 for (l = set; l; l = l->next)
231 dump_attribute (string, depth, attribute);
236 compare_attr (gconstpointer a, gconstpointer b)
238 AtkAttribute *aattr = a;
239 AtkAttribute *battr = b;
241 return strcmp (aattr->name, battr->name);
245 dump_text_attributes (GString *string,
248 AtkAttributeSet *attributes)
254 if (attributes == NULL)
257 attributes = g_slist_sort (attributes, compare_attr);
259 for (l = attributes; l; l = l->next)
262 /* don't dump values that depend on the environment */
263 if (strcmp (attr->name, "family-name") == 0 ||
264 strcmp (attr->name, "size") == 0 ||
265 strcmp (attr->name, "weight") == 0 ||
266 strcmp (attr->name, "stretch") == 0 ||
267 strcmp (attr->name, "variant") == 0 ||
268 strcmp (attr->name, "style") == 0 ||
269 strcmp (attr->name, "language") == 0 ||
270 strcmp (attr->name, "fg-color") == 0 ||
271 strcmp (attr->name, "bg-color") == 0 ||
272 strcmp (attr->name, "direction") == 0)
279 /* first time this loop is run */
280 g_string_append_printf (string, "%*s%s: %s: %s\n", depth, "", name, attr->name, value);
281 depth += strlen (name) + 2;
286 /* every other time */
287 g_string_append_printf (string, "%*s%s: %s\n", depth, "", attr->name, value);
291 atk_attribute_set_free (attributes);
294 extern GType atk_layer_get_type (void);
297 layer_name (AtkLayer layer)
302 class = g_type_class_ref (atk_layer_get_type ());
303 value = g_enum_get_value (class, layer);
304 g_type_class_unref (class);
306 return value->value_nick;
310 dump_atk_component (AtkComponent *atk_component,
316 g_string_append_printf (string, "%*s<AtkComponent>\n", depth, "");
318 layer = atk_component_get_layer (atk_component);
319 g_string_append_printf (string, "%*slayer: %s\n", depth, "", layer_name (layer));
321 g_string_append_printf (string, "%*salpha: %g\n", depth, "", atk_component_get_alpha (atk_component));
325 dump_atk_text (AtkText *atk_text,
332 g_string_append_printf (string, "%*s<AtkText>\n", depth, "");
334 text = atk_text_get_text (atk_text, 0, -1);
335 g_string_append_printf (string, "%*stext: %s\n", depth, "", text);
338 g_string_append_printf (string, "%*scharacter count: %d\n", depth, "", atk_text_get_character_count (atk_text));
340 g_string_append_printf (string, "%*scaret offset: %d\n", depth, "", atk_text_get_caret_offset (atk_text));
342 for (i = 0; i < atk_text_get_n_selections (atk_text); i++)
344 text = atk_text_get_selection (atk_text, i, &start, &end);
346 g_string_append_printf (string, "%*sselection %d: (%d, %d) %s\n", depth, "", i, start, end, text);
350 dump_text_attributes (string, depth, "default attributes", atk_text_get_default_attributes (atk_text));
354 dump_atk_image (AtkImage *atk_image,
360 g_string_append_printf (string, "%*s<AtkImage>\n", depth, "");
362 atk_image_get_image_size (atk_image, &width, &height);
363 g_string_append_printf (string, "%*simage size: %d x %d\n", depth, "", width, height);
365 g_string_append_printf (string, "%*simage description: %s\n", depth, "", atk_image_get_image_description (atk_image));
369 dump_atk_action (AtkAction *atk_action,
375 g_string_append_printf (string, "%*s<AtkAction>\n", depth, "");
377 for (i = 0; i < atk_action_get_n_actions (atk_action); i++)
379 if (atk_action_get_name (atk_action, i))
380 g_string_append_printf (string, "%*saction %d name: %s\n", depth, "", i, atk_action_get_name (atk_action, i));
381 if (atk_action_get_description (atk_action, i))
382 g_string_append_printf (string, "%*saction %d description: %s\n", depth, "", i, atk_action_get_description (atk_action, i));
383 if (atk_action_get_keybinding (atk_action, i))
384 g_string_append_printf (string, "%*saction %d keybinding: %s\n", depth, "", i, atk_action_get_keybinding (atk_action, i));
389 dump_atk_selection (AtkSelection *atk_selection,
395 g_string_append_printf (string, "%*s<AtkSelection>\n", depth, "");
397 g_string_append_printf (string, "%*sselection count: %d\n", depth, "", atk_selection_get_selection_count (atk_selection));
399 if (atk_selection_get_selection_count (atk_selection) > 0)
401 g_string_append_printf (string, "%*sselected children:", depth, "");
402 for (i = 0; i < atk_object_get_n_accessible_children (ATK_OBJECT (atk_selection)); i++)
404 if (atk_selection_is_child_selected (atk_selection, i))
405 g_string_append_printf (string, " %d", i);
407 g_string_append_c (string, '\n');
412 dump_atk_value (AtkValue *atk_value,
416 GValue value = { 0, };
417 GValue svalue = { 0, };
419 g_string_append_printf (string, "%*s<AtkValue>\n", depth, "");
421 g_value_init (&value, G_TYPE_DOUBLE);
422 g_value_init (&svalue, G_TYPE_STRING);
424 atk_value_get_minimum_value (atk_value, &value);
425 if (g_value_transform (&value, &svalue))
426 g_string_append_printf (string, "%*sminimum value: %s\n", depth, "", g_value_get_string (&svalue));
428 g_string_append_printf (string, "%*sminimum value: <%s>\n", depth, "", G_VALUE_TYPE_NAME (&value));
430 g_value_reset (&value);
431 g_value_reset (&svalue);
433 atk_value_get_maximum_value (atk_value, &value);
434 if (g_value_transform (&value, &svalue))
435 g_string_append_printf (string, "%*smaximum value: %s\n", depth, "", g_value_get_string (&svalue));
437 g_string_append_printf (string, "%*smaximum value: <%s>\n", depth, "", G_VALUE_TYPE_NAME (&value));
439 g_value_reset (&value);
440 g_value_reset (&svalue);
442 atk_value_get_current_value (atk_value, &value);
443 if (g_value_transform (&value, &svalue))
444 g_string_append_printf (string, "%*scurrent value: %s\n", depth, "", g_value_get_string (&svalue));
446 g_string_append_printf (string, "%*scurrent value: %s\n", depth, "", G_VALUE_TYPE_NAME (&value));
448 g_value_reset (&value);
449 g_value_reset (&svalue);
451 atk_value_get_minimum_increment (atk_value, &value);
452 if (g_value_transform (&value, &svalue))
453 g_string_append_printf (string, "%*sminimum increment: %s\n", depth, "", g_value_get_string (&svalue));
455 g_string_append_printf (string, "%*sminimum increment: %s\n", depth, "", G_VALUE_TYPE_NAME (&value));
457 g_value_reset (&value);
458 g_value_reset (&svalue);
462 dump_atk_hyperlink_impl (AtkHyperlinkImpl *impl,
466 AtkHyperlink *atk_link;
469 g_string_append_printf (string, "%*s<AtkHyperlinkImpl>\n", depth, "");
471 atk_link = atk_hyperlink_impl_get_hyperlink (impl);
473 g_string_append_printf (string, "%*sanchors:", depth, "");
475 for (i = 0; i < atk_hyperlink_get_n_anchors (atk_link); i++)
479 uri = atk_hyperlink_get_uri (atk_link, i);
480 g_string_append_printf (string, " %s", uri);
483 g_string_append_c (string, '\n');
485 g_object_unref (atk_link);
489 dump_atk_streamable_content (AtkStreamableContent *content,
495 g_string_append_printf (string, "%*s<AtkStreamableContent>\n", depth, "");
497 g_string_append_printf (string, "%*smime types:", depth, "");
498 for (i = 0; i < atk_streamable_content_get_n_mime_types (content); i++)
499 g_string_append_printf (string, " %s", atk_streamable_content_get_mime_type (content, i));
500 g_string_append_c (string, '\n');
503 static void dump_accessible (AtkObject *accessible,
508 dump_atk_table (AtkTable *table,
518 g_string_append_printf (string, "%*s<AtkTable>\n", depth, "");
520 obj = atk_table_get_summary (table);
523 g_string_append_printf (string, "%*s<summary>\n", depth, "");
524 dump_accessible (obj, depth, string);
527 obj = atk_table_get_caption (table);
530 g_string_append_printf (string, "%*s<caption>\n", depth, "");
531 dump_accessible (obj, depth, string);
534 g_string_append_printf (string, "%*srows: %d\n", depth, "", atk_table_get_n_rows (table));
535 g_string_append_printf (string, "%*scolumns: %d\n", depth, "", atk_table_get_n_columns (table));
538 n_selected = atk_table_get_selected_rows (table, &selected);
541 g_string_append_printf (string, "%*sselected rows:", depth, "");
542 for (i = 0; i < n_selected; i++)
543 g_string_append_printf (string, " %d", selected[i]);
544 g_string_append_c (string, '\n');
549 n_selected = atk_table_get_selected_columns (table, &selected);
552 g_string_append_printf (string, "%*sselected columns:", depth, "");
553 for (i = 0; i < n_selected; i++)
554 g_string_append_printf (string, " %d", selected[i]);
555 g_string_append_c (string, '\n');
560 for (i = 0; i < atk_table_get_n_columns (table); i++)
562 desc = atk_table_get_column_description (table, i);
564 g_string_append_printf (string, "%*scolumn %d description: %s\n", depth, "", i, desc);
565 obj = atk_table_get_column_header (table, i);
568 g_string_append_printf (string, "%*s<column %d header>\n", depth, "", i);
569 dump_accessible (obj, depth, string);
573 for (i = 0; i < atk_table_get_n_rows (table); i++)
575 desc = atk_table_get_row_description (table, i);
577 g_string_append_printf (string, "%*srow %d description: %s\n", depth, "", i, desc);
578 obj = atk_table_get_row_header (table, i);
581 g_string_append_printf (string, "%*s<row %d header>\n", depth, "", i);
582 dump_accessible (obj, depth, string);
588 dump_accessible (AtkObject *accessible,
594 g_string_append_printf (string, "%*s%s\n", depth, "", get_name (accessible));
595 depth += DEPTH_INCREMENT;
597 g_string_append_printf (string, "%*s\"%s\"\n", depth, "", atk_role_get_name (atk_object_get_role (accessible)));
598 if (GTK_IS_ACCESSIBLE (atk_object_get_parent (accessible)))
599 g_string_append_printf (string, "%*sparent: %s\n", depth, "", get_name (atk_object_get_parent (accessible)));
600 if (atk_object_get_index_in_parent (accessible) != -1)
601 g_string_append_printf (string, "%*sindex: %d\n", depth, "", atk_object_get_index_in_parent (accessible));
602 if (atk_object_get_name (accessible))
603 g_string_append_printf (string, "%*sname: %s\n", depth, "", atk_object_get_name (accessible));
604 if (atk_object_get_description (accessible))
605 g_string_append_printf (string, "%*sdescription: %s\n", depth, "", atk_object_get_description (accessible));
606 dump_relation_set (string, depth, atk_object_ref_relation_set (accessible));
607 dump_state_set (string, depth, atk_object_ref_state_set (accessible));
608 dump_attribute_set (string, depth, atk_object_get_attributes (accessible));
610 if (ATK_IS_COMPONENT (accessible))
611 dump_atk_component (ATK_COMPONENT (accessible), depth, string);
613 if (ATK_IS_TEXT (accessible))
614 dump_atk_text (ATK_TEXT (accessible), depth, string);
616 if (ATK_IS_IMAGE (accessible))
617 dump_atk_image (ATK_IMAGE (accessible), depth, string);
619 if (ATK_IS_ACTION (accessible))
620 dump_atk_action (ATK_ACTION (accessible), depth, string);
622 if (ATK_IS_SELECTION (accessible))
623 dump_atk_selection (ATK_SELECTION (accessible), depth, string);
625 if (ATK_IS_VALUE (accessible))
626 dump_atk_value (ATK_VALUE (accessible), depth, string);
628 if (ATK_IS_HYPERLINK_IMPL (accessible))
629 dump_atk_hyperlink_impl (ATK_HYPERLINK_IMPL (accessible), depth, string);
631 if (ATK_IS_STREAMABLE_CONTENT (accessible))
632 dump_atk_streamable_content (ATK_STREAMABLE_CONTENT (accessible), depth, string);
634 if (ATK_IS_TABLE (accessible))
635 dump_atk_table (ATK_TABLE (accessible), depth, string);
637 for (i = 0; i < atk_object_get_n_accessible_children (accessible); i++)
639 AtkObject *child = atk_object_ref_accessible_child (accessible, i);
640 dump_accessible (child, depth, string);
641 g_object_unref (child);
646 builder_get_toplevel (GtkBuilder *builder)
649 GtkWidget *window = NULL;
651 list = gtk_builder_get_objects (builder);
652 for (walk = list; walk; walk = walk->next)
654 if (GTK_IS_WINDOW (walk->data) &&
655 gtk_widget_get_parent (walk->data) == NULL)
668 dump_ui_file (const char *ui_file,
673 GError *error = NULL;
675 builder = gtk_builder_new ();
676 gtk_builder_add_from_file (builder, ui_file, &error);
677 g_assert_no_error (error);
678 window = builder_get_toplevel (builder);
679 g_object_unref (builder);
682 gtk_widget_show (window);
684 dump_accessible (gtk_widget_get_accessible (window), 0, string);
685 gtk_widget_destroy (window);
689 dump_to_stdout (GFile *file)
694 ui_file = g_file_get_path (file);
695 dump = g_string_new ("");
697 dump_ui_file (ui_file, dump);
698 g_print ("%s", dump->str);
700 g_string_free (dump, TRUE);
705 test_ui_file (GFile *file)
707 char *ui_file, *a11y_file;
709 GError *error = NULL;
711 ui_file = g_file_get_path (file);
712 a11y_file = get_test_file (ui_file, ".txt", TRUE);
713 dump = g_string_new ("");
715 dump_ui_file (ui_file, dump);
719 char *diff = diff_with_file (a11y_file, dump->str, dump->len, &error);
720 g_assert_no_error (error);
724 g_test_message ("Contents don't match expected contents:\n%s", diff);
729 else if (dump->str[0])
731 g_test_message ("Expected a reference file:\n%s", dump->str);
735 g_string_free (dump, TRUE);
741 add_test_for_file (GFile *file)
743 g_test_add_vtable (g_file_get_path (file),
746 (GTestFixtureFunc) setup_test,
747 (GTestFixtureFunc) test_ui_file,
748 (GTestFixtureFunc) g_object_unref);
752 compare_files (gconstpointer a, gconstpointer b)
754 GFile *file1 = G_FILE (a);
755 GFile *file2 = G_FILE (b);
759 path1 = g_file_get_path (file1);
760 path2 = g_file_get_path (file2);
762 result = strcmp (path1, path2);
771 add_tests_for_files_in_directory (GFile *dir)
773 GFileEnumerator *enumerator;
776 GError *error = NULL;
778 enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
779 g_assert_no_error (error);
782 while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
784 const char *filename;
786 filename = g_file_info_get_name (info);
788 if (!g_str_has_suffix (filename, ".ui"))
790 g_object_unref (info);
794 files = g_list_prepend (files, g_file_get_child (dir, filename));
796 g_object_unref (info);
799 g_assert_no_error (error);
800 g_object_unref (enumerator);
802 files = g_list_sort (files, compare_files);
803 g_list_foreach (files, (GFunc) add_test_for_file, NULL);
804 g_list_free_full (files, g_object_unref);
808 main (int argc, char **argv)
810 gtk_test_init (&argc, &argv);
817 if (g_getenv ("srcdir"))
818 basedir = g_getenv ("srcdir");
822 dir = g_file_new_for_path (basedir);
824 add_tests_for_files_in_directory (dir);
826 g_object_unref (dir);
828 else if (argc == 3 && strcmp (argv[1], "--generate") == 0)
830 GFile *file = g_file_new_for_commandline_arg (argv[2]);
832 dump_to_stdout (file);
834 g_object_unref (file);
842 for (i = 1; i < argc; i++)
844 GFile *file = g_file_new_for_commandline_arg (argv[i]);
846 add_test_for_file (file);
848 g_object_unref (file);
852 return g_test_run ();