1/*
2 * Copyright (C) 2010 Igalia S.L.
3 *
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.
8 *
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.
13 *
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.
18 */
19
20#include "autotoolsconfig.h"
21#include "test_utils.h"
22
23#include <glib.h>
24#include <glib/gstdio.h>
25#include <gtk/gtk.h>
26#include <webkit/webkit.h>
27
28#define HTML_DOCUMENT_HIERARCHY_NAVIGATION "<html><head><title>This is the title</title></head><body><p>1</p><p>2</p><p>3</p></body></html>"
29#define HTML_DOCUMENT_NODE_INSERTION "<html><body></body></html>"
30
31typedef struct {
32    GtkWidget* webView;
33    GMainLoop* loop;
34} DomNodeFixture;
35
36static gboolean finish_loading(DomNodeFixture* fixture)
37{
38    if (g_main_loop_is_running(fixture->loop))
39        g_main_loop_quit(fixture->loop);
40
41    return FALSE;
42}
43
44static void dom_node_fixture_setup(DomNodeFixture* fixture, gconstpointer data)
45{
46    fixture->loop = g_main_loop_new(NULL, TRUE);
47    fixture->webView = webkit_web_view_new();
48    g_object_ref_sink(fixture->webView);
49
50    if (data != NULL)
51        webkit_web_view_load_string(WEBKIT_WEB_VIEW(fixture->webView), (const char*)data, NULL, NULL, NULL);
52
53    g_idle_add((GSourceFunc)finish_loading, fixture);
54    g_main_loop_run(fixture->loop);
55}
56
57static void dom_node_fixture_teardown(DomNodeFixture* fixture, gconstpointer data)
58{
59    g_object_unref(fixture->webView);
60    g_main_loop_unref(fixture->loop);
61}
62
63static void test_dom_node_hierarchy_navigation(DomNodeFixture* fixture, gconstpointer data)
64{
65    WebKitDOMDocument* document;
66    WebKitDOMHTMLHeadElement* head;
67    WebKitDOMHTMLBodyElement* body;
68    WebKitDOMNodeList* list;
69    WebKitDOMNode* ptr;
70    gulong i, length;
71
72    document = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(fixture->webView));
73    g_assert(document);
74    g_assert(WEBKIT_DOM_IS_DOCUMENT(document));
75    head = webkit_dom_document_get_head(document);
76    g_assert(head);
77    g_assert(WEBKIT_DOM_IS_HTML_HEAD_ELEMENT(head));
78
79    /* Title, head's child */
80    g_assert(webkit_dom_node_has_child_nodes(WEBKIT_DOM_NODE(head)));
81    list = webkit_dom_node_get_child_nodes(WEBKIT_DOM_NODE(head));
82    g_assert_cmpint(webkit_dom_node_list_get_length(list), ==, 1);
83    ptr = webkit_dom_node_list_item(list, 0);
84    g_assert(ptr);
85    g_assert(WEBKIT_DOM_IS_HTML_TITLE_ELEMENT(ptr));
86    g_object_unref(list);
87
88    /* Body, Head sibling */
89    ptr = webkit_dom_node_get_next_sibling(WEBKIT_DOM_NODE(head));
90    g_assert(ptr);
91    body = WEBKIT_DOM_HTML_BODY_ELEMENT(ptr);
92    g_assert(WEBKIT_DOM_IS_HTML_BODY_ELEMENT(body));
93
94    /* There is no third sibling */
95    ptr = webkit_dom_node_get_next_sibling(ptr);
96    g_assert(ptr == NULL);
97
98    /* Body's previous sibling is Head */
99    ptr = webkit_dom_node_get_previous_sibling(WEBKIT_DOM_NODE(body));
100    g_assert(ptr);
101    g_assert(WEBKIT_DOM_IS_HTML_HEAD_ELEMENT(ptr));
102
103    /* Body has 3 children */
104    g_assert(webkit_dom_node_has_child_nodes(WEBKIT_DOM_NODE(body)));
105    list = webkit_dom_node_get_child_nodes(WEBKIT_DOM_NODE(body));
106    length = webkit_dom_node_list_get_length(list);
107    g_assert_cmpint(length, ==, 3);
108
109    /* The three of them are P tags */
110    for (i = 0; i < length; i++) {
111        ptr = webkit_dom_node_list_item(list, i);
112        g_assert(ptr);
113        g_assert(WEBKIT_DOM_IS_HTML_PARAGRAPH_ELEMENT(ptr));
114    }
115
116    /* Go backwards */
117    for (i = 0; ptr; ptr = webkit_dom_node_get_previous_sibling(ptr), i++)
118        /* Nothing */;
119
120    g_assert_cmpint(i, ==, 3);
121    g_object_unref(list);
122}
123
124static void test_dom_node_insertion(DomNodeFixture* fixture, gconstpointer data)
125{
126    WebKitDOMDocument* document;
127    WebKitDOMHTMLElement* body;
128    WebKitDOMElement* p, *div;
129    WebKitDOMNodeList* list;
130    WebKitDOMNode* node;
131
132    document = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(fixture->webView));
133    g_assert(document);
134    body = webkit_dom_document_get_body(document);
135    g_assert(body);
136    g_assert(WEBKIT_DOM_IS_HTML_ELEMENT(body));
137
138    /* Body shouldn't have any children at this point */
139    g_assert(webkit_dom_node_has_child_nodes(WEBKIT_DOM_NODE(body)) == FALSE);
140
141    /* Insert one P element */
142    p = webkit_dom_document_create_element(document, "P", NULL);
143    webkit_dom_node_append_child(WEBKIT_DOM_NODE(body), WEBKIT_DOM_NODE(p), NULL);
144
145    /* Now it should have one, the same that we inserted */
146    g_assert(webkit_dom_node_has_child_nodes(WEBKIT_DOM_NODE(body)));
147    list = webkit_dom_node_get_child_nodes(WEBKIT_DOM_NODE(body));
148    g_assert_cmpint(webkit_dom_node_list_get_length(list), ==, 1);
149    node = webkit_dom_node_list_item(list, 0);
150    g_assert(node);
151    g_assert(webkit_dom_node_is_same_node(WEBKIT_DOM_NODE(p), node));
152    g_object_unref(list);
153
154    /* Replace the P tag with a DIV tag */
155    div = webkit_dom_document_create_element(document, "DIV", NULL);
156    webkit_dom_node_replace_child(WEBKIT_DOM_NODE(body), WEBKIT_DOM_NODE(div), WEBKIT_DOM_NODE(p), NULL);
157    g_assert(webkit_dom_node_has_child_nodes(WEBKIT_DOM_NODE(body)));
158    list = webkit_dom_node_get_child_nodes(WEBKIT_DOM_NODE(body));
159    g_assert_cmpint(webkit_dom_node_list_get_length(list), ==, 1);
160    node = webkit_dom_node_list_item(list, 0);
161    g_assert(node);
162    g_assert(webkit_dom_node_is_same_node(WEBKIT_DOM_NODE(div), node));
163    g_object_unref(list);
164
165    /* Now remove the tag */
166    webkit_dom_node_remove_child(WEBKIT_DOM_NODE(body), node, NULL);
167    list = webkit_dom_node_get_child_nodes(WEBKIT_DOM_NODE(body));
168    g_assert_cmpint(webkit_dom_node_list_get_length(list), ==, 0);
169    g_object_unref(list);
170
171    /* Test insert_before */
172
173    /* If refChild is null, insert newChild as last element of parent */
174    div = webkit_dom_document_create_element(document, "DIV", NULL);
175    webkit_dom_node_insert_before(WEBKIT_DOM_NODE(body), WEBKIT_DOM_NODE(div), NULL, NULL);
176    g_assert(webkit_dom_node_has_child_nodes(WEBKIT_DOM_NODE(body)));
177    list = webkit_dom_node_get_child_nodes(WEBKIT_DOM_NODE(body));
178    g_assert_cmpint(webkit_dom_node_list_get_length(list), ==, 1);
179    node = webkit_dom_node_list_item(list, 0);
180    g_assert(node);
181    g_assert(webkit_dom_node_is_same_node(WEBKIT_DOM_NODE(div), node));
182    g_object_unref(list);
183
184    /* Now insert a 'p' before 'div' */
185    p = webkit_dom_document_create_element(document, "P", NULL);
186    webkit_dom_node_insert_before(WEBKIT_DOM_NODE(body), WEBKIT_DOM_NODE(p), WEBKIT_DOM_NODE(div), NULL);
187    g_assert(webkit_dom_node_has_child_nodes(WEBKIT_DOM_NODE(body)));
188    list = webkit_dom_node_get_child_nodes(WEBKIT_DOM_NODE(body));
189    g_assert_cmpint(webkit_dom_node_list_get_length(list), ==, 2);
190    node = webkit_dom_node_list_item(list, 0);
191    g_assert(node);
192    g_assert(webkit_dom_node_is_same_node(WEBKIT_DOM_NODE(p), node));
193    node = webkit_dom_node_list_item(list, 1);
194    g_assert(node);
195    g_assert(webkit_dom_node_is_same_node(WEBKIT_DOM_NODE(div), node));
196    g_object_unref(list);
197}
198
199int main(int argc, char** argv)
200{
201    gtk_test_init(&argc, &argv, NULL);
202
203    g_test_bug_base("https://bugs.webkit.org/");
204
205    g_test_add("/webkit/domnode/test_hierarchy_navigation",
206               DomNodeFixture, HTML_DOCUMENT_HIERARCHY_NAVIGATION,
207               dom_node_fixture_setup,
208               test_dom_node_hierarchy_navigation,
209               dom_node_fixture_teardown);
210
211    g_test_add("/webkit/domnode/test_insertion",
212               DomNodeFixture, HTML_DOCUMENT_NODE_INSERTION,
213               dom_node_fixture_setup,
214               test_dom_node_insertion,
215               dom_node_fixture_teardown);
216
217    return g_test_run();
218}
219
220