]> Pileus Git - ~andy/gtk/blob - tests/a11y/text.c
Merge branch 'bgo593793-filechooser-recent-folders-master'
[~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 #include <string.h>
25
26 static void
27 set_text (GtkWidget   *widget,
28           const gchar *text)
29 {
30   if (GTK_IS_LABEL (widget))
31     gtk_label_set_text (GTK_LABEL (widget), text);
32   else if (GTK_IS_ENTRY (widget))
33     gtk_entry_set_text (GTK_ENTRY (widget), text);
34   else if (GTK_IS_TEXT_VIEW (widget))
35     gtk_text_buffer_set_text (gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget)), text, -1);
36   else
37     g_assert_not_reached ();
38 }
39
40 static void
41 test_basic (GtkWidget *widget)
42 {
43   AtkText *atk_text;
44   const gchar *text = "Text goes here";
45   gchar *ret;
46   gint count;
47   gunichar c;
48
49   atk_text = ATK_TEXT (gtk_widget_get_accessible (widget));
50
51   set_text (widget, text);
52   ret = atk_text_get_text (atk_text, 5, 9);
53   g_assert_cmpstr (ret, ==, "goes");
54   g_free (ret);
55
56   ret = atk_text_get_text (atk_text, 0, 14);
57   g_assert_cmpstr (ret, ==, text);
58   g_free (ret);
59
60   ret = atk_text_get_text (atk_text, 0, -1);
61   g_assert_cmpstr (ret, ==, text);
62   g_free (ret);
63
64   count = atk_text_get_character_count (atk_text);
65   g_assert_cmpint (count, ==, g_utf8_strlen (text, -1));
66
67   c = atk_text_get_character_at_offset (atk_text, 0);
68   g_assert_cmpint (c, ==, 'T');
69
70   c = atk_text_get_character_at_offset (atk_text, 13);
71   g_assert_cmpint (c, ==, 'e');
72 }
73
74 typedef struct {
75   gint count;
76   gint position;
77   gint length;
78 } SignalData;
79
80 static void
81 text_deleted (AtkText *atk_text, gint position, gint length, SignalData *data)
82 {
83   data->count++;
84   data->position = position;
85   data->length = length;
86 }
87
88 static void
89 text_inserted (AtkText *atk_text, gint position, gint length, SignalData *data)
90 {
91   data->count++;
92   data->position = position;
93   data->length = length;
94 }
95
96 static void
97 test_text_changed (GtkWidget *widget)
98 {
99   AtkText *atk_text;
100   const gchar *text = "Text goes here";
101   const gchar *text2 = "Text again";
102   SignalData delete_data;
103   SignalData insert_data;
104
105   atk_text = ATK_TEXT (gtk_widget_get_accessible (widget));
106
107   delete_data.count = 0;
108   insert_data.count = 0;
109
110   g_signal_connect (atk_text, "text_changed::delete",
111                     G_CALLBACK (text_deleted), &delete_data);
112   g_signal_connect (atk_text, "text_changed::insert",
113                     G_CALLBACK (text_inserted), &insert_data);
114
115   set_text (widget, text);
116
117   g_assert_cmpint (delete_data.count, ==, 0);
118
119   g_assert_cmpint (insert_data.count, ==, 1);
120   g_assert_cmpint (insert_data.position, ==, 0);
121   g_assert_cmpint (insert_data.length, ==, g_utf8_strlen (text, -1));
122
123   set_text (widget, text2);
124
125   g_assert_cmpint (delete_data.count, ==, 1);
126   g_assert_cmpint (delete_data.position, ==, 0);
127   g_assert_cmpint (delete_data.length, ==, g_utf8_strlen (text, -1));
128
129   g_assert_cmpint (insert_data.count, ==, 2);
130   g_assert_cmpint (insert_data.position, ==, 0);
131   g_assert_cmpint (insert_data.length, ==, g_utf8_strlen (text2, -1));
132
133   set_text (widget, "");
134
135   g_assert_cmpint (delete_data.count, ==, 2);
136   g_assert_cmpint (delete_data.position, ==, 0);
137   g_assert_cmpint (delete_data.length, ==, g_utf8_strlen (text2, -1));
138
139   g_assert_cmpint (insert_data.count, ==, 2);
140
141   g_signal_handlers_disconnect_by_func (atk_text, G_CALLBACK (text_deleted), &delete_data);
142   g_signal_handlers_disconnect_by_func (atk_text, G_CALLBACK (text_inserted), &insert_data);
143 }
144
145 typedef struct {
146   gint gravity;
147   gint offset;
148   AtkTextBoundary boundary;
149   gint start;
150   gint end;
151   const gchar *word;
152 } Word;
153
154 #ifdef DUMP_RESULTS
155 static const gchar *
156 boundary (AtkTextBoundary b)
157 {
158   switch (b)
159     {
160     case ATK_TEXT_BOUNDARY_CHAR:           return "ATK_TEXT_BOUNDARY_CHAR,          ";
161     case ATK_TEXT_BOUNDARY_WORD_START:     return "ATK_TEXT_BOUNDARY_WORD_START,    ";
162     case ATK_TEXT_BOUNDARY_WORD_END:       return "ATK_TEXT_BOUNDARY_WORD_END,      ";
163     case ATK_TEXT_BOUNDARY_SENTENCE_START: return "ATK_TEXT_BOUNDARY_SENTENCE_START,";
164     case ATK_TEXT_BOUNDARY_SENTENCE_END:   return "ATK_TEXT_BOUNDARY_SENTENCE_END,  ";
165     case ATK_TEXT_BOUNDARY_LINE_START:     return "ATK_TEXT_BOUNDARY_LINE_START,    ";
166     case ATK_TEXT_BOUNDARY_LINE_END:       return "ATK_TEXT_BOUNDARY_LINE_END,      ";
167     default: g_assert_not_reached ();
168     }
169 }
170
171 static const gchar *
172 gravity (gint g)
173 {
174   if (g < 0) return "before";
175   else if (g > 0) return "after";
176   else return "around";
177 }
178
179 const gchar *
180 char_rep (gunichar c)
181 {
182   static gchar out[6];
183
184   switch (c)
185     {
186       case '\n': return "\\n";
187       case 196: return "?";
188       case 214: return "?";
189       case 220: return "?";
190       default:
191         memset (out, 0, 6);
192         g_unichar_to_utf8 (c, out);
193         return out;
194     }
195 }
196
197 gchar *
198 escape (const gchar *p)
199 {
200   GString *s;
201
202   s = g_string_new ("");
203
204   while (*p)
205     {
206       if (*p == '\n')
207         g_string_append (s, "\\n");
208       else
209         g_string_append_c (s, *p);
210       p++;
211     }
212
213   return g_string_free (s, FALSE);
214 }
215 #endif
216
217 #ifdef SHOW_TEXT_ATTRIBUTES
218 static void
219 show_text_attributes (PangoLayout *l)
220 {
221   const PangoLogAttr *attr;
222   gint n_attrs;
223   const gchar *s;
224   gchar e;
225   const gchar *p;
226   gint i;
227   const gchar *text;
228   GSList *lines, *li;
229   glong so, eo;
230
231   printf ("\n");
232
233   text = pango_layout_get_text (l);
234   attr = pango_layout_get_log_attrs_readonly (l, &n_attrs);
235
236   p = text;
237   while (*p)
238     {
239       s = char_rep (g_utf8_get_char (p));
240       printf (" %s", s);
241       p = g_utf8_next_char (p);
242     }
243   printf ("\n");
244   p = text;
245   i = 0;
246   do
247     {
248       if (*p)
249         s = char_rep (g_utf8_get_char (p));
250       else
251         s = "";
252       if (attr[i].is_word_start && attr[i].is_word_end)
253         e = '|';
254       else if (attr[i].is_word_start)
255         e = '<';
256       else if (attr[i].is_word_end)
257         e = '>';
258       else
259         e = ' ';
260       printf ("%c%*s", e, strlen (s), "");
261       if (*p)
262         p = g_utf8_next_char (p);
263       i++;
264     }
265   while (*p || i < n_attrs);
266   printf ("\n");
267
268   p = text;
269   i = 0;
270   do
271     {
272       if (*p)
273         s = char_rep (g_utf8_get_char (p));
274       else
275         s = "";
276       if (attr[i].is_sentence_start && attr[i].is_sentence_end)
277         e = '|';
278       else if (attr[i].is_sentence_start)
279         e = '<';
280       else if (attr[i].is_sentence_end)
281         e = '>';
282       else
283         e = ' ';
284       printf ("%c%*s", e, strlen (s), "");
285       if (*p)
286         p = g_utf8_next_char (p);
287       i++;
288     }
289   while (*p || i < n_attrs);
290   printf ("\n");
291
292   lines = pango_layout_get_lines_readonly (l);
293   p = text;
294   i = 0;
295   do
296     {
297       gboolean start, end;
298
299       if (*p)
300         s = char_rep (g_utf8_get_char (p));
301       else
302         s = "";
303       start = end = FALSE;
304       for (li = lines; li; li = li->next)
305         {
306           PangoLayoutLine *line = li->data;
307           so = g_utf8_pointer_to_offset (text, text + line->start_index);
308           eo = g_utf8_pointer_to_offset (text, text + line->start_index + line->length);
309           if (so == i)
310             start = TRUE;
311           if (eo == i)
312             end = TRUE;
313         }
314       if (start && end)
315         e = '|';
316       else if (start)
317         e = '<';
318       else if (end)
319         e = '>';
320       else
321         e = ' ';
322       printf ("%c%*s", e, strlen (s), "");
323       if (*p)
324         p = g_utf8_next_char (p);
325       i++;
326     }
327   while (*p || i < n_attrs);
328   printf ("\n");
329 }
330 #endif
331
332 static void
333 test_words (GtkWidget *widget)
334 {
335   AtkText *atk_text;
336   const gchar *text = "abc! def\nghi jkl\nmno";
337   Word expected[] = {
338     { -1,  0, ATK_TEXT_BOUNDARY_CHAR,  0,  0, "" },
339     { -1,  1, ATK_TEXT_BOUNDARY_CHAR,  0,  1, "a" },
340     { -1,  2, ATK_TEXT_BOUNDARY_CHAR,  1,  2, "b" },
341     { -1,  3, ATK_TEXT_BOUNDARY_CHAR,  2,  3, "c" },
342     { -1,  4, ATK_TEXT_BOUNDARY_CHAR,  3,  4, "!" },
343     { -1,  5, ATK_TEXT_BOUNDARY_CHAR,  4,  5, " " },
344     { -1,  6, ATK_TEXT_BOUNDARY_CHAR,  5,  6, "d" },
345     { -1,  7, ATK_TEXT_BOUNDARY_CHAR,  6,  7, "e" },
346     { -1,  8, ATK_TEXT_BOUNDARY_CHAR,  7,  8, "f" },
347     { -1,  9, ATK_TEXT_BOUNDARY_CHAR,  8,  9, "\n" },
348     { -1, 10, ATK_TEXT_BOUNDARY_CHAR,  9, 10, "g" },
349     { -1, 11, ATK_TEXT_BOUNDARY_CHAR, 10, 11, "h" },
350     { -1, 12, ATK_TEXT_BOUNDARY_CHAR, 11, 12, "i" },
351     { -1, 13, ATK_TEXT_BOUNDARY_CHAR, 12, 13, " " },
352     { -1, 14, ATK_TEXT_BOUNDARY_CHAR, 13, 14, "j" },
353     { -1, 15, ATK_TEXT_BOUNDARY_CHAR, 14, 15, "k" },
354     { -1, 16, ATK_TEXT_BOUNDARY_CHAR, 15, 16, "l" },
355     { -1, 17, ATK_TEXT_BOUNDARY_CHAR, 16, 17, "\n" },
356     { -1, 18, ATK_TEXT_BOUNDARY_CHAR, 17, 18, "m" },
357     { -1, 19, ATK_TEXT_BOUNDARY_CHAR, 18, 19, "n" },
358     { -1, 20, ATK_TEXT_BOUNDARY_CHAR, 19, 20, "o" },
359     { -1,  0, ATK_TEXT_BOUNDARY_WORD_START,  0,  0, "" },
360     { -1,  1, ATK_TEXT_BOUNDARY_WORD_START,  0,  0, "" },
361     { -1,  2, ATK_TEXT_BOUNDARY_WORD_START,  0,  0, "" },
362     { -1,  3, ATK_TEXT_BOUNDARY_WORD_START,  0,  0, "" },
363     { -1,  4, ATK_TEXT_BOUNDARY_WORD_START,  0,  0, "" },
364     { -1,  5, ATK_TEXT_BOUNDARY_WORD_START,  0,  5, "abc! " },
365     { -1,  6, ATK_TEXT_BOUNDARY_WORD_START,  0,  5, "abc! " },
366     { -1,  7, ATK_TEXT_BOUNDARY_WORD_START,  0,  5, "abc! " },
367     { -1,  8, ATK_TEXT_BOUNDARY_WORD_START,  0,  5, "abc! " },
368     { -1,  9, ATK_TEXT_BOUNDARY_WORD_START,  5,  9, "def\n" },
369     { -1, 10, ATK_TEXT_BOUNDARY_WORD_START,  5,  9, "def\n" },
370     { -1, 11, ATK_TEXT_BOUNDARY_WORD_START,  5,  9, "def\n" },
371     { -1, 12, ATK_TEXT_BOUNDARY_WORD_START,  5,  9, "def\n" },
372     { -1, 13, ATK_TEXT_BOUNDARY_WORD_START,  9, 13, "ghi " },
373     { -1, 14, ATK_TEXT_BOUNDARY_WORD_START,  9, 13, "ghi " },
374     { -1, 15, ATK_TEXT_BOUNDARY_WORD_START,  9, 13, "ghi " },
375     { -1, 16, ATK_TEXT_BOUNDARY_WORD_START,  9, 13, "ghi " },
376     { -1, 17, ATK_TEXT_BOUNDARY_WORD_START, 13, 17, "jkl\n" },
377     { -1, 18, ATK_TEXT_BOUNDARY_WORD_START, 13, 17, "jkl\n" },
378     { -1, 19, ATK_TEXT_BOUNDARY_WORD_START, 13, 17, "jkl\n" },
379     { -1, 20, ATK_TEXT_BOUNDARY_WORD_START, 13, 17, "jkl\n" },
380     { -1,  0, ATK_TEXT_BOUNDARY_WORD_END,  0,  0, "" },
381     { -1,  1, ATK_TEXT_BOUNDARY_WORD_END,  0,  0, "" },
382     { -1,  2, ATK_TEXT_BOUNDARY_WORD_END,  0,  0, "" },
383     { -1,  3, ATK_TEXT_BOUNDARY_WORD_END,  0,  3, "abc" },
384     { -1,  4, ATK_TEXT_BOUNDARY_WORD_END,  0,  3, "abc" },
385     { -1,  5, ATK_TEXT_BOUNDARY_WORD_END,  0,  3, "abc" },
386     { -1,  6, ATK_TEXT_BOUNDARY_WORD_END,  0,  3, "abc" },
387     { -1,  7, ATK_TEXT_BOUNDARY_WORD_END,  0,  3, "abc" },
388     { -1,  8, ATK_TEXT_BOUNDARY_WORD_END,  3,  8, "! def" },
389     { -1,  9, ATK_TEXT_BOUNDARY_WORD_END,  3,  8, "! def" },
390     { -1, 10, ATK_TEXT_BOUNDARY_WORD_END,  3,  8, "! def" },
391     { -1, 11, ATK_TEXT_BOUNDARY_WORD_END,  3,  8, "! def" },
392     { -1, 12, ATK_TEXT_BOUNDARY_WORD_END,  8, 12, "\nghi" },
393     { -1, 13, ATK_TEXT_BOUNDARY_WORD_END,  8, 12, "\nghi" },
394     { -1, 14, ATK_TEXT_BOUNDARY_WORD_END,  8, 12, "\nghi" },
395     { -1, 15, ATK_TEXT_BOUNDARY_WORD_END,  8, 12, "\nghi" },
396     { -1, 16, ATK_TEXT_BOUNDARY_WORD_END, 12, 16, " jkl" },
397     { -1, 17, ATK_TEXT_BOUNDARY_WORD_END, 12, 16, " jkl" },
398     { -1, 18, ATK_TEXT_BOUNDARY_WORD_END, 12, 16, " jkl" },
399     { -1, 19, ATK_TEXT_BOUNDARY_WORD_END, 12, 16, " jkl" },
400     { -1, 20, ATK_TEXT_BOUNDARY_WORD_END, 16, 20, "\nmno" },
401     { -1,  0, ATK_TEXT_BOUNDARY_SENTENCE_START,  0,  0, "" },
402     { -1,  1, ATK_TEXT_BOUNDARY_SENTENCE_START,  0,  0, "" },
403     { -1,  2, ATK_TEXT_BOUNDARY_SENTENCE_START,  0,  0, "" },
404     { -1,  3, ATK_TEXT_BOUNDARY_SENTENCE_START,  0,  0, "" },
405     { -1,  4, ATK_TEXT_BOUNDARY_SENTENCE_START,  0,  0, "" },
406     { -1,  5, ATK_TEXT_BOUNDARY_SENTENCE_START,  0,  5, "abc! " },
407     { -1,  6, ATK_TEXT_BOUNDARY_SENTENCE_START,  0,  5, "abc! " },
408     { -1,  7, ATK_TEXT_BOUNDARY_SENTENCE_START,  0,  5, "abc! " },
409     { -1,  8, ATK_TEXT_BOUNDARY_SENTENCE_START,  0,  5, "abc! " },
410     { -1,  9, ATK_TEXT_BOUNDARY_SENTENCE_START,  5,  9, "def\n" },
411     { -1, 10, ATK_TEXT_BOUNDARY_SENTENCE_START,  5,  9, "def\n" },
412     { -1, 11, ATK_TEXT_BOUNDARY_SENTENCE_START,  5,  9, "def\n" },
413     { -1, 12, ATK_TEXT_BOUNDARY_SENTENCE_START,  5,  9, "def\n" },
414     { -1, 13, ATK_TEXT_BOUNDARY_SENTENCE_START,  5,  9, "def\n" },
415     { -1, 14, ATK_TEXT_BOUNDARY_SENTENCE_START,  5,  9, "def\n" },
416     { -1, 15, ATK_TEXT_BOUNDARY_SENTENCE_START,  5,  9, "def\n" },
417     { -1, 16, ATK_TEXT_BOUNDARY_SENTENCE_START,  5,  9, "def\n" },
418     { -1, 17, ATK_TEXT_BOUNDARY_SENTENCE_START,  9, 17, "ghi jkl\n" },
419     { -1, 18, ATK_TEXT_BOUNDARY_SENTENCE_START,  9, 17, "ghi jkl\n" },
420     { -1, 19, ATK_TEXT_BOUNDARY_SENTENCE_START,  9, 17, "ghi jkl\n" },
421     { -1, 20, ATK_TEXT_BOUNDARY_SENTENCE_START,  9, 17, "ghi jkl\n" },
422     { -1,  0, ATK_TEXT_BOUNDARY_SENTENCE_END,  0,  0, "" },
423     { -1,  1, ATK_TEXT_BOUNDARY_SENTENCE_END,  0,  0, "" },
424     { -1,  2, ATK_TEXT_BOUNDARY_SENTENCE_END,  0,  0, "" },
425     { -1,  3, ATK_TEXT_BOUNDARY_SENTENCE_END,  0,  0, "" },
426     { -1,  4, ATK_TEXT_BOUNDARY_SENTENCE_END,  0,  4, "abc!" },
427     { -1,  5, ATK_TEXT_BOUNDARY_SENTENCE_END,  0,  4, "abc!" },
428     { -1,  6, ATK_TEXT_BOUNDARY_SENTENCE_END,  0,  4, "abc!" },
429     { -1,  7, ATK_TEXT_BOUNDARY_SENTENCE_END,  0,  4, "abc!" },
430     { -1,  8, ATK_TEXT_BOUNDARY_SENTENCE_END,  4,  8, " def" },
431     { -1,  9, ATK_TEXT_BOUNDARY_SENTENCE_END,  4,  8, " def" },
432     { -1, 10, ATK_TEXT_BOUNDARY_SENTENCE_END,  4,  8, " def" },
433     { -1, 11, ATK_TEXT_BOUNDARY_SENTENCE_END,  4,  8, " def" },
434     { -1, 12, ATK_TEXT_BOUNDARY_SENTENCE_END,  4,  8, " def" },
435     { -1, 13, ATK_TEXT_BOUNDARY_SENTENCE_END,  4,  8, " def" },
436     { -1, 14, ATK_TEXT_BOUNDARY_SENTENCE_END,  4,  8, " def" },
437     { -1, 15, ATK_TEXT_BOUNDARY_SENTENCE_END,  4,  8, " def" },
438     { -1, 16, ATK_TEXT_BOUNDARY_SENTENCE_END,  8, 16, "\nghi jkl" },
439     { -1, 17, ATK_TEXT_BOUNDARY_SENTENCE_END,  8, 16, "\nghi jkl" },
440     { -1, 18, ATK_TEXT_BOUNDARY_SENTENCE_END,  8, 16, "\nghi jkl" },
441     { -1, 19, ATK_TEXT_BOUNDARY_SENTENCE_END,  8, 16, "\nghi jkl" },
442     { -1, 20, ATK_TEXT_BOUNDARY_SENTENCE_END, 16, 20, "\nmno" },
443     { -1,  0, ATK_TEXT_BOUNDARY_LINE_START,  0,  0, "" },
444     { -1,  1, ATK_TEXT_BOUNDARY_LINE_START,  0,  0, "" },
445     { -1,  2, ATK_TEXT_BOUNDARY_LINE_START,  0,  0, "" },
446     { -1,  3, ATK_TEXT_BOUNDARY_LINE_START,  0,  0, "" },
447     { -1,  4, ATK_TEXT_BOUNDARY_LINE_START,  0,  0, "" },
448     { -1,  5, ATK_TEXT_BOUNDARY_LINE_START,  0,  0, "" },
449     { -1,  6, ATK_TEXT_BOUNDARY_LINE_START,  0,  0, "" },
450     { -1,  7, ATK_TEXT_BOUNDARY_LINE_START,  0,  0, "" },
451     { -1,  8, ATK_TEXT_BOUNDARY_LINE_START,  0,  0, "" },
452     { -1,  9, ATK_TEXT_BOUNDARY_LINE_START,  0,  9, "abc! def\n" },
453     { -1, 10, ATK_TEXT_BOUNDARY_LINE_START,  0,  9, "abc! def\n" },
454     { -1, 11, ATK_TEXT_BOUNDARY_LINE_START,  0,  9, "abc! def\n" },
455     { -1, 12, ATK_TEXT_BOUNDARY_LINE_START,  0,  9, "abc! def\n" },
456     { -1, 13, ATK_TEXT_BOUNDARY_LINE_START,  0,  9, "abc! def\n" },
457     { -1, 14, ATK_TEXT_BOUNDARY_LINE_START,  0,  9, "abc! def\n" },
458     { -1, 15, ATK_TEXT_BOUNDARY_LINE_START,  0,  9, "abc! def\n" },
459     { -1, 16, ATK_TEXT_BOUNDARY_LINE_START,  0,  9, "abc! def\n" },
460     { -1, 17, ATK_TEXT_BOUNDARY_LINE_START,  9, 17, "ghi jkl\n" },
461     { -1, 18, ATK_TEXT_BOUNDARY_LINE_START,  9, 17, "ghi jkl\n" },
462     { -1, 19, ATK_TEXT_BOUNDARY_LINE_START,  9, 17, "ghi jkl\n" },
463     { -1, 20, ATK_TEXT_BOUNDARY_LINE_START,  9, 17, "ghi jkl\n" },
464     { -1,  0, ATK_TEXT_BOUNDARY_LINE_END,  0,  0, "" },
465     { -1,  1, ATK_TEXT_BOUNDARY_LINE_END,  0,  0, "" },
466     { -1,  2, ATK_TEXT_BOUNDARY_LINE_END,  0,  0, "" },
467     { -1,  3, ATK_TEXT_BOUNDARY_LINE_END,  0,  0, "" },
468     { -1,  4, ATK_TEXT_BOUNDARY_LINE_END,  0,  0, "" },
469     { -1,  5, ATK_TEXT_BOUNDARY_LINE_END,  0,  0, "" },
470     { -1,  6, ATK_TEXT_BOUNDARY_LINE_END,  0,  0, "" },
471     { -1,  7, ATK_TEXT_BOUNDARY_LINE_END,  0,  0, "" },
472     { -1,  8, ATK_TEXT_BOUNDARY_LINE_END,  0,  0, "" },
473     { -1,  9, ATK_TEXT_BOUNDARY_LINE_END,  0,  8, "abc! def" },
474     { -1, 10, ATK_TEXT_BOUNDARY_LINE_END,  0,  8, "abc! def" },
475     { -1, 11, ATK_TEXT_BOUNDARY_LINE_END,  0,  8, "abc! def" },
476     { -1, 12, ATK_TEXT_BOUNDARY_LINE_END,  0,  8, "abc! def" },
477     { -1, 13, ATK_TEXT_BOUNDARY_LINE_END,  0,  8, "abc! def" },
478     { -1, 14, ATK_TEXT_BOUNDARY_LINE_END,  0,  8, "abc! def" },
479     { -1, 15, ATK_TEXT_BOUNDARY_LINE_END,  0,  8, "abc! def" },
480     { -1, 16, ATK_TEXT_BOUNDARY_LINE_END,  0,  8, "abc! def" },
481     { -1, 17, ATK_TEXT_BOUNDARY_LINE_END,  8, 16, "\nghi jkl" },
482     { -1, 18, ATK_TEXT_BOUNDARY_LINE_END,  8, 16, "\nghi jkl" },
483     { -1, 19, ATK_TEXT_BOUNDARY_LINE_END,  8, 16, "\nghi jkl" },
484     { -1, 20, ATK_TEXT_BOUNDARY_LINE_END,  8, 16, "\nghi jkl" },
485     {  0, -1, }
486   };
487   gint start, end;
488   gchar *word;
489   gint i;
490
491   atk_text = ATK_TEXT (gtk_widget_get_accessible (widget));
492
493   set_text (widget, text);
494 #ifdef SHOW_TEXT_ATTRIBUTES
495   if (GTK_IS_LABEL (widget))
496     show_text_attributes (gtk_label_get_layout (GTK_LABEL (widget)));
497   else if (GTK_IS_ENTRY (widget))
498     show_text_attributes (gtk_entry_get_layout (GTK_ENTRY (widget)));
499 #endif
500
501 #if DUMP_RESULTS
502   for (i = -1; i < 2; i++)
503     {
504       gint j, k;
505       for (j = ATK_TEXT_BOUNDARY_CHAR; j <= ATK_TEXT_BOUNDARY_LINE_END; j++)
506         for (k = 0; k <= strlen (text); k++)
507           {
508             switch (i)
509               {
510               case -1:
511                 word = atk_text_get_text_before_offset (atk_text, k, j, &start, &end);
512                 break;
513               case 0:
514                 word = atk_text_get_text_at_offset (atk_text, k, j, &start, &end);
515                 break;
516               case 1:
517                 word = atk_text_get_text_after_offset (atk_text, k, j, &start, &end);
518                 break;
519               default:
520                 g_assert_not_reached ();
521                 break;
522               }
523             printf ("    { %2d, %2d, %s %2d, %2d, \"%s\" },\n", i, k, boundary(j), start, end, escape (word));
524             g_free (word);
525           }
526     }
527 #endif
528
529   for (i = 0; expected[i].offset != -1; i++)
530     {
531       if (GTK_IS_ENTRY (widget))
532         {
533           /* GtkEntry sets single-paragraph mode on its pango layout */
534           if (expected[i].boundary == ATK_TEXT_BOUNDARY_LINE_START ||
535               expected[i].boundary == ATK_TEXT_BOUNDARY_LINE_END)
536             continue;
537         }
538
539       switch (expected[i].gravity)
540         {
541           case -1:
542             word = atk_text_get_text_before_offset (atk_text,
543                                                     expected[i].offset,
544                                                     expected[i].boundary,
545                                                     &start, &end);
546             break;
547           case 0:
548             word = atk_text_get_text_at_offset (atk_text,
549                                                 expected[i].offset,
550                                                 expected[i].boundary,
551                                                 &start, &end);
552             break;
553           case 1:
554             word = atk_text_get_text_after_offset (atk_text,
555                                                    expected[i].offset,
556                                                    expected[i].boundary,
557                                                    &start, &end);
558             break;
559           default:
560             g_assert_not_reached ();
561             break;
562         }
563
564       g_assert_cmpstr (word, ==, expected[i].word);
565       g_assert_cmpint (start, ==, expected[i].start);
566       g_assert_cmpint (end, ==, expected[i].end);
567       g_free (word);
568     }
569 }
570
571 static void
572 select_region (GtkWidget *widget,
573                gint       start,
574                gint       end)
575 {
576   if (GTK_IS_EDITABLE (widget))
577     gtk_editable_select_region (GTK_EDITABLE (widget), start, end);
578   else if (GTK_IS_LABEL (widget))
579     gtk_label_select_region (GTK_LABEL (widget), start, end);
580   else if (GTK_IS_TEXT_VIEW (widget))
581     {
582       GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
583       GtkTextIter start_iter, end_iter;
584
585       gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, end);
586       gtk_text_buffer_get_iter_at_offset (buffer, &end_iter, start);
587
588       gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
589     }
590   else
591     g_assert_not_reached ();
592 }
593
594 typedef struct {
595   gint count;
596   gint position;
597   gint bound;
598 } SelectionData;
599
600 static void
601 caret_moved_cb (AtkText *text, gint position, SelectionData *data)
602 {
603   data->count++;
604   data->position = position;
605 }
606
607 static void
608 selection_changed_cb (AtkText *text, SelectionData *data)
609 {
610   data->count++;
611
612   atk_text_get_selection (text, 0, &data->bound, &data->position);
613 }
614
615 static void
616 test_selection (GtkWidget *widget)
617 {
618   AtkText *atk_text;
619   const gchar *text = "Bla bla bla";
620   gint n;
621   gchar *ret;
622   gint start, end;
623   SelectionData data1;
624   SelectionData data2;
625
626   if (GTK_IS_LABEL (widget))
627     gtk_label_set_selectable (GTK_LABEL (widget), TRUE);
628
629   atk_text = ATK_TEXT (gtk_widget_get_accessible (widget));
630
631   data1.count = 0;
632   data2.count = 0;
633   g_signal_connect (atk_text, "text_caret_moved",
634                     G_CALLBACK (caret_moved_cb), &data1);
635   g_signal_connect (atk_text, "text_selection_changed",
636                     G_CALLBACK (selection_changed_cb), &data2);
637
638   set_text (widget, text);
639
640   n = atk_text_get_n_selections (atk_text);
641   g_assert_cmpint (n, ==, 0);
642
643   if (data1.count == 1)
644     /* insertion before cursor */
645     g_assert_cmpint (data1.position, ==, 11);
646   else
647     /* insertion after cursor */
648     g_assert_cmpint (data1.count, ==, 0);
649   g_assert_cmpint (data2.count, ==, 0);
650
651   select_region (widget, 4, 7);
652
653   g_assert_cmpint (data1.count, >=, 1);
654   g_assert_cmpint (data1.position, ==, 7);
655   g_assert_cmpint (data2.count, >=, 1);
656   g_assert_cmpint (data2.bound, ==, 4);
657   g_assert_cmpint (data2.position, ==, 7);
658
659   n = atk_text_get_n_selections (atk_text);
660   g_assert_cmpint (n, ==, 1);
661
662   ret = atk_text_get_selection (atk_text, 0, &start, &end);
663   g_assert_cmpstr (ret, ==, "bla");
664   g_assert_cmpint (start, ==, 4);
665   g_assert_cmpint (end, ==, 7);
666   g_free (ret);
667
668   atk_text_remove_selection (atk_text, 0);
669   n = atk_text_get_n_selections (atk_text);
670   g_assert_cmpint (n, ==, 0);
671
672   g_assert_cmpint (data1.count, >=, 1);
673   g_assert_cmpint (data2.count, >=, 2);
674   g_assert_cmpint (data2.position, ==, 7);
675   g_assert_cmpint (data2.bound, ==, 7);
676 }
677
678 static void
679 setup_test (GtkWidget *widget)
680 {
681   set_text (widget, "");
682 }
683
684 static void
685 add_text_test (const gchar      *prefix,
686                GTestFixtureFunc  test_func,
687                GtkWidget        *widget)
688 {
689   gchar *path;
690
691   path = g_strdup_printf ("%s/%s", prefix, G_OBJECT_TYPE_NAME (widget));
692   g_test_add_vtable (path,
693                      0,
694                      g_object_ref (widget),
695                      (GTestFixtureFunc) setup_test,
696                      (GTestFixtureFunc) test_func,
697                      (GTestFixtureFunc) g_object_unref);
698   g_free (path);
699 }
700
701 static void
702 add_text_tests (GtkWidget *widget)
703 {
704   g_object_ref_sink (widget);
705   add_text_test ("/text/basic", (GTestFixtureFunc) test_basic, widget);
706   add_text_test ("/text/words", (GTestFixtureFunc) test_words, widget);
707   add_text_test ("/text/changed", (GTestFixtureFunc) test_text_changed, widget);
708   add_text_test ("/text/selection", (GTestFixtureFunc) test_selection, widget);
709   g_object_unref (widget);
710 }
711
712 static void
713 test_bold_label (void)
714 {
715   GtkWidget *label;
716   AtkObject *atk_obj;
717   gchar *text;
718
719   g_test_bug ("126797");
720
721   label = gtk_label_new ("<b>Bold?</b>");
722   g_object_ref_sink (label);
723
724   atk_obj = gtk_widget_get_accessible (label);
725
726   text = atk_text_get_text (ATK_TEXT (atk_obj), 0, -1);
727   g_assert_cmpstr (text, ==, "<b>Bold?</b>");
728   g_free (text);
729
730   gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
731
732   text = atk_text_get_text (ATK_TEXT (atk_obj), 0, -1);
733   g_assert_cmpstr (text, ==, "Bold?");
734   g_free (text);
735
736   g_object_unref (label);
737 }
738
739 int
740 main (int argc, char *argv[])
741 {
742   gtk_test_init (&argc, &argv, NULL);
743
744   g_test_bug_base ("http://bugzilla.gnome.org/");
745
746   g_test_add_func ("/text/bold/GtkLabel", test_bold_label);
747
748   add_text_tests (gtk_label_new (""));
749   add_text_tests (gtk_entry_new ());
750   add_text_tests (gtk_text_view_new ());
751
752   return g_test_run ();
753 }