testatk.c 88.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright (C) 2009 Igalia S.L.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <errno.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
24
#include <locale.h>
25
#include <unistd.h>
26 27
#include <webkit/webkit.h>

28
#if GTK_CHECK_VERSION(2, 14, 0)
29

30 31
static const char* centeredContents = "<html><body><p style='text-align: center;'>Short line</p><p style='text-align: center;'>Long-size line with some foo bar baz content</p><p style='text-align: center;'>Short line</p><p style='text-align: center;'>This is a multi-line paragraph<br />where the first line<br />is the biggest one</p></body></html>";

32 33
static const char* contents = "<html><body><p>This is a test. This is the second sentence. And this the third.</p></body></html>";

34 35
static const char* contentsWithNewlines = "<html><body><p>This is a test. \n\nThis\n is the second sentence. And this the third.</p></body></html>";

36 37
static const char* contentsWithPreformattedText = "<html><body><pre>\n\t\n\tfirst line\n\tsecond line\n</pre></body></html>";

38
static const char* contentsWithSpecialChars = "<html><body><p>&laquo;&nbsp;This is a paragraph with &ldquo;special&rdquo; characters inside.&nbsp;&raquo;</p><ul><li style='max-width:100px;'>List item with some text that wraps across different lines.</li><li style='max-width:100px;'><p>List item with some text that wraps across different lines.</p></li></ul></body></html>";
39

40 41 42 43
static const char* contentsInTextarea = "<html><body><textarea cols='80'>This is a test. This is the second sentence. And this the third.</textarea></body></html>";

static const char* contentsInTextInput = "<html><body><input type='text' size='80' value='This is a test. This is the second sentence. And this the third.'/></body></html>";

44 45 46 47
static const char* contentsInParagraphAndBodySimple = "<html><body><p>This is a test.</p>Hello world.</body></html>";

static const char* contentsInParagraphAndBodyModerate = "<html><body><p>This is a test.</p>Hello world.<br /><font color='#00cc00'>This sentence is green.</font><br />This one is not.</body></html>";

48 49
static const char* contentsInTable = "<html><body><table><tr><td>foo</td><td>bar</td></tr></table></body></html>";

50 51
static const char* contentsInTableWithHeaders = "<html><body><table><tr><th>foo</th><th>bar</th><th colspan='2'>baz</th></tr><tr><th>qux</th><td>1</td><td>2</td><td>3</td></tr><tr><th rowspan='2'>quux</th><td>4</td><td>5</td><td>6</td></tr><tr><td>6</td><td>7</td><td>8</td></tr><tr><th>corge</th><td>9</td><td>10</td><td>11</td></tr></table><table><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></table></body></html>";

52 53
static const char* contentsWithExtraneousWhiteSpaces = "<html><head><body><p>This\n                          paragraph\n                                                      is\n                                                                                                                                                                                                                                                                                                                                                                            borked!</p></body></html>";

54 55
static const char* comboBoxSelector = "<html><body><select><option selected value='foo'>foo</option><option value='bar'>bar</option></select></body></html>";

56 57
static const char* embeddedObjects = "<html><body><p>Choose: <input value='foo' type='checkbox'/>foo <input value='bar' type='checkbox'/>bar (pick one)</p><p>Choose: <select name='foo'><option>bar</option><option>baz</option></select> (pick one)</p><p><input name='foobarbutton' value='foobar' type='button'/></p></body></html>";

58
static const char* formWithTextInputs = "<html><body><form><input type='text' name='entry' /><input type='password' name='passwordEntry' /></form></body></html>";
59

60
static const char* hypertextAndHyperlinks = "<html><body><p>A paragraph with no links at all</p><p><a href='http://foo.bar.baz/'>A line</a> with <a href='http://bar.baz.foo/'>a link in the middle</a> as well as at the beginning and <a href='http://baz.foo.bar/'>at the end</a></p><ol><li>List item with a <span><a href='http://foo.bar.baz/'>link inside a span node</a></span></li></ol></body></html>";
61

62 63
static const char* layoutAndDataTables = "<html><body><table><tr><th>Odd</th><th>Even</th></tr><tr><td>1</td><td>2</td></tr></table><table><tr><td>foo</td><td>bar</td></tr></table></body></html>";

64 65
static const char* linksWithInlineImages = "<html><head><style>a.http:before {content: url(no-image.png);}</style><body><p><a class='http' href='foo'>foo</a> bar baz</p><p>foo <a class='http' href='bar'>bar</a> baz</p><p>foo bar <a class='http' href='baz'>baz</a></p></body></html>";

66 67
static const char* listsOfItems = "<html><body><ul><li>text only</li><li><a href='foo'>link only</a></li><li>text and a <a href='bar'>link</a></li></ul><ol><li>text only</li><li><a href='foo'>link only</a></li><li>text and a <a href='bar'>link</a></li></ol></body></html>";

68
static const char* textForCaretBrowsing = "<html><body><h1>A text header</h1><p>A paragraph <a href='http://foo.bar.baz/'>with a link</a> in the middle</p><ol><li>A list item</li><li><span style='display:block;'>Block span in a list item</span><span>Inline span in a list item</span></li><li><a href='foo'><span style='display:block;'>Block span in a link in a list item</span><span>Inline span in a link in a list item</span></a></li></ol><select><option selected value='foo'>An option in a combo box</option></select><input type='text' name='foo' value='foo bar baz' /><table><tr><td>a table cell</td><td></td><td><a href='foo'><span style='display:block;'>Block span in a link in a table cell</span><span>Inline span in a link in a table cell</span></a></td><td><span style='display:block;'>Block span in a table cell</span><span>Inline span in a table cell</span></td></tr></table><h4><a href='foo'><span style='display:block;'>Block span in a link in a heading</span><span>Inline span in a link in a heading</span></h4><h4><span style='display:block;'>Block span in a heading</span><span>Inline span in a heading</span></h4></body></html>";
69

70
static const char* textForSelections = "<html><body><p>A paragraph with plain text</p><p>A paragraph with <a href='http://webkit.org'>a link</a> in the middle</p><ol><li>A list item</li></ol><select></body></html>";
71 72 73

static const char* textWithAttributes = "<html><head><style>.st1 {font-family: monospace; color:rgb(120,121,122);} .st2 {text-decoration:underline; background-color:rgb(80,81,82);}</style></head><body><p style=\"font-size:14; text-align:right;\">This is the <i>first</i><b> sentence of this text.</b></p><p class=\"st1\">This sentence should have an style applied <span class=\"st2\">and this part should have another one</span>.</p><p>x<sub>1</sub><sup>2</sup>=x<sub>2</sub><sup>3</sup></p><p style=\"text-align:center;\">This sentence is the <strike>last</strike> one.</p></body></html>";

74
static AtkObject* getWebAreaObject(WebKitWebView* webView)
75
{
76 77 78 79
    /* Manually spin the main context to make sure the accessible
       objects are properly created before continuing. */
    while (g_main_context_pending(0))
        g_main_context_iteration(0, TRUE);
80 81 82 83 84 85 86 87 88 89 90 91 92

    AtkObject* rootObject = gtk_widget_get_accessible(GTK_WIDGET(webView));
    if (!rootObject)
        return NULL;

    AtkObject* webAreaObject = atk_object_ref_accessible_child(rootObject, 0);
    if (!webAreaObject)
        return NULL;

    /* We don't need the extra ref here. */
    g_object_unref(webAreaObject);

    return webAreaObject;
93 94 95 96
}

typedef gchar* (*AtkGetTextFunction) (AtkText*, gint, AtkTextBoundary, gint*, gint*);

97
static void testGetTextFunction(AtkText* textObject, AtkGetTextFunction fn, AtkTextBoundary boundary, gint offset, const char* textResult, gint startOffsetResult, gint endOffsetResult)
98
{
99 100 101 102 103 104
    gint startOffset;
    gint endOffset;
    char* text = fn(textObject, offset, boundary, &startOffset, &endOffset);
    g_assert_cmpstr(text, ==, textResult);
    g_assert_cmpint(startOffset, ==, startOffsetResult);
    g_assert_cmpint(endOffset, ==, endOffsetResult);
105 106 107
    g_free(text);
}

108
static void runGetTextTests(AtkText* textObject)
109
{
110
    char* text = atk_text_get_text(textObject, 0, -1);
111 112 113 114
    g_assert_cmpstr(text, ==, "This is a test. This is the second sentence. And this the third.");
    g_free(text);

    /* ATK_TEXT_BOUNDARY_CHAR */
115 116
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_CHAR,
                        0, "T", 0, 1);
117

118 119
    testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_CHAR,
                        0, "h", 1, 2);
120

121 122 123 124 125
    testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_CHAR,
                        0, "", 0, 0);

    testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_CHAR,
                        1, "T", 0, 1);
126 127

    /* ATK_TEXT_BOUNDARY_WORD_START */
128 129
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START,
                        0, "This ", 0, 5);
130

131 132
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START,
                        4, "This ", 0, 5);
133

134 135
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START,
                        10, "test. ", 10, 16);
136

137 138
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START,
                        58, "third.", 58, 64);
139

140 141
    testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_START,
                        5, "This ", 0, 5);
142

143 144
    testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_START,
                        7, "This ", 0, 5);
145

146 147
    testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_START,
                        0, "is ", 5, 8);
148

149 150
    testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_START,
                        4, "is ", 5, 8);
151

152 153
    testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_START,
                        3, "is ", 5, 8);
154 155

    /* ATK_TEXT_BOUNDARY_WORD_END */
156 157
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END,
                        0, "This", 0, 4);
158

159 160
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END,
                        4, " is", 4, 7);
161

162 163
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END,
                        5, " is", 4, 7);
164

165 166
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END,
                        9, " test", 9, 14);
167

168 169
    testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_END,
                        5, "This", 0, 4);
170

171 172
    testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_END,
                        4, "This", 0, 4);
173

174 175
    testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_END,
                        7, " is", 4, 7);
176

177 178
    testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_END,
                        5, " a", 7, 9);
179

180 181
    testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_END,
                        4, " a", 7, 9);
182

183 184
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END,
                        58, " third", 57, 63);
185 186

    /* ATK_TEXT_BOUNDARY_SENTENCE_START */
187 188
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
                        0, "This is a test. ", 0, 16);
189

190 191
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
                        15, "This is a test. ", 0, 16);
192

193 194
    testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
                        0, "This is the second sentence. ", 16, 45);
195

196 197
    testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
                        15, "This is the second sentence. ", 16, 45);
198

199 200
    testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
                        16, "This is a test. ", 0, 16);
201

202 203
    testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
                        44, "This is a test. ", 0, 16);
204

205 206
    testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
                        15, "", 0, 0);
207 208

    /* ATK_TEXT_BOUNDARY_SENTENCE_END */
209 210
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
                        0, "This is a test.", 0, 15);
211

212 213
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
                        15, " This is the second sentence.", 15, 44);
214

215 216
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
                        16, " This is the second sentence.", 15, 44);
217

218 219
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
                        17, " This is the second sentence.", 15, 44);
220

221 222
    testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
                        0, " This is the second sentence.", 15, 44);
223

224 225
    testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
                        15, " And this the third.", 44, 64);
226

227 228
    testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
                        16, "This is a test.", 0, 15);
229

230 231
    testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
                        15, "This is a test.", 0, 15);
232

233 234
    testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
                        14, "", 0, 0);
235

236 237
    testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
                        44, " This is the second sentence.", 15, 44);
238

239
    /* It's tricky to test these properly right now, since our a11y
240
       implementation splits different lines in different a11y items. */
241
    /* ATK_TEXT_BOUNDARY_LINE_START */
242 243
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START,
                        0, "This is a test. This is the second sentence. And this the third.", 0, 64);
244 245

    /* ATK_TEXT_BOUNDARY_LINE_END */
246 247
    testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END,
                        0, "This is a test. This is the second sentence. And this the third.", 0, 64);
248 249 250 251 252 253 254 255 256

    /* For objects implementing AtkEditableText, try to change the
       exposed text and retrieve it again as a full line.
       (see https://bugs.webkit.org/show_bug.cgi?id=72830) */
    if (ATK_IS_EDITABLE_TEXT(textObject)) {
        atk_editable_text_set_text_contents(ATK_EDITABLE_TEXT(textObject), "foo bar baz");
        testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 0, "foo bar baz", 0, 11);
        testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END, 0, "foo bar baz", 0, 11);
    }
xan@webkit.org's avatar
xan@webkit.org committed
257 258
}

259 260 261 262 263 264 265 266 267 268 269
static gchar* textCaretMovedResult = 0;

static void textCaretMovedCallback(AtkText* text, gint pos, gpointer data)
{
    g_assert(ATK_IS_TEXT(text));

    g_free(textCaretMovedResult);
    AtkRole role = atk_object_get_role(ATK_OBJECT(text));
    textCaretMovedResult = g_strdup_printf("|%s|%d|", atk_role_get_name(role), pos);
}

270 271 272 273 274 275 276 277
static void testWebkitAtkCaretOffsets()
{
    WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
    g_object_ref_sink(webView);
    GtkAllocation allocation = { 0, 0, 800, 600 };
    gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
    webkit_web_view_load_string(webView, textForCaretBrowsing, 0, 0, 0);

278
    AtkObject* object = getWebAreaObject(webView);
279 280 281 282
    g_assert(object);

    AtkObject* header = atk_object_ref_accessible_child(object, 0);
    g_assert(ATK_IS_TEXT(header));
283 284
    g_signal_connect(header, "text-caret-moved", G_CALLBACK(textCaretMovedCallback), 0);

285 286 287 288 289 290 291 292 293
    gchar* text = atk_text_get_text(ATK_TEXT(header), 0, -1);
    g_assert_cmpstr(text, ==, "A text header");
    g_free (text);

    /* It should be possible to place the caret inside a header. */
    gboolean result = atk_text_set_caret_offset(ATK_TEXT(header), 5);
    g_assert_cmpint(result, ==, TRUE);
    gint offset = atk_text_get_caret_offset(ATK_TEXT(header));
    g_assert_cmpint(offset, ==, 5);
294
    g_assert_cmpstr(textCaretMovedResult, ==, "|heading|5|");
295 296 297

    AtkObject* paragraph = atk_object_ref_accessible_child(object, 1);
    g_assert(ATK_IS_TEXT(paragraph));
298 299
    g_signal_connect(paragraph, "text-caret-moved", G_CALLBACK(textCaretMovedCallback), 0);

300 301 302 303 304 305 306 307 308
    text = atk_text_get_text(ATK_TEXT(paragraph), 0, -1);
    g_assert_cmpstr(text, ==, "A paragraph with a link in the middle");
    g_free (text);

    /* It should be possible to place the caret inside a paragraph and a link. */
    result = atk_text_set_caret_offset(ATK_TEXT(paragraph), 5);
    g_assert_cmpint(result, ==, TRUE);
    offset = atk_text_get_caret_offset(ATK_TEXT(paragraph));
    g_assert_cmpint(offset, ==, 5);
309
    g_assert_cmpstr(textCaretMovedResult, ==, "|paragraph|5|");
310 311 312 313 314

    result = atk_text_set_caret_offset(ATK_TEXT(paragraph), 20);
    g_assert_cmpint(result, ==, TRUE);
    offset = atk_text_get_caret_offset(ATK_TEXT(paragraph));
    g_assert_cmpint(offset, ==, 20);
315
    g_assert_cmpstr(textCaretMovedResult, ==, "|paragraph|20|");
316 317 318 319 320

    result = atk_text_set_caret_offset(ATK_TEXT(paragraph), 30);
    g_assert_cmpint(result, ==, TRUE);
    offset = atk_text_get_caret_offset(ATK_TEXT(paragraph));
    g_assert_cmpint(offset, ==, 30);
321 322 323 324 325 326 327 328 329 330 331 332 333 334
    g_assert_cmpstr(textCaretMovedResult, ==, "|paragraph|30|");

    AtkObject* link = atk_object_ref_accessible_child(paragraph, 0);
    g_assert(ATK_IS_TEXT(link));
    text = atk_text_get_text(ATK_TEXT(link), 0, -1);
    g_assert_cmpstr(text, ==, "with a link");
    g_free (text);

    result = atk_text_set_caret_offset(ATK_TEXT(link), 5);
    g_assert_cmpint(result, ==, TRUE);
    offset = atk_text_get_caret_offset(ATK_TEXT(link));
    g_assert_cmpint(offset, ==, 5);
    /* Positions inside links are reported relative to the paragraph. */
    g_assert_cmpstr(textCaretMovedResult, ==, "|paragraph|17|");
335 336 337 338

    AtkObject* list = atk_object_ref_accessible_child(object, 2);
    g_assert(ATK_OBJECT(list));
    g_assert(atk_object_get_role(list) == ATK_ROLE_LIST);
339
    g_assert_cmpint(atk_object_get_n_accessible_children(list), ==, 3);
340 341 342 343 344 345 346

    AtkObject* listItem = atk_object_ref_accessible_child(list, 0);
    g_assert(ATK_IS_TEXT(listItem));
    text = atk_text_get_text(ATK_TEXT(listItem), 0, -1);
    g_assert_cmpstr(text, ==, "1. A list item");
    g_free (text);

347 348 349 350 351 352 353 354 355 356 357 358
    listItem = atk_object_ref_accessible_child(list, 1);
    g_assert(ATK_IS_TEXT(listItem));
    text = atk_text_get_text(ATK_TEXT(listItem), 0, -1);
    g_assert_cmpstr(text, ==, "2. Block span in a list item\nInline span in a list item");
    g_free (text);

    listItem = atk_object_ref_accessible_child(list, 2);
    g_assert(ATK_IS_TEXT(listItem));
    text = atk_text_get_text(ATK_TEXT(listItem), 0, -1);
    g_assert_cmpstr(text, ==, "3. Block span in a link in a list item\nInline span in a link in a list item");
    g_free (text);

359 360 361 362
    /* It's not possible to place the caret inside an item's marker. */
    result = atk_text_set_caret_offset(ATK_TEXT(listItem), 1);
    g_assert_cmpint(result, ==, FALSE);

363 364 365
    /* It should be possible to place the caret inside an item's text. */
    result = atk_text_set_caret_offset(ATK_TEXT(listItem), 5);
    g_assert_cmpint(result, ==, TRUE);
366 367
    offset = atk_text_get_caret_offset(ATK_TEXT(listItem));
    g_assert_cmpint(offset, ==, 5);
368 369 370 371 372 373 374

    AtkObject* panel = atk_object_ref_accessible_child(object, 3);
    g_assert(ATK_IS_OBJECT(panel));
    g_assert(atk_object_get_role(panel) == ATK_ROLE_PANEL);

    AtkObject* comboBox = atk_object_ref_accessible_child(panel, 0);
    g_assert(ATK_IS_OBJECT(comboBox));
375
    g_assert(!ATK_IS_TEXT(comboBox));
376 377 378 379
    g_assert(atk_object_get_role(comboBox) == ATK_ROLE_COMBO_BOX);

    AtkObject* menuPopup = atk_object_ref_accessible_child(comboBox, 0);
    g_assert(ATK_IS_OBJECT(menuPopup));
380
    g_assert(!ATK_IS_TEXT(menuPopup));
381 382 383 384 385 386 387 388
    g_assert(atk_object_get_role(menuPopup) == ATK_ROLE_MENU);

    AtkObject* comboBoxOption = atk_object_ref_accessible_child(menuPopup, 0);
    g_assert(ATK_IS_OBJECT(comboBoxOption));
    g_assert(atk_object_get_role(comboBoxOption) == ATK_ROLE_MENU_ITEM);
    g_assert(ATK_IS_TEXT(comboBoxOption));
    text = atk_text_get_text(ATK_TEXT(comboBoxOption), 0, -1);
    g_assert_cmpstr(text, ==, "An option in a combo box");
389
    g_free(text);
390 391 392 393 394

    /* It's not possible to place the caret inside an option for a combobox. */
    result = atk_text_set_caret_offset(ATK_TEXT(comboBoxOption), 1);
    g_assert_cmpint(result, ==, FALSE);

395 396 397 398 399 400
    AtkObject* textEntry = atk_object_ref_accessible_child(panel, 1);
    g_assert(ATK_IS_OBJECT(textEntry));
    g_assert(atk_object_get_role(textEntry) == ATK_ROLE_ENTRY);
    g_assert(ATK_IS_TEXT(textEntry));
    text = atk_text_get_text(ATK_TEXT(textEntry), 0, -1);
    g_assert_cmpstr(text, ==, "foo bar baz");
401
    g_free(text);
402 403 404 405 406 407

    result = atk_text_set_caret_offset(ATK_TEXT(textEntry), 5);
    g_assert_cmpint(result, ==, TRUE);
    offset = atk_text_get_caret_offset(ATK_TEXT(textEntry));
    g_assert_cmpint(offset, ==, 5);

408 409
    AtkObject* table = atk_object_ref_accessible_child(object, 4);
    g_assert(ATK_IS_OBJECT(table));
410
    g_assert(!ATK_IS_TEXT(table));
411
    g_assert(atk_object_get_role(table) == ATK_ROLE_TABLE);
412
    g_assert_cmpint(atk_object_get_n_accessible_children(table), ==, 4);
413 414 415 416 417 418 419 420 421 422 423 424 425

    AtkObject* tableCell = atk_object_ref_accessible_child(table, 0);
    g_assert(ATK_IS_TEXT(tableCell));
    g_assert(atk_object_get_role(tableCell) == ATK_ROLE_TABLE_CELL);
    text = atk_text_get_text(ATK_TEXT(tableCell), 0, -1);
    g_assert_cmpstr(text, ==, "a table cell");
    g_free(text);

    result = atk_text_set_caret_offset(ATK_TEXT(tableCell), 2);
    g_assert_cmpint(result, ==, TRUE);
    offset = atk_text_get_caret_offset(ATK_TEXT(tableCell));
    g_assert_cmpint(offset, ==, 2);

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    /* Even empty table cells should implement AtkText, but report an empty string */
    tableCell = atk_object_ref_accessible_child(table, 1);
    g_assert(ATK_IS_TEXT(tableCell));
    g_assert(atk_object_get_role(tableCell) == ATK_ROLE_TABLE_CELL);
    text = atk_text_get_text(ATK_TEXT(tableCell), 0, -1);
    g_assert_cmpstr(text, ==, "");
    g_free(text);

    tableCell = atk_object_ref_accessible_child(table, 2);
    g_assert(ATK_IS_TEXT(tableCell));
    g_assert(atk_object_get_role(tableCell) == ATK_ROLE_TABLE_CELL);
    text = atk_text_get_text(ATK_TEXT(tableCell), 0, -1);
    g_assert_cmpstr(text, ==, "Block span in a link in a table cell\nInline span in a link in a table cell");
    g_free(text);

    tableCell = atk_object_ref_accessible_child(table, 3);
    g_assert(ATK_IS_TEXT(tableCell));
    g_assert(atk_object_get_role(tableCell) == ATK_ROLE_TABLE_CELL);
    text = atk_text_get_text(ATK_TEXT(tableCell), 0, -1);
    g_assert_cmpstr(text, ==, "Block span in a table cell\nInline span in a table cell");
    g_free(text);

    header = atk_object_ref_accessible_child(object, 5);
    g_assert(ATK_IS_TEXT(header));
    g_assert(atk_object_get_role(header) == ATK_ROLE_HEADING);
    text = atk_text_get_text(ATK_TEXT(header), 0, -1);
    g_assert_cmpstr(text, ==, "Block span in a link in a heading\nInline span in a link in a heading");
    g_free(text);

    header = atk_object_ref_accessible_child(object, 6);
    g_assert(ATK_IS_TEXT(header));
    g_assert(atk_object_get_role(header) == ATK_ROLE_HEADING);
    text = atk_text_get_text(ATK_TEXT(header), 0, -1);
    g_assert_cmpstr(text, ==, "Block span in a heading\nInline span in a heading");
    g_free(text);

462 463
    g_free(textCaretMovedResult);

464 465
    g_object_unref(header);
    g_object_unref(paragraph);
466
    g_object_unref(link);
467 468 469 470 471 472
    g_object_unref(list);
    g_object_unref(listItem);
    g_object_unref(panel);
    g_object_unref(comboBox);
    g_object_unref(menuPopup);
    g_object_unref(comboBoxOption);
473
    g_object_unref(textEntry);
474 475
    g_object_unref(table);
    g_object_unref(tableCell);
476 477 478
    g_object_unref(webView);
}

479 480 481 482 483 484 485 486 487 488 489 490 491 492
static void testWebkitAtkCaretOffsetsAndExtranousWhiteSpaces()
{
    WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
    g_object_ref_sink(webView);
    GtkAllocation allocation = { 0, 0, 800, 600 };
    gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
    webkit_web_view_load_string(webView, contentsWithExtraneousWhiteSpaces, 0, 0, 0);

    /* Enable caret browsing. */
    WebKitWebSettings* settings = webkit_web_view_get_settings(webView);
    g_object_set(G_OBJECT(settings), "enable-caret-browsing", TRUE, NULL);
    webkit_web_view_set_settings(webView, settings);

    /* Get to the inner AtkText object. */
493
    AtkObject* object = getWebAreaObject(webView);
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
    g_assert(object);
    object = atk_object_ref_accessible_child(object, 0);
    g_assert(object);

    AtkText* textObject = ATK_TEXT(object);
    g_assert(ATK_IS_TEXT(textObject));

    gchar* text = atk_text_get_text(textObject, 0, -1);
    g_assert_cmpstr(text, ==, "This paragraph is borked!");
    g_free(text);

    gint characterCount = atk_text_get_character_count(textObject);
    g_assert_cmpint(characterCount, ==, 25);

    gboolean result = atk_text_set_caret_offset(textObject, characterCount - 1);
    g_assert_cmpint(result, ==, TRUE);

    gint caretOffset = atk_text_get_caret_offset(textObject);
    g_assert_cmpint(caretOffset, ==, characterCount - 1);

514 515 516 517 518 519
    result = atk_text_set_caret_offset(textObject, characterCount);
    g_assert_cmpint(result, ==, TRUE);

    caretOffset = atk_text_get_caret_offset(textObject);
    g_assert_cmpint(caretOffset, ==, characterCount);

520 521 522
    g_object_unref(webView);
}

523 524 525 526 527 528 529 530
static void testWebkitAtkComboBox()
{
    WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
    g_object_ref_sink(webView);
    GtkAllocation allocation = { 0, 0, 800, 600 };
    gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
    webkit_web_view_load_string(webView, comboBoxSelector, 0, 0, 0);

531
    AtkObject* object = getWebAreaObject(webView);
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
    g_assert(object);

    AtkObject* formObject = atk_object_ref_accessible_child(object, 0);
    g_assert(formObject);

    AtkObject* comboBox = atk_object_ref_accessible_child(formObject, 0);
    g_assert(ATK_IS_OBJECT(comboBox));

    AtkObject* menuPopup = atk_object_ref_accessible_child(comboBox, 0);
    g_assert(ATK_IS_OBJECT(menuPopup));

    AtkObject* item1 = atk_object_ref_accessible_child(menuPopup, 0);
    g_assert(ATK_IS_OBJECT(item1));

    AtkObject* item2 = atk_object_ref_accessible_child(menuPopup, 1);
    g_assert(ATK_IS_OBJECT(item2));

    /* Check roles. */
    g_assert(atk_object_get_role(comboBox) == ATK_ROLE_COMBO_BOX);
    g_assert(atk_object_get_role(menuPopup) == ATK_ROLE_MENU);
    g_assert(atk_object_get_role(item1) == ATK_ROLE_MENU_ITEM);
    g_assert(atk_object_get_role(item2) == ATK_ROLE_MENU_ITEM);

    /* Check the implementation of the AtkSelection interface. */
    g_assert(ATK_IS_SELECTION(comboBox));
    AtkSelection* atkSelection = ATK_SELECTION(comboBox);
    g_assert_cmpint(atk_selection_get_selection_count(atkSelection), ==, 1);
    g_assert(atk_selection_is_child_selected(atkSelection, 0));
    g_assert(!atk_selection_is_child_selected(atkSelection, 1));
    AtkObject* selectedItem = atk_selection_ref_selection(atkSelection, 0);
    g_assert(selectedItem == item1);
    g_object_unref(selectedItem);

565 566 567 568
    /* Check that the menu popup has 0 links and doesn't crash from checking. */
    gint nLinks = atk_hypertext_get_n_links(ATK_HYPERTEXT(menuPopup));
    g_assert_cmpint(nLinks, ==, 0);

569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
    /* Check the implementations of the AtkAction interface. */
    g_assert(ATK_IS_ACTION(comboBox));
    AtkAction* atkAction = ATK_ACTION(comboBox);
    g_assert_cmpint(atk_action_get_n_actions(atkAction), ==, 1);
    g_assert(atk_action_do_action(atkAction, 0));

    g_assert(ATK_IS_ACTION(menuPopup));
    atkAction = ATK_ACTION(menuPopup);
    g_assert_cmpint(atk_action_get_n_actions(atkAction), ==, 1);
    g_assert(atk_action_do_action(atkAction, 0));

    g_assert(ATK_IS_ACTION(item1));
    atkAction = ATK_ACTION(item1);
    g_assert_cmpint(atk_action_get_n_actions(atkAction), ==, 1);
    g_assert(atk_action_do_action(atkAction, 0));

    g_assert(ATK_IS_ACTION(item2));
    atkAction = ATK_ACTION(item2);
    g_assert_cmpint(atk_action_get_n_actions(atkAction), ==, 1);
    g_assert(atk_action_do_action(atkAction, 0));

    /* After selecting the second item, selection should have changed. */
    g_assert_cmpint(atk_selection_get_selection_count(atkSelection), ==, 1);
    g_assert(!atk_selection_is_child_selected(atkSelection, 0));
    g_assert(atk_selection_is_child_selected(atkSelection, 1));
    selectedItem = atk_selection_ref_selection(atkSelection, 0);
    g_assert(selectedItem == item2);
    g_object_unref(selectedItem);

    /* Check the implementation of the AtkText interface. */
    g_assert(ATK_IS_TEXT(item1));
    AtkText* atkText = ATK_TEXT(item1);
    char *text = atk_text_get_text(atkText, 0, -1);
    g_assert_cmpstr(text, ==, "foo");
    g_free(text);
    text = atk_text_get_text(atkText, 0, 2);
    g_assert_cmpstr(text, ==, "fo");
    g_free(text);

    g_assert(ATK_IS_TEXT(item2));
    atkText = ATK_TEXT(item2);
    text = atk_text_get_text(atkText, 0, -1);
    g_assert_cmpstr(text, ==, "bar");
    g_free(text);
    text = atk_text_get_text(atkText, 1, 3);
    g_assert_cmpstr(text, ==, "ar");
    g_free(text);

    g_object_unref(formObject);
    g_object_unref(comboBox);
    g_object_unref(menuPopup);
    g_object_unref(item1);
    g_object_unref(item2);
    g_object_unref(webView);
}

625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
static gchar* loadingEventsResult = 0;

static void updateLoadingEventsResult(const gchar* signalName)
{
    g_assert(signalName);

    gchar* previousResult = loadingEventsResult;
    loadingEventsResult = g_strdup_printf("%s|%s", previousResult, signalName);
    g_free(previousResult);
}

static gboolean documentLoadingEventCallback(GSignalInvocationHint *signalHint, guint numParamValues, const GValue *paramValues, gpointer data)
{
    // At least we should receive the instance emitting the signal.
    if (numParamValues < 1)
        return TRUE;

    GSignalQuery signal_query;
    g_signal_query(signalHint->signal_id, &signal_query);

    updateLoadingEventsResult(signal_query.signal_name);
    return TRUE;
}

static void testWebkitAtkDocumentLoadingEvents()
{
    WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
    g_object_ref_sink(webView);
    GtkAllocation allocation = { 0, 0, 800, 600 };
    gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
655 656 657 658 659 660

    /* Connect globally to see those events during a future load. */
    guint loadCompleteListenerId = atk_add_global_event_listener(documentLoadingEventCallback, "ATK:AtkDocument:load-complete");

    /* Do the load, so we can see those events happening. */
    loadingEventsResult = g_strdup("");
661 662 663 664 665 666 667 668
    webkit_web_view_load_string(webView, contents, 0, 0, 0);

    /* Trigger the creation of the full accessibility hierarchy by
       asking for the webArea object, so we can listen to events. */
    getWebAreaObject(webView);

    atk_remove_global_event_listener(loadCompleteListenerId);

669
    g_assert_cmpstr(loadingEventsResult, ==, "|load-complete");
670 671 672 673 674

    g_free(loadingEventsResult);
    g_object_unref(webView);
}

675 676 677 678 679 680 681 682
static void testWebkitAtkEmbeddedObjects()
{
    WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
    g_object_ref_sink(webView);
    GtkAllocation allocation = { 0, 0, 800, 600 };
    gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
    webkit_web_view_load_string(webView, embeddedObjects, 0, 0, 0);

683
    AtkObject* object = getWebAreaObject(webView);
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
    g_assert(object);

    AtkText* paragraph1 = ATK_TEXT(atk_object_ref_accessible_child(object, 0));
    g_assert(ATK_IS_TEXT(paragraph1));
    g_assert(ATK_IS_HYPERTEXT(paragraph1));

    const gchar* expectedText = "Choose: \357\277\274foo \357\277\274bar (pick one)";
    char* text = atk_text_get_text(paragraph1, 0, -1);
    g_assert_cmpstr(text, ==, expectedText);
    g_free(text);

    gint nLinks = atk_hypertext_get_n_links(ATK_HYPERTEXT(paragraph1));
    g_assert_cmpint(nLinks, ==, 2);

    AtkHyperlink* hLink = atk_hypertext_get_link(ATK_HYPERTEXT(paragraph1), 0);
    g_assert(ATK_HYPERLINK(hLink));
    AtkObject* hLinkObject = atk_hyperlink_get_object(hLink, 0);
    g_assert(ATK_OBJECT(hLinkObject));
    g_assert(atk_object_get_role(hLinkObject) == ATK_ROLE_CHECK_BOX);
    g_assert_cmpint(atk_hyperlink_get_start_index(hLink), ==, 8);
    g_assert_cmpint(atk_hyperlink_get_end_index(hLink), ==, 9);
    g_assert_cmpint(atk_hyperlink_get_n_anchors(hLink), ==, 1);
    g_assert_cmpstr(atk_hyperlink_get_uri(hLink, 0), ==, 0);

    AtkText* paragraph2 = ATK_TEXT(atk_object_ref_accessible_child(object, 1));
    g_assert(ATK_IS_TEXT(paragraph2));
    g_assert(ATK_IS_HYPERTEXT(paragraph2));

    expectedText = "Choose: \357\277\274 (pick one)";
    text = atk_text_get_text(paragraph2, 0, -1);
    g_assert_cmpstr(text, ==, expectedText);
    g_free(text);

    nLinks = atk_hypertext_get_n_links(ATK_HYPERTEXT(paragraph2));
    g_assert_cmpint(nLinks, ==, 1);

    hLink = atk_hypertext_get_link(ATK_HYPERTEXT(paragraph2), 0);
    g_assert(ATK_HYPERLINK(hLink));
    hLinkObject = atk_hyperlink_get_object(hLink, 0);
    g_assert(ATK_OBJECT(hLinkObject));
    g_assert(atk_object_get_role(hLinkObject) == ATK_ROLE_COMBO_BOX);
    g_assert_cmpint(atk_hyperlink_get_start_index(hLink), ==, 8);
    g_assert_cmpint(atk_hyperlink_get_end_index(hLink), ==, 9);
    g_assert_cmpint(atk_hyperlink_get_n_anchors(hLink), ==, 1);
    g_assert_cmpstr(atk_hyperlink_get_uri(hLink, 0), ==, 0);

    AtkText* paragraph3 = ATK_TEXT(atk_object_ref_accessible_child(object, 2));
    g_assert(ATK_IS_TEXT(paragraph3));
    g_assert(ATK_IS_HYPERTEXT(paragraph3));

    expectedText = "\357\277\274";
    text = atk_text_get_text(paragraph3, 0, -1);
    g_assert_cmpstr(text, ==, expectedText);
    g_free(text);

    nLinks = atk_hypertext_get_n_links(ATK_HYPERTEXT(paragraph3));
    g_assert_cmpint(nLinks, ==, 1);

    hLink = atk_hypertext_get_link(ATK_HYPERTEXT(paragraph3), 0);
    g_assert(ATK_HYPERLINK(hLink));
    hLinkObject = atk_hyperlink_get_object(hLink, 0);
    g_assert(ATK_OBJECT(hLinkObject));
    g_assert(atk_object_get_role(hLinkObject) == ATK_ROLE_PUSH_BUTTON);
    g_assert_cmpint(atk_hyperlink_get_start_index(hLink), ==, 0);
    g_assert_cmpint(atk_hyperlink_get_end_index(hLink), ==, 1);
    g_assert_cmpint(atk_hyperlink_get_n_anchors(hLink), ==, 1);
    g_assert_cmpstr(atk_hyperlink_get_uri(hLink, 0), ==, 0);

    g_object_unref(paragraph1);
    g_object_unref(paragraph2);
    g_object_unref(paragraph3);
    g_object_unref(webView);
}

758
static void testWebkitAtkGetTextAtOffset()
xan@webkit.org's avatar
xan@webkit.org committed
759
{
760
    WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
xan@webkit.org's avatar
xan@webkit.org committed
761
    g_object_ref_sink(webView);
762 763 764
    GtkAllocation allocation = { 0, 0, 800, 600 };
    gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
    webkit_web_view_load_string(webView, contents, 0, 0, 0);
xan@webkit.org's avatar
xan@webkit.org committed
765

766
    /* Get to the inner AtkText object. */
767
    AtkObject* object = getWebAreaObject(webView);
768 769 770
    g_assert(object);
    object = atk_object_ref_accessible_child(object, 0);
    g_assert(object);
xan@webkit.org's avatar
xan@webkit.org committed
771

772 773
    AtkText* textObject = ATK_TEXT(object);
    g_assert(ATK_IS_TEXT(textObject));
xan@webkit.org's avatar
xan@webkit.org committed
774

775
    runGetTextTests(textObject);
776 777 778 779

    g_object_unref(webView);
}

780
static void testWebkitAtkGetTextAtOffsetNewlines()
781
{
782
    WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
783
    g_object_ref_sink(webView);
784 785 786
    GtkAllocation allocation = { 0, 0, 800, 600 };
    gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
    webkit_web_view_load_string(webView, contentsWithNewlines, 0, 0, 0);
787

788
    /* Get to the inner AtkText object. */
789
    AtkObject* object = getWebAreaObject(webView);
790 791 792
    g_assert(object);
    object = atk_object_ref_accessible_child(object, 0);
    g_assert(object);
793

794 795
    AtkText* textObject = ATK_TEXT(object);
    g_assert(ATK_IS_TEXT(textObject));
796

797
    runGetTextTests(textObject);
798 799 800 801

    g_object_unref(webView);
}

802
static void testWebkitAtkGetTextAtOffsetTextarea()
803
{
804
    WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
805
    g_object_ref_sink(webView);
806 807 808
    GtkAllocation allocation = { 0, 0, 800, 600 };
    gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
    webkit_web_view_load_string(webView, contentsInTextarea, 0, 0, 0);
809

810
    /* Get to the inner AtkText object. */
811
    AtkObject* object = getWebAreaObject(webView);
812 813 814 815 816
    g_assert(object);
    object = atk_object_ref_accessible_child(object, 0);
    g_assert(object);
    object = atk_object_ref_accessible_child(object, 0);
    g_assert(object);
817

818 819
    AtkText* textObject = ATK_TEXT(object);
    g_assert(ATK_IS_TEXT(textObject));
820

821
    runGetTextTests(textObject);
822 823 824 825

    g_object_unref(webView);
}

826
static void testWebkitAtkGetTextAtOffsetTextInput()
827
{
828
    WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
829
    g_object_ref_sink(webView);
830 831 832
    GtkAllocation allocation = { 0, 0, 800, 600 };
    gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
    webkit_web_view_load_string(webView, contentsInTextInput, 0, 0, 0);
833

834
    /* Get to the inner AtkText object. */
835
    AtkObject* object = getWebAreaObject(webView);
836 837 838 839 840
    g_assert(object);
    object = atk_object_ref_accessible_child(object, 0);
    g_assert(object);
    object = atk_object_ref_accessible_child(object, 0);
    g_assert(object);
841

842 843
    AtkText* textObject = ATK_TEXT(object);
    g_assert(ATK_IS_TEXT(textObject));
844

845
    runGetTextTests(textObject);
846 847 848 849

    g_object_unref(webView);
}

850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
static void testWebkitAtkGetTextAtOffsetWithPreformattedText()
{
    WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
    g_object_ref_sink(webView);
    GtkAllocation allocation = { 0, 0, 800, 600 };
    gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
    webkit_web_view_load_string(webView, contentsWithPreformattedText, 0, 0, 0);

    AtkObject* object = getWebAreaObject(webView);
    g_assert(object);

    AtkObject* preformattedText = atk_object_ref_accessible_child(object, 0);
    g_assert(ATK_IS_OBJECT(preformattedText));
    g_assert(atk_object_get_role(preformattedText) == ATK_ROLE_PANEL);
    g_assert(ATK_IS_TEXT(preformattedText));
    char* text = atk_text_get_text(ATK_TEXT(preformattedText), 0, -1);
    g_assert_cmpstr(text, ==, "\t\n\tfirst line\n\tsecond line\n");
    g_free(text);

    /* Try retrieving all the lines indicating the position of the offsets at the beginning of each of them. */
    testGetTextFunction(ATK_TEXT(preformattedText), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 0, "\t\n", 0, 2);
    testGetTextFunction(ATK_TEXT(preformattedText), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 2, "\tfirst line\n", 2, 14);
    testGetTextFunction(ATK_TEXT(preformattedText), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 14, "\tsecond line\n", 14, 27);

    g_object_unref(preformattedText);
    g_object_unref(webView);
}

878 879 880 881 882 883 884 885 886
static void testWebkitAtkGetTextAtOffsetWithSpecialCharacters()
{
    WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
    g_object_ref_sink(webView);
    GtkAllocation allocation = { 0, 0, 800, 600 };
    gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
    webkit_web_view_load_string(webView, contentsWithSpecialChars, 0, 0, 0);

    /* Get to the inner AtkText object. */
887
    AtkObject* object = getWebAreaObject(webView);
888 889
    g_assert(object);

890 891
    AtkObject* paragraph = atk_object_ref_accessible_child(object, 0);
    g_assert(ATK_IS_TEXT(paragraph));
892

893 894
    gchar* expectedText = g_strdup("\302\253\302\240This is a paragraph with \342\200\234special\342\200\235 characters inside.\302\240\302\273");
    char* text = atk_text_get_text(ATK_TEXT(paragraph), 0, -1);
895 896 897 898 899 900
    g_assert_cmpstr(text, ==, expectedText);
    g_free(text);

    /* Check that getting the text with ATK_TEXT_BOUNDARY_LINE_START
       and ATK_TEXT_BOUNDARY_LINE_END does not crash because of not
       properly handling characters inside the UTF-8 string. */
901 902 903 904 905 906 907 908 909
    testGetTextFunction(ATK_TEXT(paragraph), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 0, expectedText, 0, 57);
    testGetTextFunction(ATK_TEXT(paragraph), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END, 0, expectedText, 0, 57);
    g_free(expectedText);

    AtkObject* list = atk_object_ref_accessible_child(object, 1);
    g_assert(ATK_OBJECT(list));

    AtkText* listItem = ATK_TEXT(atk_object_ref_accessible_child(list, 0));
    g_assert(ATK_IS_TEXT(listItem));
910

911 912 913 914 915 916 917 918 919 920 921 922 923
    text = atk_text_get_text(ATK_TEXT(listItem), 0, -1);
    g_assert_cmpstr(text, ==, "\342\200\242 List item with some text that wraps across different lines.");
    g_free(text);

    /* Check that getting the text with ATK_TEXT_BOUNDARY_LINE_START
       and ATK_TEXT_BOUNDARY_LINE_END for line items with bullets
       (special character) and wrapped text always return the right
       piece of text for each line. */
    testGetTextFunction(ATK_TEXT(listItem), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 3, "\342\200\242 List item ", 0, 12);
    testGetTextFunction(ATK_TEXT(listItem), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 13, "with some ", 12, 22);
    testGetTextFunction(ATK_TEXT(listItem), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END, 0, "\342\200\242 List item", 0, 11);
    testGetTextFunction(ATK_TEXT(listItem), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END, 12, " with some", 11, 21);

924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
    g_object_unref(listItem);

    listItem = ATK_TEXT(atk_object_ref_accessible_child(list, 1));
    g_assert(ATK_IS_TEXT(listItem));

    /* Check that placing the same text in a paragraph doesn't break things. */
    text = atk_text_get_text(ATK_TEXT(listItem), 0, -1);
    g_assert_cmpstr(text, ==, "\342\200\242 List item with some text that wraps across different lines.");
    g_free(text);

    testGetTextFunction(ATK_TEXT(listItem), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 3, "\342\200\242 List item ", 0, 12);
    testGetTextFunction(ATK_TEXT(listItem), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 13, "with some ", 12, 22);
    testGetTextFunction(ATK_TEXT(listItem), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END, 0, "\342\200\242 List item", 0, 11);
    testGetTextFunction(ATK_TEXT(listItem), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END, 12, " with some", 11, 21);

939 940 941
    g_object_unref(list);
    g_object_unref(listItem);
    g_object_unref(paragraph);
942 943 944
    g_object_unref(webView);
}

945
static void testWebkitAtkGetTextInParagraphAndBodySimple()
946
{
947
    WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
948
    g_object_ref_sink(webView);
949 950 951 952 953
    GtkAllocation allocation = { 0, 0, 800, 600 };
    gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
    webkit_web_view_load_string(webView, contentsInParagraphAndBodySimple, 0, 0, 0);

    /* Get to the inner AtkText object. */
954
    AtkObject* object = getWebAreaObject(webView);
955 956 957 958 959 960 961 962 963 964 965 966
    g_assert(object);
    AtkObject* object1 = atk_object_ref_accessible_child(object, 0);
    g_assert(object1);
    AtkObject* object2 = atk_object_ref_accessible_child(object, 1);
    g_assert(object2);

    AtkText* textObject1 = ATK_TEXT(object1);
    g_assert(ATK_IS_TEXT(textObject1));
    AtkText* textObject2 = ATK_TEXT(object2);
    g_assert(ATK_IS_TEXT(textObject2));

    char *text = atk_text_get_text(textObject1, 0, -1);
967 968
    g_assert_cmpstr(text, ==, "This is a test.");

969
    text = atk_text_get_text(textObject2, 0, 12);
970 971
    g_assert_cmpstr(text, ==, "Hello world.");

972 973
    g_object_unref(object1);
    g_object_unref(object2);
974 975 976
    g_object_unref(webView);
}

977
static void testWebkitAtkGetTextInParagraphAndBodyModerate()
978
{
979
    WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
980
    g_object_ref_sink(webView);
981 982 983 984 985
    GtkAllocation allocation = { 0, 0, 800, 600 };
    gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
    webkit_web_view_load_string(webView, contentsInParagraphAndBodyModerate, 0, 0, 0);

    /* Get to the inner AtkText object. */
986
    AtkObject* object = getWebAreaObject(webView);
987 988 989 990 991 992 993 994 995 996 997 998
    g_assert(object);
    AtkObject* object1 = atk_object_ref_accessible_child(object, 0);
    g_assert(object1);
    AtkObject* object2 = atk_object_ref_accessible_child(object, 1);
    g_assert(object2);

    AtkText* textObject1 = ATK_TEXT(object1);
    g_assert(ATK_IS_TEXT(textObject1));
    AtkText* textObject2 = ATK_TEXT(object2);
    g_assert(ATK_IS_TEXT(textObject2));

    char *text = atk_text_get_text(textObject1, 0, -1);
999 1000
    g_assert_cmpstr(text, ==, "This is a test.");

1001
    text = atk_text_get_text(textObject2, 0, 53);
1002 1003
    g_assert_cmpstr(text, ==, "Hello world.\nThis sentence is green.\nThis one is not.");

1004 1005
    g_object_unref(object1);
    g_object_unref(object2);
1006 1007 1008
    g_object_unref(webView);
}

1009
static void testWebkitAtkGetTextInTable()
1010
{
1011
    WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
1012
    g_object_ref_sink(webView);
1013 1014 1015
    GtkAllocation allocation = { 0, 0, 800, 600 };
    gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
    webkit_web_view_load_string(webView, contentsInTable, 0, 0, 0);
1016

1017
    AtkObject* object = getWebAreaObject(webView);
1018 1019 1020
    g_assert(object);
    object = atk_object_ref_accessible_child(object, 0);
    g_assert(object);
1021

1022 1023
    /* Tables should not implement AtkText. */
    g_assert(!G_TYPE_INSTANCE_GET_INTERFACE(object, ATK_TYPE_TEXT, AtkTextIface));
1024

1025
    g_object_unref(object);
1026 1027 1028
    g_object_unref(webView);
}

1029
static void testWebkitAtkGetHeadersInTable()
1030
{
1031
    WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
1032
    g_object_ref_sink(webView);
1033 1034 1035
    GtkAllocation allocation = { 0, 0, 800, 600 };
    gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
    webkit_web_view_load_string(webView, contentsInTableWithHeaders, 0, 0, 0);
1036

1037
    AtkObject* axWebView = getWebAreaObject(webView);
1038 1039
    g_assert(axWebView);

1040 1041
    /* Check table with both column and row headers. */
    AtkObject* table = atk_object_ref_accessible_child(axWebView, 0);
1042 1043 1044
    g_assert(table);
    g_assert(atk_object_get_role(table) == ATK_ROLE_TABLE);

1045
    AtkObject* colHeader = atk_table_get_column_header(ATK_TABLE(table), 0);
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
    g_assert(colHeader);
    g_assert(atk_object_get_role(colHeader) == ATK_ROLE_TABLE_CELL);
    g_assert(atk_object_get_index_in_parent(colHeader) == 0);

    colHeader = atk_table_get_column_header(ATK_TABLE(table), 1);
    g_assert(colHeader);
    g_assert(atk_object_get_role(colHeader) == ATK_ROLE_TABLE_CELL);
    g_assert(atk_object_get_index_in_parent(colHeader) == 1);

    colHeader = atk_table_get_column_header(ATK_TABLE(table), 2);
    g_assert(colHeader);
    g_assert(atk_object_get_role(colHeader) == ATK_ROLE_TABLE_CELL);
    g_assert(atk_object_get_index_in_parent(colHeader) == 2);

    colHeader = atk_table_get_column_header(ATK_TABLE(table), 3);
    g_assert(colHeader);
    g_assert(atk_object_get_role(colHeader) == ATK_ROLE_TABLE_CELL);
    g_assert(atk_object_get_index_in_parent(colHeader) == 2);

1065
    AtkObject* rowHeader = atk_table_get_row_header(ATK_TABLE(table), 0);
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
    g_assert(rowHeader);
    g_assert(atk_object_get_role(rowHeader) == ATK_ROLE_TABLE_CELL);
    g_assert(atk_object_get_index_in_parent(rowHeader) == 0);

    rowHeader = atk_table_get_row_header(ATK_TABLE(table), 1);
    g_assert(rowHeader);
    g_assert(atk_object_get_role(rowHeader) == ATK_ROLE_TABLE_CELL);
    g_assert(atk_object_get_index_in_parent(rowHeader) == 3);

    rowHeader = atk_table_get_row_header(ATK_TABLE(ta