]> Pileus Git - ~andy/gtk/blob - tests/a11y/accessibility-dump.c
Make the dumped output a bit more readable
[~andy/gtk] / tests / a11y / accessibility-dump.c
1 /*
2  * Copyright (C) 2011 Red Hat Inc.
3  *
4  * Author:
5  *      Benjamin Otte <otte@redhat.com>
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, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26 #include <glib/gstdio.h>
27 #include <gtk/gtk.h>
28
29 #define DEPTH_INCREMENT 2
30
31 static char *
32 get_test_file (const char *test_file,
33                const char *extension,
34                gboolean    must_exist)
35 {
36   GString *file = g_string_new (NULL);
37
38   if (g_str_has_suffix (test_file, ".ui"))
39     g_string_append_len (file, test_file, strlen (test_file) - strlen (".ui"));
40   else
41     g_string_append (file, test_file);
42   
43   g_string_append (file, extension);
44
45   if (must_exist &&
46       !g_file_test (file->str, G_FILE_TEST_EXISTS))
47     {
48       g_string_free (file, TRUE);
49       return NULL;
50     }
51
52   return g_string_free (file, FALSE);
53 }
54
55 static char *
56 diff_with_file (const char  *file1,
57                 char        *text,
58                 gssize       len,
59                 GError     **error)
60 {
61   const char *command[] = { "diff", "-u", file1, NULL, NULL };
62   char *diff, *tmpfile;
63   int fd;
64
65   diff = NULL;
66
67   if (len < 0)
68     len = strlen (text);
69   
70   /* write the text buffer to a temporary file */
71   fd = g_file_open_tmp (NULL, &tmpfile, error);
72   if (fd < 0)
73     return NULL;
74
75   if (write (fd, text, len) != (int) len)
76     {
77       close (fd);
78       g_set_error (error,
79                    G_FILE_ERROR, G_FILE_ERROR_FAILED,
80                    "Could not write data to temporary file '%s'", tmpfile);
81       goto done;
82     }
83   close (fd);
84   command[3] = tmpfile;
85
86   /* run diff command */
87   g_spawn_sync (NULL, 
88                 (char **) command,
89                 NULL,
90                 G_SPAWN_SEARCH_PATH,
91                 NULL, NULL,
92                 &diff,
93                 NULL, NULL,
94                 error);
95
96 done:
97   g_unlink (tmpfile);
98   g_free (tmpfile);
99
100   return diff;
101 }
102
103 static const char *
104 get_name (AtkObject *accessible)
105 {
106   char *name;
107
108   name = g_object_get_data (G_OBJECT (accessible), "gtk-accessibility-dump-name");
109   if (name)
110     return name;
111
112   if (GTK_IS_ACCESSIBLE (accessible))
113     {
114       GtkWidget *widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
115       
116       name = g_strdup (gtk_buildable_get_name (GTK_BUILDABLE (widget)));
117     }
118   else if (ATK_IS_TEXT (accessible))
119     {
120       name = atk_text_get_text (ATK_TEXT (accessible), 0, -1);
121     }
122   else
123     {
124       g_warning ("get_name called on a %s\n", g_type_name_from_instance ((GTypeInstance *)accessible));
125       name = NULL;
126     }
127
128   if (name == NULL)
129     {
130       /* XXX: Generate a unique, repeatable name */
131       g_assert_not_reached ();
132     }
133
134   g_object_set_data_full (G_OBJECT (accessible), "gtk-accessibility-dump-name", name, g_free);
135   return name;
136 }
137
138 static void
139 dump_relation (GString     *string,
140                guint        depth,
141                AtkRelation *relation)
142 {
143   GPtrArray *targets;
144   const char *name;
145   guint i;
146
147   targets = atk_relation_get_target (relation);
148   if (targets == NULL || targets->len == 0)
149     return;
150
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;
154
155   for (i = 1; i < targets->len; i++)
156     {
157       g_string_append_printf (string, "%*s%s\n", depth, "", get_name (g_ptr_array_index (targets, i)));
158     }
159 }
160
161 static void
162 dump_relation_set (GString        *string,
163                    guint           depth,
164                    AtkRelationSet *set)
165 {
166   guint i;
167
168   if (set == NULL)
169     return;
170
171   for (i = 0; i < atk_relation_set_get_n_relations (set); i++)
172     {
173       AtkRelation *relation;
174       
175       relation = atk_relation_set_get_relation (set, i);
176
177       dump_relation (string, depth, relation);
178     }
179
180   g_object_unref (set);
181 }
182
183 static void
184 dump_state_set (GString     *string,
185                 guint        depth,
186                 AtkStateSet *set)
187 {
188   guint i;
189
190   if (set == NULL)
191     return;
192
193   if (!atk_state_set_is_empty (set))
194     {
195       g_string_append_printf (string, "%*sstate:", depth, "");
196       for (i = 0; i < ATK_STATE_LAST_DEFINED; i++)
197         {
198           if (atk_state_set_contains_state (set, i))
199             g_string_append_printf (string, " %s", atk_state_type_get_name (i));
200         }
201       g_string_append_c (string, '\n');
202     }
203
204   g_object_unref (set);
205 }
206
207 static void
208 dump_attribute (GString      *string,
209                 guint         depth,
210                 AtkAttribute *attribute)
211 {
212   g_string_append_printf (string, "%*s%s: %s\n", depth, "", attribute->name, attribute->value);
213 }
214
215 static void
216 dump_attribute_set (GString         *string,
217                     guint            depth,
218                     AtkAttributeSet *set)
219 {
220   GSList *l;
221   AtkAttribute *attribute;
222
223   for (l = set; l; l = l->next)
224     {
225       attribute = l->data;
226
227       dump_attribute (string, depth, attribute);
228     }
229 }
230
231 static void
232 dump_text_attributes (GString         *string,
233                       gint             depth,
234                       const gchar     *name,
235                       AtkAttributeSet *attributes)
236 {
237   GSList *l;
238   AtkAttribute *attr;
239   const gchar *value;
240
241   if (attributes == NULL)
242     return;
243
244   g_string_append_printf (string, "%*s%s:", depth, "", name);
245   for (l = attributes; l; l = l->next)
246     {
247       attr = l->data;
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)
257         value = "<omitted>";
258       else
259         value = attr->value;
260       g_string_append_printf (string, " %s:%s", attr->name, value);
261     }
262   g_string_append_c (string, '\n');
263
264   atk_attribute_set_free (attributes);
265 }
266
267 static void
268 dump_atk_text (AtkText *atk_text,
269                guint    depth,
270                GString *string)
271 {
272   gchar *text;
273   gint i, start, end;
274
275   g_string_append_printf (string, "%*s<AtkText>\n", depth, "");
276
277   text = atk_text_get_text (atk_text, 0, -1);
278   g_string_append_printf (string, "%*stext: %s\n", depth, "", text);
279   g_free (text);
280
281   g_string_append_printf (string, "%*scharacter count: %d\n", depth, "", atk_text_get_character_count (atk_text));
282
283   g_string_append_printf (string, "%*scaret offset: %d\n", depth, "", atk_text_get_caret_offset (atk_text));
284
285   for (i = 0; i < atk_text_get_n_selections (atk_text); i++)
286     {
287       text = atk_text_get_selection (atk_text, i, &start, &end);
288       if (text)
289         g_string_append_printf (string, "%*sselection %d: (%d, %d) %s\n", depth, "", i, start, end, text);
290       g_free (text);
291     }
292
293   dump_text_attributes (string, depth, "default attributes", atk_text_get_default_attributes (atk_text));
294 }
295
296 static void
297 dump_atk_image (AtkImage *atk_image,
298                 guint     depth,
299                 GString  *string)
300 {
301   gint width, height;
302
303   g_string_append_printf (string, "%*s<AtkImage>\n", depth, "");
304
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);
307
308   g_string_append_printf (string, "%*simage description: %s\n", depth, "", atk_image_get_image_description (atk_image));
309 }
310
311 static void
312 dump_atk_action (AtkAction *atk_action,
313                  guint      depth,
314                  GString   *string)
315 {
316   gint i;
317
318   g_string_append_printf (string, "%*s<AtkAction>\n", depth, "");
319
320   for (i = 0; i < atk_action_get_n_actions (atk_action); i++)
321     {
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));
328     }
329 }
330
331 static void
332 dump_atk_selection (AtkSelection *atk_selection,
333                     guint         depth,
334                     GString      *string)
335 {
336   gint i;
337
338   g_string_append_printf (string, "%*s<AtkSelection>\n", depth, "");
339
340   g_string_append_printf (string, "%*sselection count: %d\n", depth, "", atk_selection_get_selection_count (atk_selection));
341
342   if (atk_selection_get_selection_count (atk_selection) > 0)
343     {
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++)
346         {
347           if (atk_selection_is_child_selected (atk_selection, i))
348             g_string_append_printf (string, " %d", i);
349         }
350       g_string_append_c (string, '\n');
351     }
352 }
353
354 static void
355 dump_atk_value (AtkValue *atk_value,
356                 guint     depth,
357                 GString  *string)
358 {
359   GValue value = { 0, };
360
361   g_string_append_printf (string, "%*s<AtkValue>\n", depth, "");
362
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));
371 }
372
373 static void
374 dump_atk_hyperlink_impl (AtkHyperlinkImpl *impl,
375                          guint             depth,
376                          GString          *string)
377 {
378   AtkHyperlink *atk_link;
379   gint i;
380
381   g_string_append_printf (string, "%*s<AtkHyperlinkImpl>\n", depth, "");
382
383   atk_link = atk_hyperlink_impl_get_hyperlink (impl);
384
385   g_string_append_printf (string, "%*sanchors:", depth, "");
386
387   for (i = 0; i < atk_hyperlink_get_n_anchors (atk_link); i++)
388     {
389       gchar *uri;
390
391       uri = atk_hyperlink_get_uri (atk_link, i);
392       g_string_append_printf (string, " %s", uri);
393       g_free (uri);
394     }
395   g_string_append_c (string, '\n');
396
397   g_object_unref (atk_link);
398 }
399
400 static void
401 dump_atk_streamable_content (AtkStreamableContent *content,
402                              guint                 depth,
403                              GString              *string)
404 {
405   gint i;
406
407   g_string_append_printf (string, "%*s<AtkStreamableContent>\n", depth, "");
408
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');
413 }
414
415 static void
416 dump_accessible (AtkObject     *accessible,
417                  guint          depth,
418                  GString       *string)
419 {
420   guint i;
421
422   g_string_append_printf (string, "%*s%s\n", depth, "", get_name (accessible));
423   depth += DEPTH_INCREMENT;
424
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));
437
438   if (ATK_IS_TEXT (accessible))
439     dump_atk_text (ATK_TEXT (accessible), depth, string);
440
441   if (ATK_IS_IMAGE (accessible))
442     dump_atk_image (ATK_IMAGE (accessible), depth, string);
443
444   if (ATK_IS_ACTION (accessible))
445     dump_atk_action (ATK_ACTION (accessible), depth, string);
446
447   if (ATK_IS_SELECTION (accessible))
448     dump_atk_selection (ATK_SELECTION (accessible), depth, string);
449
450   if (ATK_IS_VALUE (accessible))
451     dump_atk_value (ATK_VALUE (accessible), depth, string);
452
453   if (ATK_IS_HYPERLINK_IMPL (accessible))
454     dump_atk_hyperlink_impl (ATK_HYPERLINK_IMPL (accessible), depth, string);
455
456   if (ATK_IS_STREAMABLE_CONTENT (accessible))
457     dump_atk_streamable_content (ATK_STREAMABLE_CONTENT (accessible), depth, string);
458
459   for (i = 0; i < atk_object_get_n_accessible_children (accessible); i++)
460     {
461       AtkObject *child = atk_object_ref_accessible_child (accessible, i);
462       dump_accessible (child, depth, string);
463       g_object_unref (child);
464     }
465 }
466
467 static GtkWidget *
468 builder_get_toplevel (GtkBuilder *builder)
469 {
470   GSList *list, *walk;
471   GtkWidget *window = NULL;
472
473   list = gtk_builder_get_objects (builder);
474   for (walk = list; walk; walk = walk->next)
475     {
476       if (GTK_IS_WINDOW (walk->data) &&
477           gtk_widget_get_parent (walk->data) == NULL)
478         {
479           window = walk->data;
480           break;
481         }
482     }
483   
484   g_slist_free (list);
485
486   return window;
487 }
488
489 static void
490 dump_ui_file (const char *ui_file,
491               GString *string)
492 {
493   GtkWidget *window;
494   GtkBuilder *builder;
495   GError *error = NULL;
496
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);
502   g_assert (window);
503
504   gtk_widget_show (window);
505
506   dump_accessible (gtk_widget_get_accessible (window), 0, string);
507   gtk_widget_destroy (window);
508 }
509
510 static void
511 dump_to_stdout (GFile *file)
512 {
513   char *ui_file;
514   GString *dump;
515
516   ui_file = g_file_get_path (file);
517   dump = g_string_new ("");
518
519   dump_ui_file (ui_file, dump);
520   g_print ("%s", dump->str);
521
522   g_string_free (dump, TRUE);
523   g_free (ui_file);
524 }
525
526 static void
527 test_ui_file (GFile *file)
528 {
529   char *diff;
530   char *ui_file, *a11y_file;
531   GString *dump;
532   GError *error = NULL;
533
534   ui_file = g_file_get_path (file);
535   a11y_file = get_test_file (ui_file, ".txt", TRUE);
536   dump = g_string_new ("");
537
538   dump_ui_file (ui_file, dump);
539
540   if (a11y_file)
541     {
542       diff = diff_with_file (a11y_file, dump->str, dump->len, &error);
543       g_assert_no_error (error);
544
545       if (diff && diff[0])
546         {
547           g_test_message ("Contents don't match expected contents:\n%s", diff);
548           g_test_fail ();
549         }
550     }
551   else if (dump->str[0])
552     {
553       g_test_message ("Expected a reference file:\n%s", dump->str);
554       g_test_fail ();
555     }
556
557   g_string_free (dump, TRUE);
558   g_free (diff);
559   g_free (a11y_file);
560   g_free (ui_file);
561 }
562
563 static void
564 add_test_for_file (GFile *file)
565 {
566   g_test_add_vtable (g_file_get_path (file),
567                      0,
568                      g_object_ref (file),
569                      NULL,
570                      (GTestFixtureFunc) test_ui_file,
571                      (GTestFixtureFunc) g_object_unref);
572 }
573
574 static int
575 compare_files (gconstpointer a, gconstpointer b)
576 {
577   GFile *file1 = G_FILE (a);
578   GFile *file2 = G_FILE (b);
579   char *path1, *path2;
580   int result;
581
582   path1 = g_file_get_path (file1);
583   path2 = g_file_get_path (file2);
584
585   result = strcmp (path1, path2);
586
587   g_free (path1);
588   g_free (path2);
589
590   return result;
591 }
592
593 static void
594 add_tests_for_files_in_directory (GFile *dir)
595 {
596   GFileEnumerator *enumerator;
597   GFileInfo *info;
598   GList *files;
599   GError *error = NULL;
600
601   enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
602   g_assert_no_error (error);
603   files = NULL;
604
605   while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
606     {
607       const char *filename;
608
609       filename = g_file_info_get_name (info);
610
611       if (!g_str_has_suffix (filename, ".ui"))
612         {
613           g_object_unref (info);
614           continue;
615         }
616
617       files = g_list_prepend (files, g_file_get_child (dir, filename));
618
619       g_object_unref (info);
620     }
621   
622   g_assert_no_error (error);
623   g_object_unref (enumerator);
624
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);
628 }
629
630 int
631 main (int argc, char **argv)
632 {
633   gtk_test_init (&argc, &argv);
634
635   if (argc < 2)
636     {
637       const char *basedir;
638       GFile *dir;
639
640       if (g_getenv ("srcdir"))
641         basedir = g_getenv ("srcdir");
642       else
643         basedir = ".";
644         
645       dir = g_file_new_for_path (basedir);
646       
647       add_tests_for_files_in_directory (dir);
648
649       g_object_unref (dir);
650     }
651   else if (argc == 3 && strcmp (argv[1], "--generate") == 0)
652     {
653       GFile *file = g_file_new_for_commandline_arg (argv[2]);
654
655       dump_to_stdout (file);
656
657       g_object_unref (file);
658
659       return 0;
660     }
661   else
662     {
663       guint i;
664
665       for (i = 1; i < argc; i++)
666         {
667           GFile *file = g_file_new_for_commandline_arg (argv[i]);
668
669           add_test_for_file (file);
670
671           g_object_unref (file);
672         }
673     }
674
675   return g_test_run ();
676 }
677