]> Pileus Git - ~andy/gtk/blob - tests/a11y/text.c
Add some AtkText tests
[~andy/gtk] / tests / a11y / text.c
1 /*
2  * Copyright (C) 2011 Red Hat Inc.
3  *
4  * Author:
5  *      Matthias Clasen <mclasen@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 <gtk/gtk.h>
24
25 typedef struct {
26   gint count;
27   gint position;
28   gint length;
29 } SignalData;
30
31 static void
32 text_deleted (AtkText *atk_text, gint position, gint length, SignalData *data)
33 {
34   data->count++;
35   data->position = position;
36   data->length = length;
37 }
38
39 static void
40 text_inserted (AtkText *atk_text, gint position, gint length, SignalData *data)
41 {
42   data->count++;
43   data->position = position;
44   data->length = length;
45 }
46
47 static void
48 set_text (GtkWidget   *widget,
49           const gchar *text)
50 {
51   if (GTK_IS_LABEL (widget))
52     gtk_label_set_text (GTK_LABEL (widget), text);
53   else if (GTK_IS_ENTRY (widget))
54     gtk_entry_set_text (GTK_ENTRY (widget), text);
55   else if (GTK_IS_TEXT_VIEW (widget))
56     gtk_text_buffer_set_text (gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget)), text, -1);
57   else
58     g_assert_not_reached ();
59 }
60
61 static void
62 test_text_changed (GtkWidget *widget)
63 {
64   AtkText *atk_text;
65   const gchar *text = "Text goes here";
66   SignalData delete_data;
67   SignalData insert_data;
68
69   atk_text = ATK_TEXT (gtk_widget_get_accessible (widget));
70
71   delete_data.count = 0;
72   insert_data.count = 0;
73
74   g_signal_connect (atk_text, "text_changed::delete",
75                     G_CALLBACK (text_deleted), &delete_data);
76   g_signal_connect (atk_text, "text_changed::insert",
77                     G_CALLBACK (text_inserted), &insert_data);
78
79   set_text (widget, text);
80
81   g_assert_cmpint (delete_data.count, >, 0);
82   g_assert_cmpint (delete_data.position, ==, 0);
83   g_assert_cmpint (delete_data.length, ==, -1);
84
85   g_assert_cmpint (insert_data.count, >, 0);
86   g_assert_cmpint (insert_data.position, ==, 0);
87   g_assert_cmpint (insert_data.length, ==, -1);
88
89   g_signal_handlers_disconnect_by_func (atk_text, G_CALLBACK (text_deleted), &delete_data);
90   g_signal_handlers_disconnect_by_func (atk_text, G_CALLBACK (text_inserted), &insert_data);
91 }
92
93 typedef struct {
94   gint offset;
95   AtkTextBoundary boundary;
96   const gchar *word;
97   gint start;
98   gint end;
99 } Word;
100
101 static void
102 test_words (GtkWidget *widget)
103 {
104   AtkText *atk_text;
105   const gchar *text = "This is a medium-size test string, including some \303\204\303\226\303\234 and 123 for good measure.";
106   const gchar *expected_words[] = {
107     "This ",
108     "is ",
109     "a ",
110     "medium-",
111     "size ",
112     "test ",
113     "string, ",
114     "including ",
115     "some ",
116     "\303\204\303\226\303\234 ",
117     "and ",
118     "123 ",
119     "for ",
120     "good ",
121     "measure.",
122     NULL
123   };
124   gint start, end;
125   gchar *word;
126   gchar *last_word;
127   gint offset;
128   gint i;
129
130   atk_text = ATK_TEXT (gtk_widget_get_accessible (widget));
131
132   set_text (widget, text);
133
134   last_word = NULL;
135   i = 0;
136   for (offset = 0; offset < g_utf8_strlen (text, -1); offset++)
137     {
138       word = atk_text_get_text_at_offset (atk_text,
139                                           offset,
140                                           ATK_TEXT_BOUNDARY_WORD_START,
141                                           &start, &end);
142       if (g_strcmp0 (last_word, word) != 0)
143         {
144           g_assert_cmpstr (word, ==, expected_words[i]);
145           g_free (last_word);
146           last_word = word;
147           i++;
148         }
149     }
150   g_free (last_word);
151   g_assert (expected_words[i] == 0);
152 }
153
154 static void
155 setup_test (GtkWidget *widget)
156 {
157   set_text (widget, "");
158 }
159
160 static void
161 add_text_test (const gchar      *prefix,
162                GTestFixtureFunc  test_func,
163                GtkWidget        *widget)
164 {
165   gchar *path;
166
167   path = g_strdup_printf ("%s/%s", prefix, G_OBJECT_TYPE_NAME (widget));
168   g_test_add_vtable (path,
169                      0,
170                      g_object_ref (widget),
171                      (GTestFixtureFunc) setup_test,
172                      (GTestFixtureFunc) test_func,
173                      (GTestFixtureFunc) g_object_unref);
174   g_free (path);
175 }
176
177 static void
178 add_text_tests (GtkWidget *widget)
179 {
180   g_object_ref_sink (widget);
181   add_text_test ("/text/words", (GTestFixtureFunc) test_words, widget);
182   add_text_test ("/text/changed", (GTestFixtureFunc) test_text_changed, widget);
183   g_object_unref (widget);
184 }
185
186 int
187 main (int argc, char *argv[])
188 {
189   gtk_test_init (&argc, &argv, NULL);
190
191   add_text_tests (gtk_text_view_new ());
192   add_text_tests (gtk_entry_new ());
193   add_text_tests (gtk_label_new (""));
194
195   return g_test_run ();
196 }