2010-02-23 Arno Renevier <arno@renevier.net>
[WebKit-https.git] / WebKit / gtk / tests / testwebview.c
1 /*
2  * Copyright (C) 2008 Holger Hans Peter Freyther
3  * Copyright (C) 2009 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "test_utils.h"
22
23 #include <errno.h>
24 #include <unistd.h>
25 #include <string.h>
26
27 #include <glib.h>
28 #include <glib/gstdio.h>
29 #include <gtk/gtk.h>
30 #include <webkit/webkit.h>
31
32 #if GLIB_CHECK_VERSION(2, 16, 0) && GTK_CHECK_VERSION(2, 14, 0)
33
34 GMainLoop* loop;
35 SoupSession *session;
36 char* base_uri;
37
38 /* For real request testing */
39 static void
40 server_callback(SoupServer* server, SoupMessage* msg,
41                  const char* path, GHashTable* query,
42                  SoupClientContext* context, gpointer data)
43 {
44     if (msg->method != SOUP_METHOD_GET) {
45         soup_message_set_status(msg, SOUP_STATUS_NOT_IMPLEMENTED);
46         return;
47     }
48
49     soup_message_set_status(msg, SOUP_STATUS_OK);
50
51     if (g_str_equal(path, "/favicon.ico")) {
52         char* contents;
53         gsize length;
54         GError* error = NULL;
55
56         g_file_get_contents("blank.ico", &contents, &length, &error);
57         g_assert(!error);
58
59         soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, length);
60     } else if (g_str_equal(path, "/bigdiv.html")) {
61         char* contents = g_strdup("<html><body><div style=\"background-color: green; height: 1200px;\"></div></body></html>");
62         soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, strlen(contents));
63     } else if (g_str_equal(path, "/iframe.html")) {
64         char* contents = g_strdup("<html><body><div style=\"background-color: green; height: 50px;\"></div><iframe src=\"bigdiv.html\"></iframe></body></html>");
65         soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, strlen(contents));
66     } else {
67         char* contents = g_strdup("<html><body>test</body></html>");
68         soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, strlen(contents));
69     }
70
71     soup_message_body_complete(msg->response_body);
72 }
73
74 static void idle_quit_loop_cb(WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
75 {
76     if (webkit_web_view_get_load_status(web_view) == WEBKIT_LOAD_FINISHED ||
77         webkit_web_view_get_load_status(web_view) == WEBKIT_LOAD_FAILED)
78         g_main_loop_quit(loop);
79 }
80
81 static void icon_uri_changed_cb(WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
82 {
83     gboolean* been_here = (gboolean*)data;
84     char* expected_uri;
85
86     g_assert_cmpstr(g_param_spec_get_name(pspec), ==, "icon-uri");
87
88     expected_uri = g_strdup_printf("%sfavicon.ico", base_uri);
89     g_assert_cmpstr(webkit_web_view_get_icon_uri(web_view), ==, expected_uri);
90     g_free(expected_uri);
91
92     *been_here = TRUE;
93 }
94
95 static void icon_loaded_cb(WebKitWebView* web_view, char* icon_uri, gpointer data)
96 {
97     gboolean* been_here = (gboolean*)data;
98     char* expected_uri = g_strdup_printf("%sfavicon.ico", base_uri);
99     g_assert_cmpstr(icon_uri, ==, expected_uri);
100     g_free(expected_uri);
101
102     g_assert_cmpstr(icon_uri, ==, webkit_web_view_get_icon_uri(web_view));
103
104     *been_here = TRUE;
105 }
106
107 static void test_webkit_web_view_icon_uri()
108 {
109     gboolean been_to_uri_changed = FALSE;
110     gboolean been_to_icon_loaded = FALSE;
111     WebKitWebView* view = WEBKIT_WEB_VIEW(webkit_web_view_new());
112     g_object_ref_sink(G_OBJECT(view));
113
114     loop = g_main_loop_new(NULL, TRUE);
115
116     g_object_connect(G_OBJECT(view),
117                      "signal::notify::progress", idle_quit_loop_cb, NULL,
118                      "signal::notify::icon-uri", icon_uri_changed_cb, &been_to_uri_changed,
119                      "signal::icon-loaded", icon_loaded_cb, &been_to_icon_loaded,
120                      NULL);
121
122     webkit_web_view_load_uri(view, base_uri);
123
124     g_main_loop_run(loop);
125
126     g_assert(been_to_uri_changed);
127     g_assert(been_to_icon_loaded);
128
129     g_object_unref(view);
130 }
131
132 static gboolean map_event_cb(GtkWidget *widget, GdkEvent* event, gpointer data)
133 {
134     GMainLoop* loop = (GMainLoop*)data;
135     g_main_loop_quit(loop);
136
137     return FALSE;
138 }
139
140 static void do_test_webkit_web_view_adjustments(gboolean with_page_cache)
141 {
142     char* effective_uri = g_strconcat(base_uri, "bigdiv.html", NULL);
143     char* second_uri = g_strconcat(base_uri, "iframe.html", NULL);
144     GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
145     GtkWidget* scrolled_window = gtk_scrolled_window_new(NULL, NULL);
146     WebKitWebView* view = WEBKIT_WEB_VIEW(webkit_web_view_new());
147     GtkAdjustment* adjustment;
148     double lower;
149     double upper;
150
151     if (with_page_cache) {
152         WebKitWebSettings* settings = webkit_web_view_get_settings(view);
153         g_object_set(settings, "enable-page-cache", TRUE, NULL);
154     }
155
156     gtk_window_set_default_size(GTK_WINDOW(window), 400, 200);
157
158     gtk_container_add(GTK_CONTAINER(window), scrolled_window);
159     gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(view));
160
161     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
162                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
163
164     loop = g_main_loop_new(NULL, TRUE);
165
166     g_object_connect(G_OBJECT(view),
167                      "signal::notify::progress", idle_quit_loop_cb, NULL,
168                      NULL);
169
170     /* Wait for window to show up */
171     gtk_widget_show_all(window);
172     g_signal_connect(window, "map-event",
173                      G_CALLBACK(map_event_cb), loop);
174     g_main_loop_run(loop);
175
176     /* Load a page with a big div that will cause scrollbars to appear */
177     webkit_web_view_load_uri(view, effective_uri);
178     g_main_loop_run(loop);
179
180     adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrolled_window));
181     g_assert_cmpfloat(gtk_adjustment_get_value(adjustment), ==, 0.0);
182
183     lower = gtk_adjustment_get_lower(adjustment);
184     upper = gtk_adjustment_get_upper(adjustment);
185
186     /* Scroll the view using JavaScript */
187     webkit_web_view_execute_script(view, "window.scrollBy(0, 100)");
188
189     /* Make sure the ScrolledWindow noticed the scroll */
190     g_assert_cmpfloat(gtk_adjustment_get_value(adjustment), ==, 100.0);
191
192     /* Load a second URI */
193     webkit_web_view_load_uri(view, second_uri);
194     g_main_loop_run(loop);
195
196     /* Make sure the scrollbar has been reset */
197     g_assert_cmpfloat(gtk_adjustment_get_value(adjustment), ==, 0.0);
198
199     /* Go back */
200     webkit_web_view_go_back(view);
201
202     /* When using page cache, go_back will return syncronously */
203     if (!with_page_cache)
204         g_main_loop_run(loop);
205
206     /* Make sure GTK+ has time to process the changes in size, for the adjusments */
207     while (gtk_events_pending())
208         gtk_main_iteration();
209
210     /* Make sure upper and lower bounds have been restored correctly */
211     g_assert_cmpfloat(lower, ==, gtk_adjustment_get_lower(adjustment));
212     g_assert_cmpfloat(upper, ==, gtk_adjustment_get_upper(adjustment));
213
214     g_assert_cmpfloat(gtk_adjustment_get_value(adjustment), ==, 100.0);
215
216     g_free(effective_uri);
217     g_free(second_uri);
218
219     gtk_widget_destroy(window);
220 }
221
222 static void test_webkit_web_view_adjustments()
223 {
224     /* Test this with page cache disabled, and enabled. */
225     do_test_webkit_web_view_adjustments(FALSE);
226     do_test_webkit_web_view_adjustments(TRUE);
227 }
228
229 int main(int argc, char** argv)
230 {
231     SoupServer* server;
232     SoupURI* soup_uri;
233
234     g_thread_init(NULL);
235     gtk_test_init(&argc, &argv, NULL);
236
237     /* Hopefully make test independent of the path it's called from. */
238     testutils_relative_chdir("WebKit/gtk/tests/resources/test.html", argv[0]);
239
240     server = soup_server_new(SOUP_SERVER_PORT, 0, NULL);
241     soup_server_run_async(server);
242
243     soup_server_add_handler(server, NULL, server_callback, NULL, NULL);
244
245     soup_uri = soup_uri_new("http://127.0.0.1/");
246     soup_uri_set_port(soup_uri, soup_server_get_port(server));
247
248     base_uri = soup_uri_to_string(soup_uri, FALSE);
249     soup_uri_free(soup_uri);
250
251     g_test_bug_base("https://bugs.webkit.org/");
252     g_test_add_func("/webkit/webview/icon-uri", test_webkit_web_view_icon_uri);
253     g_test_add_func("/webkit/webview/adjustments", test_webkit_web_view_adjustments);
254
255     return g_test_run ();
256 }
257
258 #else
259 int main(int argc, char** argv)
260 {
261     g_critical("You will need at least glib-2.16.0 and gtk-2.14.0 to run the unit tests. Doing nothing now.");
262     return 0;
263 }
264
265 #endif