Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2009 Jan Michael Alonzo
      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 <glib.h>
     21 #include <gtk/gtk.h>
     22 #include <webkit/webkit.h>
     23 
     24 #if GTK_CHECK_VERSION(2, 14, 0)
     25 
     26 static const gshort defaultTimeout = 10;
     27 guint waitTimer;
     28 gboolean shouldWait;
     29 
     30 typedef struct {
     31     WebKitWebView* webView;
     32     WebKitWebFrame* mainFrame;
     33 } WebDataSourceFixture;
     34 
     35 static void test_webkit_web_data_source_get_initial_request()
     36 {
     37     WebKitWebView* view;
     38     WebKitWebFrame* frame;
     39     WebKitWebDataSource* dataSource;
     40     WebKitNetworkRequest* initialRequest;
     41 
     42     view = WEBKIT_WEB_VIEW(webkit_web_view_new());
     43     g_object_ref_sink(view);
     44     frame = webkit_web_view_get_main_frame(view);
     45 
     46     WebKitNetworkRequest* request = webkit_network_request_new("http://www.google.com");
     47     webkit_web_frame_load_request(frame, request);
     48     g_object_unref(request);
     49 
     50     dataSource = webkit_web_frame_get_provisional_data_source(frame);
     51     g_assert(dataSource);
     52     initialRequest = webkit_web_data_source_get_initial_request(dataSource);
     53     g_assert_cmpstr(webkit_network_request_get_uri(initialRequest), ==, "http://www.google.com/");
     54 
     55     g_object_unref(view);
     56 }
     57 
     58 static void notify_load_status_unreachable_cb(WebKitWebView* view, GParamSpec* pspec, GMainLoop* loop)
     59 {
     60     WebKitLoadStatus status = webkit_web_view_get_load_status (view);
     61     WebKitWebFrame* frame = webkit_web_view_get_main_frame(view);
     62 
     63     g_assert(status != WEBKIT_LOAD_FINISHED);
     64 
     65     if (status != WEBKIT_LOAD_FAILED)
     66         return;
     67 
     68     WebKitWebDataSource* datasource = webkit_web_frame_get_data_source(frame);
     69 
     70     g_assert_cmpstr("http://this.host.does.not.exist/doireallyexist.html", ==,
     71                     webkit_web_data_source_get_unreachable_uri(datasource));
     72 
     73     g_main_loop_quit(loop);
     74 }
     75 
     76 static void notify_load_status_cb(WebKitWebView* view, GParamSpec* pspec, GMainLoop* loop)
     77 {
     78     WebKitLoadStatus status = webkit_web_view_get_load_status (view);
     79     WebKitWebFrame* frame = webkit_web_view_get_main_frame(view);
     80     WebKitWebDataSource* dataSource = webkit_web_frame_get_data_source(frame);
     81 
     82     if (status == WEBKIT_LOAD_COMMITTED) {
     83         g_assert(webkit_web_data_source_is_loading(dataSource));
     84         return;
     85     }
     86     else if (status != WEBKIT_LOAD_FINISHED)
     87         return;
     88 
     89     /* Test get_request */
     90     g_test_message("Testing webkit_web_data_source_get_request");
     91     WebKitNetworkRequest* request = webkit_web_data_source_get_request(dataSource);
     92     g_assert_cmpstr(webkit_network_request_get_uri(request), ==, "http://www.webkit.org/");
     93 
     94     /* Test get_main_resource */
     95     g_test_message("Testing webkit_web_data_source_get_main_resource");
     96     WebKitWebResource* resource = webkit_web_data_source_get_main_resource(dataSource);
     97     g_assert_cmpstr("text/html", ==, webkit_web_resource_get_mime_type(resource));
     98     g_assert_cmpstr("http://www.webkit.org/", ==, webkit_web_resource_get_uri(resource));
     99 
    100     /* Test get_data. We just test if data has certain size for the mean time */
    101     g_test_message("Testing webkit_web_data_source_get_data has certain size");
    102     GString* data = webkit_web_data_source_get_data(dataSource);
    103     g_assert(data->len > 100);
    104 
    105     /* FIXME: Add test for get_encoding */
    106 
    107     g_main_loop_quit(loop);
    108 }
    109 
    110 static gboolean wait_timer_fired(GMainLoop* loop)
    111 {
    112     waitTimer = 0;
    113     g_main_loop_quit(loop);
    114 
    115     return FALSE;
    116 }
    117 
    118 static void test_webkit_web_data_source()
    119 {
    120     WebKitWebView* view;
    121     GMainLoop* loop;
    122 
    123     view = WEBKIT_WEB_VIEW(webkit_web_view_new());
    124     g_object_ref_sink(view);
    125     loop = g_main_loop_new(NULL, TRUE);
    126     g_signal_connect(view, "notify::load-status", G_CALLBACK(notify_load_status_cb), loop);
    127     webkit_web_view_load_uri(view, "http://www.webkit.org");
    128 
    129     waitTimer = g_timeout_add_seconds(defaultTimeout, (GSourceFunc)wait_timer_fired, loop);
    130 
    131     g_main_loop_run(loop);
    132 
    133     if (waitTimer)
    134         g_source_remove(waitTimer);
    135 
    136     waitTimer = 0;
    137 
    138     g_main_loop_unref(loop);
    139     g_object_unref(view);
    140 }
    141 
    142 static void notify_load_status_lifetime_cb(WebKitWebView* view, GParamSpec* pspec, GMainLoop* loop)
    143 {
    144     WebKitLoadStatus status = webkit_web_view_get_load_status (view);
    145     WebKitWebFrame* frame = webkit_web_view_get_main_frame(view);
    146     WebKitWebDataSource* dataSource = webkit_web_frame_get_data_source(frame);
    147 
    148     if (status == WEBKIT_LOAD_COMMITTED) {
    149         g_assert(webkit_web_data_source_is_loading(dataSource));
    150         return;
    151     } else if (status != WEBKIT_LOAD_FINISHED)
    152         return;
    153 
    154     g_main_loop_quit(loop);
    155 }
    156 
    157 static void test_webkit_web_data_source_lifetime()
    158 {
    159     WebKitWebView* view;
    160     GMainLoop* loop;
    161 
    162     view = WEBKIT_WEB_VIEW(webkit_web_view_new());
    163     g_object_ref_sink(view);
    164     loop = g_main_loop_new(NULL, TRUE);
    165     g_signal_connect(view, "notify::load-status", G_CALLBACK(notify_load_status_lifetime_cb), loop);
    166     webkit_web_view_load_uri(view, "http://www.webkit.org");
    167 
    168     waitTimer = g_timeout_add_seconds(defaultTimeout, (GSourceFunc)wait_timer_fired, loop);
    169 
    170     g_main_loop_run(loop);
    171 
    172     WebKitWebDataSource* dataSource = webkit_web_frame_get_data_source(webkit_web_view_get_main_frame(view));
    173     GList* subResources = webkit_web_data_source_get_subresources(dataSource);
    174     gint numberOfResources = g_list_length(subResources);
    175     g_list_free(subResources);
    176 
    177     g_assert_cmpint(webkit_web_view_get_load_status(view), ==, WEBKIT_LOAD_FINISHED);
    178 
    179     webkit_web_view_load_uri(view, "http://gnome.org");
    180 
    181     g_assert_cmpint(webkit_web_view_get_load_status(view), ==, WEBKIT_LOAD_PROVISIONAL);
    182 
    183     webkit_web_view_stop_loading(view);
    184 
    185     g_assert_cmpint(webkit_web_view_get_load_status(view), ==, WEBKIT_LOAD_FAILED);
    186 
    187     subResources = webkit_web_data_source_get_subresources(dataSource);
    188     g_assert_cmpint(numberOfResources, ==, g_list_length(subResources));
    189     g_list_free(subResources);
    190 
    191     if (waitTimer)
    192         g_source_remove(waitTimer);
    193 
    194     waitTimer = 0;
    195 
    196     g_main_loop_unref(loop);
    197     g_object_unref(view);
    198 }
    199 
    200 static void test_webkit_web_data_source_unreachable_uri()
    201 {
    202     /* FIXME: this test fails currently. */
    203     return;
    204 
    205     WebKitWebView* view;
    206     GMainLoop* loop;
    207 
    208     view = WEBKIT_WEB_VIEW(webkit_web_view_new());
    209     g_object_ref_sink(view);
    210     loop = g_main_loop_new(NULL, TRUE);
    211     g_signal_connect(view, "notify::load-status", G_CALLBACK(notify_load_status_unreachable_cb), loop);
    212     webkit_web_view_load_uri(view, "http://this.host.does.not.exist/doireallyexist.html");
    213 
    214     waitTimer = g_timeout_add_seconds(defaultTimeout, (GSourceFunc)wait_timer_fired, loop);
    215 
    216     g_main_loop_run(loop);
    217 
    218     if (waitTimer)
    219         g_source_remove(waitTimer);
    220 
    221     waitTimer = 0;
    222 
    223     g_main_loop_unref(loop);
    224     g_object_unref(view);
    225 }
    226 
    227 int main(int argc, char** argv)
    228 {
    229     g_thread_init(NULL);
    230     gtk_test_init(&argc, &argv, NULL);
    231 
    232     g_test_bug_base("https://bugs.webkit.org/");
    233     g_test_bug("24758");
    234     g_test_add_func("/webkit/webdatasource/get_initial_request",
    235                     test_webkit_web_data_source_get_initial_request);
    236     g_test_add_func("/webkit/webdatasource/api",
    237                     test_webkit_web_data_source);
    238     g_test_add_func("/webkit/webdatasource/unreachable_uri",
    239                     test_webkit_web_data_source_unreachable_uri);
    240     g_test_add_func("/webkit/webdatasource/lifetime",
    241                     test_webkit_web_data_source_lifetime);
    242 
    243     return g_test_run ();
    244 }
    245 
    246 #else
    247 int main(int argc, char** argv)
    248 {
    249     g_critical("You will need gtk-2.14.0 to run the unit tests. Doing nothing now.");
    250     return 0;
    251 }
    252 
    253 #endif
    254