2 * Copyright (C) 2009 Jan Michael Alonzo
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.
22 #include <webkit/webkit.h>
24 #if GTK_CHECK_VERSION(2, 14, 0)
26 static const gshort defaultTimeout = 10;
31 WebKitWebView* webView;
32 WebKitWebFrame* mainFrame;
33 } WebDataSourceFixture;
35 static void test_webkit_web_data_source_get_initial_request()
38 WebKitWebFrame* frame;
39 WebKitWebDataSource* dataSource;
40 WebKitNetworkRequest* initialRequest;
42 view = WEBKIT_WEB_VIEW(webkit_web_view_new());
43 g_object_ref_sink(view);
44 frame = webkit_web_view_get_main_frame(view);
46 WebKitNetworkRequest* request = webkit_network_request_new("http://www.google.com");
47 webkit_web_frame_load_request(frame, request);
48 g_object_unref(request);
50 dataSource = webkit_web_frame_get_provisional_data_source(frame);
52 initialRequest = webkit_web_data_source_get_initial_request(dataSource);
53 g_assert_cmpstr(webkit_network_request_get_uri(initialRequest), ==, "http://www.google.com/");
58 static void notify_load_status_unreachable_cb(WebKitWebView* view, GParamSpec* pspec, GMainLoop* loop)
60 WebKitLoadStatus status = webkit_web_view_get_load_status (view);
61 WebKitWebFrame* frame = webkit_web_view_get_main_frame(view);
63 g_assert(status != WEBKIT_LOAD_FINISHED);
65 if (status != WEBKIT_LOAD_FAILED)
68 WebKitWebDataSource* datasource = webkit_web_frame_get_data_source(frame);
70 g_assert_cmpstr("http://this.host.does.not.exist/doireallyexist.html", ==,
71 webkit_web_data_source_get_unreachable_uri(datasource));
73 g_main_loop_quit(loop);
76 static void notify_load_status_cb(WebKitWebView* view, GParamSpec* pspec, GMainLoop* loop)
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);
82 if (status == WEBKIT_LOAD_COMMITTED) {
83 g_assert(webkit_web_data_source_is_loading(dataSource));
86 else if (status != WEBKIT_LOAD_FINISHED)
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/");
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));
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);
105 /* FIXME: Add test for get_encoding */
107 g_main_loop_quit(loop);
110 static gboolean wait_timer_fired(GMainLoop* loop)
113 g_main_loop_quit(loop);
118 static void test_webkit_web_data_source()
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");
129 waitTimer = g_timeout_add_seconds(defaultTimeout, (GSourceFunc)wait_timer_fired, loop);
131 g_main_loop_run(loop);
134 g_source_remove(waitTimer);
138 g_main_loop_unref(loop);
139 g_object_unref(view);
142 static void notify_load_status_lifetime_cb(WebKitWebView* view, GParamSpec* pspec, GMainLoop* loop)
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);
148 if (status == WEBKIT_LOAD_COMMITTED) {
149 g_assert(webkit_web_data_source_is_loading(dataSource));
151 } else if (status != WEBKIT_LOAD_FINISHED)
154 g_main_loop_quit(loop);
157 static void test_webkit_web_data_source_lifetime()
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");
168 waitTimer = g_timeout_add_seconds(defaultTimeout, (GSourceFunc)wait_timer_fired, loop);
170 g_main_loop_run(loop);
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);
177 g_assert_cmpint(webkit_web_view_get_load_status(view), ==, WEBKIT_LOAD_FINISHED);
179 webkit_web_view_load_uri(view, "http://gnome.org");
181 g_assert_cmpint(webkit_web_view_get_load_status(view), ==, WEBKIT_LOAD_PROVISIONAL);
183 webkit_web_view_stop_loading(view);
185 g_assert_cmpint(webkit_web_view_get_load_status(view), ==, WEBKIT_LOAD_FAILED);
187 subResources = webkit_web_data_source_get_subresources(dataSource);
188 g_assert_cmpint(numberOfResources, ==, g_list_length(subResources));
189 g_list_free(subResources);
192 g_source_remove(waitTimer);
196 g_main_loop_unref(loop);
197 g_object_unref(view);
200 static void test_webkit_web_data_source_unreachable_uri()
202 /* FIXME: this test fails currently. */
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");
214 waitTimer = g_timeout_add_seconds(defaultTimeout, (GSourceFunc)wait_timer_fired, loop);
216 g_main_loop_run(loop);
219 g_source_remove(waitTimer);
223 g_main_loop_unref(loop);
224 g_object_unref(view);
227 int main(int argc, char** argv)
230 gtk_test_init(&argc, &argv, NULL);
232 g_test_bug_base("https://bugs.webkit.org/");
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);
243 return g_test_run ();
247 int main(int argc, char** argv)
249 g_critical("You will need gtk-2.14.0 to run the unit tests. Doing nothing now.");