]> Pileus Git - ~andy/gtk/blob - tests/a11y/accessibility-dump.c
stylecontext: Do invalidation on first resize container
[~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, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "config.h"
22
23 #include <string.h>
24 #include <glib/gstdio.h>
25 #include <gtk/gtk.h>
26
27 #define DEPTH_INCREMENT 2
28
29 static char *
30 get_test_file (const char *test_file,
31                const char *extension,
32                gboolean    must_exist)
33 {
34   GString *file = g_string_new (NULL);
35
36   if (g_str_has_suffix (test_file, ".ui"))
37     g_string_append_len (file, test_file, strlen (test_file) - strlen (".ui"));
38   else
39     g_string_append (file, test_file);
40   
41   g_string_append (file, extension);
42
43   if (must_exist &&
44       !g_file_test (file->str, G_FILE_TEST_EXISTS))
45     {
46       g_string_free (file, TRUE);
47       return NULL;
48     }
49
50   return g_string_free (file, FALSE);
51 }
52
53 static char *
54 diff_with_file (const char  *file1,
55                 char        *text,
56                 gssize       len,
57                 GError     **error)
58 {
59   const char *command[] = { "diff", "-u", file1, NULL, NULL };
60   char *diff, *tmpfile;
61   int fd;
62
63   diff = NULL;
64
65   if (len < 0)
66     len = strlen (text);
67   
68   /* write the text buffer to a temporary file */
69   fd = g_file_open_tmp (NULL, &tmpfile, error);
70   if (fd < 0)
71     return NULL;
72
73   if (write (fd, text, len) != (int) len)
74     {
75       close (fd);
76       g_set_error (error,
77                    G_FILE_ERROR, G_FILE_ERROR_FAILED,
78                    "Could not write data to temporary file '%s'", tmpfile);
79       goto done;
80     }
81   close (fd);
82   command[3] = tmpfile;
83
84   /* run diff command */
85   g_spawn_sync (NULL,
86                 (char **) command,
87                 NULL,
88                 G_SPAWN_SEARCH_PATH,
89                 NULL, NULL,
90                 &diff,
91                 NULL, NULL,
92                 error);
93
94 done:
95   g_unlink (tmpfile);
96   g_free (tmpfile);
97
98   return diff;
99 }
100
101 static int unnamed_object_count;
102
103 static void
104 setup_test (void)
105 {
106   unnamed_object_count = 0;
107 }
108
109 static const char *
110 get_name (AtkObject *accessible)
111 {
112   char *name;
113
114   name = g_object_get_data (G_OBJECT (accessible), "gtk-accessibility-dump-name");
115   if (name)
116     return name;
117
118   if (GTK_IS_ACCESSIBLE (accessible))
119     {
120       GtkWidget *widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
121
122       name = g_strdup (gtk_buildable_get_name (GTK_BUILDABLE (widget)));
123     }
124
125   if (name == NULL && ATK_IS_TEXT (accessible))
126     {
127       name = atk_text_get_text (ATK_TEXT (accessible), 0, -1);
128     }
129
130   if (name == NULL)
131     {
132       /* Generate a unique, repeatable name */
133       name = g_strdup_printf ("unnamed-%s-%d", G_OBJECT_TYPE_NAME (accessible), unnamed_object_count++);
134     }
135
136   g_object_set_data_full (G_OBJECT (accessible), "gtk-accessibility-dump-name", name, g_free);
137   return name;
138 }
139
140 static void
141 dump_relation (GString     *string,
142                guint        depth,
143                AtkRelation *relation)
144 {
145   GPtrArray *targets;
146   const char *name;
147   guint i;
148
149   targets = atk_relation_get_target (relation);
150   if (targets == NULL || targets->len == 0)
151     return;
152
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;
156
157   for (i = 1; i < targets->len; i++)
158     {
159       g_string_append_printf (string, "%*s%s\n", depth, "", get_name (g_ptr_array_index (targets, i)));
160     }
161 }
162
163 static void
164 dump_relation_set (GString        *string,
165                    guint           depth,
166                    AtkRelationSet *set)
167 {
168   guint i;
169
170   if (set == NULL)
171     return;
172
173   for (i = 0; i < atk_relation_set_get_n_relations (set); i++)
174     {
175       AtkRelation *relation;
176       
177       relation = atk_relation_set_get_relation (set, i);
178
179       dump_relation (string, depth, relation);
180     }
181
182   g_object_unref (set);
183 }
184
185 static void
186 dump_state_set (GString     *string,
187                 guint        depth,
188                 AtkStateSet *set)
189 {
190   guint i;
191
192   if (set == NULL)
193     return;
194
195   if (!atk_state_set_is_empty (set))
196     {
197       g_string_append_printf (string, "%*sstate:", depth, "");
198       for (i = 0; i < ATK_STATE_LAST_DEFINED; i++)
199         {
200           if (atk_state_set_contains_state (set, i))
201             g_string_append_printf (string, " %s", atk_state_type_get_name (i));
202         }
203       g_string_append_c (string, '\n');
204     }
205
206   g_object_unref (set);
207 }
208
209 static void
210 dump_attribute (GString      *string,
211                 guint         depth,
212                 AtkAttribute *attribute)
213 {
214   g_string_append_printf (string, "%*s%s: %s\n", depth, "", attribute->name, attribute->value);
215 }
216
217 static void
218 dump_attribute_set (GString         *string,
219                     guint            depth,
220                     AtkAttributeSet *set)
221 {
222   GSList *l;
223   AtkAttribute *attribute;
224
225   for (l = set; l; l = l->next)
226     {
227       attribute = l->data;
228
229       dump_attribute (string, depth, attribute);
230     }
231 }
232
233 static gint
234 compare_attr (gconstpointer a, gconstpointer b)
235 {
236   const AtkAttribute *aattr = a;
237   const AtkAttribute *battr = b;
238
239   return strcmp (aattr->name, battr->name);
240 }
241
242 static void
243 dump_text_attributes (GString         *string,
244                       gint             depth,
245                       const gchar     *name,
246                       AtkAttributeSet *attributes)
247 {
248   GSList *l;
249   AtkAttribute *attr;
250   const gchar *value;
251
252   if (attributes == NULL)
253     return;
254
255   attributes = g_slist_sort (attributes, compare_attr);
256
257   for (l = attributes; l; l = l->next)
258     {
259       attr = l->data;
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)
271         value = "<omitted>";
272       else
273         value = attr->value;
274
275       if (name)
276         {
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;
280           name = NULL;
281         }
282       else
283         {
284           /* every other time */
285           g_string_append_printf (string, "%*s%s: %s\n", depth, "", attr->name, value);
286         }
287     }
288
289   atk_attribute_set_free (attributes);
290 }
291
292 extern GType atk_layer_get_type (void);
293
294 static const gchar *
295 layer_name (AtkLayer layer)
296 {
297   GEnumClass *class;
298   GEnumValue *value;
299
300   class = g_type_class_ref (atk_layer_get_type ());
301   value = g_enum_get_value (class, layer);
302   g_type_class_unref (class);
303
304   return value->value_nick;
305 }
306
307 static void
308 dump_atk_component (AtkComponent *atk_component,
309                     guint         depth,
310                     GString      *string)
311 {
312   AtkLayer layer;
313
314   g_string_append_printf (string, "%*s<AtkComponent>\n", depth, "");
315
316   layer = atk_component_get_layer (atk_component);
317   g_string_append_printf (string, "%*slayer: %s\n", depth, "", layer_name (layer));
318
319   g_string_append_printf (string, "%*salpha: %g\n", depth, "", atk_component_get_alpha (atk_component));
320 }
321
322 static void
323 dump_atk_text (AtkText *atk_text,
324                guint    depth,
325                GString *string)
326 {
327   gchar *text;
328   gint i, start, end;
329
330   g_string_append_printf (string, "%*s<AtkText>\n", depth, "");
331
332   text = atk_text_get_text (atk_text, 0, -1);
333   g_string_append_printf (string, "%*stext: %s\n", depth, "", text);
334   g_free (text);
335
336   g_string_append_printf (string, "%*scharacter count: %d\n", depth, "", atk_text_get_character_count (atk_text));
337
338   g_string_append_printf (string, "%*scaret offset: %d\n", depth, "", atk_text_get_caret_offset (atk_text));
339
340   for (i = 0; i < atk_text_get_n_selections (atk_text); i++)
341     {
342       text = atk_text_get_selection (atk_text, i, &start, &end);
343       if (text)
344         g_string_append_printf (string, "%*sselection %d: (%d, %d) %s\n", depth, "", i, start, end, text);
345       g_free (text);
346     }
347
348   dump_text_attributes (string, depth, "default attributes", atk_text_get_default_attributes (atk_text));
349 }
350
351 static void
352 dump_atk_image (AtkImage *atk_image,
353                 guint     depth,
354                 GString  *string)
355 {
356   gint width, height;
357
358   g_string_append_printf (string, "%*s<AtkImage>\n", depth, "");
359
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);
362
363   g_string_append_printf (string, "%*simage description: %s\n", depth, "", atk_image_get_image_description (atk_image));
364 }
365
366 static void
367 dump_atk_action (AtkAction *atk_action,
368                  guint      depth,
369                  GString   *string)
370 {
371   gint i;
372
373   g_string_append_printf (string, "%*s<AtkAction>\n", depth, "");
374
375   for (i = 0; i < atk_action_get_n_actions (atk_action); i++)
376     {
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));
383     }
384 }
385
386 static void
387 dump_atk_selection (AtkSelection *atk_selection,
388                     guint         depth,
389                     GString      *string)
390 {
391   guint n_selections, n_counted_selections;
392   gint i;
393
394   g_string_append_printf (string, "%*s<AtkSelection>\n", depth, "");
395
396   n_selections = atk_selection_get_selection_count (atk_selection);
397
398   n_counted_selections = 0;
399   for (i = 0; i < atk_object_get_n_accessible_children (ATK_OBJECT (atk_selection)); i++)
400     {
401       if (atk_selection_is_child_selected (atk_selection, i))
402         {
403           AtkObject *object = atk_object_ref_accessible_child (ATK_OBJECT (atk_selection), i);
404           
405           g_assert (object);
406           
407           if (n_counted_selections == 0)
408             {
409               g_string_append_printf (string, "%*sselected children: %s\n", depth, "", get_name (object));
410               depth += strlen ("selected children: ");
411             }
412           else
413             g_string_append_printf (string, "%*s%s\n", depth, "", get_name (object));
414           n_counted_selections++;
415         }
416     }
417   
418   g_assert_cmpint (n_selections, ==, n_counted_selections);
419   g_assert_cmpint (n_selections, ==, atk_selection_get_selection_count (atk_selection));
420 }
421
422 static void
423 dump_atk_value (AtkValue *atk_value,
424                 guint     depth,
425                 GString  *string)
426 {
427   GValue value = G_VALUE_INIT;
428   GValue svalue = G_VALUE_INIT;
429
430   g_string_append_printf (string, "%*s<AtkValue>\n", depth, "");
431
432   g_value_init (&value, G_TYPE_DOUBLE);
433   g_value_init (&svalue, G_TYPE_STRING);
434
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));
438   else
439     g_string_append_printf (string, "%*sminimum value: <%s>\n", depth, "", G_VALUE_TYPE_NAME (&value));
440
441   g_value_reset (&value);
442   g_value_reset (&svalue);
443
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));
447   else
448     g_string_append_printf (string, "%*smaximum value: <%s>\n", depth, "", G_VALUE_TYPE_NAME (&value));
449
450   g_value_reset (&value);
451   g_value_reset (&svalue);
452
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));
456   else
457     g_string_append_printf (string, "%*scurrent value: %s\n", depth, "", G_VALUE_TYPE_NAME (&value));
458
459   g_value_reset (&value);
460   g_value_reset (&svalue);
461
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));
465   else
466     g_string_append_printf (string, "%*sminimum increment: %s\n", depth, "", G_VALUE_TYPE_NAME (&value));
467
468   g_value_reset (&value);
469   g_value_reset (&svalue);
470 }
471
472 static void
473 dump_atk_hyperlink_impl (AtkHyperlinkImpl *impl,
474                          guint             depth,
475                          GString          *string)
476 {
477   AtkHyperlink *atk_link;
478   gint i;
479
480   g_string_append_printf (string, "%*s<AtkHyperlinkImpl>\n", depth, "");
481
482   atk_link = atk_hyperlink_impl_get_hyperlink (impl);
483
484   g_string_append_printf (string, "%*sanchors:", depth, "");
485
486   for (i = 0; i < atk_hyperlink_get_n_anchors (atk_link); i++)
487     {
488       gchar *uri;
489
490       uri = atk_hyperlink_get_uri (atk_link, i);
491       g_string_append_printf (string, " %s", uri);
492       g_free (uri);
493     }
494   g_string_append_c (string, '\n');
495
496   g_object_unref (atk_link);
497 }
498
499 static void
500 dump_atk_streamable_content (AtkStreamableContent *content,
501                              guint                 depth,
502                              GString              *string)
503 {
504   gint i;
505
506   g_string_append_printf (string, "%*s<AtkStreamableContent>\n", depth, "");
507
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');
512 }
513
514 static void
515 dump_atk_table (AtkTable *table,
516                 guint     depth,
517                 GString  *string)
518 {
519   gint *selected;
520   gint n_selected;
521   gint i, j;
522   AtkObject *obj;
523   const gchar *desc;
524
525   g_string_append_printf (string, "%*s<AtkTable>\n", depth, "");
526
527   obj = atk_table_get_summary (table);
528   if (obj)
529     g_string_append_printf (string, "%*ssummary: %s\n", depth, "", get_name (obj));
530
531   obj = atk_table_get_caption (table);
532   if (obj)
533     g_string_append_printf (string, "%*scaption: %s\n", depth, "", get_name (obj));
534
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));
537
538   selected = NULL;
539   n_selected = atk_table_get_selected_rows (table, &selected);
540   if (n_selected > 0)
541     {
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');
546     }
547   g_free (selected);
548
549   selected = NULL;
550   n_selected = atk_table_get_selected_columns (table, &selected);
551   if (n_selected > 0)
552     {
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');
557     }
558   g_free (selected);
559
560   
561   for (i = 0; i < atk_table_get_n_columns (table); i++)
562     {
563       desc = atk_table_get_column_description (table, i);
564       if (desc)
565         g_string_append_printf (string, "%*scolumn %d description: %s\n", depth, "", i, desc);
566       obj = atk_table_get_column_header (table, i);
567       if (obj)
568         g_string_append_printf (string, "%*scolumn %d header: %s\n", depth, "", i, get_name (obj));
569     }
570
571   for (i = 0; i < atk_table_get_n_rows (table); i++)
572     {
573       desc = atk_table_get_row_description (table, i);
574       if (desc)
575         g_string_append_printf (string, "%*srow %d description: %s\n", depth, "", i, desc);
576       obj = atk_table_get_row_header (table, i);
577       if (obj)
578         g_string_append_printf (string, "%*srow %d header: %s\n", depth, "", i, get_name (obj));
579     }
580
581   g_string_append_printf (string, "%*stable indexes:\n", depth, "");
582   for (i = 0; i < atk_table_get_n_rows (table); i++)
583     {
584       g_string_append_printf (string, "%*s", depth + DEPTH_INCREMENT, "");
585       for (j = 0; j < atk_table_get_n_columns (table); j++)
586         {
587           int id = atk_table_get_index_at (table, i, j);
588
589           obj = atk_object_ref_accessible_child (ATK_OBJECT (table), id);
590           if (j > 0)
591             g_string_append (string, " ");
592
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 ? "✓" : "⚠",
596                                   get_name (obj));
597         }
598       g_string_append (string, "\n");
599     }
600 }
601
602 static void
603 dump_accessible (AtkObject     *accessible,
604                  guint          depth,
605                  GString       *string)
606 {
607   guint i;
608
609   g_string_append_printf (string, "%*s%s\n", depth, "", get_name (accessible));
610   depth += DEPTH_INCREMENT;
611
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));
624
625   if (ATK_IS_COMPONENT (accessible))
626     dump_atk_component (ATK_COMPONENT (accessible), depth, string);
627
628   if (ATK_IS_TEXT (accessible))
629     dump_atk_text (ATK_TEXT (accessible), depth, string);
630
631   if (ATK_IS_IMAGE (accessible))
632     dump_atk_image (ATK_IMAGE (accessible), depth, string);
633
634   if (ATK_IS_ACTION (accessible))
635     dump_atk_action (ATK_ACTION (accessible), depth, string);
636
637   if (ATK_IS_SELECTION (accessible))
638     dump_atk_selection (ATK_SELECTION (accessible), depth, string);
639
640   if (ATK_IS_VALUE (accessible))
641     dump_atk_value (ATK_VALUE (accessible), depth, string);
642
643   if (ATK_IS_HYPERLINK_IMPL (accessible))
644     dump_atk_hyperlink_impl (ATK_HYPERLINK_IMPL (accessible), depth, string);
645
646   if (ATK_IS_STREAMABLE_CONTENT (accessible))
647     dump_atk_streamable_content (ATK_STREAMABLE_CONTENT (accessible), depth, string);
648
649   if (ATK_IS_TABLE (accessible))
650     dump_atk_table (ATK_TABLE (accessible), depth, string);
651
652   for (i = 0; i < atk_object_get_n_accessible_children (accessible); i++)
653     {
654       AtkObject *child = atk_object_ref_accessible_child (accessible, i);
655       dump_accessible (child, depth, string);
656       g_object_unref (child);
657     }
658 }
659
660 static GtkWidget *
661 builder_get_toplevel (GtkBuilder *builder)
662 {
663   GSList *list, *walk;
664   GtkWidget *window = NULL;
665
666   list = gtk_builder_get_objects (builder);
667   for (walk = list; walk; walk = walk->next)
668     {
669       if (GTK_IS_WINDOW (walk->data) &&
670           gtk_widget_get_parent (walk->data) == NULL)
671         {
672           window = walk->data;
673           break;
674         }
675     }
676   
677   g_slist_free (list);
678
679   return window;
680 }
681
682 static void
683 dump_ui_file (const char *ui_file,
684               GString *string)
685 {
686   GtkWidget *window;
687   GtkBuilder *builder;
688   GError *error = NULL;
689
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);
695   g_assert (window);
696
697   gtk_widget_show (window);
698
699   dump_accessible (gtk_widget_get_accessible (window), 0, string);
700   gtk_widget_destroy (window);
701 }
702
703 static void
704 dump_to_stdout (GFile *file)
705 {
706   char *ui_file;
707   GString *dump;
708
709   ui_file = g_file_get_path (file);
710   dump = g_string_new ("");
711
712   dump_ui_file (ui_file, dump);
713   g_print ("%s", dump->str);
714
715   g_string_free (dump, TRUE);
716   g_free (ui_file);
717 }
718
719 static void
720 test_ui_file (GFile *file)
721 {
722   char *ui_file, *a11y_file;
723   GString *dump;
724   GError *error = NULL;
725
726   ui_file = g_file_get_path (file);
727   a11y_file = get_test_file (ui_file, ".txt", TRUE);
728   dump = g_string_new ("");
729
730   dump_ui_file (ui_file, dump);
731
732   if (a11y_file)
733     {
734       char *diff = diff_with_file (a11y_file, dump->str, dump->len, &error);
735       g_assert_no_error (error);
736
737       if (diff && diff[0])
738         {
739           g_test_message ("Contents don't match expected contents:\n%s", diff);
740           g_test_fail ();
741           g_free (diff);
742         }
743     }
744   else if (dump->str[0])
745     {
746       g_test_message ("Expected a reference file:\n%s", dump->str);
747       g_test_fail ();
748     }
749
750   g_string_free (dump, TRUE);
751   g_free (a11y_file);
752   g_free (ui_file);
753 }
754
755 static void
756 add_test_for_file (GFile *file)
757 {
758   g_test_add_vtable (g_file_get_path (file),
759                      0,
760                      g_object_ref (file),
761                      (GTestFixtureFunc) setup_test,
762                      (GTestFixtureFunc) test_ui_file,
763                      (GTestFixtureFunc) g_object_unref);
764 }
765
766 static int
767 compare_files (gconstpointer a, gconstpointer b)
768 {
769   GFile *file1 = G_FILE (a);
770   GFile *file2 = G_FILE (b);
771   char *path1, *path2;
772   int result;
773
774   path1 = g_file_get_path (file1);
775   path2 = g_file_get_path (file2);
776
777   result = strcmp (path1, path2);
778
779   g_free (path1);
780   g_free (path2);
781
782   return result;
783 }
784
785 static void
786 add_tests_for_files_in_directory (GFile *dir)
787 {
788   GFileEnumerator *enumerator;
789   GFileInfo *info;
790   GList *files;
791   GError *error = NULL;
792
793   enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
794   g_assert_no_error (error);
795   files = NULL;
796
797   while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
798     {
799       const char *filename;
800
801       filename = g_file_info_get_name (info);
802
803       if (!g_str_has_suffix (filename, ".ui"))
804         {
805           g_object_unref (info);
806           continue;
807         }
808
809       files = g_list_prepend (files, g_file_get_child (dir, filename));
810
811       g_object_unref (info);
812     }
813   
814   g_assert_no_error (error);
815   g_object_unref (enumerator);
816
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);
820 }
821
822 int
823 main (int argc, char **argv)
824 {
825   gtk_test_init (&argc, &argv);
826
827   if (argc < 2)
828     {
829       const char *basedir;
830       GFile *dir;
831
832       if (g_getenv ("srcdir"))
833         basedir = g_getenv ("srcdir");
834       else
835         basedir = ".";
836         
837       dir = g_file_new_for_path (basedir);
838       
839       add_tests_for_files_in_directory (dir);
840
841       g_object_unref (dir);
842     }
843   else if (argc == 3 && strcmp (argv[1], "--generate") == 0)
844     {
845       GFile *file = g_file_new_for_commandline_arg (argv[2]);
846
847       dump_to_stdout (file);
848
849       g_object_unref (file);
850
851       return 0;
852     }
853   else
854     {
855       guint i;
856
857       for (i = 1; i < argc; i++)
858         {
859           GFile *file = g_file_new_for_commandline_arg (argv[i]);
860
861           add_test_for_file (file);
862
863           g_object_unref (file);
864         }
865     }
866
867   return g_test_run ();
868 }
869