[GTK] FullScreen signals
[WebKit-https.git] / Source / WebKit / gtk / tests / testwebview.c
1 /*
2  * Copyright (C) 2008 Holger Hans Peter Freyther
3  * Copyright (C) 2009, 2010 Collabora Ltd.
4  * Copyright (C) 2012 Igalia S.L.
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include "test_utils.h"
23
24 #include <errno.h>
25 #include <unistd.h>
26 #include <string.h>
27
28 #include <glib.h>
29 #include <glib/gstdio.h>
30 #include <gtk/gtk.h>
31 #include <webkit/webkit.h>
32
33 #if GTK_CHECK_VERSION(2, 14, 0)
34
35 GMainLoop* loop;
36 SoupSession *session;
37 char* base_uri;
38
39 /* For real request testing */
40 static void
41 server_callback(SoupServer* server, SoupMessage* msg,
42                  const char* path, GHashTable* query,
43                  SoupClientContext* context, gpointer data)
44 {
45     if (msg->method != SOUP_METHOD_GET) {
46         soup_message_set_status(msg, SOUP_STATUS_NOT_IMPLEMENTED);
47         return;
48     }
49
50     soup_message_set_status(msg, SOUP_STATUS_OK);
51
52     if (g_str_equal(path, "/favicon.ico")) {
53         char* contents;
54         gsize length;
55         GError* error = NULL;
56
57         g_file_get_contents("blank.ico", &contents, &length, &error);
58         g_assert(!error);
59
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));
67     } else {
68         char* contents = g_strdup("<html><body>test</body></html>");
69         soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, strlen(contents));
70     }
71
72     soup_message_body_complete(msg->response_body);
73 }
74
75 static void idle_quit_loop_cb(WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
76 {
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);
80 }
81
82 static gboolean timeout_cb(gpointer data)
83 {
84     g_error("Didn't get icon-uri before timing out.");
85     return FALSE;
86 }
87
88 static void icon_uri_changed_cb(WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
89 {
90     char* expected_uri;
91
92     g_assert_cmpstr(g_param_spec_get_name(pspec), ==, "icon-uri");
93
94     expected_uri = g_strdup_printf("%sfavicon.ico", base_uri);
95     g_assert_cmpstr(webkit_web_view_get_icon_uri(web_view), ==, expected_uri);
96     g_free(expected_uri);
97
98     g_main_loop_quit(loop);
99 }
100
101 static void icon_loaded_cb(WebKitWebView* web_view, char* icon_uri, gpointer data)
102 {
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);
107
108     g_assert_cmpstr(icon_uri, ==, webkit_web_view_get_icon_uri(web_view));
109
110     *been_here = TRUE;
111 }
112
113 static void test_webkit_web_view_icon_uri()
114 {
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));
118
119     loop = g_main_loop_new(NULL, TRUE);
120
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,
124                      NULL);
125
126     webkit_web_view_load_uri(view, base_uri);
127
128     guint timeout_id = g_timeout_add(500, timeout_cb, 0);
129
130     g_main_loop_run(loop);
131
132     g_source_remove(timeout_id);
133
134     g_assert(been_to_icon_loaded);
135
136     g_object_unref(view);
137 }
138
139 static gboolean map_event_cb(GtkWidget *widget, GdkEvent* event, gpointer data)
140 {
141     GMainLoop* loop = (GMainLoop*)data;
142     g_main_loop_quit(loop);
143
144     return FALSE;
145 }
146
147 static gboolean quit_after_short_delay_cb(gpointer data)
148 {
149     g_main_loop_quit((GMainLoop*)data);
150     return FALSE;
151 }
152
153 static void test_webkit_web_view_grab_focus()
154 {
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;
160
161     gtk_window_set_default_size(GTK_WINDOW(window), 400, 200);
162
163     gtk_container_add(GTK_CONTAINER(window), scrolled_window);
164     gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(view));
165
166     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
167                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
168
169     loop = g_main_loop_new(NULL, TRUE);
170
171     g_signal_connect(view, "notify::load-status", G_CALLBACK(idle_quit_loop_cb), NULL);
172
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);
178
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);
182
183     adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrolled_window));
184     g_assert_cmpfloat(gtk_adjustment_get_value(adjustment), ==, 0.0);
185
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.
192     */
193     char script[] = "var innerDoc = document.defaultView.frames[0].document; \
194                      innerDoc.getElementById(\"link\").focus();              \
195                      if (innerDoc.hasFocus())                                \
196                         window.scrollBy(0, 100);";
197
198     /* Focus an element using JavaScript */
199     webkit_web_view_execute_script(view, script);
200
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);
204
205     /* Make sure the ScrolledWindow noticed the scroll */
206     g_assert_cmpfloat(gtk_adjustment_get_value(adjustment), !=, 0.0);
207
208     g_free(uri);
209     gtk_widget_destroy(window);
210 }
211
212 static void do_test_webkit_web_view_adjustments(gboolean with_page_cache)
213 {
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;
220     double lower;
221     double upper;
222
223     if (with_page_cache) {
224         WebKitWebSettings* settings = webkit_web_view_get_settings(view);
225         g_object_set(settings, "enable-page-cache", TRUE, NULL);
226     }
227
228     gtk_window_set_default_size(GTK_WINDOW(window), 400, 200);
229
230     gtk_container_add(GTK_CONTAINER(window), scrolled_window);
231     gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(view));
232
233     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
234                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
235
236     loop = g_main_loop_new(NULL, TRUE);
237
238     g_object_connect(G_OBJECT(view),
239                      "signal::notify::load-status", idle_quit_loop_cb, NULL,
240                      NULL);
241
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);
247
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);
251
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);
255
256     adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrolled_window));
257     g_assert_cmpfloat(gtk_adjustment_get_value(adjustment), ==, 0.0);
258
259     lower = gtk_adjustment_get_lower(adjustment);
260     upper = gtk_adjustment_get_upper(adjustment);
261
262     /* Scroll the view using JavaScript */
263     webkit_web_view_execute_script(view, "window.scrollBy(0, 100)");
264
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);
268
269     /* Make sure the ScrolledWindow noticed the scroll */
270     g_assert_cmpfloat(gtk_adjustment_get_value(adjustment), ==, 100.0);
271
272     /* Load a second URI */
273     webkit_web_view_load_uri(view, second_uri);
274     g_main_loop_run(loop);
275
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);
279
280     /* Make sure the scrollbar has been reset */
281     g_assert_cmpfloat(gtk_adjustment_get_value(adjustment), ==, 0.0);
282
283     /* Go back */
284     webkit_web_view_go_back(view);
285
286     /* When using page cache, go_back will return syncronously */
287     if (!with_page_cache)
288         g_main_loop_run(loop);
289
290     /* Make sure GTK+ has time to process the changes in size, for the adjusments */
291     while (gtk_events_pending())
292         gtk_main_iteration();
293
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);
298
299     g_free(effective_uri);
300     g_free(second_uri);
301
302     gtk_widget_destroy(window);
303 }
304
305 static void test_webkit_web_view_adjustments()
306 {
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);
310 }
311
312 gboolean delayed_destroy(gpointer data)
313 {
314     gtk_widget_destroy(GTK_WIDGET(data));
315     g_main_loop_quit(loop);
316     return FALSE;
317 }
318
319 static void test_webkit_web_view_destroy()
320 {
321     GtkWidget* window;
322     GtkWidget* web_view;
323
324     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
325     web_view = webkit_web_view_new();
326
327     gtk_container_add(GTK_CONTAINER(window), web_view);
328
329     gtk_widget_show_all(window);
330
331     loop = g_main_loop_new(NULL, TRUE);
332
333     g_signal_connect(window, "map-event",
334                      G_CALLBACK(map_event_cb), loop);
335     g_main_loop_run(loop);
336
337     g_idle_add(delayed_destroy, web_view);
338     g_main_loop_run(loop);
339
340     gtk_widget_destroy(window);
341 }
342
343 static void test_webkit_web_view_window_features()
344 {
345     GtkWidget* window;
346     GtkWidget* web_view;
347     
348     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
349     web_view = webkit_web_view_new();
350     
351     gtk_container_add(GTK_CONTAINER(window), web_view);
352     
353     gtk_widget_show_all(window);
354     
355     loop = g_main_loop_new(NULL, TRUE);
356
357     g_signal_connect(window, "map-event",
358                      G_CALLBACK(map_event_cb), loop);
359     g_main_loop_run(loop);
360     
361     /* Bug #36144 */
362     g_object_set(G_OBJECT(web_view), "window-features", NULL, NULL);
363     
364     gtk_widget_destroy(window);
365 }    
366
367 static void test_webkit_web_view_in_offscreen_window_does_not_crash()
368 {
369     loop = g_main_loop_new(NULL, TRUE);
370
371     GtkWidget *window = gtk_offscreen_window_new();
372     GtkWidget *web_view = webkit_web_view_new();
373
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);
378
379     g_main_loop_run(loop);
380
381     gtk_widget_destroy(window);
382     g_main_loop_unref(loop);
383 }
384
385 static void test_webkit_web_view_does_not_steal_focus()
386 {
387     loop = g_main_loop_new(NULL, TRUE);
388
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);
393
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);
398
399     gtk_widget_grab_focus(entry);
400     g_assert(gtk_widget_is_focus(entry));
401
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),
404         "<html><body>"
405         "    <input id=\"entry\" type=\"text\"/>"
406         "    <script>"
407         "        document.getElementById(\"entry\").focus();"
408         "    </script>"
409         "</body></html>", "file://");
410
411     g_main_loop_run(loop);
412
413     g_assert(gtk_widget_is_focus(entry));
414
415     gtk_widget_destroy(window);
416     g_main_loop_unref(loop);
417 }
418
419 static gboolean emitKeyStroke(WebKitWebView* webView)
420 {
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);
426
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));
429
430     // When synthesizing an event, an invalid hardware_keycode value
431     // can cause it to be badly processed by Gtk+.
432     GdkKeymapKey* keys;
433     gint n_keys;
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;
436         g_free(keys);
437     }
438
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);
445
446     return FALSE;
447 }
448
449 static gboolean entering_fullscreen_cb(WebKitWebView* webView, GObject* element, gboolean blocked)
450 {
451     if (blocked)
452         g_main_loop_quit(loop);
453     else
454         g_timeout_add(200, (GSourceFunc) emitKeyStroke, webView);
455     return blocked;
456 }
457
458 static gboolean leaving_fullscreen_cb(WebKitWebView* webView, GObject* element, gpointer data)
459 {
460     g_main_loop_quit(loop);
461     return FALSE;
462 }
463
464 static void test_webkit_web_view_fullscreen(gconstpointer blocked)
465 {
466     GtkWidget* window;
467     GtkWidget* web_view;
468     WebKitWebSettings *settings;
469
470     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
471     web_view = webkit_web_view_new();
472
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);
476
477     gtk_container_add(GTK_CONTAINER(window), web_view);
478
479     gtk_widget_show_all(window);
480
481     loop = g_main_loop_new(NULL, TRUE);
482
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);
485
486     webkit_web_view_load_string(WEBKIT_WEB_VIEW(web_view), "<html><body>"
487                    "<script>"
488                    "var eventName = 'keypress';"
489                    "document.addEventListener(eventName, function () {"
490                    "    document.documentElement.webkitRequestFullScreen();"
491                    "}, false);"
492                    "</script></body></html>", NULL, NULL, NULL);
493
494     g_timeout_add(100, (GSourceFunc) emitKeyStroke, WEBKIT_WEB_VIEW(web_view));
495     g_main_loop_run(loop);
496
497     gtk_widget_destroy(window);
498 }
499
500 int main(int argc, char** argv)
501 {
502     SoupServer* server;
503     SoupURI* soup_uri;
504
505     gtk_test_init(&argc, &argv, NULL);
506
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]);
509
510     server = soup_server_new(SOUP_SERVER_PORT, 0, NULL);
511     soup_server_run_async(server);
512
513     soup_server_add_handler(server, NULL, server_callback, NULL, NULL);
514
515     soup_uri = soup_uri_new("http://127.0.0.1/");
516     soup_uri_set_port(soup_uri, soup_server_get_port(server));
517
518     base_uri = soup_uri_to_string(soup_uri, FALSE);
519     soup_uri_free(soup_uri);
520
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);
531
532     return g_test_run ();
533 }
534
535 #else
536 int main(int argc, char** argv)
537 {
538     g_critical("You will need gtk-2.14.0 to run the unit tests. Doing nothing now.");
539     return 0;
540 }
541
542 #endif