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