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, see <http://www.gnu.org/licenses/>.
24 #include <glib/gstdio.h>
27 #define DEPTH_INCREMENT 2
30 get_test_file (const char *test_file,
31 const char *extension,
34 GString *file = g_string_new (NULL);
36 if (g_str_has_suffix (test_file, ".ui"))
37 g_string_append_len (file, test_file, strlen (test_file) - strlen (".ui"));
39 g_string_append (file, test_file);
41 g_string_append (file, extension);
44 !g_file_test (file->str, G_FILE_TEST_EXISTS))
46 g_string_free (file, TRUE);
50 return g_string_free (file, FALSE);
54 diff_with_file (const char *file1,
59 const char *command[] = { "diff", "-u", file1, NULL, NULL };
68 /* write the text buffer to a temporary file */
69 fd = g_file_open_tmp (NULL, &tmpfile, error);
73 if (write (fd, text, len) != (int) len)
77 G_FILE_ERROR, G_FILE_ERROR_FAILED,
78 "Could not write data to temporary file '%s'", tmpfile);
84 /* run diff command */
101 static int unnamed_object_count;
106 unnamed_object_count = 0;
110 get_name (AtkObject *accessible)
114 name = g_object_get_data (G_OBJECT (accessible), "gtk-accessibility-dump-name");
118 if (GTK_IS_ACCESSIBLE (accessible))
120 GtkWidget *widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
122 name = g_strdup (gtk_buildable_get_name (GTK_BUILDABLE (widget)));
125 if (name == NULL && ATK_IS_TEXT (accessible))
127 name = atk_text_get_text (ATK_TEXT (accessible), 0, -1);
132 /* Generate a unique, repeatable name */
133 name = g_strdup_printf ("unnamed-%s-%d", G_OBJECT_TYPE_NAME (accessible), unnamed_object_count++);
136 g_object_set_data_full (G_OBJECT (accessible), "gtk-accessibility-dump-name", name, g_free);
141 dump_relation (GString *string,
143 AtkRelation *relation)
149 targets = atk_relation_get_target (relation);
150 if (targets == NULL || targets->len == 0)
153 name = atk_relation_type_get_name (atk_relation_get_relation_type (relation));
154 g_string_append_printf (string, "%*s%s: %s\n", depth, "", name, get_name (g_ptr_array_index (targets, 0)));
155 depth += strlen (name) + 2;
157 for (i = 1; i < targets->len; i++)
159 g_string_append_printf (string, "%*s%s\n", depth, "", get_name (g_ptr_array_index (targets, i)));
164 dump_relation_set (GString *string,
173 for (i = 0; i < atk_relation_set_get_n_relations (set); i++)
175 AtkRelation *relation;
177 relation = atk_relation_set_get_relation (set, i);
179 dump_relation (string, depth, relation);
182 g_object_unref (set);
186 dump_state_set (GString *string,
195 if (!atk_state_set_is_empty (set))
197 g_string_append_printf (string, "%*sstate:", depth, "");
198 for (i = 0; i < ATK_STATE_LAST_DEFINED; i++)
200 if (atk_state_set_contains_state (set, i))
201 g_string_append_printf (string, " %s", atk_state_type_get_name (i));
203 g_string_append_c (string, '\n');
206 g_object_unref (set);
210 dump_attribute (GString *string,
212 AtkAttribute *attribute)
214 g_string_append_printf (string, "%*s%s: %s\n", depth, "", attribute->name, attribute->value);
218 dump_attribute_set (GString *string,
220 AtkAttributeSet *set)
223 AtkAttribute *attribute;
225 for (l = set; l; l = l->next)
229 dump_attribute (string, depth, attribute);
234 compare_attr (gconstpointer a, gconstpointer b)
236 const AtkAttribute *aattr = a;
237 const AtkAttribute *battr = b;
239 return strcmp (aattr->name, battr->name);
243 dump_text_attributes (GString *string,
246 AtkAttributeSet *attributes)
252 if (attributes == NULL)
255 attributes = g_slist_sort (attributes, compare_attr);
257 for (l = attributes; l; l = l->next)
260 /* don't dump values that depend on the environment */
261 if (strcmp (attr->name, "family-name") == 0 ||
262 strcmp (attr->name, "size") == 0 ||
263 strcmp (attr->name, "weight") == 0 ||
264 strcmp (attr->name, "stretch") == 0 ||
265 strcmp (attr->name, "variant") == 0 ||
266 strcmp (attr->name, "style") == 0 ||
267 strcmp (attr->name, "language") == 0 ||
268 strcmp (attr->name, "fg-color") == 0 ||
269 strcmp (attr->name, "bg-color") == 0 ||
270 strcmp (attr->name, "direction") == 0)
277 /* first time this loop is run */
278 g_string_append_printf (string, "%*s%s: %s: %s\n", depth, "", name, attr->name, value);
279 depth += strlen (name) + 2;
284 /* every other time */
285 g_string_append_printf (string, "%*s%s: %s\n", depth, "", attr->name, value);
289 atk_attribute_set_free (attributes);
292 extern GType atk_layer_get_type (void);
295 layer_name (AtkLayer layer)
300 class = g_type_class_ref (atk_layer_get_type ());
301 value = g_enum_get_value (class, layer);
302 g_type_class_unref (class);
304 return value->value_nick;
308 dump_atk_component (AtkComponent *atk_component,
314 g_string_append_printf (string, "%*s<AtkComponent>\n", depth, "");
316 layer = atk_component_get_layer (atk_component);
317 g_string_append_printf (string, "%*slayer: %s\n", depth, "", layer_name (layer));
319 g_string_append_printf (string, "%*salpha: %g\n", depth, "", atk_component_get_alpha (atk_component));
323 dump_atk_text (AtkText *atk_text,
330 g_string_append_printf (string, "%*s<AtkText>\n", depth, "");
332 text = atk_text_get_text (atk_text, 0, -1);
333 g_string_append_printf (string, "%*stext: %s\n", depth, "", text);
336 g_string_append_printf (string, "%*scharacter count: %d\n", depth, "", atk_text_get_character_count (atk_text));
338 g_string_append_printf (string, "%*scaret offset: %d\n", depth, "", atk_text_get_caret_offset (atk_text));
340 for (i = 0; i < atk_text_get_n_selections (atk_text); i++)
342 text = atk_text_get_selection (atk_text, i, &start, &end);
344 g_string_append_printf (string, "%*sselection %d: (%d, %d) %s\n", depth, "", i, start, end, text);
348 dump_text_attributes (string, depth, "default attributes", atk_text_get_default_attributes (atk_text));
352 dump_atk_image (AtkImage *atk_image,
358 g_string_append_printf (string, "%*s<AtkImage>\n", depth, "");
360 atk_image_get_image_size (atk_image, &width, &height);
361 g_string_append_printf (string, "%*simage size: %d x %d\n", depth, "", width, height);
363 g_string_append_printf (string, "%*simage description: %s\n", depth, "", atk_image_get_image_description (atk_image));
367 dump_atk_action (AtkAction *atk_action,
373 g_string_append_printf (string, "%*s<AtkAction>\n", depth, "");
375 for (i = 0; i < atk_action_get_n_actions (atk_action); i++)
377 if (atk_action_get_name (atk_action, i))
378 g_string_append_printf (string, "%*saction %d name: %s\n", depth, "", i, atk_action_get_name (atk_action, i));
379 if (atk_action_get_description (atk_action, i))
380 g_string_append_printf (string, "%*saction %d description: %s\n", depth, "", i, atk_action_get_description (atk_action, i));
381 if (atk_action_get_keybinding (atk_action, i))
382 g_string_append_printf (string, "%*saction %d keybinding: %s\n", depth, "", i, atk_action_get_keybinding (atk_action, i));
387 dump_atk_selection (AtkSelection *atk_selection,
391 guint n_selections, n_counted_selections;
394 g_string_append_printf (string, "%*s<AtkSelection>\n", depth, "");
396 n_selections = atk_selection_get_selection_count (atk_selection);
398 n_counted_selections = 0;
399 for (i = 0; i < atk_object_get_n_accessible_children (ATK_OBJECT (atk_selection)); i++)
401 if (atk_selection_is_child_selected (atk_selection, i))
403 AtkObject *object = atk_object_ref_accessible_child (ATK_OBJECT (atk_selection), i);
407 if (n_counted_selections == 0)
409 g_string_append_printf (string, "%*sselected children: %s\n", depth, "", get_name (object));
410 depth += strlen ("selected children: ");
413 g_string_append_printf (string, "%*s%s\n", depth, "", get_name (object));
414 n_counted_selections++;
418 g_assert_cmpint (n_selections, ==, n_counted_selections);
419 g_assert_cmpint (n_selections, ==, atk_selection_get_selection_count (atk_selection));
423 dump_atk_value (AtkValue *atk_value,
427 GValue value = G_VALUE_INIT;
428 GValue svalue = G_VALUE_INIT;
430 g_string_append_printf (string, "%*s<AtkValue>\n", depth, "");
432 g_value_init (&value, G_TYPE_DOUBLE);
433 g_value_init (&svalue, G_TYPE_STRING);
435 atk_value_get_minimum_value (atk_value, &value);
436 if (g_value_transform (&value, &svalue))
437 g_string_append_printf (string, "%*sminimum value: %s\n", depth, "", g_value_get_string (&svalue));
439 g_string_append_printf (string, "%*sminimum value: <%s>\n", depth, "", G_VALUE_TYPE_NAME (&value));
441 g_value_reset (&value);
442 g_value_reset (&svalue);
444 atk_value_get_maximum_value (atk_value, &value);
445 if (g_value_transform (&value, &svalue))
446 g_string_append_printf (string, "%*smaximum value: %s\n", depth, "", g_value_get_string (&svalue));
448 g_string_append_printf (string, "%*smaximum value: <%s>\n", depth, "", G_VALUE_TYPE_NAME (&value));
450 g_value_reset (&value);
451 g_value_reset (&svalue);
453 atk_value_get_current_value (atk_value, &value);
454 if (g_value_transform (&value, &svalue))
455 g_string_append_printf (string, "%*scurrent value: %s\n", depth, "", g_value_get_string (&svalue));
457 g_string_append_printf (string, "%*scurrent value: %s\n", depth, "", G_VALUE_TYPE_NAME (&value));
459 g_value_reset (&value);
460 g_value_reset (&svalue);
462 atk_value_get_minimum_increment (atk_value, &value);
463 if (g_value_transform (&value, &svalue))
464 g_string_append_printf (string, "%*sminimum increment: %s\n", depth, "", g_value_get_string (&svalue));
466 g_string_append_printf (string, "%*sminimum increment: %s\n", depth, "", G_VALUE_TYPE_NAME (&value));
468 g_value_reset (&value);
469 g_value_reset (&svalue);
473 dump_atk_hyperlink_impl (AtkHyperlinkImpl *impl,
477 AtkHyperlink *atk_link;
480 g_string_append_printf (string, "%*s<AtkHyperlinkImpl>\n", depth, "");
482 atk_link = atk_hyperlink_impl_get_hyperlink (impl);
484 g_string_append_printf (string, "%*sanchors:", depth, "");
486 for (i = 0; i < atk_hyperlink_get_n_anchors (atk_link); i++)
490 uri = atk_hyperlink_get_uri (atk_link, i);
491 g_string_append_printf (string, " %s", uri);
494 g_string_append_c (string, '\n');
496 g_object_unref (atk_link);
500 dump_atk_streamable_content (AtkStreamableContent *content,
506 g_string_append_printf (string, "%*s<AtkStreamableContent>\n", depth, "");
508 g_string_append_printf (string, "%*smime types:", depth, "");
509 for (i = 0; i < atk_streamable_content_get_n_mime_types (content); i++)
510 g_string_append_printf (string, " %s", atk_streamable_content_get_mime_type (content, i));
511 g_string_append_c (string, '\n');
515 dump_atk_table (AtkTable *table,
525 g_string_append_printf (string, "%*s<AtkTable>\n", depth, "");
527 obj = atk_table_get_summary (table);
529 g_string_append_printf (string, "%*ssummary: %s\n", depth, "", get_name (obj));
531 obj = atk_table_get_caption (table);
533 g_string_append_printf (string, "%*scaption: %s\n", depth, "", get_name (obj));
535 g_string_append_printf (string, "%*srows: %d\n", depth, "", atk_table_get_n_rows (table));
536 g_string_append_printf (string, "%*scolumns: %d\n", depth, "", atk_table_get_n_columns (table));
539 n_selected = atk_table_get_selected_rows (table, &selected);
542 g_string_append_printf (string, "%*sselected rows:", depth, "");
543 for (i = 0; i < n_selected; i++)
544 g_string_append_printf (string, " %d", selected[i]);
545 g_string_append_c (string, '\n');
550 n_selected = atk_table_get_selected_columns (table, &selected);
553 g_string_append_printf (string, "%*sselected columns:", depth, "");
554 for (i = 0; i < n_selected; i++)
555 g_string_append_printf (string, " %d", selected[i]);
556 g_string_append_c (string, '\n');
561 for (i = 0; i < atk_table_get_n_columns (table); i++)
563 desc = atk_table_get_column_description (table, i);
565 g_string_append_printf (string, "%*scolumn %d description: %s\n", depth, "", i, desc);
566 obj = atk_table_get_column_header (table, i);
568 g_string_append_printf (string, "%*scolumn %d header: %s\n", depth, "", i, get_name (obj));
571 for (i = 0; i < atk_table_get_n_rows (table); i++)
573 desc = atk_table_get_row_description (table, i);
575 g_string_append_printf (string, "%*srow %d description: %s\n", depth, "", i, desc);
576 obj = atk_table_get_row_header (table, i);
578 g_string_append_printf (string, "%*srow %d header: %s\n", depth, "", i, get_name (obj));
581 g_string_append_printf (string, "%*stable indexes:\n", depth, "");
582 for (i = 0; i < atk_table_get_n_rows (table); i++)
584 g_string_append_printf (string, "%*s", depth + DEPTH_INCREMENT, "");
585 for (j = 0; j < atk_table_get_n_columns (table); j++)
587 int id = atk_table_get_index_at (table, i, j);
589 obj = atk_object_ref_accessible_child (ATK_OBJECT (table), id);
591 g_string_append (string, " ");
593 g_string_append_printf (string, "%s%s%s",
594 atk_table_get_row_at_index (table, id) == i ? "✓" : "⚠",
595 atk_table_get_column_at_index (table, id) == j ? "✓" : "⚠",
598 g_string_append (string, "\n");
603 dump_accessible (AtkObject *accessible,
609 g_string_append_printf (string, "%*s%s\n", depth, "", get_name (accessible));
610 depth += DEPTH_INCREMENT;
612 g_string_append_printf (string, "%*s\"%s\"\n", depth, "", atk_role_get_name (atk_object_get_role (accessible)));
613 if (GTK_IS_ACCESSIBLE (atk_object_get_parent (accessible)))
614 g_string_append_printf (string, "%*sparent: %s\n", depth, "", get_name (atk_object_get_parent (accessible)));
615 if (atk_object_get_index_in_parent (accessible) != -1)
616 g_string_append_printf (string, "%*sindex: %d\n", depth, "", atk_object_get_index_in_parent (accessible));
617 if (atk_object_get_name (accessible))
618 g_string_append_printf (string, "%*sname: %s\n", depth, "", atk_object_get_name (accessible));
619 if (atk_object_get_description (accessible))
620 g_string_append_printf (string, "%*sdescription: %s\n", depth, "", atk_object_get_description (accessible));
621 dump_relation_set (string, depth, atk_object_ref_relation_set (accessible));
622 dump_state_set (string, depth, atk_object_ref_state_set (accessible));
623 dump_attribute_set (string, depth, atk_object_get_attributes (accessible));
625 if (ATK_IS_COMPONENT (accessible))
626 dump_atk_component (ATK_COMPONENT (accessible), depth, string);
628 if (ATK_IS_TEXT (accessible))
629 dump_atk_text (ATK_TEXT (accessible), depth, string);
631 if (ATK_IS_IMAGE (accessible))
632 dump_atk_image (ATK_IMAGE (accessible), depth, string);
634 if (ATK_IS_ACTION (accessible))
635 dump_atk_action (ATK_ACTION (accessible), depth, string);
637 if (ATK_IS_SELECTION (accessible))
638 dump_atk_selection (ATK_SELECTION (accessible), depth, string);
640 if (ATK_IS_VALUE (accessible))
641 dump_atk_value (ATK_VALUE (accessible), depth, string);
643 if (ATK_IS_HYPERLINK_IMPL (accessible))
644 dump_atk_hyperlink_impl (ATK_HYPERLINK_IMPL (accessible), depth, string);
646 if (ATK_IS_STREAMABLE_CONTENT (accessible))
647 dump_atk_streamable_content (ATK_STREAMABLE_CONTENT (accessible), depth, string);
649 if (ATK_IS_TABLE (accessible))
650 dump_atk_table (ATK_TABLE (accessible), depth, string);
652 for (i = 0; i < atk_object_get_n_accessible_children (accessible); i++)
654 AtkObject *child = atk_object_ref_accessible_child (accessible, i);
655 dump_accessible (child, depth, string);
656 g_object_unref (child);
661 builder_get_toplevel (GtkBuilder *builder)
664 GtkWidget *window = NULL;
666 list = gtk_builder_get_objects (builder);
667 for (walk = list; walk; walk = walk->next)
669 if (GTK_IS_WINDOW (walk->data) &&
670 gtk_widget_get_parent (walk->data) == NULL)
683 dump_ui_file (const char *ui_file,
688 GError *error = NULL;
690 builder = gtk_builder_new ();
691 gtk_builder_add_from_file (builder, ui_file, &error);
692 g_assert_no_error (error);
693 window = builder_get_toplevel (builder);
694 g_object_unref (builder);
697 gtk_widget_show (window);
699 dump_accessible (gtk_widget_get_accessible (window), 0, string);
700 gtk_widget_destroy (window);
704 dump_to_stdout (GFile *file)
709 ui_file = g_file_get_path (file);
710 dump = g_string_new ("");
712 dump_ui_file (ui_file, dump);
713 g_print ("%s", dump->str);
715 g_string_free (dump, TRUE);
720 test_ui_file (GFile *file)
722 char *ui_file, *a11y_file;
724 GError *error = NULL;
726 ui_file = g_file_get_path (file);
727 a11y_file = get_test_file (ui_file, ".txt", TRUE);
728 dump = g_string_new ("");
730 dump_ui_file (ui_file, dump);
734 char *diff = diff_with_file (a11y_file, dump->str, dump->len, &error);
735 g_assert_no_error (error);
739 g_test_message ("Contents don't match expected contents:\n%s", diff);
744 else if (dump->str[0])
746 g_test_message ("Expected a reference file:\n%s", dump->str);
750 g_string_free (dump, TRUE);
756 add_test_for_file (GFile *file)
758 g_test_add_vtable (g_file_get_path (file),
761 (GTestFixtureFunc) setup_test,
762 (GTestFixtureFunc) test_ui_file,
763 (GTestFixtureFunc) g_object_unref);
767 compare_files (gconstpointer a, gconstpointer b)
769 GFile *file1 = G_FILE (a);
770 GFile *file2 = G_FILE (b);
774 path1 = g_file_get_path (file1);
775 path2 = g_file_get_path (file2);
777 result = strcmp (path1, path2);
786 add_tests_for_files_in_directory (GFile *dir)
788 GFileEnumerator *enumerator;
791 GError *error = NULL;
793 enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
794 g_assert_no_error (error);
797 while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
799 const char *filename;
801 filename = g_file_info_get_name (info);
803 if (!g_str_has_suffix (filename, ".ui"))
805 g_object_unref (info);
809 files = g_list_prepend (files, g_file_get_child (dir, filename));
811 g_object_unref (info);
814 g_assert_no_error (error);
815 g_object_unref (enumerator);
817 files = g_list_sort (files, compare_files);
818 g_list_foreach (files, (GFunc) add_test_for_file, NULL);
819 g_list_free_full (files, g_object_unref);
823 main (int argc, char **argv)
825 gtk_test_init (&argc, &argv);
832 if (g_getenv ("srcdir"))
833 basedir = g_getenv ("srcdir");
837 dir = g_file_new_for_path (basedir);
839 add_tests_for_files_in_directory (dir);
841 g_object_unref (dir);
843 else if (argc == 3 && strcmp (argv[1], "--generate") == 0)
845 GFile *file = g_file_new_for_commandline_arg (argv[2]);
847 dump_to_stdout (file);
849 g_object_unref (file);
857 for (i = 1; i < argc; i++)
859 GFile *file = g_file_new_for_commandline_arg (argv[i]);
861 add_test_for_file (file);
863 g_object_unref (file);
867 return g_test_run ();