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 GLIB_CHECK_VERSION(2, 16, 0) && GTK_CHECK_VERSION(2, 14, 0)
25
26static const gshort defaultTimeout = 10;
27guint waitTimer;
28gboolean shouldWait;
29
30typedef struct {
31    WebKitWebView* webView;
32    WebKitWebFrame* mainFrame;
33} WebDataSourceFixture;
34
35static 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
58static 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    if (status != WEBKIT_LOAD_FINISHED)
64        return;
65
66    if (waitTimer) {
67        g_source_remove(waitTimer);
68        waitTimer = 0;
69    }
70
71    WebKitWebDataSource* datasource = webkit_web_frame_get_data_source(frame);
72
73    g_assert_cmpstr("http://this.host.does.not.exist/doireallyexist.html", ==,
74                    webkit_web_data_source_get_unreachable_uri(datasource));
75
76    g_main_loop_quit(loop);
77}
78
79static void notify_load_status_cb(WebKitWebView* view, GParamSpec* pspec, GMainLoop* loop)
80{
81    WebKitLoadStatus status = webkit_web_view_get_load_status (view);
82    WebKitWebFrame* frame = webkit_web_view_get_main_frame(view);
83    WebKitWebDataSource* dataSource = webkit_web_frame_get_data_source(frame);
84
85    if (status == WEBKIT_LOAD_COMMITTED) {
86        g_assert(webkit_web_data_source_is_loading(dataSource));
87        return;
88    }
89    else if (status != WEBKIT_LOAD_FINISHED)
90        return;
91
92    if (waitTimer) {
93        g_source_remove(waitTimer);
94        waitTimer = 0;
95    }
96
97    /* Test get_request */
98    g_test_message("Testing webkit_web_data_source_get_request");
99    WebKitNetworkRequest* request = webkit_web_data_source_get_request(dataSource);
100    g_assert_cmpstr(webkit_network_request_get_uri(request), ==, "http://webkit.org/");
101
102    /* Test get_main_resource */
103    g_test_message("Testing webkit_web_data_source_get_main_resource");
104    WebKitWebResource* resource = webkit_web_data_source_get_main_resource(dataSource);
105    g_assert_cmpstr("text/html", ==, webkit_web_resource_get_mime_type(resource));
106    g_assert_cmpstr("http://webkit.org/", ==, webkit_web_resource_get_uri(resource));
107
108    /* Test get_data. We just test if data has certain size for the mean time */
109    g_test_message("Testing webkit_web_data_source_get_data has certain size");
110    GString* data = webkit_web_data_source_get_data(dataSource);
111    g_assert(data->len > 100);
112
113    /* FIXME: Add test for get_encoding */
114
115    g_main_loop_quit(loop);
116}
117
118static gboolean wait_timer_fired(GMainLoop* loop)
119{
120    waitTimer = 0;
121    g_main_loop_quit(loop);
122
123    return FALSE;
124}
125
126static void test_webkit_web_data_source()
127{
128    WebKitWebView* view;
129    GMainLoop* loop;
130
131    view = WEBKIT_WEB_VIEW(webkit_web_view_new());
132    g_object_ref_sink(view);
133    loop = g_main_loop_new(NULL, TRUE);
134    g_signal_connect(view, "notify::load-status", G_CALLBACK(notify_load_status_cb), loop);
135    webkit_web_view_load_uri(view, "http://webkit.org");
136
137    if (!waitTimer)
138        waitTimer = g_timeout_add_seconds(defaultTimeout, (GSourceFunc)wait_timer_fired, loop);
139
140    g_main_loop_run(loop);
141    g_object_unref(view);
142}
143
144static void test_webkit_web_data_source_unreachable_uri()
145{
146    WebKitWebView* view;
147    GMainLoop* loop;
148
149    view = WEBKIT_WEB_VIEW(webkit_web_view_new());
150    g_object_ref_sink(view);
151    loop = g_main_loop_new(NULL, TRUE);
152    g_signal_connect(view, "notify::load-status", G_CALLBACK(notify_load_status_unreachable_cb), loop);
153    webkit_web_view_load_uri(view, "http://this.host.does.not.exist/doireallyexist.html");
154
155    if (!waitTimer)
156        waitTimer = g_timeout_add_seconds(defaultTimeout, (GSourceFunc)wait_timer_fired, loop);
157
158    g_main_loop_run(loop);
159    g_object_unref(view);
160}
161
162int main(int argc, char** argv)
163{
164    g_thread_init(NULL);
165    gtk_test_init(&argc, &argv, NULL);
166
167    g_test_bug_base("https://bugs.webkit.org/");
168    g_test_bug("24758");
169    g_test_add_func("/webkit/webdatasource/get_initial_request",
170                    test_webkit_web_data_source_get_initial_request);
171    g_test_add_func("/webkit/webdatasource/api",
172                    test_webkit_web_data_source);
173    g_test_add_func("/webkit/webdatasource/unreachable_uri",
174                    test_webkit_web_data_source_unreachable_uri);
175    return g_test_run ();
176}
177
178#else
179int main(int argc, char** argv)
180{
181    g_critical("You will need at least glib-2.16.0 and gtk-2.14.0 to run the unit tests. Doing nothing now.");
182    return 0;
183}
184
185#endif
186