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