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 g_string_append_printf (string, "%*s<AtkText>\n", depth, "");
277 text = atk_text_get_text (atk_text, 0, -1);
278 g_string_append_printf (string, "%*stext: %s\n", depth, "", text);
281 g_string_append_printf (string, "%*scharacter count: %d\n", depth, "", atk_text_get_character_count (atk_text));
283 g_string_append_printf (string, "%*scaret offset: %d\n", depth, "", atk_text_get_caret_offset (atk_text));
285 for (i = 0; i < atk_text_get_n_selections (atk_text); i++)
287 text = atk_text_get_selection (atk_text, i, &start, &end);
289 g_string_append_printf (string, "%*sselection %d: (%d, %d) %s\n", depth, "", i, start, end, text);
293 dump_text_attributes (string, depth, "default attributes", atk_text_get_default_attributes (atk_text));
297 dump_atk_image (AtkImage *atk_image,
303 g_string_append_printf (string, "%*s<AtkImage>\n", depth, "");
305 atk_image_get_image_size (atk_image, &width, &height);
306 g_string_append_printf (string, "%*simage size: %d x %d\n", depth, "", width, height);
308 g_string_append_printf (string, "%*simage description: %s\n", depth, "", atk_image_get_image_description (atk_image));
312 dump_atk_action (AtkAction *atk_action,
318 g_string_append_printf (string, "%*s<AtkAction>\n", depth, "");
320 for (i = 0; i < atk_action_get_n_actions (atk_action); i++)
322 if (atk_action_get_name (atk_action, i))
323 g_string_append_printf (string, "%*saction %d name: %s\n", depth, "", i, atk_action_get_name (atk_action, i));
324 if (atk_action_get_description (atk_action, i))
325 g_string_append_printf (string, "%*saction %d description: %s\n", depth, "", i, atk_action_get_description (atk_action, i));
326 if (atk_action_get_keybinding (atk_action, i))
327 g_string_append_printf (string, "%*saction %d keybinding: %s\n", depth, "", i, atk_action_get_keybinding (atk_action, i));
332 dump_atk_selection (AtkSelection *atk_selection,
338 g_string_append_printf (string, "%*s<AtkSelection>\n", depth, "");
340 g_string_append_printf (string, "%*sselection count: %d\n", depth, "", atk_selection_get_selection_count (atk_selection));
342 if (atk_selection_get_selection_count (atk_selection) > 0)
344 g_string_append_printf (string, "%*sselected children:", depth, "");
345 for (i = 0; i < atk_object_get_n_accessible_children (ATK_OBJECT (atk_selection)); i++)
347 if (atk_selection_is_child_selected (atk_selection, i))
348 g_string_append_printf (string, " %d", i);
350 g_string_append_c (string, '\n');
355 dump_atk_value (AtkValue *atk_value,
359 GValue value = { 0, };
361 g_string_append_printf (string, "%*s<AtkValue>\n", depth, "");
363 atk_value_get_minimum_value (atk_value, &value);
364 g_string_append_printf (string, "%*sminimum value: %g\n", depth, "", g_value_get_double (&value));
365 atk_value_get_maximum_value (atk_value, &value);
366 g_string_append_printf (string, "%*smaximum value: %g\n", depth, "", g_value_get_double (&value));
367 atk_value_get_current_value (atk_value, &value);
368 g_string_append_printf (string, "%*scurrent value: %g\n", depth, "", g_value_get_double (&value));
369 atk_value_get_minimum_increment (atk_value, &value);
370 g_string_append_printf (string, "%*sminimum increment: %g\n", depth, "", g_value_get_double (&value));
374 dump_atk_hyperlink_impl (AtkHyperlinkImpl *impl,
378 AtkHyperlink *atk_link;
381 g_string_append_printf (string, "%*s<AtkHyperlinkImpl>\n", depth, "");
383 atk_link = atk_hyperlink_impl_get_hyperlink (impl);
385 g_string_append_printf (string, "%*sanchors:", depth, "");
387 for (i = 0; i < atk_hyperlink_get_n_anchors (atk_link); i++)
391 uri = atk_hyperlink_get_uri (atk_link, i);
392 g_string_append_printf (string, " %s", uri);
395 g_string_append_c (string, '\n');
397 g_object_unref (atk_link);
401 dump_atk_streamable_content (AtkStreamableContent *content,
407 g_string_append_printf (string, "%*s<AtkStreamableContent>\n", depth, "");
409 g_string_append_printf (string, "%*smime types:", depth, "");
410 for (i = 0; i < atk_streamable_content_get_n_mime_types (content); i++)
411 g_string_append_printf (string, " %s", atk_streamable_content_get_mime_type (content, i));
412 g_string_append_c (string, '\n');
416 dump_accessible (AtkObject *accessible,
422 g_string_append_printf (string, "%*s%s\n", depth, "", get_name (accessible));
423 depth += DEPTH_INCREMENT;
425 g_string_append_printf (string, "%*s\"%s\"\n", depth, "", atk_role_get_name (atk_object_get_role (accessible)));
426 if (GTK_IS_ACCESSIBLE (atk_object_get_parent (accessible)))
427 g_string_append_printf (string, "%*sparent: %s\n", depth, "", get_name (atk_object_get_parent (accessible)));
428 if (atk_object_get_index_in_parent (accessible) != -1)
429 g_string_append_printf (string, "%*sindex: %d\n", depth, "", atk_object_get_index_in_parent (accessible));
430 if (atk_object_get_name (accessible))
431 g_string_append_printf (string, "%*sname: %s\n", depth, "", atk_object_get_name (accessible));
432 if (atk_object_get_description (accessible))
433 g_string_append_printf (string, "%*sdescription: %s\n", depth, "", atk_object_get_description (accessible));
434 dump_relation_set (string, depth, atk_object_ref_relation_set (accessible));
435 dump_state_set (string, depth, atk_object_ref_state_set (accessible));
436 dump_attribute_set (string, depth, atk_object_get_attributes (accessible));
438 if (ATK_IS_TEXT (accessible))
439 dump_atk_text (ATK_TEXT (accessible), depth, string);
441 if (ATK_IS_IMAGE (accessible))
442 dump_atk_image (ATK_IMAGE (accessible), depth, string);
444 if (ATK_IS_ACTION (accessible))
445 dump_atk_action (ATK_ACTION (accessible), depth, string);
447 if (ATK_IS_SELECTION (accessible))
448 dump_atk_selection (ATK_SELECTION (accessible), depth, string);
450 if (ATK_IS_VALUE (accessible))
451 dump_atk_value (ATK_VALUE (accessible), depth, string);
453 if (ATK_IS_HYPERLINK_IMPL (accessible))
454 dump_atk_hyperlink_impl (ATK_HYPERLINK_IMPL (accessible), depth, string);
456 if (ATK_IS_STREAMABLE_CONTENT (accessible))
457 dump_atk_streamable_content (ATK_STREAMABLE_CONTENT (accessible), depth, string);
459 for (i = 0; i < atk_object_get_n_accessible_children (accessible); i++)
461 AtkObject *child = atk_object_ref_accessible_child (accessible, i);
462 dump_accessible (child, depth, string);
463 g_object_unref (child);
468 builder_get_toplevel (GtkBuilder *builder)
471 GtkWidget *window = NULL;
473 list = gtk_builder_get_objects (builder);
474 for (walk = list; walk; walk = walk->next)
476 if (GTK_IS_WINDOW (walk->data) &&
477 gtk_widget_get_parent (walk->data) == NULL)
490 dump_ui_file (const char *ui_file,
495 GError *error = NULL;
497 builder = gtk_builder_new ();
498 gtk_builder_add_from_file (builder, ui_file, &error);
499 g_assert_no_error (error);
500 window = builder_get_toplevel (builder);
501 g_object_unref (builder);
504 gtk_widget_show (window);
506 dump_accessible (gtk_widget_get_accessible (window), 0, string);
507 gtk_widget_destroy (window);
511 dump_to_stdout (GFile *file)
516 ui_file = g_file_get_path (file);
517 dump = g_string_new ("");
519 dump_ui_file (ui_file, dump);
520 g_print ("%s", dump->str);
522 g_string_free (dump, TRUE);
527 test_ui_file (GFile *file)
530 char *ui_file, *a11y_file;
532 GError *error = NULL;
534 ui_file = g_file_get_path (file);
535 a11y_file = get_test_file (ui_file, ".txt", TRUE);
536 dump = g_string_new ("");
538 dump_ui_file (ui_file, dump);
542 diff = diff_with_file (a11y_file, dump->str, dump->len, &error);
543 g_assert_no_error (error);
547 g_test_message ("Contents don't match expected contents:\n%s", diff);
551 else if (dump->str[0])
553 g_test_message ("Expected a reference file:\n%s", dump->str);
557 g_string_free (dump, TRUE);
564 add_test_for_file (GFile *file)
566 g_test_add_vtable (g_file_get_path (file),
570 (GTestFixtureFunc) test_ui_file,
571 (GTestFixtureFunc) g_object_unref);
575 compare_files (gconstpointer a, gconstpointer b)
577 GFile *file1 = G_FILE (a);
578 GFile *file2 = G_FILE (b);
582 path1 = g_file_get_path (file1);
583 path2 = g_file_get_path (file2);
585 result = strcmp (path1, path2);
594 add_tests_for_files_in_directory (GFile *dir)
596 GFileEnumerator *enumerator;
599 GError *error = NULL;
601 enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
602 g_assert_no_error (error);
605 while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
607 const char *filename;
609 filename = g_file_info_get_name (info);
611 if (!g_str_has_suffix (filename, ".ui"))
613 g_object_unref (info);
617 files = g_list_prepend (files, g_file_get_child (dir, filename));
619 g_object_unref (info);
622 g_assert_no_error (error);
623 g_object_unref (enumerator);
625 files = g_list_sort (files, compare_files);
626 g_list_foreach (files, (GFunc) add_test_for_file, NULL);
627 g_list_free_full (files, g_object_unref);
631 main (int argc, char **argv)
633 gtk_test_init (&argc, &argv);
640 if (g_getenv ("srcdir"))
641 basedir = g_getenv ("srcdir");
645 dir = g_file_new_for_path (basedir);
647 add_tests_for_files_in_directory (dir);
649 g_object_unref (dir);
651 else if (argc == 3 && strcmp (argv[1], "--generate") == 0)
653 GFile *file = g_file_new_for_commandline_arg (argv[2]);
655 dump_to_stdout (file);
657 g_object_unref (file);
665 for (i = 1; i < argc; i++)
667 GFile *file = g_file_new_for_commandline_arg (argv[i]);
669 add_test_for_file (file);
671 g_object_unref (file);
675 return g_test_run ();