1 /*
2 * Copyright © 2010 Joanmarie Diggs
3 * Copyright © 2010 Igalia S.L.
4 *
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.
9 *
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.
14 *
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.
19 */
20
21 #include <glib.h>
22 #include <glib/gstdio.h>
23 #include <gtk/gtk.h>
24 #include <webkit/webkit.h>
25
26 #if GTK_CHECK_VERSION(2, 14, 0)
27
28 /* Non form roles */
29 #define HTML_DOCUMENT_FRAME "<html><body>This is a test.</body></html>"
30 #define HTML_HEADING "<html><body><h1>1</h1><h2>2</h2><h3>3</h3><h4>4</h4><h5>5</h5><h6>6</h6></body></html>"
31 #define HTML_IMAGE "<html><body><img src='foobar.png' alt='This is a test.'/></body></html>"
32 #define HTML_LINK_TEXT "<html><body><a href='foobar.html'>This is a test.</a></body></html>"
33 #define HTML_LIST "<html><body><ul><li>1</li><li>2</li></ul><ol><li>1</li><li>2</li></ol></body></html>"
34 #define HTML_PARAGRAPH "<html><body><p>This is a test.</p></body></html>"
35 #define HTML_SECTION "<html><body><div>This is a test.</div></body></html>"
36 #define HTML_TABLE "<html><body><table border='1'><tr><td>This is</td><td>a test.</td></tr></table></body></html>"
37 #define HTML_SEPARATOR "<html><body><hr/></body></html>"
38 #define HTML_COMBOBOX "<html><body><select size='1'><option>one</option><option>two</option><option>three</option></select></body></html>"
39 /* Form roles */
40 #define HTML_FORM "<html><body><form>This is a test.</form></body></html>"
41 #define HTML_CHECK_BOX "<html><body><input type='checkbox' />This is a test.</body></html>"
42 #define HTML_LABELED_ENTRY "<html><body><label for='foo'>Name:</label><input type='text' id='foo' /></body></html>"
43 #define HTML_LISTBOX "<html><body><select size='3'><option>one</option><option>two</option><option>three</option></select></body></html>"
44 #define HTML_PASSWORD_TEXT "<html><body><input type='password' /></body></html>"
45 #define HTML_PUSH_BUTTON "<html><body><input type='submit' value='ok' />This is a test.</body></html>"
46 #define HTML_RADIO_BUTTON "<html><body><input type='radio' />This is a test.</body></html>"
47
48 typedef struct {
49 AtkObject* documentFrame;
50 AtkObject* obj;
51 AtkRole role;
52 GtkWidget* webView;
53 GtkAllocation alloc;
54 GMainLoop* loop;
55 } AtkRolesFixture;
56
finish_loading(AtkRolesFixture * fixture)57 static gboolean finish_loading(AtkRolesFixture* fixture)
58 {
59 if (g_main_loop_is_running(fixture->loop))
60 g_main_loop_quit(fixture->loop);
61
62 fixture->documentFrame = gtk_widget_get_accessible(fixture->webView);
63 g_assert(fixture->documentFrame);
64
65 return FALSE;
66 }
67
atk_roles_fixture_setup(AtkRolesFixture * fixture,gconstpointer data)68 static void atk_roles_fixture_setup(AtkRolesFixture* fixture, gconstpointer data)
69 {
70 fixture->loop = g_main_loop_new(NULL, TRUE);
71 fixture->alloc = (GtkAllocation) { 0, 0, 800, 600 };
72 fixture->webView = webkit_web_view_new();
73 g_object_ref_sink(fixture->webView);
74
75 gtk_widget_size_allocate(fixture->webView, &fixture->alloc);
76
77 if (data != NULL)
78 webkit_web_view_load_string(WEBKIT_WEB_VIEW (fixture->webView), (const char*) data, NULL, NULL, NULL);
79
80 g_idle_add((GSourceFunc) finish_loading, fixture);
81 g_main_loop_run(fixture->loop);
82 }
83
atk_roles_fixture_teardown(AtkRolesFixture * fixture,gconstpointer data)84 static void atk_roles_fixture_teardown(AtkRolesFixture* fixture, gconstpointer data)
85 {
86 g_object_unref(fixture->webView);
87 g_main_loop_unref(fixture->loop);
88 }
89
get_child_and_test_role(AtkObject * obj,gint pos,AtkRole role)90 static void get_child_and_test_role(AtkObject* obj, gint pos, AtkRole role)
91 {
92 AtkObject* child;
93 AtkRole child_role;
94
95 child = atk_object_ref_accessible_child(obj, pos);
96 g_assert(child);
97 child_role = atk_object_get_role(child);
98 g_assert(child_role == role);
99
100 g_object_unref(child);
101 }
102
test_webkit_atk_get_role_document_frame(AtkRolesFixture * fixture,gconstpointer data)103 static void test_webkit_atk_get_role_document_frame(AtkRolesFixture* fixture, gconstpointer data)
104 {
105 fixture->role = atk_object_get_role(fixture->documentFrame);
106 g_assert(fixture->role == ATK_ROLE_DOCUMENT_FRAME);
107 }
108
test_webkit_atk_get_role_heading(AtkRolesFixture * fixture,gconstpointer data)109 static void test_webkit_atk_get_role_heading(AtkRolesFixture* fixture, gconstpointer data)
110 {
111 get_child_and_test_role(fixture->documentFrame, 0, ATK_ROLE_HEADING);
112 get_child_and_test_role(fixture->documentFrame, 1, ATK_ROLE_HEADING);
113 get_child_and_test_role(fixture->documentFrame, 2, ATK_ROLE_HEADING);
114 get_child_and_test_role(fixture->documentFrame, 3, ATK_ROLE_HEADING);
115 get_child_and_test_role(fixture->documentFrame, 4, ATK_ROLE_HEADING);
116 get_child_and_test_role(fixture->documentFrame, 5, ATK_ROLE_HEADING);
117 }
118
test_webkit_atk_get_role_image(AtkRolesFixture * fixture,gconstpointer data)119 static void test_webkit_atk_get_role_image(AtkRolesFixture* fixture, gconstpointer data)
120 {
121 // This is an extraneous object of ATK_ROLE_PANEL which we should get rid of.
122 fixture->obj = atk_object_ref_accessible_child(fixture->documentFrame, 0);
123 g_assert(fixture->obj);
124
125 get_child_and_test_role(fixture->obj, 0, ATK_ROLE_IMAGE);
126
127 g_object_unref(fixture->obj);
128 }
129
test_webkit_atk_get_role_link(AtkRolesFixture * fixture,gconstpointer data)130 static void test_webkit_atk_get_role_link(AtkRolesFixture* fixture, gconstpointer data)
131 {
132 // This is an extraneous object of ATK_ROLE_PANEL which we should get rid of.
133 fixture->obj = atk_object_ref_accessible_child(fixture->documentFrame, 0);
134 g_assert(fixture->obj);
135
136 get_child_and_test_role(fixture->obj, 0, ATK_ROLE_LINK);
137
138 g_object_unref(fixture->obj);
139 }
140
test_webkit_atk_get_role_list_and_item(AtkRolesFixture * fixture,gconstpointer data)141 static void test_webkit_atk_get_role_list_and_item(AtkRolesFixture* fixture, gconstpointer data)
142 {
143 AtkObject* listObj;
144
145 listObj = atk_object_ref_accessible_child(fixture->documentFrame, 0);
146 g_assert(listObj);
147 fixture->role = atk_object_get_role(listObj);
148 g_assert(fixture->role == ATK_ROLE_LIST);
149
150 get_child_and_test_role(listObj, 0, ATK_ROLE_LIST_ITEM);
151 get_child_and_test_role(listObj, 1, ATK_ROLE_LIST_ITEM);
152 g_object_unref(listObj);
153
154 listObj = atk_object_ref_accessible_child(fixture->documentFrame, 1);
155 g_assert(listObj);
156 fixture->role = atk_object_get_role(listObj);
157 g_assert(fixture->role == ATK_ROLE_LIST);
158
159 get_child_and_test_role(listObj, 0, ATK_ROLE_LIST_ITEM);
160 get_child_and_test_role(listObj, 1, ATK_ROLE_LIST_ITEM);
161 g_object_unref(listObj);
162 }
163
test_webkit_atk_get_role_paragraph(AtkRolesFixture * fixture,gconstpointer data)164 static void test_webkit_atk_get_role_paragraph(AtkRolesFixture* fixture, gconstpointer data)
165 {
166 get_child_and_test_role(fixture->documentFrame, 0, ATK_ROLE_PARAGRAPH);
167 }
168
test_webkit_atk_get_role_section(AtkRolesFixture * fixture,gconstpointer data)169 static void test_webkit_atk_get_role_section(AtkRolesFixture* fixture, gconstpointer data)
170 {
171 get_child_and_test_role(fixture->documentFrame, 0, ATK_ROLE_SECTION);
172 }
173
174 // Does not yet test table cells because of bug 30895.
test_webkit_atk_get_role_table(AtkRolesFixture * fixture,gconstpointer data)175 static void test_webkit_atk_get_role_table(AtkRolesFixture* fixture, gconstpointer data)
176 {
177 get_child_and_test_role(fixture->documentFrame, 0, ATK_ROLE_TABLE);
178 }
179
test_webkit_atk_get_role_separator(AtkRolesFixture * fixture,gconstpointer data)180 static void test_webkit_atk_get_role_separator(AtkRolesFixture *fixture, gconstpointer data)
181 {
182 get_child_and_test_role(fixture->documentFrame, 0, ATK_ROLE_SEPARATOR);
183 }
184
test_webkit_atk_get_role_combobox(AtkRolesFixture * fixture,gconstpointer data)185 static void test_webkit_atk_get_role_combobox(AtkRolesFixture *fixture, gconstpointer data)
186 {
187 AtkObject* comboboxMenu;
188
189 // This is an extraneous object of ATK_ROLE_PANEL which we should get rid of.
190 fixture->obj = atk_object_ref_accessible_child(fixture->documentFrame, 0);
191 g_assert(fixture->obj);
192
193 fixture->obj = atk_object_ref_accessible_child(fixture->obj, 0);
194 g_assert(fixture->obj);
195 fixture->role = atk_object_get_role(fixture->obj);
196 g_assert(fixture->role == ATK_ROLE_COMBO_BOX);
197
198 comboboxMenu = atk_object_ref_accessible_child(fixture->obj, 0);
199 g_assert(comboboxMenu);
200 fixture->role = atk_object_get_role(comboboxMenu);
201 g_assert(fixture->role == ATK_ROLE_MENU);
202
203 get_child_and_test_role(comboboxMenu, 0, ATK_ROLE_MENU_ITEM);
204 get_child_and_test_role(comboboxMenu, 1, ATK_ROLE_MENU_ITEM);
205 get_child_and_test_role(comboboxMenu, 2, ATK_ROLE_MENU_ITEM);
206
207 g_object_unref(fixture->obj);
208 g_object_unref(comboboxMenu);
209 }
210
211 /* Form roles */
test_webkit_atk_get_role_form(AtkRolesFixture * fixture,gconstpointer data)212 static void test_webkit_atk_get_role_form(AtkRolesFixture *fixture, gconstpointer data)
213 {
214 get_child_and_test_role(fixture->documentFrame, 0, ATK_ROLE_FORM);
215 }
216
test_webkit_atk_get_role_check_box(AtkRolesFixture * fixture,gconstpointer data)217 static void test_webkit_atk_get_role_check_box(AtkRolesFixture* fixture, gconstpointer data)
218 {
219 // This is an extraneous object of ATK_ROLE_PANEL which we should get rid of.
220 fixture->obj = atk_object_ref_accessible_child(fixture->documentFrame, 0);
221 g_assert(fixture->obj);
222
223 get_child_and_test_role(fixture->obj, 0, ATK_ROLE_CHECK_BOX);
224
225 g_object_unref(fixture->obj);
226 }
227
test_webkit_atk_get_role_entry(AtkRolesFixture * fixture,gconstpointer data)228 static void test_webkit_atk_get_role_entry(AtkRolesFixture* fixture, gconstpointer data)
229 {
230 // This is an extraneous object of ATK_ROLE_PANEL which we should get rid of.
231 fixture->obj = atk_object_ref_accessible_child(fixture->documentFrame, 0);
232 g_assert(fixture->obj);
233
234 get_child_and_test_role(fixture->obj, 1, ATK_ROLE_ENTRY);
235
236 g_object_unref(fixture->obj);
237 }
238
test_webkit_atk_get_role_label(AtkRolesFixture * fixture,gconstpointer data)239 static void test_webkit_atk_get_role_label(AtkRolesFixture* fixture, gconstpointer data)
240 {
241 // This is an extraneous object of ATK_ROLE_PANEL which we should get rid of.
242 fixture->obj = atk_object_ref_accessible_child(fixture->documentFrame, 0);
243 g_assert(fixture->obj);
244
245 get_child_and_test_role(fixture->obj, 0, ATK_ROLE_LABEL);
246
247 g_object_unref(fixture->obj);
248 }
249
test_webkit_atk_get_role_listbox(AtkRolesFixture * fixture,gconstpointer data)250 static void test_webkit_atk_get_role_listbox(AtkRolesFixture* fixture, gconstpointer data)
251 {
252 AtkObject* listboxObj;
253 // This is an extraneous object of ATK_ROLE_PANEL which we should get rid of.
254 fixture->obj = atk_object_ref_accessible_child(fixture->documentFrame, 0);
255 g_assert(fixture->obj);
256
257 listboxObj = atk_object_ref_accessible_child(fixture->obj, 0);
258 g_assert(listboxObj);
259 fixture->role = atk_object_get_role(listboxObj);
260 g_assert(fixture->role == ATK_ROLE_LIST);
261
262 get_child_and_test_role(listboxObj, 0, ATK_ROLE_LIST_ITEM);
263 get_child_and_test_role(listboxObj, 1, ATK_ROLE_LIST_ITEM);
264 get_child_and_test_role(listboxObj, 2, ATK_ROLE_LIST_ITEM);
265
266 g_object_unref(fixture->obj);
267 g_object_unref(listboxObj);
268 }
269
test_webkit_atk_get_role_password_text(AtkRolesFixture * fixture,gconstpointer data)270 static void test_webkit_atk_get_role_password_text(AtkRolesFixture* fixture, gconstpointer data)
271 {
272 // This is an extraneous object of ATK_ROLE_PANEL which we should get rid of.
273 fixture->obj = atk_object_ref_accessible_child(fixture->documentFrame, 0);
274 g_assert(fixture->obj);
275
276 get_child_and_test_role(fixture->obj, 0, ATK_ROLE_PASSWORD_TEXT);
277
278 g_object_unref(fixture->obj);
279 }
280
test_webkit_atk_get_role_push_button(AtkRolesFixture * fixture,gconstpointer data)281 static void test_webkit_atk_get_role_push_button(AtkRolesFixture* fixture, gconstpointer data)
282 {
283 // This is an extraneous object of ATK_ROLE_PANEL which we should get rid of.
284 fixture->obj = atk_object_ref_accessible_child(fixture->documentFrame, 0);
285 g_assert(fixture->obj);
286
287 get_child_and_test_role(fixture->obj, 0, ATK_ROLE_PUSH_BUTTON);
288
289 g_object_unref(fixture->obj);
290 }
291
test_webkit_atk_get_role_radio_button(AtkRolesFixture * fixture,gconstpointer data)292 static void test_webkit_atk_get_role_radio_button(AtkRolesFixture* fixture, gconstpointer data)
293 {
294 // This is an extraneous object of ATK_ROLE_PANEL which we should get rid of.
295 fixture->obj = atk_object_ref_accessible_child(fixture->documentFrame, 0);
296 g_assert(fixture->obj);
297
298 get_child_and_test_role(fixture->obj, 0, ATK_ROLE_RADIO_BUTTON);
299
300 g_object_unref(fixture->obj);
301 }
302
main(int argc,char ** argv)303 int main(int argc, char** argv)
304 {
305 g_thread_init(NULL);
306 gtk_test_init(&argc, &argv, NULL);
307
308 g_test_bug_base("https://bugs.webkit.org/");
309
310 g_test_add("/webkit/atk/test_webkit_atk_get_role_document_frame",
311 AtkRolesFixture, HTML_DOCUMENT_FRAME,
312 atk_roles_fixture_setup,
313 test_webkit_atk_get_role_document_frame,
314 atk_roles_fixture_teardown);
315
316 g_test_add("/webkit/atk/test_webkit_atk_get_role_heading",
317 AtkRolesFixture, HTML_HEADING,
318 atk_roles_fixture_setup,
319 test_webkit_atk_get_role_heading,
320 atk_roles_fixture_teardown);
321
322 g_test_add("/webkit/atk/test_webkit_atk_get_role_image",
323 AtkRolesFixture, HTML_IMAGE,
324 atk_roles_fixture_setup,
325 test_webkit_atk_get_role_image,
326 atk_roles_fixture_teardown);
327
328 g_test_add("/webkit/atk/test_webkit_atk_get_role_link",
329 AtkRolesFixture, HTML_LINK_TEXT,
330 atk_roles_fixture_setup,
331 test_webkit_atk_get_role_link,
332 atk_roles_fixture_teardown);
333
334 g_test_add("/webkit/atk/test_webkit_atk_get_role_list_and_item",
335 AtkRolesFixture, HTML_LIST,
336 atk_roles_fixture_setup,
337 test_webkit_atk_get_role_list_and_item,
338 atk_roles_fixture_teardown);
339
340 g_test_add("/webkit/atk/test_webkit_atk_get_role_paragraph",
341 AtkRolesFixture, HTML_PARAGRAPH,
342 atk_roles_fixture_setup,
343 test_webkit_atk_get_role_paragraph,
344 atk_roles_fixture_teardown);
345
346 g_test_add("/webkit/atk/test_webkit_atk_get_role_section",
347 AtkRolesFixture, HTML_SECTION,
348 atk_roles_fixture_setup,
349 test_webkit_atk_get_role_section,
350 atk_roles_fixture_teardown);
351
352 g_test_add("/webkit/atk/test_webkit_atk_get_role_table",
353 AtkRolesFixture, HTML_TABLE,
354 atk_roles_fixture_setup,
355 test_webkit_atk_get_role_table,
356 atk_roles_fixture_teardown);
357
358 g_test_add("/webkit/atk/test_webkit_atk_get_role_separator",
359 AtkRolesFixture, HTML_SEPARATOR,
360 atk_roles_fixture_setup,
361 test_webkit_atk_get_role_separator,
362 atk_roles_fixture_teardown);
363
364 g_test_add("/webkit/atk/test_webkit_atk_get_role_combobox",
365 AtkRolesFixture, HTML_COMBOBOX,
366 atk_roles_fixture_setup,
367 test_webkit_atk_get_role_combobox,
368 atk_roles_fixture_teardown);
369
370 /* Form roles */
371 g_test_add("/webkit/atk/test_webkit_atk_get_role_form",
372 AtkRolesFixture, HTML_FORM,
373 atk_roles_fixture_setup,
374 test_webkit_atk_get_role_form,
375 atk_roles_fixture_teardown);
376 g_test_add("/webkit/atk/test_webkit_atk_get_role_check_box",
377 AtkRolesFixture, HTML_CHECK_BOX,
378 atk_roles_fixture_setup,
379 test_webkit_atk_get_role_check_box,
380 atk_roles_fixture_teardown);
381
382 g_test_add("/webkit/atk/test_webkit_atk_get_role_entry",
383 AtkRolesFixture, HTML_LABELED_ENTRY,
384 atk_roles_fixture_setup,
385 test_webkit_atk_get_role_entry,
386 atk_roles_fixture_teardown);
387
388 g_test_add("/webkit/atk/test_webkit_atk_get_role_label",
389 AtkRolesFixture, HTML_LABELED_ENTRY,
390 atk_roles_fixture_setup,
391 test_webkit_atk_get_role_label,
392 atk_roles_fixture_teardown);
393
394 g_test_add("/webkit/atk/test_webkit_atk_get_role_listbox",
395 AtkRolesFixture, HTML_LISTBOX,
396 atk_roles_fixture_setup,
397 test_webkit_atk_get_role_listbox,
398 atk_roles_fixture_teardown);
399
400 g_test_add("/webkit/atk/test_webkit_atk_get_role_password_text",
401 AtkRolesFixture, HTML_PASSWORD_TEXT,
402 atk_roles_fixture_setup,
403 test_webkit_atk_get_role_password_text,
404 atk_roles_fixture_teardown);
405
406 g_test_add("/webkit/atk/test_webkit_atk_get_role_push_button",
407 AtkRolesFixture, HTML_PUSH_BUTTON,
408 atk_roles_fixture_setup,
409 test_webkit_atk_get_role_push_button,
410 atk_roles_fixture_teardown);
411
412 g_test_add("/webkit/atk/test_webkit_atk_get_role_radio_button",
413 AtkRolesFixture, HTML_RADIO_BUTTON,
414 atk_roles_fixture_setup,
415 test_webkit_atk_get_role_radio_button,
416 atk_roles_fixture_teardown);
417
418 return g_test_run();
419 }
420
421 #else
main(int argc,char ** argv)422 int main(int argc, char** argv)
423 {
424 g_critical("You will need gtk-2.14.0 to run the unit tests. Doing nothing now.");
425 return 0;
426 }
427
428 #endif
429