]> Pileus Git - ~andy/gtk/blob - tests/a11y/accessibility-dump.c
Add a test involving links
[~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   text = atk_text_get_text (atk_text, 0, -1);
276   g_string_append_printf (string, "%*stext: %s\n", depth, "", text);
277   g_free (text);
278
279   g_string_append_printf (string, "%*scharacter count: %d\n", depth, "", atk_text_get_character_count (atk_text));
280
281   g_string_append_printf (string, "%*scaret offset: %d\n", depth, "", atk_text_get_caret_offset (atk_text));
282
283   for (i = 0; i < atk_text_get_n_selections (atk_text); i++)
284     {
285       text = atk_text_get_selection (atk_text, i, &start, &end);
286       if (text)
287         g_string_append_printf (string, "%*sselection %d: (%d, %d) %s\n", depth, "", i, start, end, text);
288       g_free (text);
289     }
290
291   dump_text_attributes (string, depth, "default attributes", atk_text_get_default_attributes (atk_text));
292 }
293
294 static void
295 dump_atk_image (AtkImage *atk_image,
296                 guint     depth,
297                 GString  *string)
298 {
299   gint width, height;
300
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);
303
304   g_string_append_printf (string, "%*simage description: %s\n", depth, "", atk_image_get_image_description (atk_image));
305 }
306
307 static void
308 dump_atk_action (AtkAction *atk_action,
309                  guint      depth,
310                  GString   *string)
311 {
312   gint i;
313
314   for (i = 0; i < atk_action_get_n_actions (atk_action); i++)
315     {
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));
322     }
323 }
324
325 static void
326 dump_atk_selection (AtkSelection *atk_selection,
327                     guint         depth,
328                     GString      *string)
329 {
330   gint i;
331
332   g_string_append_printf (string, "%*sselection count: %d\n", depth, "", atk_selection_get_selection_count (atk_selection));
333
334   if (atk_selection_get_selection_count (atk_selection) > 0)
335     {
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++)
338         {
339           if (atk_selection_is_child_selected (atk_selection, i))
340             g_string_append_printf (string, " %d", i);
341         }
342       g_string_append_c (string, '\n');
343     }
344 }
345
346 static void
347 dump_atk_value (AtkValue *atk_value,
348                 guint     depth,
349                 GString  *string)
350 {
351   GValue value = { 0, };
352
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));
361 }
362
363 static void
364 dump_atk_hyperlink_impl (AtkHyperlinkImpl *impl,
365                          guint             depth,
366                          GString          *string)
367 {
368   AtkHyperlink *atk_link;
369   gint i;
370
371   atk_link = atk_hyperlink_impl_get_hyperlink (impl);
372
373   g_string_append_printf (string, "%*sanchors: %d\n", depth, "", atk_hyperlink_get_n_anchors (atk_link));
374
375   for (i = 0; i < atk_hyperlink_get_n_anchors (atk_link); i++)
376     {
377       gchar *uri;
378
379       uri = atk_hyperlink_get_uri (atk_link, i);
380       g_string_append_printf (string, "%*suri %d: %s\n", depth, "", i, uri);
381       g_free (uri);
382     }
383
384   g_object_unref (atk_link);
385 }
386
387 static void
388 dump_accessible (AtkObject     *accessible,
389                  guint          depth,
390                  GString       *string)
391 {
392   guint i;
393
394   g_string_append_printf (string, "%*s%s\n", depth, "", get_name (accessible));
395   depth += DEPTH_INCREMENT;
396
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));
409
410   if (ATK_IS_TEXT (accessible))
411     dump_atk_text (ATK_TEXT (accessible), depth, string);
412
413   if (ATK_IS_IMAGE (accessible))
414     dump_atk_image (ATK_IMAGE (accessible), depth, string);
415
416   if (ATK_IS_ACTION (accessible))
417     dump_atk_action (ATK_ACTION (accessible), depth, string);
418
419   if (ATK_IS_SELECTION (accessible))
420     dump_atk_selection (ATK_SELECTION (accessible), depth, string);
421
422   if (ATK_IS_VALUE (accessible))
423     dump_atk_value (ATK_VALUE (accessible), depth, string);
424
425   if (ATK_IS_HYPERLINK_IMPL (accessible))
426     dump_atk_hyperlink_impl (ATK_HYPERLINK_IMPL (accessible), depth, string);
427
428   for (i = 0; i < atk_object_get_n_accessible_children (accessible); i++)
429     {
430       AtkObject *child = atk_object_ref_accessible_child (accessible, i);
431       dump_accessible (child, depth, string);
432       g_object_unref (child);
433     }
434 }
435
436 static GtkWidget *
437 builder_get_toplevel (GtkBuilder *builder)
438 {
439   GSList *list, *walk;
440   GtkWidget *window = NULL;
441
442   list = gtk_builder_get_objects (builder);
443   for (walk = list; walk; walk = walk->next)
444     {
445       if (GTK_IS_WINDOW (walk->data) &&
446           gtk_widget_get_parent (walk->data) == NULL)
447         {
448           window = walk->data;
449           break;
450         }
451     }
452   
453   g_slist_free (list);
454
455   return window;
456 }
457
458 static void
459 dump_ui_file (const char *ui_file,
460               GString *string)
461 {
462   GtkWidget *window;
463   GtkBuilder *builder;
464   GError *error = NULL;
465
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);
471   g_assert (window);
472
473   gtk_widget_show (window);
474
475   dump_accessible (gtk_widget_get_accessible (window), 0, string);
476   gtk_widget_destroy (window);
477 }
478
479 static void
480 dump_to_stdout (GFile *file)
481 {
482   char *ui_file;
483   GString *dump;
484
485   ui_file = g_file_get_path (file);
486   dump = g_string_new ("");
487
488   dump_ui_file (ui_file, dump);
489   g_print ("%s", dump->str);
490
491   g_string_free (dump, TRUE);
492   g_free (ui_file);
493 }
494
495 static void
496 test_ui_file (GFile *file)
497 {
498   char *diff;
499   char *ui_file, *a11y_file;
500   GString *dump;
501   GError *error = NULL;
502
503   ui_file = g_file_get_path (file);
504   a11y_file = get_test_file (ui_file, ".txt", TRUE);
505   dump = g_string_new ("");
506
507   dump_ui_file (ui_file, dump);
508
509   if (a11y_file)
510     {
511       diff = diff_with_file (a11y_file, dump->str, dump->len, &error);
512       g_assert_no_error (error);
513
514       if (diff && diff[0])
515         {
516           g_test_message ("Contents don't match expected contents:\n%s", diff);
517           g_test_fail ();
518         }
519     }
520   else if (dump->str[0])
521     {
522       g_test_message ("Expected a reference file:\n%s", dump->str);
523       g_test_fail ();
524     }
525
526   g_string_free (dump, TRUE);
527   g_free (diff);
528   g_free (a11y_file);
529   g_free (ui_file);
530 }
531
532 static void
533 add_test_for_file (GFile *file)
534 {
535   g_test_add_vtable (g_file_get_path (file),
536                      0,
537                      g_object_ref (file),
538                      NULL,
539                      (GTestFixtureFunc) test_ui_file,
540                      (GTestFixtureFunc) g_object_unref);
541 }
542
543 static int
544 compare_files (gconstpointer a, gconstpointer b)
545 {
546   GFile *file1 = G_FILE (a);
547   GFile *file2 = G_FILE (b);
548   char *path1, *path2;
549   int result;
550
551   path1 = g_file_get_path (file1);
552   path2 = g_file_get_path (file2);
553
554   result = strcmp (path1, path2);
555
556   g_free (path1);
557   g_free (path2);
558
559   return result;
560 }
561
562 static void
563 add_tests_for_files_in_directory (GFile *dir)
564 {
565   GFileEnumerator *enumerator;
566   GFileInfo *info;
567   GList *files;
568   GError *error = NULL;
569
570   enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
571   g_assert_no_error (error);
572   files = NULL;
573
574   while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
575     {
576       const char *filename;
577
578       filename = g_file_info_get_name (info);
579
580       if (!g_str_has_suffix (filename, ".ui"))
581         {
582           g_object_unref (info);
583           continue;
584         }
585
586       files = g_list_prepend (files, g_file_get_child (dir, filename));
587
588       g_object_unref (info);
589     }
590   
591   g_assert_no_error (error);
592   g_object_unref (enumerator);
593
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);
597 }
598
599 int
600 main (int argc, char **argv)
601 {
602   gtk_test_init (&argc, &argv);
603
604   if (argc < 2)
605     {
606       const char *basedir;
607       GFile *dir;
608
609       if (g_getenv ("srcdir"))
610         basedir = g_getenv ("srcdir");
611       else
612         basedir = ".";
613         
614       dir = g_file_new_for_path (basedir);
615       
616       add_tests_for_files_in_directory (dir);
617
618       g_object_unref (dir);
619     }
620   else if (argc == 3 && strcmp (argv[1], "--generate") == 0)
621     {
622       GFile *file = g_file_new_for_commandline_arg (argv[2]);
623
624       dump_to_stdout (file);
625
626       g_object_unref (file);
627
628       return 0;
629     }
630   else
631     {
632       guint i;
633
634       for (i = 1; i < argc; i++)
635         {
636           GFile *file = g_file_new_for_commandline_arg (argv[i]);
637
638           add_test_for_file (file);
639
640           g_object_unref (file);
641         }
642     }
643
644   return g_test_run ();
645 }
646