]> Pileus Git - ~andy/gtk/blob - tests/a11y/accessibility-dump.c
Use G_VALUE_INIT
[~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 int unnamed_object_count;
104
105 static void
106 setup_test (void)
107 {
108   unnamed_object_count = 0;
109 }
110
111 static const char *
112 get_name (AtkObject *accessible)
113 {
114   char *name;
115
116   name = g_object_get_data (G_OBJECT (accessible), "gtk-accessibility-dump-name");
117   if (name)
118     return name;
119
120   if (GTK_IS_ACCESSIBLE (accessible))
121     {
122       GtkWidget *widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
123
124       name = g_strdup (gtk_buildable_get_name (GTK_BUILDABLE (widget)));
125     }
126
127   if (name == NULL && ATK_IS_TEXT (accessible))
128     {
129       name = atk_text_get_text (ATK_TEXT (accessible), 0, -1);
130     }
131
132   if (name == NULL)
133     {
134       /* Generate a unique, repeatable name */
135       name = g_strdup_printf ("unnamed-%s-%d", G_OBJECT_TYPE_NAME (accessible), unnamed_object_count++);
136     }
137
138   g_object_set_data_full (G_OBJECT (accessible), "gtk-accessibility-dump-name", name, g_free);
139   return name;
140 }
141
142 static void
143 dump_relation (GString     *string,
144                guint        depth,
145                AtkRelation *relation)
146 {
147   GPtrArray *targets;
148   const char *name;
149   guint i;
150
151   targets = atk_relation_get_target (relation);
152   if (targets == NULL || targets->len == 0)
153     return;
154
155   name = atk_relation_type_get_name (atk_relation_get_relation_type (relation));
156   g_string_append_printf (string, "%*s%s: %s\n", depth, "", name, get_name (g_ptr_array_index (targets, 0)));
157   depth += strlen (name) + 2;
158
159   for (i = 1; i < targets->len; i++)
160     {
161       g_string_append_printf (string, "%*s%s\n", depth, "", get_name (g_ptr_array_index (targets, i)));
162     }
163 }
164
165 static void
166 dump_relation_set (GString        *string,
167                    guint           depth,
168                    AtkRelationSet *set)
169 {
170   guint i;
171
172   if (set == NULL)
173     return;
174
175   for (i = 0; i < atk_relation_set_get_n_relations (set); i++)
176     {
177       AtkRelation *relation;
178       
179       relation = atk_relation_set_get_relation (set, i);
180
181       dump_relation (string, depth, relation);
182     }
183
184   g_object_unref (set);
185 }
186
187 static void
188 dump_state_set (GString     *string,
189                 guint        depth,
190                 AtkStateSet *set)
191 {
192   guint i;
193
194   if (set == NULL)
195     return;
196
197   if (!atk_state_set_is_empty (set))
198     {
199       g_string_append_printf (string, "%*sstate:", depth, "");
200       for (i = 0; i < ATK_STATE_LAST_DEFINED; i++)
201         {
202           if (atk_state_set_contains_state (set, i))
203             g_string_append_printf (string, " %s", atk_state_type_get_name (i));
204         }
205       g_string_append_c (string, '\n');
206     }
207
208   g_object_unref (set);
209 }
210
211 static void
212 dump_attribute (GString      *string,
213                 guint         depth,
214                 AtkAttribute *attribute)
215 {
216   g_string_append_printf (string, "%*s%s: %s\n", depth, "", attribute->name, attribute->value);
217 }
218
219 static void
220 dump_attribute_set (GString         *string,
221                     guint            depth,
222                     AtkAttributeSet *set)
223 {
224   GSList *l;
225   AtkAttribute *attribute;
226
227   for (l = set; l; l = l->next)
228     {
229       attribute = l->data;
230
231       dump_attribute (string, depth, attribute);
232     }
233 }
234
235 static gint
236 compare_attr (gconstpointer a, gconstpointer b)
237 {
238   const AtkAttribute *aattr = a;
239   const AtkAttribute *battr = b;
240
241   return strcmp (aattr->name, battr->name);
242 }
243
244 static void
245 dump_text_attributes (GString         *string,
246                       gint             depth,
247                       const gchar     *name,
248                       AtkAttributeSet *attributes)
249 {
250   GSList *l;
251   AtkAttribute *attr;
252   const gchar *value;
253
254   if (attributes == NULL)
255     return;
256
257   attributes = g_slist_sort (attributes, compare_attr);
258
259   for (l = attributes; l; l = l->next)
260     {
261       attr = l->data;
262       /* don't dump values that depend on the environment */
263       if (strcmp (attr->name, "family-name") == 0 ||
264           strcmp (attr->name, "size") == 0 ||
265           strcmp (attr->name, "weight") == 0 ||
266           strcmp (attr->name, "stretch") == 0 ||
267           strcmp (attr->name, "variant") == 0 ||
268           strcmp (attr->name, "style") == 0 ||
269           strcmp (attr->name, "language") == 0 ||
270           strcmp (attr->name, "fg-color") == 0 ||
271           strcmp (attr->name, "bg-color") == 0 ||
272           strcmp (attr->name, "direction") == 0)
273         value = "<omitted>";
274       else
275         value = attr->value;
276
277       if (name)
278         {
279           /* first time this loop is run */
280           g_string_append_printf (string, "%*s%s: %s: %s\n", depth, "", name, attr->name, value);
281           depth += strlen (name) + 2;
282           name = NULL;
283         }
284       else
285         {
286           /* every other time */
287           g_string_append_printf (string, "%*s%s: %s\n", depth, "", attr->name, value);
288         }
289     }
290
291   atk_attribute_set_free (attributes);
292 }
293
294 extern GType atk_layer_get_type (void);
295
296 static const gchar *
297 layer_name (AtkLayer layer)
298 {
299   GEnumClass *class;
300   GEnumValue *value;
301
302   class = g_type_class_ref (atk_layer_get_type ());
303   value = g_enum_get_value (class, layer);
304   g_type_class_unref (class);
305
306   return value->value_nick;
307 }
308
309 static void
310 dump_atk_component (AtkComponent *atk_component,
311                     guint         depth,
312                     GString      *string)
313 {
314   AtkLayer layer;
315
316   g_string_append_printf (string, "%*s<AtkComponent>\n", depth, "");
317
318   layer = atk_component_get_layer (atk_component);
319   g_string_append_printf (string, "%*slayer: %s\n", depth, "", layer_name (layer));
320
321   g_string_append_printf (string, "%*salpha: %g\n", depth, "", atk_component_get_alpha (atk_component));
322 }
323
324 static void
325 dump_atk_text (AtkText *atk_text,
326                guint    depth,
327                GString *string)
328 {
329   gchar *text;
330   gint i, start, end;
331
332   g_string_append_printf (string, "%*s<AtkText>\n", depth, "");
333
334   text = atk_text_get_text (atk_text, 0, -1);
335   g_string_append_printf (string, "%*stext: %s\n", depth, "", text);
336   g_free (text);
337
338   g_string_append_printf (string, "%*scharacter count: %d\n", depth, "", atk_text_get_character_count (atk_text));
339
340   g_string_append_printf (string, "%*scaret offset: %d\n", depth, "", atk_text_get_caret_offset (atk_text));
341
342   for (i = 0; i < atk_text_get_n_selections (atk_text); i++)
343     {
344       text = atk_text_get_selection (atk_text, i, &start, &end);
345       if (text)
346         g_string_append_printf (string, "%*sselection %d: (%d, %d) %s\n", depth, "", i, start, end, text);
347       g_free (text);
348     }
349
350   dump_text_attributes (string, depth, "default attributes", atk_text_get_default_attributes (atk_text));
351 }
352
353 static void
354 dump_atk_image (AtkImage *atk_image,
355                 guint     depth,
356                 GString  *string)
357 {
358   gint width, height;
359
360   g_string_append_printf (string, "%*s<AtkImage>\n", depth, "");
361
362   atk_image_get_image_size (atk_image, &width, &height);
363   g_string_append_printf (string, "%*simage size: %d x %d\n", depth, "", width, height);
364
365   g_string_append_printf (string, "%*simage description: %s\n", depth, "", atk_image_get_image_description (atk_image));
366 }
367
368 static void
369 dump_atk_action (AtkAction *atk_action,
370                  guint      depth,
371                  GString   *string)
372 {
373   gint i;
374
375   g_string_append_printf (string, "%*s<AtkAction>\n", depth, "");
376
377   for (i = 0; i < atk_action_get_n_actions (atk_action); i++)
378     {
379       if (atk_action_get_name (atk_action, i))
380         g_string_append_printf (string, "%*saction %d name: %s\n", depth, "", i, atk_action_get_name (atk_action, i));
381       if (atk_action_get_description (atk_action, i))
382         g_string_append_printf (string, "%*saction %d description: %s\n", depth, "", i, atk_action_get_description (atk_action, i));
383       if (atk_action_get_keybinding (atk_action, i))
384         g_string_append_printf (string, "%*saction %d keybinding: %s\n", depth, "", i, atk_action_get_keybinding (atk_action, i));
385     }
386 }
387
388 static void
389 dump_atk_selection (AtkSelection *atk_selection,
390                     guint         depth,
391                     GString      *string)
392 {
393   guint n_selections, n_counted_selections;
394   gint i;
395
396   g_string_append_printf (string, "%*s<AtkSelection>\n", depth, "");
397
398   n_selections = atk_selection_get_selection_count (atk_selection);
399
400   n_counted_selections = 0;
401   for (i = 0; i < atk_object_get_n_accessible_children (ATK_OBJECT (atk_selection)); i++)
402     {
403       if (atk_selection_is_child_selected (atk_selection, i))
404         {
405           AtkObject *object = atk_object_ref_accessible_child (ATK_OBJECT (atk_selection), i);
406           
407           g_assert (object);
408           
409           if (n_counted_selections == 0)
410             {
411               g_string_append_printf (string, "%*sselected children: %s\n", depth, "", get_name (object));
412               depth += strlen ("selected children: ");
413             }
414           else
415             g_string_append_printf (string, "%*s%s\n", depth, "", get_name (object));
416           n_counted_selections++;
417         }
418     }
419   
420   g_assert_cmpint (n_selections, ==, n_counted_selections);
421   g_assert_cmpint (n_selections, ==, atk_selection_get_selection_count (atk_selection));
422 }
423
424 static void
425 dump_atk_value (AtkValue *atk_value,
426                 guint     depth,
427                 GString  *string)
428 {
429   GValue value = G_VALUE_INIT;
430   GValue svalue = G_VALUE_INIT;
431
432   g_string_append_printf (string, "%*s<AtkValue>\n", depth, "");
433
434   g_value_init (&value, G_TYPE_DOUBLE);
435   g_value_init (&svalue, G_TYPE_STRING);
436
437   atk_value_get_minimum_value (atk_value, &value);
438   if (g_value_transform (&value, &svalue))
439     g_string_append_printf (string, "%*sminimum value: %s\n", depth, "", g_value_get_string (&svalue));
440   else
441     g_string_append_printf (string, "%*sminimum value: <%s>\n", depth, "", G_VALUE_TYPE_NAME (&value));
442
443   g_value_reset (&value);
444   g_value_reset (&svalue);
445
446   atk_value_get_maximum_value (atk_value, &value);
447   if (g_value_transform (&value, &svalue))
448     g_string_append_printf (string, "%*smaximum value: %s\n", depth, "", g_value_get_string (&svalue));
449   else
450     g_string_append_printf (string, "%*smaximum value: <%s>\n", depth, "", G_VALUE_TYPE_NAME (&value));
451
452   g_value_reset (&value);
453   g_value_reset (&svalue);
454
455   atk_value_get_current_value (atk_value, &value);
456   if (g_value_transform (&value, &svalue))
457     g_string_append_printf (string, "%*scurrent value: %s\n", depth, "", g_value_get_string (&svalue));
458   else
459     g_string_append_printf (string, "%*scurrent value: %s\n", depth, "", G_VALUE_TYPE_NAME (&value));
460
461   g_value_reset (&value);
462   g_value_reset (&svalue);
463
464   atk_value_get_minimum_increment (atk_value, &value);
465   if (g_value_transform (&value, &svalue))
466     g_string_append_printf (string, "%*sminimum increment: %s\n", depth, "", g_value_get_string (&svalue));
467   else
468     g_string_append_printf (string, "%*sminimum increment: %s\n", depth, "", G_VALUE_TYPE_NAME (&value));
469
470   g_value_reset (&value);
471   g_value_reset (&svalue);
472 }
473
474 static void
475 dump_atk_hyperlink_impl (AtkHyperlinkImpl *impl,
476                          guint             depth,
477                          GString          *string)
478 {
479   AtkHyperlink *atk_link;
480   gint i;
481
482   g_string_append_printf (string, "%*s<AtkHyperlinkImpl>\n", depth, "");
483
484   atk_link = atk_hyperlink_impl_get_hyperlink (impl);
485
486   g_string_append_printf (string, "%*sanchors:", depth, "");
487
488   for (i = 0; i < atk_hyperlink_get_n_anchors (atk_link); i++)
489     {
490       gchar *uri;
491
492       uri = atk_hyperlink_get_uri (atk_link, i);
493       g_string_append_printf (string, " %s", uri);
494       g_free (uri);
495     }
496   g_string_append_c (string, '\n');
497
498   g_object_unref (atk_link);
499 }
500
501 static void
502 dump_atk_streamable_content (AtkStreamableContent *content,
503                              guint                 depth,
504                              GString              *string)
505 {
506   gint i;
507
508   g_string_append_printf (string, "%*s<AtkStreamableContent>\n", depth, "");
509
510   g_string_append_printf (string, "%*smime types:", depth, "");
511   for (i = 0; i < atk_streamable_content_get_n_mime_types (content); i++)
512     g_string_append_printf (string, " %s", atk_streamable_content_get_mime_type (content, i));
513   g_string_append_c (string, '\n');
514 }
515
516 static void dump_accessible (AtkObject *accessible,
517                              guint      depth,
518                              GString   *string);
519
520 static void
521 dump_atk_table (AtkTable *table,
522                 guint     depth,
523                 GString  *string)
524 {
525   gint *selected;
526   gint n_selected;
527   gint i;
528   AtkObject *obj;
529   const gchar *desc;
530
531   g_string_append_printf (string, "%*s<AtkTable>\n", depth, "");
532
533   obj = atk_table_get_summary (table);
534   if (obj)
535     {
536       g_string_append_printf (string, "%*s<summary>\n", depth, "");
537       dump_accessible (obj, depth, string);
538     }
539
540   obj = atk_table_get_caption (table);
541   if (obj)
542     {
543       g_string_append_printf (string, "%*s<caption>\n", depth, "");
544       dump_accessible (obj, depth, string);
545     }
546
547   g_string_append_printf (string, "%*srows: %d\n", depth, "", atk_table_get_n_rows (table));
548   g_string_append_printf (string, "%*scolumns: %d\n", depth, "", atk_table_get_n_columns (table));
549
550   selected = NULL;
551   n_selected = atk_table_get_selected_rows (table, &selected);
552   if (n_selected > 0)
553     {
554       g_string_append_printf (string, "%*sselected rows:", depth, "");
555       for (i = 0; i < n_selected; i++)
556         g_string_append_printf (string, " %d", selected[i]);
557       g_string_append_c (string, '\n');
558     }
559   g_free (selected);
560
561   selected = NULL;
562   n_selected = atk_table_get_selected_columns (table, &selected);
563   if (n_selected > 0)
564     {
565       g_string_append_printf (string, "%*sselected columns:", depth, "");
566       for (i = 0; i < n_selected; i++)
567         g_string_append_printf (string, " %d", selected[i]);
568       g_string_append_c (string, '\n');
569     }
570   g_free (selected);
571
572   
573   for (i = 0; i < atk_table_get_n_columns (table); i++)
574     {
575       desc = atk_table_get_column_description (table, i);
576       if (desc)
577         g_string_append_printf (string, "%*scolumn %d description: %s\n", depth, "", i, desc);
578       obj = atk_table_get_column_header (table, i);
579       if (obj)
580         {
581           g_string_append_printf (string, "%*s<column %d header>\n", depth, "", i);
582           dump_accessible (obj, depth, string);
583         }
584     }
585
586   for (i = 0; i < atk_table_get_n_rows (table); i++)
587     {
588       desc = atk_table_get_row_description (table, i);
589       if (desc)
590         g_string_append_printf (string, "%*srow %d description: %s\n", depth, "", i, desc);
591       obj = atk_table_get_row_header (table, i);
592       if (obj)
593         {
594           g_string_append_printf (string, "%*s<row %d header>\n", depth, "", i);
595           dump_accessible (obj, depth, string);
596         }
597     }
598 }
599
600 static void
601 dump_accessible (AtkObject     *accessible,
602                  guint          depth,
603                  GString       *string)
604 {
605   guint i;
606
607   g_string_append_printf (string, "%*s%s\n", depth, "", get_name (accessible));
608   depth += DEPTH_INCREMENT;
609
610   g_string_append_printf (string, "%*s\"%s\"\n", depth, "", atk_role_get_name (atk_object_get_role (accessible)));
611   if (GTK_IS_ACCESSIBLE (atk_object_get_parent (accessible)))
612     g_string_append_printf (string, "%*sparent: %s\n", depth, "", get_name (atk_object_get_parent (accessible)));
613   if (atk_object_get_index_in_parent (accessible) != -1)
614     g_string_append_printf (string, "%*sindex: %d\n", depth, "", atk_object_get_index_in_parent (accessible));
615   if (atk_object_get_name (accessible))
616     g_string_append_printf (string, "%*sname: %s\n", depth, "", atk_object_get_name (accessible));
617   if (atk_object_get_description (accessible))
618     g_string_append_printf (string, "%*sdescription: %s\n", depth, "", atk_object_get_description (accessible));
619   dump_relation_set (string, depth, atk_object_ref_relation_set (accessible));
620   dump_state_set (string, depth, atk_object_ref_state_set (accessible));
621   dump_attribute_set (string, depth, atk_object_get_attributes (accessible));
622
623   if (ATK_IS_COMPONENT (accessible))
624     dump_atk_component (ATK_COMPONENT (accessible), depth, string);
625
626   if (ATK_IS_TEXT (accessible))
627     dump_atk_text (ATK_TEXT (accessible), depth, string);
628
629   if (ATK_IS_IMAGE (accessible))
630     dump_atk_image (ATK_IMAGE (accessible), depth, string);
631
632   if (ATK_IS_ACTION (accessible))
633     dump_atk_action (ATK_ACTION (accessible), depth, string);
634
635   if (ATK_IS_SELECTION (accessible))
636     dump_atk_selection (ATK_SELECTION (accessible), depth, string);
637
638   if (ATK_IS_VALUE (accessible))
639     dump_atk_value (ATK_VALUE (accessible), depth, string);
640
641   if (ATK_IS_HYPERLINK_IMPL (accessible))
642     dump_atk_hyperlink_impl (ATK_HYPERLINK_IMPL (accessible), depth, string);
643
644   if (ATK_IS_STREAMABLE_CONTENT (accessible))
645     dump_atk_streamable_content (ATK_STREAMABLE_CONTENT (accessible), depth, string);
646
647   if (ATK_IS_TABLE (accessible))
648     dump_atk_table (ATK_TABLE (accessible), depth, string);
649
650   for (i = 0; i < atk_object_get_n_accessible_children (accessible); i++)
651     {
652       AtkObject *child = atk_object_ref_accessible_child (accessible, i);
653       dump_accessible (child, depth, string);
654       g_object_unref (child);
655     }
656 }
657
658 static GtkWidget *
659 builder_get_toplevel (GtkBuilder *builder)
660 {
661   GSList *list, *walk;
662   GtkWidget *window = NULL;
663
664   list = gtk_builder_get_objects (builder);
665   for (walk = list; walk; walk = walk->next)
666     {
667       if (GTK_IS_WINDOW (walk->data) &&
668           gtk_widget_get_parent (walk->data) == NULL)
669         {
670           window = walk->data;
671           break;
672         }
673     }
674   
675   g_slist_free (list);
676
677   return window;
678 }
679
680 static void
681 dump_ui_file (const char *ui_file,
682               GString *string)
683 {
684   GtkWidget *window;
685   GtkBuilder *builder;
686   GError *error = NULL;
687
688   builder = gtk_builder_new ();
689   gtk_builder_add_from_file (builder, ui_file, &error);
690   g_assert_no_error (error);
691   window = builder_get_toplevel (builder);
692   g_object_unref (builder);
693   g_assert (window);
694
695   gtk_widget_show (window);
696
697   dump_accessible (gtk_widget_get_accessible (window), 0, string);
698   gtk_widget_destroy (window);
699 }
700
701 static void
702 dump_to_stdout (GFile *file)
703 {
704   char *ui_file;
705   GString *dump;
706
707   ui_file = g_file_get_path (file);
708   dump = g_string_new ("");
709
710   dump_ui_file (ui_file, dump);
711   g_print ("%s", dump->str);
712
713   g_string_free (dump, TRUE);
714   g_free (ui_file);
715 }
716
717 static void
718 test_ui_file (GFile *file)
719 {
720   char *ui_file, *a11y_file;
721   GString *dump;
722   GError *error = NULL;
723
724   ui_file = g_file_get_path (file);
725   a11y_file = get_test_file (ui_file, ".txt", TRUE);
726   dump = g_string_new ("");
727
728   dump_ui_file (ui_file, dump);
729
730   if (a11y_file)
731     {
732       char *diff = diff_with_file (a11y_file, dump->str, dump->len, &error);
733       g_assert_no_error (error);
734
735       if (diff && diff[0])
736         {
737           g_test_message ("Contents don't match expected contents:\n%s", diff);
738           g_test_fail ();
739           g_free (diff);
740         }
741     }
742   else if (dump->str[0])
743     {
744       g_test_message ("Expected a reference file:\n%s", dump->str);
745       g_test_fail ();
746     }
747
748   g_string_free (dump, TRUE);
749   g_free (a11y_file);
750   g_free (ui_file);
751 }
752
753 static void
754 add_test_for_file (GFile *file)
755 {
756   g_test_add_vtable (g_file_get_path (file),
757                      0,
758                      g_object_ref (file),
759                      (GTestFixtureFunc) setup_test,
760                      (GTestFixtureFunc) test_ui_file,
761                      (GTestFixtureFunc) g_object_unref);
762 }
763
764 static int
765 compare_files (gconstpointer a, gconstpointer b)
766 {
767   GFile *file1 = G_FILE (a);
768   GFile *file2 = G_FILE (b);
769   char *path1, *path2;
770   int result;
771
772   path1 = g_file_get_path (file1);
773   path2 = g_file_get_path (file2);
774
775   result = strcmp (path1, path2);
776
777   g_free (path1);
778   g_free (path2);
779
780   return result;
781 }
782
783 static void
784 add_tests_for_files_in_directory (GFile *dir)
785 {
786   GFileEnumerator *enumerator;
787   GFileInfo *info;
788   GList *files;
789   GError *error = NULL;
790
791   enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
792   g_assert_no_error (error);
793   files = NULL;
794
795   while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
796     {
797       const char *filename;
798
799       filename = g_file_info_get_name (info);
800
801       if (!g_str_has_suffix (filename, ".ui"))
802         {
803           g_object_unref (info);
804           continue;
805         }
806
807       files = g_list_prepend (files, g_file_get_child (dir, filename));
808
809       g_object_unref (info);
810     }
811   
812   g_assert_no_error (error);
813   g_object_unref (enumerator);
814
815   files = g_list_sort (files, compare_files);
816   g_list_foreach (files, (GFunc) add_test_for_file, NULL);
817   g_list_free_full (files, g_object_unref);
818 }
819
820 int
821 main (int argc, char **argv)
822 {
823   gtk_test_init (&argc, &argv);
824
825   if (argc < 2)
826     {
827       const char *basedir;
828       GFile *dir;
829
830       if (g_getenv ("srcdir"))
831         basedir = g_getenv ("srcdir");
832       else
833         basedir = ".";
834         
835       dir = g_file_new_for_path (basedir);
836       
837       add_tests_for_files_in_directory (dir);
838
839       g_object_unref (dir);
840     }
841   else if (argc == 3 && strcmp (argv[1], "--generate") == 0)
842     {
843       GFile *file = g_file_new_for_commandline_arg (argv[2]);
844
845       dump_to_stdout (file);
846
847       g_object_unref (file);
848
849       return 0;
850     }
851   else
852     {
853       guint i;
854
855       for (i = 1; i < argc; i++)
856         {
857           GFile *file = g_file_new_for_commandline_arg (argv[i]);
858
859           add_test_for_file (file);
860
861           g_object_unref (file);
862         }
863     }
864
865   return g_test_run ();
866 }
867