2 * Copyright (C) 2008 Holger Hans Peter Freyther
3 * Copyright (C) 2009, 2010 Collabora Ltd.
4 * Copyright (C) 2012 Igalia S.L.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #include "test_utils.h"
29 #include <glib/gstdio.h>
31 #include <webkit/webkit.h>
33 #if GTK_CHECK_VERSION(2, 14, 0)
39 /* For real request testing */
41 server_callback(SoupServer* server, SoupMessage* msg,
42 const char* path, GHashTable* query,
43 SoupClientContext* context, gpointer data)
45 if (msg->method != SOUP_METHOD_GET) {
46 soup_message_set_status(msg, SOUP_STATUS_NOT_IMPLEMENTED);
50 soup_message_set_status(msg, SOUP_STATUS_OK);
52 if (g_str_equal(path, "/favicon.ico")) {
57 g_file_get_contents("blank.ico", &contents, &length, &error);
60 soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, length);
61 } else if (g_str_equal(path, "/bigdiv.html")) {
62 char* contents = g_strdup("<html><body><a id=\"link\" href=\"http://abc.def\">test</a><div style=\"background-color: green; height: 1200px;\"></div></body></html>");
63 soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, strlen(contents));
64 } else if (g_str_equal(path, "/iframe.html")) {
65 char* contents = g_strdup("<html><body id=\"some-content\"><div style=\"background-color: green; height: 50px;\"></div><iframe src=\"bigdiv.html\"></iframe></body></html>");
66 soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, strlen(contents));
68 char* contents = g_strdup("<html><body>test</body></html>");
69 soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, strlen(contents));
72 soup_message_body_complete(msg->response_body);
75 static void idle_quit_loop_cb(WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
77 if (webkit_web_view_get_load_status(web_view) == WEBKIT_LOAD_FINISHED ||
78 webkit_web_view_get_load_status(web_view) == WEBKIT_LOAD_FAILED)
79 g_main_loop_quit(loop);
82 static gboolean timeout_cb(gpointer data)
84 g_error("Didn't get icon-uri before timing out.");
88 static void icon_uri_changed_cb(WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
92 g_assert_cmpstr(g_param_spec_get_name(pspec), ==, "icon-uri");
94 expected_uri = g_strdup_printf("%sfavicon.ico", base_uri);
95 g_assert_cmpstr(webkit_web_view_get_icon_uri(web_view), ==, expected_uri);
98 g_main_loop_quit(loop);
101 static void icon_loaded_cb(WebKitWebView* web_view, char* icon_uri, gpointer data)
103 gboolean* been_here = (gboolean*)data;
104 char* expected_uri = g_strdup_printf("%sfavicon.ico", base_uri);
105 g_assert_cmpstr(icon_uri, ==, expected_uri);
106 g_free(expected_uri);
108 g_assert_cmpstr(icon_uri, ==, webkit_web_view_get_icon_uri(web_view));
113 static void test_webkit_web_view_icon_uri()
115 gboolean been_to_icon_loaded = FALSE;
116 WebKitWebView* view = WEBKIT_WEB_VIEW(webkit_web_view_new());
117 g_object_ref_sink(G_OBJECT(view));
119 loop = g_main_loop_new(NULL, TRUE);
121 g_object_connect(G_OBJECT(view),
122 "signal::notify::icon-uri", icon_uri_changed_cb, NULL,
123 "signal::icon-loaded", icon_loaded_cb, &been_to_icon_loaded,
126 webkit_web_view_load_uri(view, base_uri);
128 guint timeout_id = g_timeout_add(500, timeout_cb, 0);
130 g_main_loop_run(loop);
132 g_source_remove(timeout_id);
134 g_assert(been_to_icon_loaded);
136 g_object_unref(view);
139 static gboolean map_event_cb(GtkWidget *widget, GdkEvent* event, gpointer data)
141 GMainLoop* loop = (GMainLoop*)data;
142 g_main_loop_quit(loop);
147 static gboolean quit_after_short_delay_cb(gpointer data)
149 g_main_loop_quit((GMainLoop*)data);
153 static void test_webkit_web_view_grab_focus()
155 char* uri = g_strconcat(base_uri, "iframe.html", NULL);
156 GtkWidget* window = gtk_window_new(GTK_WINDOW_POPUP);
157 GtkWidget* scrolled_window = gtk_scrolled_window_new(NULL, NULL);
158 WebKitWebView* view = WEBKIT_WEB_VIEW(webkit_web_view_new());
159 GtkAdjustment* adjustment;
161 gtk_window_set_default_size(GTK_WINDOW(window), 400, 200);
163 gtk_container_add(GTK_CONTAINER(window), scrolled_window);
164 gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(view));
166 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
167 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
169 loop = g_main_loop_new(NULL, TRUE);
171 g_signal_connect(view, "notify::load-status", G_CALLBACK(idle_quit_loop_cb), NULL);
173 /* Wait for window to show up */
174 gtk_widget_show_all(window);
175 g_signal_connect(window, "map-event",
176 G_CALLBACK(map_event_cb), loop);
177 g_main_loop_run(loop);
179 /* Load a page with a big div that will cause scrollbars to appear */
180 webkit_web_view_load_uri(view, uri);
181 g_main_loop_run(loop);
183 adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrolled_window));
184 g_assert_cmpfloat(gtk_adjustment_get_value(adjustment), ==, 0.0);
186 /* Since webkit_web_view_execute_script does not return a value,
187 it is impossible to know if an inner document has focus after
188 a node of it was focused via .focus() method.
189 The code below is an workaround: if the node has focus, a scroll
190 action is performed and afterward it is checked if the adjustment
191 has to be different from 0.
193 char script[] = "var innerDoc = document.defaultView.frames[0].document; \
194 innerDoc.getElementById(\"link\").focus(); \
195 if (innerDoc.hasFocus()) \
196 window.scrollBy(0, 100);";
198 /* Focus an element using JavaScript */
199 webkit_web_view_execute_script(view, script);
201 /* Adjustments update asynchronously, so we must wait a bit. */
202 g_timeout_add(100, quit_after_short_delay_cb, loop);
203 g_main_loop_run(loop);
205 /* Make sure the ScrolledWindow noticed the scroll */
206 g_assert_cmpfloat(gtk_adjustment_get_value(adjustment), !=, 0.0);
209 gtk_widget_destroy(window);
212 static void do_test_webkit_web_view_adjustments(gboolean with_page_cache)
214 char* effective_uri = g_strconcat(base_uri, "bigdiv.html", NULL);
215 char* second_uri = g_strconcat(base_uri, "iframe.html", NULL);
216 GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
217 GtkWidget* scrolled_window = gtk_scrolled_window_new(NULL, NULL);
218 WebKitWebView* view = WEBKIT_WEB_VIEW(webkit_web_view_new());
219 GtkAdjustment* adjustment;
223 if (with_page_cache) {
224 WebKitWebSettings* settings = webkit_web_view_get_settings(view);
225 g_object_set(settings, "enable-page-cache", TRUE, NULL);
228 gtk_window_set_default_size(GTK_WINDOW(window), 400, 200);
230 gtk_container_add(GTK_CONTAINER(window), scrolled_window);
231 gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(view));
233 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
234 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
236 loop = g_main_loop_new(NULL, TRUE);
238 g_object_connect(G_OBJECT(view),
239 "signal::notify::load-status", idle_quit_loop_cb, NULL,
242 /* Wait for window to show up */
243 gtk_widget_show_all(window);
244 g_signal_connect(window, "map-event",
245 G_CALLBACK(map_event_cb), loop);
246 g_main_loop_run(loop);
248 /* Load a page with a big div that will cause scrollbars to appear */
249 webkit_web_view_load_uri(view, effective_uri);
250 g_main_loop_run(loop);
252 /* Adjustments update asynchronously, so we must wait a bit. */
253 g_timeout_add(100, quit_after_short_delay_cb, loop);
254 g_main_loop_run(loop);
256 adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrolled_window));
257 g_assert_cmpfloat(gtk_adjustment_get_value(adjustment), ==, 0.0);
259 lower = gtk_adjustment_get_lower(adjustment);
260 upper = gtk_adjustment_get_upper(adjustment);
262 /* Scroll the view using JavaScript */
263 webkit_web_view_execute_script(view, "window.scrollBy(0, 100)");
265 /* Adjustments update asynchronously, so we must wait a bit. */
266 g_timeout_add(100, quit_after_short_delay_cb, loop);
267 g_main_loop_run(loop);
269 /* Make sure the ScrolledWindow noticed the scroll */
270 g_assert_cmpfloat(gtk_adjustment_get_value(adjustment), ==, 100.0);
272 /* Load a second URI */
273 webkit_web_view_load_uri(view, second_uri);
274 g_main_loop_run(loop);
276 /* The page loaded but the adjustments may not be updated yet. Wait a bit. */
277 g_timeout_add(100, quit_after_short_delay_cb, loop);
278 g_main_loop_run(loop);
280 /* Make sure the scrollbar has been reset */
281 g_assert_cmpfloat(gtk_adjustment_get_value(adjustment), ==, 0.0);
284 webkit_web_view_go_back(view);
286 /* When using page cache, go_back will return syncronously */
287 if (!with_page_cache)
288 g_main_loop_run(loop);
290 /* Make sure GTK+ has time to process the changes in size, for the adjusments */
291 while (gtk_events_pending())
292 gtk_main_iteration();
294 /* Make sure upper and lower bounds have been restored correctly */
295 g_assert_cmpfloat(lower, ==, gtk_adjustment_get_lower(adjustment));
296 g_assert_cmpfloat(upper, ==, gtk_adjustment_get_upper(adjustment));
297 g_assert_cmpfloat(gtk_adjustment_get_value(adjustment), ==, 100.0);
299 g_free(effective_uri);
302 gtk_widget_destroy(window);
305 static void test_webkit_web_view_adjustments()
307 /* Test this with page cache disabled, and enabled. */
308 do_test_webkit_web_view_adjustments(FALSE);
309 do_test_webkit_web_view_adjustments(TRUE);
312 gboolean delayed_destroy(gpointer data)
314 gtk_widget_destroy(GTK_WIDGET(data));
315 g_main_loop_quit(loop);
319 static void test_webkit_web_view_destroy()
324 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
325 web_view = webkit_web_view_new();
327 gtk_container_add(GTK_CONTAINER(window), web_view);
329 gtk_widget_show_all(window);
331 loop = g_main_loop_new(NULL, TRUE);
333 g_signal_connect(window, "map-event",
334 G_CALLBACK(map_event_cb), loop);
335 g_main_loop_run(loop);
337 g_idle_add(delayed_destroy, web_view);
338 g_main_loop_run(loop);
340 gtk_widget_destroy(window);
343 static void test_webkit_web_view_window_features()
348 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
349 web_view = webkit_web_view_new();
351 gtk_container_add(GTK_CONTAINER(window), web_view);
353 gtk_widget_show_all(window);
355 loop = g_main_loop_new(NULL, TRUE);
357 g_signal_connect(window, "map-event",
358 G_CALLBACK(map_event_cb), loop);
359 g_main_loop_run(loop);
362 g_object_set(G_OBJECT(web_view), "window-features", NULL, NULL);
364 gtk_widget_destroy(window);
367 static void test_webkit_web_view_in_offscreen_window_does_not_crash()
369 loop = g_main_loop_new(NULL, TRUE);
371 GtkWidget *window = gtk_offscreen_window_new();
372 GtkWidget *web_view = webkit_web_view_new();
374 gtk_container_add(GTK_CONTAINER(window), web_view);
375 gtk_widget_show_all(window);
376 g_signal_connect(web_view, "notify::load-status", G_CALLBACK(idle_quit_loop_cb), NULL);
377 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(web_view), base_uri);
379 g_main_loop_run(loop);
381 gtk_widget_destroy(window);
382 g_main_loop_unref(loop);
385 static void test_webkit_web_view_does_not_steal_focus()
387 loop = g_main_loop_new(NULL, TRUE);
389 GtkWidget *window = gtk_offscreen_window_new();
390 GtkWidget *webView = webkit_web_view_new();
391 GtkWidget *entry = gtk_entry_new();
392 GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
394 gtk_container_add(GTK_CONTAINER(box), webView);
395 gtk_container_add(GTK_CONTAINER(box), entry);
396 gtk_container_add(GTK_CONTAINER(window), box);
397 gtk_widget_show_all(window);
399 gtk_widget_grab_focus(entry);
400 g_assert(gtk_widget_is_focus(entry));
402 g_signal_connect(webView, "notify::load-status", G_CALLBACK(idle_quit_loop_cb), NULL);
403 webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(webView),
405 " <input id=\"entry\" type=\"text\"/>"
407 " document.getElementById(\"entry\").focus();"
409 "</body></html>", "file://");
411 g_main_loop_run(loop);
413 g_assert(gtk_widget_is_focus(entry));
415 gtk_widget_destroy(window);
416 g_main_loop_unref(loop);
419 static gboolean emitKeyStroke(WebKitWebView* webView)
421 GdkEvent* pressEvent = gdk_event_new(GDK_KEY_PRESS);
422 pressEvent->key.keyval = GDK_KEY_f;
423 GdkWindow* window = gtk_widget_get_window(GTK_WIDGET(webView));
424 pressEvent->key.window = window;
425 g_object_ref(pressEvent->key.window);
427 GdkDeviceManager* manager = gdk_display_get_device_manager(gdk_window_get_display(window));
428 gdk_event_set_device(pressEvent, gdk_device_manager_get_client_pointer(manager));
430 // When synthesizing an event, an invalid hardware_keycode value
431 // can cause it to be badly processed by Gtk+.
434 if (gdk_keymap_get_entries_for_keyval(gdk_keymap_get_default(), GDK_KEY_f, &keys, &n_keys)) {
435 pressEvent->key.hardware_keycode = keys[0].keycode;
439 GdkEvent* releaseEvent = gdk_event_copy(pressEvent);
440 gtk_main_do_event(pressEvent);
441 gdk_event_free(pressEvent);
442 releaseEvent->key.type = GDK_KEY_RELEASE;
443 gtk_main_do_event(releaseEvent);
444 gdk_event_free(releaseEvent);
449 static gboolean entering_fullscreen_cb(WebKitWebView* webView, GObject* element, gboolean blocked)
452 g_main_loop_quit(loop);
454 g_timeout_add(200, (GSourceFunc) emitKeyStroke, webView);
458 static gboolean leaving_fullscreen_cb(WebKitWebView* webView, GObject* element, gpointer data)
460 g_main_loop_quit(loop);
464 static void test_webkit_web_view_fullscreen(gconstpointer blocked)
468 WebKitWebSettings *settings;
470 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
471 web_view = webkit_web_view_new();
473 settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(web_view));
474 g_object_set(settings, "enable-fullscreen", TRUE, NULL);
475 webkit_web_view_set_settings(WEBKIT_WEB_VIEW(web_view), settings);
477 gtk_container_add(GTK_CONTAINER(window), web_view);
479 gtk_widget_show_all(window);
481 loop = g_main_loop_new(NULL, TRUE);
483 g_signal_connect(web_view, "entering-fullscreen", G_CALLBACK(entering_fullscreen_cb), (gpointer) blocked);
484 g_signal_connect(web_view, "leaving-fullscreen", G_CALLBACK(leaving_fullscreen_cb), NULL);
486 webkit_web_view_load_string(WEBKIT_WEB_VIEW(web_view), "<html><body>"
488 "var eventName = 'keypress';"
489 "document.addEventListener(eventName, function () {"
490 " document.documentElement.webkitRequestFullScreen();"
492 "</script></body></html>", NULL, NULL, NULL);
494 g_timeout_add(100, (GSourceFunc) emitKeyStroke, WEBKIT_WEB_VIEW(web_view));
495 g_main_loop_run(loop);
497 gtk_widget_destroy(window);
500 int main(int argc, char** argv)
505 gtk_test_init(&argc, &argv, NULL);
507 /* Hopefully make test independent of the path it's called from. */
508 testutils_relative_chdir("Source/WebKit/gtk/tests/resources/test.html", argv[0]);
510 server = soup_server_new(SOUP_SERVER_PORT, 0, NULL);
511 soup_server_run_async(server);
513 soup_server_add_handler(server, NULL, server_callback, NULL, NULL);
515 soup_uri = soup_uri_new("http://127.0.0.1/");
516 soup_uri_set_port(soup_uri, soup_server_get_port(server));
518 base_uri = soup_uri_to_string(soup_uri, FALSE);
519 soup_uri_free(soup_uri);
521 g_test_bug_base("https://bugs.webkit.org/");
522 g_test_add_func("/webkit/webview/icon-uri", test_webkit_web_view_icon_uri);
523 g_test_add_func("/webkit/webview/adjustments", test_webkit_web_view_adjustments);
524 g_test_add_func("/webkit/webview/destroy", test_webkit_web_view_destroy);
525 g_test_add_func("/webkit/webview/grab_focus", test_webkit_web_view_grab_focus);
526 g_test_add_func("/webkit/webview/window-features", test_webkit_web_view_window_features);
527 g_test_add_func("/webkit/webview/webview-in-offscreen-window-does-not-crash", test_webkit_web_view_in_offscreen_window_does_not_crash);
528 g_test_add_func("/webkit/webview/webview-does-not-steal-focus", test_webkit_web_view_does_not_steal_focus);
529 g_test_add_data_func("/webkit/webview/fullscreen", GINT_TO_POINTER(FALSE), test_webkit_web_view_fullscreen);
530 g_test_add_data_func("/webkit/webview/fullscreen-blocked", GINT_TO_POINTER(TRUE), test_webkit_web_view_fullscreen);
532 return g_test_run ();
536 int main(int argc, char** argv)
538 g_critical("You will need gtk-2.14.0 to run the unit tests. Doing nothing now.");