2 * Copyright (C) 2009 Igalia S.L.
3 * Copyright (C) 2013 Samsung Electronics. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #include "autotoolsconfig.h"
24 #include <glib/gstdio.h>
28 #include <webkit/webkit.h>
30 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 static const char* contents = "<html><body><p>This is a test. This is the second sentence. And this the third.</p></body></html>";
34 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 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>« This is a paragraph with “special” characters inside. »</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>";
40 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>";
42 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 static const char* contentsInParagraphAndBodySimple = "<html><body><p>This is a test.</p>Hello world.</body></html>";
46 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 static const char* contentsInTable = "<html><body><table><tr><td>foo</td><td>bar</td></tr></table></body></html>";
50 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 static const char* contentsWithExtraneousWhiteSpaces = "<html><head><body><p>This\n paragraph\n is\n borked!</p></body></html>";
54 static const char* contentsWithWrappedLines = "<html><body><p style='max-width:150px;'>This is one line wrapped because of the maximum width of its container.</p><p>This is another line wrapped<br>because of one forced<br>line break in the middle.</body></html>";
56 static const char* contentsWithEmbeddedObjects = "<html><body>This is one line containing two <img> embedded objects <img> in the middle.</body></html>";
58 static const char* comboBoxSelector = "<html><body><select><option selected value='foo'>foo</option><option value='bar'>bar</option></select></body></html>";
60 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>";
62 static const char* formWithTextInputs = "<html><body><form><input type='text' name='entry' /><input type='password' name='passwordEntry' /></form></body></html>";
64 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>";
66 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>";
68 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>";
70 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>";
72 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>";
74 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>";
76 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>";
78 static AtkObject* getWebAreaObject(WebKitWebView* webView)
80 /* Manually spin the main context to make sure the accessible
81 objects are properly created before continuing. */
82 while (g_main_context_pending(0))
83 g_main_context_iteration(0, TRUE);
85 AtkObject* rootObject = gtk_widget_get_accessible(GTK_WIDGET(webView));
89 AtkObject* webAreaObject = atk_object_ref_accessible_child(rootObject, 0);
93 /* We don't need the extra ref here. */
94 g_object_unref(webAreaObject);
99 typedef gchar* (*AtkGetTextFunction) (AtkText*, gint, AtkTextBoundary, gint*, gint*);
101 static void testGetTextFunction(AtkText* textObject, AtkGetTextFunction fn, AtkTextBoundary boundary, gint offset, const char* textResult, gint startOffsetResult, gint endOffsetResult)
105 char* text = fn(textObject, offset, boundary, &startOffset, &endOffset);
106 g_assert_cmpstr(text, ==, textResult);
107 g_assert_cmpint(startOffset, ==, startOffsetResult);
108 g_assert_cmpint(endOffset, ==, endOffsetResult);
112 static void runGetTextTests(AtkText* textObject)
114 char* text = atk_text_get_text(textObject, 0, -1);
115 g_assert_cmpstr(text, ==, "This is a test. This is the second sentence. And this the third.");
118 /* ATK_TEXT_BOUNDARY_CHAR */
119 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_CHAR,
122 testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_CHAR,
125 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_CHAR,
128 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_CHAR,
131 /* ATK_TEXT_BOUNDARY_WORD_START */
132 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START,
135 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START,
138 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START,
139 10, "test. ", 10, 16);
141 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START,
142 58, "third.", 58, 64);
144 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START,
145 64, "third.", 58, 64);
147 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_START,
150 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_START,
153 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_START,
156 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_START,
159 testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_START,
162 testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_START,
165 testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_START,
168 testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_START,
171 /* ATK_TEXT_BOUNDARY_WORD_END */
172 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END,
175 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END,
178 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END,
181 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END,
184 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END,
185 58, " third", 57, 63);
187 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END,
190 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_END,
193 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_END,
196 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_END,
199 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_END,
202 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_END,
203 64, " third", 57, 63);
205 testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_END,
208 testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_END,
211 testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_END,
214 testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_END,
217 /* ATK_TEXT_BOUNDARY_SENTENCE_START */
218 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
219 0, "This is a test. ", 0, 16);
221 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
222 15, "This is a test. ", 0, 16);
224 testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
225 0, "This is the second sentence. ", 16, 45);
227 testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
228 15, "This is the second sentence. ", 16, 45);
230 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
231 16, "This is a test. ", 0, 16);
233 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
234 44, "This is a test. ", 0, 16);
236 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
239 /* ATK_TEXT_BOUNDARY_SENTENCE_END */
240 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
241 0, "This is a test.", 0, 15);
243 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
244 15, " This is the second sentence.", 15, 44);
246 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
247 16, " This is the second sentence.", 15, 44);
249 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
250 17, " This is the second sentence.", 15, 44);
252 testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
253 0, " This is the second sentence.", 15, 44);
255 testGetTextFunction(textObject, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
256 15, " And this the third.", 44, 64);
258 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
259 16, "This is a test.", 0, 15);
261 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
262 15, "This is a test.", 0, 15);
264 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
267 testGetTextFunction(textObject, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
268 44, " This is the second sentence.", 15, 44);
270 /* It's tricky to test these properly right now, since our a11y
271 implementation splits different lines in different a11y items. */
272 /* ATK_TEXT_BOUNDARY_LINE_START */
273 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START,
274 0, "This is a test. This is the second sentence. And this the third.", 0, 64);
276 /* ATK_TEXT_BOUNDARY_LINE_END */
277 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END,
278 0, "This is a test. This is the second sentence. And this the third.", 0, 64);
280 /* For objects implementing AtkEditableText, try to change the
281 exposed text and retrieve it again as a full line.
282 (see https://bugs.webkit.org/show_bug.cgi?id=72830) */
283 if (ATK_IS_EDITABLE_TEXT(textObject)) {
284 atk_editable_text_set_text_contents(ATK_EDITABLE_TEXT(textObject), "foo bar baz");
285 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 0, "foo bar baz", 0, 11);
286 testGetTextFunction(textObject, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END, 0, "foo bar baz", 0, 11);
290 static gchar* textCaretMovedResult = 0;
292 static void textCaretMovedCallback(AtkText* text, gint pos, gpointer data)
294 g_assert(ATK_IS_TEXT(text));
296 g_free(textCaretMovedResult);
297 AtkRole role = atk_object_get_role(ATK_OBJECT(text));
298 textCaretMovedResult = g_strdup_printf("|%s|%d|", atk_role_get_name(role), pos);
301 static void testWebkitAtkCaretOffsets()
303 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
304 g_object_ref_sink(webView);
305 GtkAllocation allocation = { 0, 0, 800, 600 };
306 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
307 webkit_web_view_load_string(webView, textForCaretBrowsing, 0, 0, 0);
309 AtkObject* object = getWebAreaObject(webView);
312 AtkObject* header = atk_object_ref_accessible_child(object, 0);
313 g_assert(ATK_IS_TEXT(header));
314 g_signal_connect(header, "text-caret-moved", G_CALLBACK(textCaretMovedCallback), 0);
316 gchar* text = atk_text_get_text(ATK_TEXT(header), 0, -1);
317 g_assert_cmpstr(text, ==, "A text header");
320 /* It should be possible to place the caret inside a header. */
321 gboolean result = atk_text_set_caret_offset(ATK_TEXT(header), 5);
322 g_assert_cmpint(result, ==, TRUE);
323 gint offset = atk_text_get_caret_offset(ATK_TEXT(header));
324 g_assert_cmpint(offset, ==, 5);
325 g_assert_cmpstr(textCaretMovedResult, ==, "|heading|5|");
327 AtkObject* paragraph = atk_object_ref_accessible_child(object, 1);
328 g_assert(ATK_IS_TEXT(paragraph));
329 g_signal_connect(paragraph, "text-caret-moved", G_CALLBACK(textCaretMovedCallback), 0);
331 text = atk_text_get_text(ATK_TEXT(paragraph), 0, -1);
332 g_assert_cmpstr(text, ==, "A paragraph with a link in the middle");
335 /* It should be possible to place the caret inside a paragraph and a link. */
336 result = atk_text_set_caret_offset(ATK_TEXT(paragraph), 5);
337 g_assert_cmpint(result, ==, TRUE);
338 offset = atk_text_get_caret_offset(ATK_TEXT(paragraph));
339 g_assert_cmpint(offset, ==, 5);
340 g_assert_cmpstr(textCaretMovedResult, ==, "|paragraph|5|");
342 result = atk_text_set_caret_offset(ATK_TEXT(paragraph), 20);
343 g_assert_cmpint(result, ==, TRUE);
344 offset = atk_text_get_caret_offset(ATK_TEXT(paragraph));
345 g_assert_cmpint(offset, ==, 20);
346 g_assert_cmpstr(textCaretMovedResult, ==, "|paragraph|20|");
348 result = atk_text_set_caret_offset(ATK_TEXT(paragraph), 30);
349 g_assert_cmpint(result, ==, TRUE);
350 offset = atk_text_get_caret_offset(ATK_TEXT(paragraph));
351 g_assert_cmpint(offset, ==, 30);
352 g_assert_cmpstr(textCaretMovedResult, ==, "|paragraph|30|");
354 AtkObject* link = atk_object_ref_accessible_child(paragraph, 0);
355 g_assert(ATK_IS_TEXT(link));
356 text = atk_text_get_text(ATK_TEXT(link), 0, -1);
357 g_assert_cmpstr(text, ==, "with a link");
360 result = atk_text_set_caret_offset(ATK_TEXT(link), 5);
361 g_assert_cmpint(result, ==, TRUE);
362 offset = atk_text_get_caret_offset(ATK_TEXT(link));
363 g_assert_cmpint(offset, ==, 5);
364 /* Positions inside links are reported relative to the paragraph. */
365 g_assert_cmpstr(textCaretMovedResult, ==, "|paragraph|17|");
367 AtkObject* list = atk_object_ref_accessible_child(object, 2);
368 g_assert(ATK_OBJECT(list));
369 g_assert(atk_object_get_role(list) == ATK_ROLE_LIST);
370 g_assert_cmpint(atk_object_get_n_accessible_children(list), ==, 3);
372 AtkObject* listItem = atk_object_ref_accessible_child(list, 0);
373 g_assert(ATK_IS_TEXT(listItem));
374 text = atk_text_get_text(ATK_TEXT(listItem), 0, -1);
375 g_assert_cmpstr(text, ==, "1. A list item");
378 listItem = atk_object_ref_accessible_child(list, 1);
379 g_assert(ATK_IS_TEXT(listItem));
380 text = atk_text_get_text(ATK_TEXT(listItem), 0, -1);
381 g_assert_cmpstr(text, ==, "2. Block span in a list item\nInline span in a list item");
384 listItem = atk_object_ref_accessible_child(list, 2);
385 g_assert(ATK_IS_TEXT(listItem));
386 text = atk_text_get_text(ATK_TEXT(listItem), 0, -1);
387 g_assert_cmpstr(text, ==, "3. Block span in a link in a list item\nInline span in a link in a list item");
390 /* It's not possible to place the caret inside an item's marker. */
391 result = atk_text_set_caret_offset(ATK_TEXT(listItem), 1);
392 g_assert_cmpint(result, ==, FALSE);
394 /* It should be possible to place the caret inside an item's text. */
395 result = atk_text_set_caret_offset(ATK_TEXT(listItem), 5);
396 g_assert_cmpint(result, ==, TRUE);
397 offset = atk_text_get_caret_offset(ATK_TEXT(listItem));
398 g_assert_cmpint(offset, ==, 5);
400 AtkObject* panel = atk_object_ref_accessible_child(object, 3);
401 g_assert(ATK_IS_OBJECT(panel));
402 g_assert(atk_object_get_role(panel) == ATK_ROLE_PANEL);
404 AtkObject* comboBox = atk_object_ref_accessible_child(panel, 0);
405 g_assert(ATK_IS_OBJECT(comboBox));
406 g_assert(!ATK_IS_TEXT(comboBox));
407 g_assert(atk_object_get_role(comboBox) == ATK_ROLE_COMBO_BOX);
409 AtkObject* menuPopup = atk_object_ref_accessible_child(comboBox, 0);
410 g_assert(ATK_IS_OBJECT(menuPopup));
411 g_assert(!ATK_IS_TEXT(menuPopup));
412 g_assert(atk_object_get_role(menuPopup) == ATK_ROLE_MENU);
414 AtkObject* comboBoxOption = atk_object_ref_accessible_child(menuPopup, 0);
415 g_assert(ATK_IS_OBJECT(comboBoxOption));
416 g_assert(atk_object_get_role(comboBoxOption) == ATK_ROLE_MENU_ITEM);
417 g_assert(ATK_IS_TEXT(comboBoxOption));
418 text = atk_text_get_text(ATK_TEXT(comboBoxOption), 0, -1);
419 g_assert_cmpstr(text, ==, "An option in a combo box");
422 /* It's not possible to place the caret inside an option for a combobox. */
423 result = atk_text_set_caret_offset(ATK_TEXT(comboBoxOption), 1);
424 g_assert_cmpint(result, ==, FALSE);
426 AtkObject* textEntry = atk_object_ref_accessible_child(panel, 1);
427 g_assert(ATK_IS_OBJECT(textEntry));
428 g_assert(atk_object_get_role(textEntry) == ATK_ROLE_ENTRY);
429 g_assert(ATK_IS_TEXT(textEntry));
430 text = atk_text_get_text(ATK_TEXT(textEntry), 0, -1);
431 g_assert_cmpstr(text, ==, "foo bar baz");
434 result = atk_text_set_caret_offset(ATK_TEXT(textEntry), 5);
435 g_assert_cmpint(result, ==, TRUE);
436 offset = atk_text_get_caret_offset(ATK_TEXT(textEntry));
437 g_assert_cmpint(offset, ==, 5);
439 AtkObject* table = atk_object_ref_accessible_child(object, 4);
440 g_assert(ATK_IS_OBJECT(table));
441 g_assert(!ATK_IS_TEXT(table));
442 g_assert(atk_object_get_role(table) == ATK_ROLE_TABLE);
443 g_assert_cmpint(atk_object_get_n_accessible_children(table), ==, 4);
445 AtkObject* tableCell = atk_object_ref_accessible_child(table, 0);
446 g_assert(ATK_IS_TEXT(tableCell));
447 g_assert(atk_object_get_role(tableCell) == ATK_ROLE_TABLE_CELL);
448 text = atk_text_get_text(ATK_TEXT(tableCell), 0, -1);
449 g_assert_cmpstr(text, ==, "a table cell");
452 result = atk_text_set_caret_offset(ATK_TEXT(tableCell), 2);
453 g_assert_cmpint(result, ==, TRUE);
454 offset = atk_text_get_caret_offset(ATK_TEXT(tableCell));
455 g_assert_cmpint(offset, ==, 2);
457 /* Even empty table cells should implement AtkText, but report an empty string */
458 tableCell = atk_object_ref_accessible_child(table, 1);
459 g_assert(ATK_IS_TEXT(tableCell));
460 g_assert(atk_object_get_role(tableCell) == ATK_ROLE_TABLE_CELL);
461 text = atk_text_get_text(ATK_TEXT(tableCell), 0, -1);
462 g_assert_cmpstr(text, ==, "");
465 tableCell = atk_object_ref_accessible_child(table, 2);
466 g_assert(ATK_IS_TEXT(tableCell));
467 g_assert(atk_object_get_role(tableCell) == ATK_ROLE_TABLE_CELL);
468 text = atk_text_get_text(ATK_TEXT(tableCell), 0, -1);
469 g_assert_cmpstr(text, ==, "Block span in a link in a table cell\nInline span in a link in a table cell");
472 tableCell = atk_object_ref_accessible_child(table, 3);
473 g_assert(ATK_IS_TEXT(tableCell));
474 g_assert(atk_object_get_role(tableCell) == ATK_ROLE_TABLE_CELL);
475 text = atk_text_get_text(ATK_TEXT(tableCell), 0, -1);
476 g_assert_cmpstr(text, ==, "Block span in a table cell\nInline span in a table cell");
479 header = atk_object_ref_accessible_child(object, 5);
480 g_assert(ATK_IS_TEXT(header));
481 g_assert(atk_object_get_role(header) == ATK_ROLE_HEADING);
482 text = atk_text_get_text(ATK_TEXT(header), 0, -1);
483 g_assert_cmpstr(text, ==, "Block span in a link in a heading\nInline span in a link in a heading");
486 header = atk_object_ref_accessible_child(object, 6);
487 g_assert(ATK_IS_TEXT(header));
488 g_assert(atk_object_get_role(header) == ATK_ROLE_HEADING);
489 text = atk_text_get_text(ATK_TEXT(header), 0, -1);
490 g_assert_cmpstr(text, ==, "Block span in a heading\nInline span in a heading");
493 g_free(textCaretMovedResult);
495 g_object_unref(header);
496 g_object_unref(paragraph);
497 g_object_unref(link);
498 g_object_unref(list);
499 g_object_unref(listItem);
500 g_object_unref(panel);
501 g_object_unref(comboBox);
502 g_object_unref(menuPopup);
503 g_object_unref(comboBoxOption);
504 g_object_unref(textEntry);
505 g_object_unref(table);
506 g_object_unref(tableCell);
507 g_object_unref(webView);
510 static void testWebkitAtkCaretOffsetsAndExtranousWhiteSpaces()
512 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
513 g_object_ref_sink(webView);
514 GtkAllocation allocation = { 0, 0, 800, 600 };
515 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
516 webkit_web_view_load_string(webView, contentsWithExtraneousWhiteSpaces, 0, 0, 0);
518 /* Enable caret browsing. */
519 WebKitWebSettings* settings = webkit_web_view_get_settings(webView);
520 g_object_set(G_OBJECT(settings), "enable-caret-browsing", TRUE, NULL);
521 webkit_web_view_set_settings(webView, settings);
523 /* Get to the inner AtkText object. */
524 AtkObject* object = getWebAreaObject(webView);
526 object = atk_object_ref_accessible_child(object, 0);
529 AtkText* textObject = ATK_TEXT(object);
530 g_assert(ATK_IS_TEXT(textObject));
532 gchar* text = atk_text_get_text(textObject, 0, -1);
533 g_assert_cmpstr(text, ==, "This paragraph is borked!");
536 gint characterCount = atk_text_get_character_count(textObject);
537 g_assert_cmpint(characterCount, ==, 25);
539 gboolean result = atk_text_set_caret_offset(textObject, characterCount - 1);
540 g_assert_cmpint(result, ==, TRUE);
542 gint caretOffset = atk_text_get_caret_offset(textObject);
543 g_assert_cmpint(caretOffset, ==, characterCount - 1);
545 result = atk_text_set_caret_offset(textObject, characterCount);
546 g_assert_cmpint(result, ==, TRUE);
548 caretOffset = atk_text_get_caret_offset(textObject);
549 g_assert_cmpint(caretOffset, ==, characterCount);
551 g_object_unref(webView);
554 static void testWebkitAtkComboBox()
556 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
557 g_object_ref_sink(webView);
558 GtkAllocation allocation = { 0, 0, 800, 600 };
559 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
560 webkit_web_view_load_string(webView, comboBoxSelector, 0, 0, 0);
562 AtkObject* object = getWebAreaObject(webView);
565 AtkObject* formObject = atk_object_ref_accessible_child(object, 0);
566 g_assert(formObject);
568 AtkObject* comboBox = atk_object_ref_accessible_child(formObject, 0);
569 g_assert(ATK_IS_OBJECT(comboBox));
571 AtkObject* menuPopup = atk_object_ref_accessible_child(comboBox, 0);
572 g_assert(ATK_IS_OBJECT(menuPopup));
574 AtkObject* item1 = atk_object_ref_accessible_child(menuPopup, 0);
575 g_assert(ATK_IS_OBJECT(item1));
577 AtkObject* item2 = atk_object_ref_accessible_child(menuPopup, 1);
578 g_assert(ATK_IS_OBJECT(item2));
581 g_assert(atk_object_get_role(comboBox) == ATK_ROLE_COMBO_BOX);
582 g_assert(atk_object_get_role(menuPopup) == ATK_ROLE_MENU);
583 g_assert(atk_object_get_role(item1) == ATK_ROLE_MENU_ITEM);
584 g_assert(atk_object_get_role(item2) == ATK_ROLE_MENU_ITEM);
586 /* Check the implementation of the AtkSelection interface. */
587 g_assert(ATK_IS_SELECTION(comboBox));
588 AtkSelection* atkSelection = ATK_SELECTION(comboBox);
589 g_assert_cmpint(atk_selection_get_selection_count(atkSelection), ==, 1);
590 g_assert(atk_selection_is_child_selected(atkSelection, 0));
591 g_assert(!atk_selection_is_child_selected(atkSelection, 1));
592 AtkObject* selectedItem = atk_selection_ref_selection(atkSelection, 0);
593 g_assert(selectedItem == item1);
594 g_object_unref(selectedItem);
596 /* Check that the menu popup has 0 links and doesn't crash from checking. */
597 gint nLinks = atk_hypertext_get_n_links(ATK_HYPERTEXT(menuPopup));
598 g_assert_cmpint(nLinks, ==, 0);
600 /* Check the implementations of the AtkAction interface. */
601 g_assert(ATK_IS_ACTION(comboBox));
602 AtkAction* atkAction = ATK_ACTION(comboBox);
603 g_assert_cmpint(atk_action_get_n_actions(atkAction), ==, 1);
604 g_assert(atk_action_do_action(atkAction, 0));
606 g_assert(ATK_IS_ACTION(menuPopup));
607 atkAction = ATK_ACTION(menuPopup);
608 g_assert_cmpint(atk_action_get_n_actions(atkAction), ==, 1);
609 g_assert(atk_action_do_action(atkAction, 0));
611 g_assert(ATK_IS_ACTION(item1));
612 atkAction = ATK_ACTION(item1);
613 g_assert_cmpint(atk_action_get_n_actions(atkAction), ==, 1);
614 g_assert(atk_action_do_action(atkAction, 0));
616 g_assert(ATK_IS_ACTION(item2));
617 atkAction = ATK_ACTION(item2);
618 g_assert_cmpint(atk_action_get_n_actions(atkAction), ==, 1);
619 g_assert(atk_action_do_action(atkAction, 0));
621 /* After selecting the second item, selection should have changed. */
622 g_assert_cmpint(atk_selection_get_selection_count(atkSelection), ==, 1);
623 g_assert(!atk_selection_is_child_selected(atkSelection, 0));
624 g_assert(atk_selection_is_child_selected(atkSelection, 1));
625 selectedItem = atk_selection_ref_selection(atkSelection, 0);
626 g_assert(selectedItem == item2);
627 g_object_unref(selectedItem);
629 /* Check the implementation of the AtkText interface. */
630 g_assert(ATK_IS_TEXT(item1));
631 AtkText* atkText = ATK_TEXT(item1);
632 char *text = atk_text_get_text(atkText, 0, -1);
633 g_assert_cmpstr(text, ==, "foo");
635 text = atk_text_get_text(atkText, 0, 2);
636 g_assert_cmpstr(text, ==, "fo");
639 g_assert(ATK_IS_TEXT(item2));
640 atkText = ATK_TEXT(item2);
641 text = atk_text_get_text(atkText, 0, -1);
642 g_assert_cmpstr(text, ==, "bar");
644 text = atk_text_get_text(atkText, 1, 3);
645 g_assert_cmpstr(text, ==, "ar");
648 g_object_unref(formObject);
649 g_object_unref(comboBox);
650 g_object_unref(menuPopup);
651 g_object_unref(item1);
652 g_object_unref(item2);
653 g_object_unref(webView);
656 static gchar* loadingEventsResult = 0;
658 static void updateLoadingEventsResult(const gchar* signalName)
660 g_assert(signalName);
662 gchar* previousResult = loadingEventsResult;
663 loadingEventsResult = g_strdup_printf("%s|%s", previousResult, signalName);
664 g_free(previousResult);
667 static gboolean documentLoadingEventCallback(GSignalInvocationHint *signalHint, guint numParamValues, const GValue *paramValues, gpointer data)
669 // At least we should receive the instance emitting the signal.
670 if (numParamValues < 1)
673 GSignalQuery signal_query;
674 g_signal_query(signalHint->signal_id, &signal_query);
676 updateLoadingEventsResult(signal_query.signal_name);
680 static void testWebkitAtkDocumentLoadingEvents()
682 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
683 g_object_ref_sink(webView);
684 GtkAllocation allocation = { 0, 0, 800, 600 };
685 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
687 /* Connect globally to see those events during a future load. */
688 guint loadCompleteListenerId = atk_add_global_event_listener(documentLoadingEventCallback, "ATK:AtkDocument:load-complete");
690 /* Do the load, so we can see those events happening. */
691 loadingEventsResult = g_strdup("");
692 webkit_web_view_load_string(webView, contents, 0, 0, 0);
694 /* Trigger the creation of the full accessibility hierarchy by
695 asking for the webArea object, so we can listen to events. */
696 getWebAreaObject(webView);
698 atk_remove_global_event_listener(loadCompleteListenerId);
700 g_assert_cmpstr(loadingEventsResult, ==, "|load-complete");
702 g_free(loadingEventsResult);
703 g_object_unref(webView);
706 static void testWebkitAtkEmbeddedObjects()
708 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
709 g_object_ref_sink(webView);
710 GtkAllocation allocation = { 0, 0, 800, 600 };
711 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
712 webkit_web_view_load_string(webView, embeddedObjects, 0, 0, 0);
714 AtkObject* object = getWebAreaObject(webView);
717 AtkText* paragraph1 = ATK_TEXT(atk_object_ref_accessible_child(object, 0));
718 g_assert(ATK_IS_TEXT(paragraph1));
719 g_assert(ATK_IS_HYPERTEXT(paragraph1));
721 const gchar* expectedText = "Choose: \357\277\274foo \357\277\274bar (pick one)";
722 char* text = atk_text_get_text(paragraph1, 0, -1);
723 g_assert_cmpstr(text, ==, expectedText);
726 gint nLinks = atk_hypertext_get_n_links(ATK_HYPERTEXT(paragraph1));
727 g_assert_cmpint(nLinks, ==, 2);
729 AtkHyperlink* hLink = atk_hypertext_get_link(ATK_HYPERTEXT(paragraph1), 0);
730 g_assert(ATK_HYPERLINK(hLink));
731 AtkObject* hLinkObject = atk_hyperlink_get_object(hLink, 0);
732 g_assert(ATK_OBJECT(hLinkObject));
733 g_assert(atk_object_get_role(hLinkObject) == ATK_ROLE_CHECK_BOX);
734 g_assert_cmpint(atk_hyperlink_get_start_index(hLink), ==, 8);
735 g_assert_cmpint(atk_hyperlink_get_end_index(hLink), ==, 9);
736 g_assert_cmpint(atk_hyperlink_get_n_anchors(hLink), ==, 1);
737 g_assert_cmpstr(atk_hyperlink_get_uri(hLink, 0), ==, 0);
739 AtkText* paragraph2 = ATK_TEXT(atk_object_ref_accessible_child(object, 1));
740 g_assert(ATK_IS_TEXT(paragraph2));
741 g_assert(ATK_IS_HYPERTEXT(paragraph2));
743 expectedText = "Choose: \357\277\274 (pick one)";
744 text = atk_text_get_text(paragraph2, 0, -1);
745 g_assert_cmpstr(text, ==, expectedText);
748 nLinks = atk_hypertext_get_n_links(ATK_HYPERTEXT(paragraph2));
749 g_assert_cmpint(nLinks, ==, 1);
751 hLink = atk_hypertext_get_link(ATK_HYPERTEXT(paragraph2), 0);
752 g_assert(ATK_HYPERLINK(hLink));
753 hLinkObject = atk_hyperlink_get_object(hLink, 0);
754 g_assert(ATK_OBJECT(hLinkObject));
755 g_assert(atk_object_get_role(hLinkObject) == ATK_ROLE_COMBO_BOX);
756 g_assert_cmpint(atk_hyperlink_get_start_index(hLink), ==, 8);
757 g_assert_cmpint(atk_hyperlink_get_end_index(hLink), ==, 9);
758 g_assert_cmpint(atk_hyperlink_get_n_anchors(hLink), ==, 1);
759 g_assert_cmpstr(atk_hyperlink_get_uri(hLink, 0), ==, 0);
761 AtkText* paragraph3 = ATK_TEXT(atk_object_ref_accessible_child(object, 2));
762 g_assert(ATK_IS_TEXT(paragraph3));
763 g_assert(ATK_IS_HYPERTEXT(paragraph3));
765 expectedText = "\357\277\274";
766 text = atk_text_get_text(paragraph3, 0, -1);
767 g_assert_cmpstr(text, ==, expectedText);
770 nLinks = atk_hypertext_get_n_links(ATK_HYPERTEXT(paragraph3));
771 g_assert_cmpint(nLinks, ==, 1);
773 hLink = atk_hypertext_get_link(ATK_HYPERTEXT(paragraph3), 0);
774 g_assert(ATK_HYPERLINK(hLink));
775 hLinkObject = atk_hyperlink_get_object(hLink, 0);
776 g_assert(ATK_OBJECT(hLinkObject));
777 g_assert(atk_object_get_role(hLinkObject) == ATK_ROLE_PUSH_BUTTON);
778 g_assert_cmpint(atk_hyperlink_get_start_index(hLink), ==, 0);
779 g_assert_cmpint(atk_hyperlink_get_end_index(hLink), ==, 1);
780 g_assert_cmpint(atk_hyperlink_get_n_anchors(hLink), ==, 1);
781 g_assert_cmpstr(atk_hyperlink_get_uri(hLink, 0), ==, 0);
783 g_object_unref(paragraph1);
784 g_object_unref(paragraph2);
785 g_object_unref(paragraph3);
786 g_object_unref(webView);
789 static void testWebkitAtkGetTextAtOffset()
791 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
792 g_object_ref_sink(webView);
793 GtkAllocation allocation = { 0, 0, 800, 600 };
794 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
795 webkit_web_view_load_string(webView, contents, 0, 0, 0);
797 /* Get to the inner AtkText object. */
798 AtkObject* object = getWebAreaObject(webView);
800 object = atk_object_ref_accessible_child(object, 0);
803 AtkText* textObject = ATK_TEXT(object);
804 g_assert(ATK_IS_TEXT(textObject));
806 runGetTextTests(textObject);
808 g_object_unref(webView);
811 static void testWebkitAtkGetTextAtOffsetNewlines()
813 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
814 g_object_ref_sink(webView);
815 GtkAllocation allocation = { 0, 0, 800, 600 };
816 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
817 webkit_web_view_load_string(webView, contentsWithNewlines, 0, 0, 0);
819 /* Get to the inner AtkText object. */
820 AtkObject* object = getWebAreaObject(webView);
822 object = atk_object_ref_accessible_child(object, 0);
825 AtkText* textObject = ATK_TEXT(object);
826 g_assert(ATK_IS_TEXT(textObject));
828 runGetTextTests(textObject);
830 g_object_unref(webView);
833 static void testWebkitAtkGetTextAtOffsetTextarea()
835 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
836 g_object_ref_sink(webView);
837 GtkAllocation allocation = { 0, 0, 800, 600 };
838 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
839 webkit_web_view_load_string(webView, contentsInTextarea, 0, 0, 0);
841 /* Get to the inner AtkText object. */
842 AtkObject* object = getWebAreaObject(webView);
844 object = atk_object_ref_accessible_child(object, 0);
846 object = atk_object_ref_accessible_child(object, 0);
849 AtkText* textObject = ATK_TEXT(object);
850 g_assert(ATK_IS_TEXT(textObject));
852 runGetTextTests(textObject);
854 g_object_unref(webView);
857 static void testWebkitAtkGetTextAtOffsetTextInput()
859 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
860 g_object_ref_sink(webView);
861 GtkAllocation allocation = { 0, 0, 800, 600 };
862 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
863 webkit_web_view_load_string(webView, contentsInTextInput, 0, 0, 0);
865 /* Get to the inner AtkText object. */
866 AtkObject* object = getWebAreaObject(webView);
868 object = atk_object_ref_accessible_child(object, 0);
870 object = atk_object_ref_accessible_child(object, 0);
873 AtkText* textObject = ATK_TEXT(object);
874 g_assert(ATK_IS_TEXT(textObject));
876 runGetTextTests(textObject);
878 g_object_unref(webView);
881 static void testWebkitAtkGetTextAtOffsetWithPreformattedText()
883 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
884 g_object_ref_sink(webView);
885 GtkAllocation allocation = { 0, 0, 800, 600 };
886 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
887 webkit_web_view_load_string(webView, contentsWithPreformattedText, 0, 0, 0);
889 AtkObject* object = getWebAreaObject(webView);
892 AtkObject* preformattedText = atk_object_ref_accessible_child(object, 0);
893 g_assert(ATK_IS_OBJECT(preformattedText));
894 g_assert(atk_object_get_role(preformattedText) == ATK_ROLE_PANEL);
895 g_assert(ATK_IS_TEXT(preformattedText));
896 char* text = atk_text_get_text(ATK_TEXT(preformattedText), 0, -1);
897 g_assert_cmpstr(text, ==, "\t\n\tfirst line\n\tsecond line\n");
900 /* Try retrieving all the lines indicating the position of the offsets at the beginning of each of them. */
901 testGetTextFunction(ATK_TEXT(preformattedText), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 0, "\t\n", 0, 2);
902 testGetTextFunction(ATK_TEXT(preformattedText), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 2, "\tfirst line\n", 2, 14);
903 testGetTextFunction(ATK_TEXT(preformattedText), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 14, "\tsecond line\n", 14, 27);
905 g_object_unref(preformattedText);
906 g_object_unref(webView);
909 static void testWebkitAtkGetTextAtOffsetWithSpecialCharacters()
911 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
912 g_object_ref_sink(webView);
913 GtkAllocation allocation = { 0, 0, 800, 600 };
914 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
915 webkit_web_view_load_string(webView, contentsWithSpecialChars, 0, 0, 0);
917 /* Get to the inner AtkText object. */
918 AtkObject* object = getWebAreaObject(webView);
921 AtkObject* paragraph = atk_object_ref_accessible_child(object, 0);
922 g_assert(ATK_IS_TEXT(paragraph));
924 gchar* expectedText = g_strdup("\302\253\302\240This is a paragraph with \342\200\234special\342\200\235 characters inside.\302\240\302\273");
925 char* text = atk_text_get_text(ATK_TEXT(paragraph), 0, -1);
926 g_assert_cmpstr(text, ==, expectedText);
929 /* Check that getting the text with ATK_TEXT_BOUNDARY_LINE_START
930 and ATK_TEXT_BOUNDARY_LINE_END does not crash because of not
931 properly handling characters inside the UTF-8 string. */
932 testGetTextFunction(ATK_TEXT(paragraph), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 0, expectedText, 0, 57);
933 testGetTextFunction(ATK_TEXT(paragraph), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END, 0, expectedText, 0, 57);
934 g_free(expectedText);
936 AtkObject* list = atk_object_ref_accessible_child(object, 1);
937 g_assert(ATK_OBJECT(list));
939 AtkText* listItem = ATK_TEXT(atk_object_ref_accessible_child(list, 0));
940 g_assert(ATK_IS_TEXT(listItem));
942 text = atk_text_get_text(ATK_TEXT(listItem), 0, -1);
943 g_assert_cmpstr(text, ==, "\342\200\242 List item with some text that wraps across different lines.");
946 /* Check that getting the text with ATK_TEXT_BOUNDARY_LINE_START
947 and ATK_TEXT_BOUNDARY_LINE_END for line items with bullets
948 (special character) and wrapped text always return the right
949 piece of text for each line. */
950 testGetTextFunction(ATK_TEXT(listItem), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 3, "\342\200\242 List item ", 0, 12);
951 testGetTextFunction(ATK_TEXT(listItem), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 13, "with some ", 12, 22);
952 testGetTextFunction(ATK_TEXT(listItem), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END, 0, "\342\200\242 List item", 0, 11);
953 testGetTextFunction(ATK_TEXT(listItem), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END, 12, " with some", 11, 21);
955 g_object_unref(listItem);
957 listItem = ATK_TEXT(atk_object_ref_accessible_child(list, 1));
958 g_assert(ATK_IS_TEXT(listItem));
960 /* Check that placing the same text in a paragraph doesn't break things. */
961 text = atk_text_get_text(ATK_TEXT(listItem), 0, -1);
962 g_assert_cmpstr(text, ==, "\342\200\242 List item with some text that wraps across different lines.");
965 testGetTextFunction(ATK_TEXT(listItem), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 3, "\342\200\242 List item ", 0, 12);
966 testGetTextFunction(ATK_TEXT(listItem), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 13, "with some ", 12, 22);
967 testGetTextFunction(ATK_TEXT(listItem), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END, 0, "\342\200\242 List item", 0, 11);
968 testGetTextFunction(ATK_TEXT(listItem), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END, 12, " with some", 11, 21);
970 g_object_unref(list);
971 g_object_unref(listItem);
972 g_object_unref(paragraph);
973 g_object_unref(webView);
976 static void testWebkitAtkGetTextAtOffsetWithWrappedLines()
978 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
979 g_object_ref_sink(webView);
980 GtkAllocation allocation = { 0, 0, 800, 600 };
981 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
982 webkit_web_view_load_string(webView, contentsWithWrappedLines, 0, 0, 0);
984 /* Enable caret browsing. */
985 WebKitWebSettings* settings = webkit_web_view_get_settings(webView);
986 g_object_set(settings, "enable-caret-browsing", TRUE, NULL);
987 webkit_web_view_set_settings(webView, settings);
989 /* Get to the inner AtkText object. */
990 AtkObject* object = getWebAreaObject(webView);
993 /* Check the paragraph with the text wrapped because of max-width. */
994 AtkText* paragraph1 = ATK_TEXT(atk_object_ref_accessible_child(object, 0));
995 g_assert(ATK_IS_TEXT(paragraph1));
997 gchar* text = atk_text_get_text(paragraph1, 0, -1);
998 g_assert_cmpstr(text, ==, "This is one line wrapped because of the maximum width of its container.");
1001 testGetTextFunction(paragraph1, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_CHAR, 16, "e", 15, 16);
1002 testGetTextFunction(paragraph1, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_CHAR, 16, " ", 16, 17);
1003 testGetTextFunction(paragraph1, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_CHAR, 16, "w", 17, 18);
1005 testGetTextFunction(paragraph1, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_START, 16, "one ", 8, 12);
1006 testGetTextFunction(paragraph1, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START, 16, "line ", 12, 17);
1007 testGetTextFunction(paragraph1, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_START, 16, "wrapped ", 17, 25);
1009 testGetTextFunction(paragraph1, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_END, 16, " line", 11, 16);
1010 testGetTextFunction(paragraph1, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END, 16, " wrapped", 16, 24);
1011 testGetTextFunction(paragraph1, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_END, 16, " because", 24, 32);
1013 testGetTextFunction(paragraph1, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_LINE_START, 17, "This is one line ", 0, 17);
1014 testGetTextFunction(paragraph1, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 17, "wrapped because ", 17, 33);
1015 testGetTextFunction(paragraph1, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_LINE_START, 17, "of the maximum ", 33, 48);
1017 /* The following line won't work at the moment because of a bug in GailTextUtil.
1018 see https://bugzilla.gnome.org/show_bug.cgi?id=703554
1019 testGetTextFunction(paragraph1, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_LINE_END, 17, "This is one line", 0, 16); */
1020 testGetTextFunction(paragraph1, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END, 17, " wrapped because", 16, 32);
1021 testGetTextFunction(paragraph1, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_LINE_END, 17, " of the maximum", 32, 47);
1023 g_object_unref(paragraph1);
1025 /* Check the paragraph with the text wrapped because of <br> elements. */
1026 AtkText* paragraph2 = ATK_TEXT(atk_object_ref_accessible_child(object, 1));
1027 g_assert(ATK_IS_TEXT(paragraph2));
1029 text = atk_text_get_text(paragraph2, 0, -1);
1030 g_assert_cmpstr(text, ==, "This is another line wrapped\nbecause of one forced\nline break in the middle.");
1033 testGetTextFunction(paragraph2, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_CHAR, 28, "d", 27, 28);
1034 testGetTextFunction(paragraph2, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_CHAR, 28, "\n", 28, 29);
1035 testGetTextFunction(paragraph2, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_CHAR, 28, "b", 29, 30);
1037 testGetTextFunction(paragraph2, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_START, 28, "line ", 16, 21);
1038 testGetTextFunction(paragraph2, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START, 28, "wrapped\n", 21, 29);
1039 testGetTextFunction(paragraph2, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_START, 28, "because ", 29, 37);
1041 testGetTextFunction(paragraph2, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_END, 28, " wrapped", 20, 28);
1042 testGetTextFunction(paragraph2, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END, 28, "\nbecause", 28, 36);
1043 testGetTextFunction(paragraph2, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_END, 28, " of", 36, 39);
1045 testGetTextFunction(paragraph2, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_LINE_START, 30, "This is another line wrapped\n", 0, 29);
1046 testGetTextFunction(paragraph2, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 30, "because of one forced\n", 29, 51);
1047 testGetTextFunction(paragraph2, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_LINE_START, 30, "line break in the middle.", 51, 76);
1049 /* The following line won't work at the moment because of a bug in GailTextUtil.
1050 see https://bugzilla.gnome.org/show_bug.cgi?id=703554
1051 testGetTextFunction(paragraph2, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_LINE_END, 30, "This is another line wrapped", 0, 28); */
1052 testGetTextFunction(paragraph2, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END, 30, "\nbecause of one forced", 28, 50);
1053 testGetTextFunction(paragraph2, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_LINE_END, 30, "\nline break in the middle.", 50, 76);
1055 g_object_unref(paragraph2);
1057 g_object_unref(webView);
1060 static void testWebkitAtkGetTextAtOffsetWithEmbeddedObjects()
1062 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
1063 g_object_ref_sink(webView);
1064 GtkAllocation allocation = { 0, 0, 800, 600 };
1065 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
1066 webkit_web_view_load_string(webView, contentsWithEmbeddedObjects, 0, 0, 0);
1068 /* Enable caret browsing. */
1069 WebKitWebSettings* settings = webkit_web_view_get_settings(webView);
1070 g_object_set(settings, "enable-caret-browsing", TRUE, NULL);
1071 webkit_web_view_set_settings(webView, settings);
1073 /* Get to the inner AtkText object. */
1074 AtkObject* object = getWebAreaObject(webView);
1077 /* Check the paragraph with the text wrapped because of max-width. */
1078 AtkText* paragraph = ATK_TEXT(atk_object_ref_accessible_child(object, 0));
1079 g_assert(ATK_IS_TEXT(paragraph));
1081 gchar* text = atk_text_get_text(paragraph, 0, -1);
1082 g_assert_cmpstr(text, ==, "This is one line containing two \357\277\274 embedded objects \357\277\274 in the middle.");
1085 /* Check right before the first embedded object */
1086 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_CHAR, 32, "\357\277\274", 32, 33);
1087 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START, 32, "two \357\277\274 ", 28, 34);
1088 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END, 32, " \357\277\274 embedded", 31, 42);
1090 /* Check right after the first embedded object (and before the first word after it) */
1091 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_CHAR, 33, " ", 33, 34);
1092 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START, 33, "two \357\277\274 ", 28, 34);
1093 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END, 33, " \357\277\274 embedded", 31, 42);
1095 /* Check at the beginning of the first word between the two embedded objects */
1096 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_CHAR, 34, "e", 34, 35);
1097 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START, 34, "embedded ", 34, 43);
1098 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END, 34, " \357\277\274 embedded", 31, 42);
1100 /* Check at the end of the first word between the two embedded objects */
1101 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_CHAR, 42, " ", 42, 43);
1102 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START, 42, "embedded ", 34, 43);
1103 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END, 42, " objects", 42, 50);
1105 /* Check right before the second embedded object */
1106 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_CHAR, 51, "\357\277\274", 51, 52);
1107 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START, 51, "objects \357\277\274 ", 43, 53);
1108 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END, 51, " \357\277\274 in", 50, 55);
1110 /* Check right after the second embedded object (and before the first word after it) */
1111 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_CHAR, 52, " ", 52, 53);
1112 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START, 52, "objects \357\277\274 ", 43, 53);
1113 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END, 52, " \357\277\274 in", 50, 55);
1115 /* Check at the beginning of the first word after the two embedded objects */
1116 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_CHAR, 53, "i", 53, 54);
1117 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START, 53, "in ", 53, 56);
1118 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END, 53, " \357\277\274 in", 50, 55);
1120 /* Check at the end of the first word after the two embedded objects */
1121 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_CHAR, 55, " ", 55, 56);
1122 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START, 55, "in ", 53, 56);
1123 testGetTextFunction(paragraph, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END, 55, " the", 55, 59);
1125 g_object_unref(paragraph);
1126 g_object_unref(webView);
1129 static void testWebkitAtkGetTextInParagraphAndBodySimple()
1131 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
1132 g_object_ref_sink(webView);
1133 GtkAllocation allocation = { 0, 0, 800, 600 };
1134 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
1135 webkit_web_view_load_string(webView, contentsInParagraphAndBodySimple, 0, 0, 0);
1137 /* Get to the inner AtkText object. */
1138 AtkObject* object = getWebAreaObject(webView);
1140 AtkObject* object1 = atk_object_ref_accessible_child(object, 0);
1142 AtkObject* object2 = atk_object_ref_accessible_child(object, 1);
1145 AtkText* textObject1 = ATK_TEXT(object1);
1146 g_assert(ATK_IS_TEXT(textObject1));
1147 AtkText* textObject2 = ATK_TEXT(object2);
1148 g_assert(ATK_IS_TEXT(textObject2));
1150 char *text = atk_text_get_text(textObject1, 0, -1);
1151 g_assert_cmpstr(text, ==, "This is a test.");
1153 text = atk_text_get_text(textObject2, 0, 12);
1154 g_assert_cmpstr(text, ==, "Hello world.");
1156 g_object_unref(object1);
1157 g_object_unref(object2);
1158 g_object_unref(webView);
1161 static void testWebkitAtkGetTextInParagraphAndBodyModerate()
1163 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
1164 g_object_ref_sink(webView);
1165 GtkAllocation allocation = { 0, 0, 800, 600 };
1166 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
1167 webkit_web_view_load_string(webView, contentsInParagraphAndBodyModerate, 0, 0, 0);
1169 /* Get to the inner AtkText object. */
1170 AtkObject* object = getWebAreaObject(webView);
1172 AtkObject* object1 = atk_object_ref_accessible_child(object, 0);
1174 AtkObject* object2 = atk_object_ref_accessible_child(object, 1);
1177 AtkText* textObject1 = ATK_TEXT(object1);
1178 g_assert(ATK_IS_TEXT(textObject1));
1179 AtkText* textObject2 = ATK_TEXT(object2);
1180 g_assert(ATK_IS_TEXT(textObject2));
1182 char *text = atk_text_get_text(textObject1, 0, -1);
1183 g_assert_cmpstr(text, ==, "This is a test.");
1185 text = atk_text_get_text(textObject2, 0, 53);
1186 g_assert_cmpstr(text, ==, "Hello world.\nThis sentence is green.\nThis one is not.");
1188 g_object_unref(object1);
1189 g_object_unref(object2);
1190 g_object_unref(webView);
1193 static void testWebkitAtkGetTextInTable()
1195 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
1196 g_object_ref_sink(webView);
1197 GtkAllocation allocation = { 0, 0, 800, 600 };
1198 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
1199 webkit_web_view_load_string(webView, contentsInTable, 0, 0, 0);
1201 AtkObject* object = getWebAreaObject(webView);
1203 object = atk_object_ref_accessible_child(object, 0);
1206 /* Tables should not implement AtkText. */
1207 g_assert(!G_TYPE_INSTANCE_GET_INTERFACE(object, ATK_TYPE_TEXT, AtkTextIface));
1209 g_object_unref(object);
1210 g_object_unref(webView);
1213 static void testWebkitAtkGetHeadersInTable()
1215 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
1216 g_object_ref_sink(webView);
1217 GtkAllocation allocation = { 0, 0, 800, 600 };
1218 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
1219 webkit_web_view_load_string(webView, contentsInTableWithHeaders, 0, 0, 0);
1221 AtkObject* axWebView = getWebAreaObject(webView);
1222 g_assert(axWebView);
1224 /* Check table with both column and row headers. */
1225 AtkObject* table = atk_object_ref_accessible_child(axWebView, 0);
1227 g_assert(atk_object_get_role(table) == ATK_ROLE_TABLE);
1229 AtkObject* colHeader = atk_table_get_column_header(ATK_TABLE(table), 0);
1230 g_assert(colHeader);
1231 g_assert(atk_object_get_role(colHeader) == ATK_ROLE_TABLE_CELL);
1232 g_assert(atk_object_get_index_in_parent(colHeader) == 0);
1234 colHeader = atk_table_get_column_header(ATK_TABLE(table), 1);
1235 g_assert(colHeader);
1236 g_assert(atk_object_get_role(colHeader) == ATK_ROLE_TABLE_CELL);
1237 g_assert(atk_object_get_index_in_parent(colHeader) == 1);
1239 colHeader = atk_table_get_column_header(ATK_TABLE(table), 2);
1240 g_assert(colHeader);
1241 g_assert(atk_object_get_role(colHeader) == ATK_ROLE_TABLE_CELL);
1242 g_assert(atk_object_get_index_in_parent(colHeader) == 2);
1244 colHeader = atk_table_get_column_header(ATK_TABLE(table), 3);
1245 g_assert(colHeader);
1246 g_assert(atk_object_get_role(colHeader) == ATK_ROLE_TABLE_CELL);
1247 g_assert(atk_object_get_index_in_parent(colHeader) == 2);
1249 AtkObject* rowHeader = atk_table_get_row_header(ATK_TABLE(table), 0);
1250 g_assert(rowHeader);
1251 g_assert(atk_object_get_role(rowHeader) == ATK_ROLE_TABLE_CELL);
1252 g_assert(atk_object_get_index_in_parent(rowHeader) == 0);
1254 rowHeader = atk_table_get_row_header(ATK_TABLE(table), 1);
1255 g_assert(rowHeader);
1256 g_assert(atk_object_get_role(rowHeader) == ATK_ROLE_TABLE_CELL);
1257 g_assert(atk_object_get_index_in_parent(rowHeader) == 3);
1259 rowHeader = atk_table_get_row_header(ATK_TABLE(table), 2);
1260 g_assert(rowHeader);
1261 g_assert(atk_object_get_role(rowHeader) == ATK_ROLE_TABLE_CELL);
1262 g_assert(atk_object_get_index_in_parent(rowHeader) == 7);
1264 rowHeader = atk_table_get_row_header(ATK_TABLE(table), 3);
1265 g_assert(rowHeader);
1266 g_assert(atk_object_get_role(rowHeader) == ATK_ROLE_TABLE_CELL);
1267 g_assert(atk_object_get_index_in_parent(rowHeader) == 7);
1269 g_object_unref(table);
1271 /* Check table with no headers at all. */
1272 table = atk_object_ref_accessible_child(axWebView, 1);
1274 g_assert(atk_object_get_role(table) == ATK_ROLE_TABLE);
1276 colHeader = atk_table_get_column_header(ATK_TABLE(table), 0);
1277 g_assert(colHeader == 0);
1279 colHeader = atk_table_get_column_header(ATK_TABLE(table), 1);
1280 g_assert(colHeader == 0);
1282 rowHeader = atk_table_get_row_header(ATK_TABLE(table), 0);
1283 g_assert(rowHeader == 0);
1285 rowHeader = atk_table_get_row_header(ATK_TABLE(table), 1);
1286 g_assert(rowHeader == 0);
1288 g_object_unref(table);
1289 g_object_unref(webView);
1292 static gint compAtkAttribute(AtkAttribute* a1, AtkAttribute* a2)
1294 gint strcmpVal = g_strcmp0(a1->name, a2->name);
1297 return g_strcmp0(a1->value, a2->value);
1300 static gint compAtkAttributeName(AtkAttribute* a1, AtkAttribute* a2)
1302 return g_strcmp0(a1->name, a2->name);
1305 static gboolean atkAttributeSetAttributeNameHasValue(AtkAttributeSet* set, const gchar* attributeName, const gchar* value)
1308 at.name = (gchar*)attributeName;
1309 GSList* element = g_slist_find_custom(set, &at, (GCompareFunc)compAtkAttributeName);
1310 return element && !g_strcmp0(((AtkAttribute*)(element->data))->value, value);
1313 static gboolean atkAttributeSetContainsAttributeName(AtkAttributeSet* set, const gchar* attributeName)
1316 at.name = (gchar*)attributeName;
1317 return g_slist_find_custom(set, &at, (GCompareFunc)compAtkAttributeName) ? true : false;
1320 static gboolean atkAttributeSetAttributeHasValue(AtkAttributeSet* set, AtkTextAttribute attribute, const gchar* value)
1322 return atkAttributeSetAttributeNameHasValue(set, atk_text_attribute_get_name(attribute), value);
1325 static gboolean atkAttributeSetAreEqual(AtkAttributeSet* set1, AtkAttributeSet* set2)
1330 set1 = g_slist_sort(set1, (GCompareFunc)compAtkAttribute);
1331 set2 = g_slist_sort(set2, (GCompareFunc)compAtkAttribute);
1334 if (!set2 || compAtkAttribute(set1->data, set2->data))
1344 static void testWebkitAtkTextAttributes()
1346 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
1347 g_object_ref_sink(webView);
1348 GtkAllocation allocation = { 0, 0, 800, 600 };
1349 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
1350 webkit_web_view_load_string(webView, textWithAttributes, 0, 0, 0);
1352 AtkObject* object = getWebAreaObject(webView);
1355 AtkObject* child = atk_object_ref_accessible_child(object, 0);
1356 g_assert(child && ATK_IS_TEXT(child));
1357 AtkText* childText = ATK_TEXT(child);
1361 AtkAttributeSet* set1 = atk_text_get_run_attributes(childText, 0, &startOffset, &endOffset);
1362 g_assert_cmpint(startOffset, ==, 0);
1363 g_assert_cmpint(endOffset, ==, 12);
1364 g_assert(atkAttributeSetAreEqual(set1, 0));
1366 AtkAttributeSet* set2 = atk_text_get_run_attributes(childText, 15, &startOffset, &endOffset);
1367 g_assert_cmpint(startOffset, ==, 12);
1368 g_assert_cmpint(endOffset, ==, 17);
1369 g_assert(atkAttributeSetAttributeHasValue(set2, ATK_TEXT_ATTR_STYLE, "italic"));
1371 AtkAttributeSet* set3 = atk_text_get_run_attributes(childText, 17, &startOffset, &endOffset);
1372 g_assert_cmpint(startOffset, ==, 17);
1373 g_assert_cmpint(endOffset, ==, 40);
1374 g_assert(atkAttributeSetAttributeHasValue(set3, ATK_TEXT_ATTR_WEIGHT, "700"));
1376 AtkAttributeSet* set4 = atk_text_get_default_attributes(childText);
1377 g_assert(atkAttributeSetAttributeHasValue(set4, ATK_TEXT_ATTR_STYLE, "normal"));
1378 g_assert(atkAttributeSetAttributeHasValue(set4, ATK_TEXT_ATTR_JUSTIFICATION, "right"));
1379 g_assert(atkAttributeSetAttributeHasValue(set4, ATK_TEXT_ATTR_SIZE, "14"));
1380 atk_attribute_set_free(set1);
1381 atk_attribute_set_free(set2);
1382 atk_attribute_set_free(set3);
1383 atk_attribute_set_free(set4);
1385 child = atk_object_ref_accessible_child(object, 1);
1386 g_assert(child && ATK_IS_TEXT(child));
1387 childText = ATK_TEXT(child);
1389 set1 = atk_text_get_default_attributes(childText);
1390 g_assert(atkAttributeSetAttributeHasValue(set1, ATK_TEXT_ATTR_FAMILY_NAME, "monospace"));
1391 g_assert(atkAttributeSetAttributeHasValue(set1, ATK_TEXT_ATTR_STYLE, "normal"));
1392 g_assert(atkAttributeSetAttributeHasValue(set1, ATK_TEXT_ATTR_STRIKETHROUGH, "false"));
1393 g_assert(atkAttributeSetAttributeHasValue(set1, ATK_TEXT_ATTR_WEIGHT, "400"));
1394 g_assert(atkAttributeSetAttributeHasValue(set1, ATK_TEXT_ATTR_FG_COLOR, "120,121,122"));
1396 set2 = atk_text_get_run_attributes(childText, 43, &startOffset, &endOffset);
1397 g_assert_cmpint(startOffset, ==, 43);
1398 g_assert_cmpint(endOffset, ==, 80);
1399 /* Checks that default attributes of text are not returned when called to atk_text_get_run_attributes. */
1400 g_assert(!atkAttributeSetAttributeHasValue(set2, ATK_TEXT_ATTR_FG_COLOR, "120,121,122"));
1401 g_assert(atkAttributeSetAttributeHasValue(set2, ATK_TEXT_ATTR_UNDERLINE, "single"));
1402 g_assert(atkAttributeSetAttributeHasValue(set2, ATK_TEXT_ATTR_BG_COLOR, "80,81,82"));
1403 atk_attribute_set_free(set1);
1404 atk_attribute_set_free(set2);
1406 child = atk_object_ref_accessible_child(object, 2);
1407 g_assert(child && ATK_IS_TEXT(child));
1408 childText = ATK_TEXT(child);
1410 set1 = atk_text_get_run_attributes(childText, 0, &startOffset, &endOffset);
1411 set2 = atk_text_get_run_attributes(childText, 3, &startOffset, &endOffset);
1412 g_assert(atkAttributeSetAreEqual(set1, set2));
1413 atk_attribute_set_free(set2);
1415 set2 = atk_text_get_run_attributes(childText, 1, &startOffset, &endOffset);
1416 set3 = atk_text_get_run_attributes(childText, 5, &startOffset, &endOffset);
1417 g_assert(atkAttributeSetAreEqual(set2, set3));
1418 g_assert(!atkAttributeSetAreEqual(set1, set2));
1419 atk_attribute_set_free(set3);
1421 set3 = atk_text_get_run_attributes(childText, 2, &startOffset, &endOffset);
1422 set4 = atk_text_get_run_attributes(childText, 6, &startOffset, &endOffset);
1423 g_assert(atkAttributeSetAreEqual(set3, set4));
1424 g_assert(!atkAttributeSetAreEqual(set1, set3));
1425 g_assert(!atkAttributeSetAreEqual(set2, set3));
1426 atk_attribute_set_free(set1);
1427 atk_attribute_set_free(set2);
1428 atk_attribute_set_free(set3);
1429 atk_attribute_set_free(set4);
1431 child = atk_object_ref_accessible_child(object, 3);
1432 g_assert(child && ATK_IS_TEXT(child));
1433 childText = ATK_TEXT(child);
1434 set1 = atk_text_get_run_attributes(childText, 24, &startOffset, &endOffset);
1435 g_assert_cmpint(startOffset, ==, 21);
1436 g_assert_cmpint(endOffset, ==, 25);
1437 g_assert(atkAttributeSetAttributeHasValue(set1, ATK_TEXT_ATTR_STRIKETHROUGH, "true"));
1439 set2 = atk_text_get_run_attributes(childText, 25, &startOffset, &endOffset);
1440 g_assert_cmpint(startOffset, ==, 25);
1441 g_assert_cmpint(endOffset, ==, 30);
1442 g_assert(atkAttributeSetAreEqual(set2, 0));
1444 set3 = atk_text_get_default_attributes(childText);
1445 g_assert(atkAttributeSetAttributeHasValue(set3, ATK_TEXT_ATTR_JUSTIFICATION, "center"));
1446 atk_attribute_set_free(set1);
1447 atk_attribute_set_free(set2);
1448 atk_attribute_set_free(set3);
1451 static gchar* textSelectionChangedResult = 0;
1453 static void textSelectionChangedCallback(AtkText* text, gpointer data)
1455 g_assert(ATK_IS_TEXT(text));
1457 g_free(textSelectionChangedResult);
1458 AtkRole role = atk_object_get_role(ATK_OBJECT(text));
1459 int startOffset = 0;
1461 atk_text_get_selection(ATK_TEXT(text), 0, &startOffset, &endOffset);
1462 textSelectionChangedResult = g_strdup_printf("|%s|%d|%d|", atk_role_get_name(role), startOffset, endOffset);
1465 static void testWebkitAtkTextSelections()
1467 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
1468 g_object_ref_sink(webView);
1469 GtkAllocation allocation = { 0, 0, 800, 600 };
1470 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
1471 webkit_web_view_load_string(webView, textForSelections, 0, 0, 0);
1473 AtkObject* object = getWebAreaObject(webView);
1476 AtkText* paragraph1 = ATK_TEXT(atk_object_ref_accessible_child(object, 0));
1477 g_assert(ATK_IS_TEXT(paragraph1));
1478 g_signal_connect(paragraph1, "text-selection-changed", G_CALLBACK(textSelectionChangedCallback), 0);
1480 AtkText* paragraph2 = ATK_TEXT(atk_object_ref_accessible_child(object, 1));
1481 g_assert(ATK_IS_TEXT(paragraph2));
1482 g_signal_connect(paragraph2, "text-selection-changed", G_CALLBACK(textSelectionChangedCallback), 0);
1484 AtkText* link = ATK_TEXT(atk_object_ref_accessible_child(ATK_OBJECT(paragraph2), 0));
1485 g_assert(ATK_IS_TEXT(link));
1487 AtkObject* list = atk_object_ref_accessible_child(object, 2);
1488 g_assert(ATK_OBJECT(list));
1490 AtkText* listItem = ATK_TEXT(atk_object_ref_accessible_child(list, 0));
1491 g_assert(ATK_IS_TEXT(listItem));
1493 /* First paragraph (simple text). */
1495 /* Basic initial checks. */
1496 g_assert_cmpint(atk_text_get_n_selections(paragraph1), ==, 0);
1500 gchar* selectedText = atk_text_get_selection(paragraph1, 0, &startOffset, &endOffset);
1501 g_assert_cmpint(startOffset, ==, 0);
1502 g_assert_cmpint(endOffset, ==, 0);
1503 g_assert_cmpstr(selectedText, ==, 0);
1504 g_free (selectedText);
1506 /* Try removing a non existing (yet) selection. */
1507 gboolean result = atk_text_remove_selection(paragraph1, 0);
1510 /* Try setting a 0-char selection. */
1511 result = atk_text_set_selection(paragraph1, 0, 5, 5);
1514 /* Make a selection and test it. */
1515 result = atk_text_set_selection(paragraph1, 0, 5, 25);
1517 g_assert_cmpint(atk_text_get_n_selections(paragraph1), ==, 1);
1518 g_assert_cmpstr(textSelectionChangedResult, ==, "|paragraph|5|25|");
1519 selectedText = atk_text_get_selection(paragraph1, 0, &startOffset, &endOffset);
1520 g_assert_cmpint(startOffset, ==, 5);
1521 g_assert_cmpint(endOffset, ==, 25);
1522 g_assert_cmpstr(selectedText, ==, "agraph with plain te");
1523 g_free (selectedText);
1525 /* Try removing the selection from other AtkText object (should fail). */
1526 result = atk_text_remove_selection(paragraph2, 0);
1529 /* Remove the selection and test everything again. */
1530 result = atk_text_remove_selection(paragraph1, 0);
1532 g_assert_cmpint(atk_text_get_n_selections(paragraph1), ==, 0);
1533 selectedText = atk_text_get_selection(paragraph1, 0, &startOffset, &endOffset);
1534 /* Now offsets should be the same, set to the last position of the caret. */
1535 g_assert_cmpint(startOffset, ==, endOffset);
1536 g_assert_cmpint(startOffset, ==, 25);
1537 g_assert_cmpint(endOffset, ==, 25);
1538 g_assert_cmpstr(selectedText, ==, 0);
1539 g_free (selectedText);
1541 /* Second paragraph (text + link + text). */
1543 /* Set a selection partially covering the link and test it. */
1544 result = atk_text_set_selection(paragraph2, 0, 7, 21);
1547 /* Test the paragraph first. */
1548 g_assert_cmpint(atk_text_get_n_selections(paragraph2), ==, 1);
1549 selectedText = atk_text_get_selection(paragraph2, 0, &startOffset, &endOffset);
1550 g_assert_cmpint(startOffset, ==, 7);
1551 g_assert_cmpint(endOffset, ==, 21);
1552 g_assert_cmpstr(selectedText, ==, "raph with a li");
1553 g_free (selectedText);
1555 /* Now test just the link. */
1556 g_assert_cmpint(atk_text_get_n_selections(link), ==, 1);
1557 selectedText = atk_text_get_selection(link, 0, &startOffset, &endOffset);
1558 g_assert_cmpint(startOffset, ==, 0);
1559 g_assert_cmpint(endOffset, ==, 4);
1560 g_assert_cmpstr(selectedText, ==, "a li");
1561 g_free (selectedText);
1563 /* Make a selection after the link and check selection for the whole paragraph. */
1564 result = atk_text_set_selection(paragraph2, 0, 27, 37);
1566 g_assert_cmpint(atk_text_get_n_selections(paragraph2), ==, 1);
1567 g_assert_cmpstr(textSelectionChangedResult, ==, "|paragraph|27|37|");
1568 selectedText = atk_text_get_selection(paragraph2, 0, &startOffset, &endOffset);
1569 g_assert_cmpint(startOffset, ==, 27);
1570 g_assert_cmpint(endOffset, ==, 37);
1571 g_assert_cmpstr(selectedText, ==, "the middle");
1572 g_free (selectedText);
1574 /* Remove selections and text everything again. */
1575 result = atk_text_remove_selection(paragraph2, 0);
1577 g_assert_cmpint(atk_text_get_n_selections(paragraph2), ==, 0);
1578 selectedText = atk_text_get_selection(paragraph2, 0, &startOffset, &endOffset);
1579 /* Now offsets should be the same (no selection). */
1580 g_assert_cmpint(startOffset, ==, endOffset);
1581 g_assert_cmpstr(selectedText, ==, 0);
1582 g_free (selectedText);
1584 g_assert_cmpint(atk_text_get_n_selections(link), ==, 0);
1585 selectedText = atk_text_get_selection(link, 0, &startOffset, &endOffset);
1586 /* Now offsets should be the same (no selection). */
1587 g_assert_cmpint(startOffset, ==, endOffset);
1588 g_assert_cmpstr(selectedText, ==, 0);
1589 g_free (selectedText);
1593 g_assert(atk_object_get_role(list) == ATK_ROLE_LIST);
1594 g_assert_cmpint(atk_object_get_n_accessible_children(list), ==, 1);
1596 gchar* text = atk_text_get_text(listItem, 0, -1);
1597 g_assert_cmpstr(text, ==, "1. A list item");
1600 /* It's not possible to select text inside an item's marker. */
1601 result = atk_text_set_selection (listItem, 0, 0, 9);
1603 result = atk_text_set_selection (listItem, 0, 9, 1);
1606 /* It should be possible to select text inside an item's text. */
1607 result = atk_text_set_selection (listItem, 0, 3, 9);
1610 g_assert_cmpint(atk_text_get_n_selections(listItem), ==, 1);
1611 selectedText = atk_text_get_selection(listItem, 0, &startOffset, &endOffset);
1612 g_assert_cmpint(startOffset, ==, 3);
1613 g_assert_cmpint(endOffset, ==, 9);
1614 g_assert_cmpstr(selectedText, ==, "A list");
1615 g_free (selectedText);
1617 g_free(textSelectionChangedResult);
1619 g_object_unref(paragraph1);
1620 g_object_unref(paragraph2);
1621 g_object_unref(list);
1622 g_object_unref(listItem);
1623 g_object_unref(webView);
1626 static void testWebkitAtkGetExtents()
1628 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
1629 g_object_ref_sink(webView);
1630 GtkAllocation allocation = { 0, 0, 800, 600 };
1631 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
1632 webkit_web_view_load_string(webView, centeredContents, 0, 0, 0);
1634 AtkObject* object = getWebAreaObject(webView);
1637 AtkText* shortText1 = ATK_TEXT(atk_object_ref_accessible_child(object, 0));
1638 g_assert(ATK_IS_TEXT(shortText1));
1639 AtkText* longText = ATK_TEXT(atk_object_ref_accessible_child(object, 1));
1640 g_assert(ATK_IS_TEXT(longText));
1641 AtkText* shortText2 = ATK_TEXT(atk_object_ref_accessible_child(object, 2));
1642 g_assert(ATK_IS_TEXT(shortText2));
1643 AtkText* multilineText = ATK_TEXT(atk_object_ref_accessible_child(object, 3));
1644 g_assert(ATK_IS_TEXT(multilineText));
1646 /* Start with window extents. */
1647 AtkTextRectangle sline_window1, sline_window2, lline_window, mline_window;
1648 atk_text_get_range_extents(shortText1, 0, 10, ATK_XY_WINDOW, &sline_window1);
1649 atk_text_get_range_extents(longText, 0, 44, ATK_XY_WINDOW, &lline_window);
1650 atk_text_get_range_extents(shortText2, 0, 10, ATK_XY_WINDOW, &sline_window2);
1651 atk_text_get_range_extents(multilineText, 0, 60, ATK_XY_WINDOW, &mline_window);
1653 /* Check vertical line position. */
1654 g_assert_cmpint(sline_window1.y + sline_window1.height, <=, lline_window.y);
1655 g_assert_cmpint(lline_window.y + lline_window.height + sline_window2.height, <=, mline_window.y);
1657 /* Paragraphs 1 and 3 have identical text and alignment. */
1658 g_assert_cmpint(sline_window1.x, ==, sline_window2.x);
1659 g_assert_cmpint(sline_window1.width, ==, sline_window2.width);
1660 g_assert_cmpint(sline_window1.height, ==, sline_window2.height);
1662 /* All lines should be the same height; line 2 is the widest line. */
1663 g_assert_cmpint(sline_window1.height, ==, lline_window.height);
1664 g_assert_cmpint(sline_window1.width, <, lline_window.width);
1666 /* Make sure the character extents jive with the range extents. */
1672 /* First paragraph (short text). */
1673 atk_text_get_character_extents(shortText1, 0, &x, &y, &width, &height, ATK_XY_WINDOW);
1674 g_assert_cmpint(x, ==, sline_window1.x);
1675 g_assert_cmpint(y, ==, sline_window1.y);
1676 g_assert_cmpint(height, ==, sline_window1.height);
1678 atk_text_get_character_extents(shortText1, 9, &x, &y, &width, &height, ATK_XY_WINDOW);
1679 g_assert_cmpint(x, ==, sline_window1.x + sline_window1.width - width);
1680 g_assert_cmpint(y, ==, sline_window1.y);
1681 g_assert_cmpint(height, ==, sline_window1.height);
1683 /* Second paragraph (long text). */
1684 atk_text_get_character_extents(longText, 0, &x, &y, &width, &height, ATK_XY_WINDOW);
1685 g_assert_cmpint(x, ==, lline_window.x);
1686 g_assert_cmpint(y, ==, lline_window.y);
1687 g_assert_cmpint(height, ==, lline_window.height);
1689 atk_text_get_character_extents(longText, 43, &x, &y, &width, &height, ATK_XY_WINDOW);
1690 g_assert_cmpint(x, ==, lline_window.x + lline_window.width - width);
1691 g_assert_cmpint(y, ==, lline_window.y);
1692 g_assert_cmpint(height, ==, lline_window.height);
1694 /* Third paragraph (short text). */
1695 atk_text_get_character_extents(shortText2, 0, &x, &y, &width, &height, ATK_XY_WINDOW);
1696 g_assert_cmpint(x, ==, sline_window2.x);
1697 g_assert_cmpint(y, ==, sline_window2.y);
1698 g_assert_cmpint(height, ==, sline_window2.height);
1700 atk_text_get_character_extents(shortText2, 9, &x, &y, &width, &height, ATK_XY_WINDOW);
1701 g_assert_cmpint(x, ==, sline_window2.x + sline_window2.width - width);
1702 g_assert_cmpint(y, ==, sline_window2.y);
1703 g_assert_cmpint(height, ==, sline_window2.height);
1705 /* Four paragraph (3 lines multi-line text). */
1706 atk_text_get_character_extents(multilineText, 0, &x, &y, &width, &height, ATK_XY_WINDOW);
1707 g_assert_cmpint(x, ==, mline_window.x);
1708 g_assert_cmpint(y, ==, mline_window.y);
1709 g_assert_cmpint(3 * height, ==, mline_window.height);
1711 atk_text_get_character_extents(multilineText, 59, &x, &y, &width, &height, ATK_XY_WINDOW);
1712 /* Last line won't fill the whole width of the rectangle. */
1713 g_assert_cmpint(x, <=, mline_window.x + mline_window.width - width);
1714 g_assert_cmpint(y, ==, mline_window.y + mline_window.height - height);
1715 g_assert_cmpint(height, <=, mline_window.height);
1717 /* Check that extent for a full line are the same height than for
1718 a partial section of the same line */
1721 gchar* text = atk_text_get_text_at_offset(multilineText, 0, ATK_TEXT_BOUNDARY_LINE_START, &startOffset, &endOffset);
1724 AtkTextRectangle fline_window;
1725 AtkTextRectangle afline_window;
1726 atk_text_get_range_extents(multilineText, startOffset, endOffset, ATK_XY_WINDOW, &fline_window);
1727 atk_text_get_range_extents(multilineText, startOffset, endOffset - 1, ATK_XY_WINDOW, &afline_window);
1728 g_assert_cmpint(fline_window.x, ==, afline_window.x);
1729 g_assert_cmpint(fline_window.y, ==, afline_window.y);
1730 g_assert_cmpint(fline_window.height, ==, afline_window.height);
1732 g_object_unref(shortText1);
1733 g_object_unref(shortText2);
1734 g_object_unref(longText);
1735 g_object_unref(multilineText);
1736 g_object_unref(webView);
1739 static void testWebkitAtkLayoutAndDataTables()
1741 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
1742 g_object_ref_sink(webView);
1743 GtkAllocation allocation = { 0, 0, 800, 600 };
1744 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
1745 webkit_web_view_load_string(webView, layoutAndDataTables, 0, 0, 0);
1747 AtkObject* object = getWebAreaObject(webView);
1750 /* Check the non-layout table (data table). */
1752 AtkObject* table1 = atk_object_ref_accessible_child(object, 0);
1753 g_assert(ATK_IS_TABLE(table1));
1754 AtkAttributeSet* set1 = atk_object_get_attributes(table1);
1756 g_assert(!atkAttributeSetContainsAttributeName(set1, "layout-guess"));
1757 atk_attribute_set_free(set1);
1759 /* Check the layout table. */
1761 AtkObject* table2 = atk_object_ref_accessible_child(object, 1);
1762 g_assert(ATK_IS_TABLE(table2));
1763 AtkAttributeSet* set2 = atk_object_get_attributes(table2);
1765 g_assert(atkAttributeSetContainsAttributeName(set2, "layout-guess"));
1766 g_assert(atkAttributeSetAttributeNameHasValue(set2, "layout-guess", "true"));
1767 atk_attribute_set_free(set2);
1769 g_object_unref(table1);
1770 g_object_unref(table2);
1771 g_object_unref(webView);
1774 static void testWebkitAtkLinksWithInlineImages()
1776 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
1777 g_object_ref_sink(webView);
1778 GtkAllocation allocation = { 0, 0, 800, 600 };
1779 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
1780 webkit_web_view_load_string(webView, linksWithInlineImages, 0, 0, 0);
1782 AtkObject* object = getWebAreaObject(webView);
1785 /* First paragraph (link at the beginning). */
1786 AtkObject* paragraph = atk_object_ref_accessible_child(object, 0);
1787 g_assert(ATK_IS_TEXT(paragraph));
1790 gchar* text = atk_text_get_text_at_offset(ATK_TEXT(paragraph), 0, ATK_TEXT_BOUNDARY_LINE_START, &startOffset, &endOffset);
1792 g_assert_cmpstr(text, ==, "foo bar baz");
1793 g_assert_cmpint(startOffset, ==, 0);
1794 g_assert_cmpint(endOffset, ==, 11);
1796 g_object_unref(paragraph);
1798 /* Second paragraph (link in the middle). */
1799 paragraph = atk_object_ref_accessible_child(object, 1);
1800 g_assert(ATK_IS_TEXT(paragraph));
1801 text = atk_text_get_text_at_offset(ATK_TEXT(paragraph), 0, ATK_TEXT_BOUNDARY_LINE_START, &startOffset, &endOffset);
1803 g_assert_cmpstr(text, ==, "foo bar baz");
1804 g_assert_cmpint(startOffset, ==, 0);
1805 g_assert_cmpint(endOffset, ==, 11);
1807 g_object_unref(paragraph);
1809 /* Third paragraph (link at the end). */
1810 paragraph = atk_object_ref_accessible_child(object, 2);
1811 g_assert(ATK_IS_TEXT(paragraph));
1812 text = atk_text_get_text_at_offset(ATK_TEXT(paragraph), 0, ATK_TEXT_BOUNDARY_LINE_START, &startOffset, &endOffset);
1814 g_assert_cmpstr(text, ==, "foo bar baz");
1815 g_assert_cmpint(startOffset, ==, 0);
1816 g_assert_cmpint(endOffset, ==, 11);
1818 g_object_unref(paragraph);
1820 g_object_unref(webView);
1823 static void testWebkitAtkHypertextAndHyperlinks()
1825 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
1826 g_object_ref_sink(webView);
1827 GtkAllocation allocation = { 0, 0, 800, 600 };
1828 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
1829 webkit_web_view_load_string(webView, hypertextAndHyperlinks, 0, 0, 0);
1831 AtkObject* object = getWebAreaObject(webView);
1834 AtkObject* paragraph1 = atk_object_ref_accessible_child(object, 0);
1835 g_assert(ATK_OBJECT(paragraph1));
1836 g_assert(atk_object_get_role(paragraph1) == ATK_ROLE_PARAGRAPH);
1837 g_assert(ATK_IS_HYPERTEXT(paragraph1));
1839 /* No links in the first paragraph. */
1840 gint nLinks = atk_hypertext_get_n_links(ATK_HYPERTEXT(paragraph1));
1841 g_assert_cmpint(nLinks, ==, 0);
1843 AtkObject* paragraph2 = atk_object_ref_accessible_child(object, 1);
1844 g_assert(ATK_OBJECT(paragraph2));
1845 g_assert(atk_object_get_role(paragraph2) == ATK_ROLE_PARAGRAPH);
1846 g_assert(ATK_IS_HYPERTEXT(paragraph2));
1848 /* Check links in the second paragraph.
1849 nLinks = atk_hypertext_get_n_links(ATK_HYPERTEXT(paragraph2));
1850 g_assert_cmpint(nLinks, ==, 3); */
1852 AtkHyperlink* hLink1 = atk_hypertext_get_link(ATK_HYPERTEXT(paragraph2), 0);
1853 g_assert(ATK_HYPERLINK(hLink1));
1854 AtkObject* hLinkObject1 = atk_hyperlink_get_object(hLink1, 0);
1855 g_assert(ATK_OBJECT(hLinkObject1));
1856 g_assert(atk_object_get_role(hLinkObject1) == ATK_ROLE_LINK);
1857 g_assert_cmpint(atk_hyperlink_get_start_index(hLink1), ==, 0);
1858 g_assert_cmpint(atk_hyperlink_get_end_index(hLink1), ==, 6);
1859 g_assert_cmpint(atk_hyperlink_get_n_anchors(hLink1), ==, 1);
1860 g_assert_cmpstr(atk_hyperlink_get_uri(hLink1, 0), ==, "http://foo.bar.baz/");
1862 AtkHyperlink* hLink2 = atk_hypertext_get_link(ATK_HYPERTEXT(paragraph2), 1);
1863 g_assert(ATK_HYPERLINK(hLink2));
1864 AtkObject* hLinkObject2 = atk_hyperlink_get_object(hLink2, 0);
1865 g_assert(ATK_OBJECT(hLinkObject2));
1866 g_assert(atk_object_get_role(hLinkObject2) == ATK_ROLE_LINK);
1867 g_assert_cmpint(atk_hyperlink_get_start_index(hLink2), ==, 12);
1868 g_assert_cmpint(atk_hyperlink_get_end_index(hLink2), ==, 32);
1869 g_assert_cmpint(atk_hyperlink_get_n_anchors(hLink2), ==, 1);
1870 g_assert_cmpstr(atk_hyperlink_get_uri(hLink2, 0), ==, "http://bar.baz.foo/");
1872 AtkHyperlink* hLink3 = atk_hypertext_get_link(ATK_HYPERTEXT(paragraph2), 2);
1873 g_assert(ATK_HYPERLINK(hLink3));
1874 AtkObject* hLinkObject3 = atk_hyperlink_get_object(hLink3, 0);
1875 g_assert(ATK_OBJECT(hLinkObject3));
1876 g_assert(atk_object_get_role(hLinkObject3) == ATK_ROLE_LINK);
1877 g_assert_cmpint(atk_hyperlink_get_start_index(hLink3), ==, 65);
1878 g_assert_cmpint(atk_hyperlink_get_end_index(hLink3), ==, 75);
1879 g_assert_cmpint(atk_hyperlink_get_n_anchors(hLink3), ==, 1);
1880 g_assert_cmpstr(atk_hyperlink_get_uri(hLink3, 0), ==, "http://baz.foo.bar/");
1882 AtkObject* list = atk_object_ref_accessible_child(object, 2);
1883 g_assert(ATK_OBJECT(list));
1884 g_assert(atk_object_get_role(list) == ATK_ROLE_LIST);
1885 g_assert_cmpint(atk_object_get_n_accessible_children(list), ==, 1);
1887 AtkObject* listItem = atk_object_ref_accessible_child(list, 0);
1888 g_assert(ATK_IS_TEXT(listItem));
1889 g_assert(ATK_IS_HYPERTEXT(listItem));
1891 AtkHyperlink* hLinkInListItem = atk_hypertext_get_link(ATK_HYPERTEXT(listItem), 0);
1892 g_assert(ATK_HYPERLINK(hLinkInListItem));
1893 AtkObject* hLinkObject = atk_hyperlink_get_object(hLinkInListItem, 0);
1894 g_assert(ATK_OBJECT(hLinkObject));
1895 g_assert(atk_object_get_role(hLinkObject) == ATK_ROLE_LINK);
1896 g_assert_cmpint(atk_hyperlink_get_start_index(hLinkInListItem), ==, 20);
1897 g_assert_cmpint(atk_hyperlink_get_end_index(hLinkInListItem), ==, 43);
1898 g_assert_cmpint(atk_hyperlink_get_n_anchors(hLinkInListItem), ==, 1);
1899 g_assert_cmpstr(atk_hyperlink_get_uri(hLinkInListItem, 0), ==, "http://foo.bar.baz/");
1901 /* Finally check the AtkAction interface for a given AtkHyperlink. */
1902 g_assert(ATK_IS_ACTION(hLink1));
1903 g_assert_cmpint(atk_action_get_n_actions(ATK_ACTION(hLink1)), ==, 1);
1904 g_assert_cmpstr(atk_action_get_keybinding(ATK_ACTION(hLink1), 0), ==, "");
1905 g_assert_cmpstr(atk_action_get_name(ATK_ACTION(hLink1), 0), ==, "jump");
1906 g_assert(atk_action_do_action(ATK_ACTION(hLink1), 0));
1908 g_object_unref(paragraph1);
1909 g_object_unref(paragraph2);
1910 g_object_unref(list);
1911 g_object_unref(listItem);
1912 g_object_unref(webView);
1915 static void testWebkitAtkListsOfItems()
1917 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
1918 g_object_ref_sink(webView);
1919 GtkAllocation allocation = { 0, 0, 800, 600 };
1920 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
1921 webkit_web_view_load_string(webView, listsOfItems, 0, 0, 0);
1923 AtkObject* object = getWebAreaObject(webView);
1926 /* Unordered list. */
1928 AtkObject* uList = atk_object_ref_accessible_child(object, 0);
1929 g_assert(ATK_OBJECT(uList));
1930 g_assert(atk_object_get_role(uList) == ATK_ROLE_LIST);
1931 g_assert_cmpint(atk_object_get_n_accessible_children(uList), ==, 3);
1933 AtkObject* item1 = atk_object_ref_accessible_child(uList, 0);
1934 g_assert(ATK_IS_TEXT(item1));
1935 AtkObject* item2 = atk_object_ref_accessible_child(uList, 1);
1936 g_assert(ATK_IS_TEXT(item2));
1937 AtkObject* item3 = atk_object_ref_accessible_child(uList, 2);
1938 g_assert(ATK_IS_TEXT(item3));
1940 g_assert_cmpint(atk_object_get_n_accessible_children(item1), ==, 0);
1941 g_assert_cmpint(atk_object_get_n_accessible_children(item2), ==, 1);
1942 g_assert_cmpint(atk_object_get_n_accessible_children(item3), ==, 1);
1944 g_assert_cmpstr(atk_text_get_text(ATK_TEXT(item1), 0, -1), ==, "\342\200\242 text only");
1945 g_assert_cmpstr(atk_text_get_text(ATK_TEXT(item2), 0, -1), ==, "\342\200\242 link only");
1946 g_assert_cmpstr(atk_text_get_text(ATK_TEXT(item3), 0, -1), ==, "\342\200\242 text and a link");
1948 g_object_unref(item1);
1949 g_object_unref(item2);
1950 g_object_unref(item3);
1954 AtkObject* oList = atk_object_ref_accessible_child(object, 1);
1955 g_assert(ATK_OBJECT(oList));
1956 g_assert(atk_object_get_role(oList) == ATK_ROLE_LIST);
1957 g_assert_cmpint(atk_object_get_n_accessible_children(oList), ==, 3);
1959 item1 = atk_object_ref_accessible_child(oList, 0);
1960 g_assert(ATK_IS_TEXT(item1));
1961 item2 = atk_object_ref_accessible_child(oList, 1);
1962 g_assert(ATK_IS_TEXT(item2));
1963 item3 = atk_object_ref_accessible_child(oList, 2);
1964 g_assert(ATK_IS_TEXT(item3));
1966 g_assert_cmpstr(atk_text_get_text(ATK_TEXT(item1), 0, -1), ==, "1. text only");
1967 g_assert_cmpstr(atk_text_get_text(ATK_TEXT(item2), 0, -1), ==, "2. link only");
1968 g_assert_cmpstr(atk_text_get_text(ATK_TEXT(item3), 0, -1), ==, "3. text and a link");
1970 g_assert_cmpint(atk_object_get_n_accessible_children(item1), ==, 0);
1971 g_assert_cmpint(atk_object_get_n_accessible_children(item2), ==, 1);
1972 g_assert_cmpint(atk_object_get_n_accessible_children(item3), ==, 1);
1974 g_object_unref(item1);
1975 g_object_unref(item2);
1976 g_object_unref(item3);
1978 g_object_unref(uList);
1979 g_object_unref(oList);
1980 g_object_unref(webView);
1984 TEXT_CHANGE_INSERT = 1,
1985 TEXT_CHANGE_REMOVE = 2
1988 static gchar* textChangedResult = 0;
1990 static void textChangedCb(AtkText* text, gint pos, gint len, gchar* modifiedText, gpointer data)
1992 g_assert(text && ATK_IS_OBJECT(text));
1994 TextChangeType type = GPOINTER_TO_INT(data);
1995 g_free(textChangedResult);
1996 textChangedResult = g_strdup_printf("|%d|%d|%d|'%s'|", type, pos, len, modifiedText);
1999 static void testWebkitAtkTextChangedNotifications()
2001 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
2002 g_object_ref_sink(webView);
2003 GtkAllocation allocation = { 0, 0, 800, 600 };
2004 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
2005 webkit_web_view_load_string(webView, formWithTextInputs, 0, 0, 0);
2007 AtkObject* object = getWebAreaObject(webView);
2010 AtkObject* form = atk_object_ref_accessible_child(object, 0);
2011 g_assert(ATK_IS_OBJECT(form));
2013 /* First check normal text entries. */
2014 AtkObject* textEntry = atk_object_ref_accessible_child(form, 0);
2015 g_assert(ATK_IS_EDITABLE_TEXT(textEntry));
2016 g_assert(atk_object_get_role(ATK_OBJECT(textEntry)) == ATK_ROLE_ENTRY);
2018 g_signal_connect(textEntry, "text-insert",
2019 G_CALLBACK(textChangedCb),
2020 GINT_TO_POINTER(TEXT_CHANGE_INSERT));
2021 g_signal_connect(textEntry, "text-remove",
2022 G_CALLBACK(textChangedCb),
2023 GINT_TO_POINTER(TEXT_CHANGE_REMOVE));
2026 atk_editable_text_insert_text(ATK_EDITABLE_TEXT(textEntry), "foo bar baz", 11, &pos);
2027 char* text = atk_text_get_text(ATK_TEXT(textEntry), 0, -1);
2028 g_assert_cmpstr(text, ==, "foo bar baz");
2029 g_assert_cmpstr(textChangedResult, ==, "|1|0|11|'foo bar baz'|");
2032 atk_editable_text_delete_text(ATK_EDITABLE_TEXT(textEntry), 4, 7);
2033 text = atk_text_get_text(ATK_TEXT(textEntry), 0, -1);
2034 g_assert_cmpstr(text, ==, "foo baz");
2035 g_assert_cmpstr(textChangedResult, ==, "|2|4|3|'bar'|");
2039 atk_editable_text_insert_text(ATK_EDITABLE_TEXT(textEntry), "qux quux tobeignored", 8, &pos);
2040 text = atk_text_get_text(ATK_TEXT(textEntry), 0, -1);
2041 g_assert_cmpstr(text, ==, "foo qux quux baz");
2042 g_assert_cmpstr(textChangedResult, ==, "|1|4|8|'qux quux'|");
2045 /* Now check for password entries. */
2046 AtkObject* passwordEntry = atk_object_ref_accessible_child(form, 1);
2047 g_assert(ATK_IS_EDITABLE_TEXT(passwordEntry));
2048 g_assert(atk_object_get_role(ATK_OBJECT(passwordEntry)) == ATK_ROLE_PASSWORD_TEXT);
2050 g_signal_connect(passwordEntry, "text-insert",
2051 G_CALLBACK(textChangedCb),
2052 GINT_TO_POINTER(TEXT_CHANGE_INSERT));
2053 g_signal_connect(passwordEntry, "text-remove",
2054 G_CALLBACK(textChangedCb),
2055 GINT_TO_POINTER(TEXT_CHANGE_REMOVE));
2058 /* A single bullet character is '\342\200\242' */
2059 atk_editable_text_insert_text(ATK_EDITABLE_TEXT(passwordEntry), "foobar", 6, &pos);
2060 g_assert_cmpstr(textChangedResult, ==, "|1|0|6|'\342\200\242\342\200\242\342\200\242\342\200\242\342\200\242\342\200\242'|");
2061 text = atk_text_get_text(ATK_TEXT(passwordEntry), 0, -1);
2062 g_assert_cmpstr(text, ==, "\342\200\242\342\200\242\342\200\242\342\200\242\342\200\242\342\200\242");
2065 atk_editable_text_delete_text(ATK_EDITABLE_TEXT(passwordEntry), 2, 4);
2066 g_assert_cmpstr(textChangedResult, ==, "|2|2|2|'\342\200\242\342\200\242'|");
2068 text = atk_text_get_text(ATK_TEXT(passwordEntry), 0, -1);
2069 g_assert_cmpstr(text, ==, "\342\200\242\342\200\242\342\200\242\342\200\242");
2073 atk_editable_text_insert_text(ATK_EDITABLE_TEXT(passwordEntry), "qux tobeignored", 3, &pos);
2074 g_assert_cmpstr(textChangedResult, ==, "|1|3|3|'\342\200\242\342\200\242\342\200\242'|");
2076 text = atk_text_get_text(ATK_TEXT(passwordEntry), 0, -1);
2077 g_assert_cmpstr(text, ==, "\342\200\242\342\200\242\342\200\242\342\200\242\342\200\242\342\200\242\342\200\242");
2080 g_free(textChangedResult);
2082 g_object_unref(form);
2083 g_object_unref(textEntry);
2084 g_object_unref(passwordEntry);
2085 g_object_unref(webView);
2088 static void testWebkitAtkParentForRootObject()
2090 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
2091 GtkAllocation allocation = { 0, 0, 800, 600 };
2092 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
2093 webkit_web_view_load_string(webView, contents, 0, 0, 0);
2095 /* We need a parent container widget for the webview. */
2096 GtkWidget* parentContainer = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2097 g_object_ref_sink(parentContainer);
2098 gtk_container_add(GTK_CONTAINER(parentContainer), GTK_WIDGET(webView));
2100 AtkObject* axParent = gtk_widget_get_accessible(parentContainer);
2101 g_assert(ATK_IS_OBJECT(axParent));
2103 AtkObject* axRoot = gtk_widget_get_accessible(GTK_WIDGET(webView));
2104 g_assert(ATK_IS_OBJECT(axRoot));
2106 /* The child for the parent container's accessibility object
2107 should be the accessibility object for the WebView's root. */
2108 AtkObject* axParentChild = atk_object_ref_accessible_child(axParent, 0);
2109 g_assert(axParentChild == axRoot);
2111 /* Bottom-up navigation should match top-down one. */
2112 g_assert(atk_object_get_parent(axParentChild) == axParent);
2114 g_object_unref(axParentChild);
2115 g_object_unref(parentContainer);
2118 static void testWebkitAtkSetParentForObject()
2120 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
2121 GtkAllocation allocation = { 0, 0, 800, 600 };
2122 gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
2123 webkit_web_view_load_string(webView, contents, 0, 0, 0);
2125 /* Put the webview in a parent container widget to check that the
2126 normal behaviour keeps working as expected by default. */
2127 GtkWidget* parentContainer = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2128 g_object_ref_sink(parentContainer);
2129 gtk_container_add(GTK_CONTAINER(parentContainer), GTK_WIDGET(webView));
2131 AtkObject* axRoot = gtk_widget_get_accessible(GTK_WIDGET(webView));
2132 g_assert(ATK_IS_OBJECT(axRoot));
2134 AtkObject* axParent = gtk_widget_get_accessible(parentContainer);
2135 g_assert(ATK_IS_OBJECT(axParent));
2137 /* The parent of the root object is the parent container's a11y object. */
2138 g_assert(atk_object_get_parent(axRoot) == axParent);
2140 /* We now need to use something as a an alternative parent for
2141 the a11y object associated with the root of the DOM tree. */
2142 GtkWidget* alternativeParent = gtk_button_new();
2143 g_object_ref_sink(alternativeParent);
2145 AtkObject* axAlternativeParent = gtk_widget_get_accessible(alternativeParent);
2146 g_assert(ATK_IS_OBJECT(axAlternativeParent));
2148 /* Manually set the alternative parent's accessibility object as
2149 the parent for the WebKit accessibility root object and check. */
2150 atk_object_set_parent(axRoot, axAlternativeParent);
2151 g_assert(atk_object_get_parent(axRoot) == axAlternativeParent);
2153 g_object_unref(alternativeParent);
2154 g_object_unref(parentContainer);
2157 #ifdef GTK_API_VERSION_2
2158 static void initializeTestingFramework(int argc, char** argv)
2160 /* Ensure GAIL is the only module loaded. */
2161 g_setenv("GTK_MODULES", "gail", TRUE);
2163 /* Following lines were taken from gtk_test_init(). */
2164 g_test_init(&argc, &argv, 0);
2165 gtk_disable_setlocale();
2166 setlocale(LC_ALL, "C");
2168 gtk_init(&argc, &argv);
2172 int main(int argc, char** argv)
2174 #ifdef GTK_API_VERSION_2
2175 /* We can't just call to gtk_test_init() in this case because its
2176 implementation makes sure that no GTK+ module will be loaded, and
2177 we will need to load GAIL for tests that need to use AtkObjects
2178 from non-WebKit GtkWidgets (e.g parentForRootObject).*/
2179 initializeTestingFramework(argc, argv);
2181 gtk_test_init(&argc, &argv, NULL);
2184 g_test_bug_base("https://bugs.webkit.org/");
2185 g_test_add_func("/webkit/atk/caretOffsets", testWebkitAtkCaretOffsets);
2186 g_test_add_func("/webkit/atk/caretOffsetsAndExtranousWhiteSpaces", testWebkitAtkCaretOffsetsAndExtranousWhiteSpaces);
2187 g_test_add_func("/webkit/atk/comboBox", testWebkitAtkComboBox);
2188 g_test_add_func("/webkit/atk/documentLoadingEvents", testWebkitAtkDocumentLoadingEvents);
2189 g_test_add_func("/webkit/atk/embeddedObjects", testWebkitAtkEmbeddedObjects);
2190 g_test_add_func("/webkit/atk/getTextAtOffset", testWebkitAtkGetTextAtOffset);
2191 g_test_add_func("/webkit/atk/getTextAtOffsetNewlines", testWebkitAtkGetTextAtOffsetNewlines);
2192 g_test_add_func("/webkit/atk/getTextAtOffsetTextarea", testWebkitAtkGetTextAtOffsetTextarea);
2193 g_test_add_func("/webkit/atk/getTextAtOffsetTextInput", testWebkitAtkGetTextAtOffsetTextInput);
2194 g_test_add_func("/webkit/atk/getTextAtOffsetWithPreformattedText", testWebkitAtkGetTextAtOffsetWithPreformattedText);
2195 g_test_add_func("/webkit/atk/getTextAtOffsetWithSpecialCharacters", testWebkitAtkGetTextAtOffsetWithSpecialCharacters);
2196 g_test_add_func("/webkit/atk/getTextAtOffsetWithWrappedLines", testWebkitAtkGetTextAtOffsetWithWrappedLines);
2197 g_test_add_func("/webkit/atk/getTextAtOffsetWithEmbeddedObjects", testWebkitAtkGetTextAtOffsetWithEmbeddedObjects);
2198 g_test_add_func("/webkit/atk/getTextInParagraphAndBodySimple", testWebkitAtkGetTextInParagraphAndBodySimple);
2199 g_test_add_func("/webkit/atk/getTextInParagraphAndBodyModerate", testWebkitAtkGetTextInParagraphAndBodyModerate);
2200 g_test_add_func("/webkit/atk/getTextInTable", testWebkitAtkGetTextInTable);
2201 g_test_add_func("/webkit/atk/getHeadersInTable", testWebkitAtkGetHeadersInTable);
2202 g_test_add_func("/webkit/atk/textAttributes", testWebkitAtkTextAttributes);
2203 g_test_add_func("/webkit/atk/textSelections", testWebkitAtkTextSelections);
2204 g_test_add_func("/webkit/atk/getExtents", testWebkitAtkGetExtents);
2205 g_test_add_func("/webkit/atk/hypertextAndHyperlinks", testWebkitAtkHypertextAndHyperlinks);
2206 g_test_add_func("/webkit/atk/layoutAndDataTables", testWebkitAtkLayoutAndDataTables);
2207 g_test_add_func("/webkit/atk/linksWithInlineImages", testWebkitAtkLinksWithInlineImages);
2208 g_test_add_func("/webkit/atk/listsOfItems", testWebkitAtkListsOfItems);
2209 g_test_add_func("/webkit/atk/textChangedNotifications", testWebkitAtkTextChangedNotifications);
2210 g_test_add_func("/webkit/atk/parentForRootObject", testWebkitAtkParentForRootObject);
2211 g_test_add_func("/webkit/atk/setParentForObject", testWebkitAtkSetParentForObject);
2212 return g_test_run ();