2 * Copyright (C) 2010 Igalia S.L.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include "test_utils.h"
23 #include <glib/gstdio.h>
25 #include <webkit/webkit.h>
27 #if GTK_CHECK_VERSION(2, 14, 0)
29 #define HTML_DOCUMENT_TITLE "<html><head><title>This is the title</title></head><body></body></html>"
30 #define HTML_DOCUMENT_ELEMENTS "<html><body><ul><li>1</li><li>2</li><li>3</li></ul></body></html>"
31 #define HTML_DOCUMENT_ELEMENTS_CLASS "<html><body><div class=\"test\"></div><div class=\"strange\"></div><div class=\"test\"></div></body></html>"
32 #define HTML_DOCUMENT_ELEMENTS_ID "<html><body><div id=\"testok\"></div><div id=\"testbad\">first</div><div id=\"testbad\">second</div></body></html>"
33 #define HTML_DOCUMENT_LINKS "<html><head><title>Title</title></head><body><a href=\"about:blank\">blank</a><a href=\"http://www.google.com\">google</a><a href=\"http://www.webkit.org\">webkit</a></body></html>"
34 #define HTML_DOCUMENT_IFRAME "<html><head><title>IFrame</title></head><body><iframe id='iframe'></iframe><div id='test'></div></body></html>"
41 static gboolean finish_loading(DomDocumentFixture* fixture)
43 if (g_main_loop_is_running(fixture->loop))
44 g_main_loop_quit(fixture->loop);
49 static void dom_document_fixture_setup(DomDocumentFixture* fixture, gconstpointer data)
51 fixture->loop = g_main_loop_new(NULL, TRUE);
52 fixture->webView = webkit_web_view_new();
53 g_object_ref_sink(fixture->webView);
56 webkit_web_view_load_string(WEBKIT_WEB_VIEW (fixture->webView), (const char*) data, NULL, NULL, NULL);
58 g_idle_add((GSourceFunc)finish_loading, fixture);
59 g_main_loop_run(fixture->loop);
62 static void dom_document_fixture_teardown(DomDocumentFixture* fixture, gconstpointer data)
65 g_object_unref(fixture->webView);
66 g_main_loop_unref(fixture->loop);
69 static void test_dom_document_title(DomDocumentFixture* fixture, gconstpointer data)
72 WebKitWebView* view = (WebKitWebView*)fixture->webView;
74 WebKitDOMDocument* document = webkit_web_view_get_dom_document(view);
76 gchar* title = webkit_dom_document_get_title(document);
78 g_assert_cmpstr(title, ==, "This is the title");
80 webkit_dom_document_set_title(document, "This is the second title");
81 title = webkit_dom_document_get_title(document);
83 g_assert_cmpstr(title, ==, "This is the second title");
87 static void test_dom_document_get_elements_by_tag_name(DomDocumentFixture* fixture, gconstpointer data)
90 WebKitWebView* view = (WebKitWebView*)fixture->webView;
92 WebKitDOMDocument* document = webkit_web_view_get_dom_document(view);
94 WebKitDOMNodeList* list = webkit_dom_document_get_elements_by_tag_name(document, "li");
96 gulong length = webkit_dom_node_list_get_length(list);
97 g_assert_cmpint(length, ==, 3);
101 for (i = 0; i < length; i++) {
102 WebKitDOMNode* item = webkit_dom_node_list_item(list, i);
104 WebKitDOMElement* element = (WebKitDOMElement*)item;
106 g_assert_cmpstr(webkit_dom_element_get_tag_name(element), ==, "LI");
107 WebKitDOMHTMLElement* htmlElement = (WebKitDOMHTMLElement*)element;
108 char* n = g_strdup_printf("%d", i+1);
109 g_assert_cmpstr(webkit_dom_html_element_get_inner_text(htmlElement), ==, n);
113 g_object_unref(list);
116 static void test_dom_document_get_elements_by_class_name(DomDocumentFixture* fixture, gconstpointer data)
119 WebKitWebView* view = (WebKitWebView*)fixture->webView;
121 WebKitDOMDocument* document = webkit_web_view_get_dom_document(view);
123 WebKitDOMNodeList* list = webkit_dom_document_get_elements_by_class_name(document, "test");
125 gulong length = webkit_dom_node_list_get_length(list);
126 g_assert_cmpint(length, ==, 2);
130 for (i = 0; i < length; i++) {
131 WebKitDOMNode* item = webkit_dom_node_list_item(list, i);
133 WebKitDOMElement* element = (WebKitDOMElement*)item;
135 g_assert_cmpstr(webkit_dom_element_get_tag_name(element), ==, "DIV");
138 g_object_unref(list);
141 static void test_dom_document_get_element_by_id(DomDocumentFixture* fixture, gconstpointer data)
144 WebKitWebView* view = (WebKitWebView*)fixture->webView;
146 WebKitDOMDocument* document = webkit_web_view_get_dom_document(view);
148 WebKitDOMElement* element = webkit_dom_document_get_element_by_id(document, "testok");
150 element = webkit_dom_document_get_element_by_id(document, "this-id-does-not-exist");
151 g_assert(element == 0);
152 /* The DOM spec says the return value is undefined when there's
153 * more than one element with the same id; in our case the first
154 * one will be returned */
155 element = webkit_dom_document_get_element_by_id(document, "testbad");
157 WebKitDOMHTMLElement* htmlElement = (WebKitDOMHTMLElement*)element;
158 g_assert_cmpstr(webkit_dom_html_element_get_inner_text(htmlElement), ==, "first");
161 static void test_dom_document_get_links(DomDocumentFixture* fixture, gconstpointer data)
164 WebKitWebView* view = (WebKitWebView*)fixture->webView;
166 WebKitDOMDocument* document = webkit_web_view_get_dom_document(view);
168 WebKitDOMHTMLCollection *collection = webkit_dom_document_get_links(document);
169 g_assert(collection);
170 gulong length = webkit_dom_html_collection_get_length(collection);
171 g_assert_cmpint(length, ==, 3);
175 for (i = 0; i < length; i++) {
176 static const char* names[] = { "blank", "google", "webkit" };
177 static const char* uris[] = { "about:blank", "http://www.google.com/", "http://www.webkit.org/" };
178 WebKitDOMNode *node = webkit_dom_html_collection_item(collection, i);
180 WebKitDOMElement* element = (WebKitDOMElement*)node;
181 g_assert_cmpstr(webkit_dom_element_get_tag_name(element), ==, "A");
182 WebKitDOMHTMLElement *htmlElement = (WebKitDOMHTMLElement*)element;
183 g_assert_cmpstr(webkit_dom_html_element_get_inner_text(htmlElement), ==, names[i]);
184 WebKitDOMHTMLAnchorElement *anchor = (WebKitDOMHTMLAnchorElement*)element;
185 g_assert_cmpstr(webkit_dom_html_anchor_element_get_href(anchor), ==, uris[i]);
187 g_object_unref(collection);
190 static void weak_notify(gpointer data, GObject* zombie)
192 guint* count = (guint*)data;
196 static void test_dom_document_garbage_collection(DomDocumentFixture* fixture, gconstpointer data)
200 WebKitWebView* view = (WebKitWebView*)fixture->webView;
202 WebKitDOMDocument* document = webkit_web_view_get_dom_document(view);
204 g_object_weak_ref(G_OBJECT(document), (GWeakNotify)weak_notify, &count);
205 WebKitDOMHTMLHeadElement* head = webkit_dom_document_get_head(document);
207 g_object_weak_ref(G_OBJECT(head), (GWeakNotify)weak_notify, &count);
208 WebKitDOMHTMLElement* body = webkit_dom_document_get_body(document);
210 g_object_weak_ref(G_OBJECT(body), (GWeakNotify)weak_notify, &count);
211 WebKitDOMHTMLCollection *collection = webkit_dom_document_get_links(document);
212 g_assert(collection);
213 g_object_weak_ref(G_OBJECT(collection), (GWeakNotify)weak_notify, &count);
215 webkit_web_view_load_string(WEBKIT_WEB_VIEW(view), HTML_DOCUMENT_LINKS, NULL, NULL, NULL);
217 while (g_main_context_pending(NULL))
218 g_main_context_iteration(NULL, FALSE);
220 g_assert_cmpuint(count, ==, 3);
222 g_object_unref(collection);
223 g_assert_cmpuint(count, ==, 4);
227 document = webkit_web_view_get_dom_document(view);
229 g_object_weak_ref(G_OBJECT(document), (GWeakNotify)weak_notify, &count);
230 head = webkit_dom_document_get_head(document);
232 g_object_weak_ref(G_OBJECT(head), (GWeakNotify)weak_notify, &count);
233 body = webkit_dom_document_get_body(document);
235 g_object_weak_ref(G_OBJECT(body), (GWeakNotify)weak_notify, &count);
236 collection = webkit_dom_document_get_links(document);
237 g_assert(collection);
238 g_object_weak_ref(G_OBJECT(collection), (GWeakNotify)weak_notify, &count);
239 /* Ask twice for the same object */
240 WebKitDOMHTMLCollection* collection2 = webkit_dom_document_get_links(document);
241 g_assert(collection2);
242 g_object_weak_ref(G_OBJECT(collection2), (GWeakNotify)weak_notify, &count);
244 g_object_unref(document);
245 g_object_unref(head);
246 g_object_unref(body);
247 g_object_unref(collection);
248 g_object_unref(collection2);
250 g_assert_cmpuint(count, ==, 5);
252 webkit_web_view_load_string(WEBKIT_WEB_VIEW(view), HTML_DOCUMENT_IFRAME, NULL, NULL, NULL);
254 while (g_main_context_pending(NULL))
255 g_main_context_iteration(NULL, FALSE);
259 document = webkit_web_view_get_dom_document(view);
260 WebKitDOMElement* div = webkit_dom_document_get_element_by_id(document, "test");
262 g_object_weak_ref(G_OBJECT(div), (GWeakNotify)weak_notify, &count);
263 WebKitDOMElement* iframe = webkit_dom_document_get_element_by_id(document, "iframe");
266 webkit_dom_element_set_attribute(iframe, "src", "data:<html><head></head></html>", NULL);
268 while (g_main_context_pending(NULL))
269 g_main_context_iteration(NULL, FALSE);
271 WebKitDOMDocument* iframeDocument = webkit_dom_html_iframe_element_get_content_document(WEBKIT_DOM_HTML_IFRAME_ELEMENT(iframe));
272 g_assert(iframeDocument);
273 head = webkit_dom_document_get_head(iframeDocument);
275 g_object_weak_ref(G_OBJECT(head), (GWeakNotify)weak_notify, &count);
277 webkit_dom_element_set_attribute(iframe, "src", "about:blank", NULL);
279 while (g_main_context_pending(NULL))
280 g_main_context_iteration(NULL, FALSE);
282 g_assert_cmpuint(count, ==, 1);
284 webkit_web_view_load_string(WEBKIT_WEB_VIEW(view), HTML_DOCUMENT_LINKS, NULL, NULL, NULL);
286 while (g_main_context_pending(NULL))
287 g_main_context_iteration(NULL, FALSE);
289 g_assert_cmpuint(count, ==, 2);
293 document = webkit_web_view_get_dom_document(view);
295 g_object_weak_ref(G_OBJECT(document), (GWeakNotify)weak_notify, &count);
296 /* Ask twice for the Document */
297 WebKitDOMDocument* document2 = webkit_web_view_get_dom_document(view);
299 g_object_weak_ref(G_OBJECT(document2), (GWeakNotify)weak_notify, &count);
300 head = webkit_dom_document_get_head(document);
302 g_object_weak_ref(G_OBJECT(head), (GWeakNotify)weak_notify, &count);
303 body = webkit_dom_document_get_body(document);
305 g_object_weak_ref(G_OBJECT(body), (GWeakNotify)weak_notify, &count);
306 collection = webkit_dom_document_get_links(document);
307 g_assert(collection);
308 g_object_weak_ref(G_OBJECT(collection), (GWeakNotify)weak_notify, &count);
310 gtk_widget_destroy(fixture->webView);
311 fixture->webView = NULL;
313 g_assert_cmpuint(count, ==, 4);
315 g_object_unref(collection);
317 g_assert_cmpuint(count, ==, 5);
320 int main(int argc, char** argv)
322 if (!g_thread_supported())
325 gtk_test_init(&argc, &argv, NULL);
327 g_test_bug_base("https://bugs.webkit.org/");
329 g_test_add("/webkit/domdocument/test_title",
330 DomDocumentFixture, HTML_DOCUMENT_TITLE,
331 dom_document_fixture_setup,
332 test_dom_document_title,
333 dom_document_fixture_teardown);
335 g_test_add("/webkit/domdocument/test_get_elements_by_tag_name",
336 DomDocumentFixture, HTML_DOCUMENT_ELEMENTS,
337 dom_document_fixture_setup,
338 test_dom_document_get_elements_by_tag_name,
339 dom_document_fixture_teardown);
341 g_test_add("/webkit/domdocument/test_get_elements_by_class_name",
342 DomDocumentFixture, HTML_DOCUMENT_ELEMENTS_CLASS,
343 dom_document_fixture_setup,
344 test_dom_document_get_elements_by_class_name,
345 dom_document_fixture_teardown);
347 g_test_add("/webkit/domdocument/test_get_element_by_id",
348 DomDocumentFixture, HTML_DOCUMENT_ELEMENTS_ID,
349 dom_document_fixture_setup,
350 test_dom_document_get_element_by_id,
351 dom_document_fixture_teardown);
353 g_test_add("/webkit/domdocument/test_get_links",
354 DomDocumentFixture, HTML_DOCUMENT_LINKS,
355 dom_document_fixture_setup,
356 test_dom_document_get_links,
357 dom_document_fixture_teardown);
359 g_test_add("/webkit/domdocument/test_garbage_collection",
360 DomDocumentFixture, HTML_DOCUMENT_LINKS,
361 dom_document_fixture_setup,
362 test_dom_document_garbage_collection,
363 dom_document_fixture_teardown);
369 int main(int argc, char** argv)
371 g_critical("You will gtk-2.14.0 to run the unit tests. Doing nothing now.");