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