Home | History | Annotate | Download | only in tests
      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 <glib.h>
     24 #include <glib/gstdio.h>
     25 #include <gtk/gtk.h>
     26 #include <webkit/webkit.h>
     27 
     28 #if GLIB_CHECK_VERSION(2, 16, 0) && GTK_CHECK_VERSION(2, 14, 0)
     29 
     30 static void test_webkit_web_frame_create_destroy(void)
     31 {
     32     GtkWidget *webView;
     33     GtkWidget *window;
     34 
     35     g_test_bug("21837");
     36     webView = webkit_web_view_new();
     37     g_object_ref_sink(webView);
     38     g_assert_cmpint(G_OBJECT(webView)->ref_count, ==, 1);
     39     // This crashed with the original version
     40     g_object_unref(webView);
     41 
     42     g_test_bug("25042");
     43     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
     44     webView = webkit_web_view_new();
     45     gtk_container_add(GTK_CONTAINER(window), webView);
     46     gtk_widget_show(window);
     47     gtk_widget_show(webView);
     48     gtk_widget_destroy(webView);
     49 }
     50 
     51 static void test_webkit_web_frame_lifetime(void)
     52 {
     53     WebKitWebView* webView;
     54     WebKitWebFrame* webFrame;
     55     g_test_bug("21837");
     56 
     57     webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
     58     g_object_ref_sink(webView);
     59     g_assert_cmpint(G_OBJECT(webView)->ref_count, ==, 1);
     60     webFrame = webkit_web_view_get_main_frame(webView);
     61     g_assert_cmpint(G_OBJECT(webFrame)->ref_count, ==, 1);
     62 
     63     // Add dummy reference on the WebKitWebFrame to keep it alive
     64     g_object_ref(webFrame);
     65     g_assert_cmpint(G_OBJECT(webFrame)->ref_count, ==, 2);
     66 
     67     // This crashed with the original version
     68     g_object_unref(webView);
     69 
     70     // Make sure that the frame got deleted as well. We did this
     71     // by adding an extra ref on the WebKitWebFrame and we should
     72     // be the one holding the last reference.
     73     g_assert_cmpint(G_OBJECT(webFrame)->ref_count, ==, 1);
     74     g_object_unref(webFrame);
     75 }
     76 
     77 static gboolean print_requested_cb(WebKitWebView* webView, WebKitWebFrame* webFrame, GMainLoop* loop)
     78 {
     79     g_object_set_data(G_OBJECT(webView), "signal-handled", GINT_TO_POINTER(TRUE));
     80     g_main_loop_quit(loop);
     81     return TRUE;
     82 }
     83 
     84 static void print_timeout(GMainLoop* loop)
     85 {
     86     if (g_main_loop_is_running(loop))
     87         g_main_loop_quit(loop);
     88 }
     89 
     90 static void test_webkit_web_frame_printing(void)
     91 {
     92     WebKitWebView* webView;
     93 
     94     webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
     95     g_object_ref_sink(webView);
     96     g_assert_cmpint(G_OBJECT(webView)->ref_count, ==, 1);
     97 
     98     webkit_web_view_load_string(webView,
     99                                 "<html><body><h1>WebKitGTK+!</h1></body></html>",
    100                                 "text/html",
    101                                 "utf-8",
    102                                 "file://");
    103 
    104     GMainLoop* loop = g_main_loop_new(NULL, TRUE);
    105 
    106     // Does javascript print() work correctly?
    107     g_signal_connect(webView, "print-requested",
    108                      G_CALLBACK(print_requested_cb),
    109                      loop);
    110 
    111     g_object_set_data(G_OBJECT(webView), "signal-handled", GINT_TO_POINTER(FALSE));
    112     webkit_web_view_execute_script (webView, "print();");
    113 
    114     // Give javascriptcore some time to process the print request, but
    115     // prepare a timeout to avoid it running forever in case the signal is
    116     // never emitted.
    117     g_timeout_add(1000, (GSourceFunc)print_timeout, loop);
    118     g_main_loop_run(loop);
    119 
    120     g_assert_cmpint(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(webView), "signal-handled")), ==, TRUE);
    121 
    122     // Does printing directly to a file?
    123     GError *error = NULL;
    124     gchar* temporaryFilename = NULL;
    125     gint fd = g_file_open_tmp ("webkit-testwebframe-XXXXXX", &temporaryFilename, &error);
    126     close(fd);
    127 
    128     if (error) {
    129         g_critical("Failed to open a temporary file for writing: %s.", error->message);
    130         g_error_free(error);
    131         goto cleanup;
    132     }
    133 
    134     // We delete the file, so that we can easily figure out that the
    135     // file got printed;
    136     if (g_unlink(temporaryFilename) == -1) {
    137         g_warning("Failed to delete the temporary file: %s.\nThis may cause the test to be bogus.", g_strerror(errno));
    138     }
    139 
    140     WebKitWebFrame* webFrame = webkit_web_view_get_main_frame(webView);
    141     GtkPrintOperation* operation = gtk_print_operation_new();
    142     GtkPrintOperationAction action = GTK_PRINT_OPERATION_ACTION_EXPORT;
    143     GtkPrintOperationResult result;
    144 
    145     gtk_print_operation_set_export_filename(operation, temporaryFilename);
    146     result = webkit_web_frame_print_full (webFrame, operation, action, NULL);
    147 
    148     g_assert_cmpint(result, ==, GTK_PRINT_OPERATION_RESULT_APPLY);
    149     g_assert_cmpint(g_file_test(temporaryFilename, G_FILE_TEST_IS_REGULAR), ==, TRUE);
    150 
    151     g_unlink(temporaryFilename);
    152     g_object_unref(operation);
    153 cleanup:
    154     g_object_unref(webView);
    155     g_free(temporaryFilename);
    156 }
    157 
    158 static void test_webkit_web_frame_response()
    159 {
    160     WebKitWebFrame* frame = g_object_new(WEBKIT_TYPE_WEB_FRAME, NULL);
    161     WebKitNetworkResponse* response = webkit_web_frame_get_network_response(frame);
    162     g_assert(!response);
    163     g_object_unref(frame);
    164 }
    165 
    166 int main(int argc, char** argv)
    167 {
    168     g_thread_init(NULL);
    169     gtk_test_init(&argc, &argv, NULL);
    170 
    171     g_test_bug_base("https://bugs.webkit.org/");
    172     g_test_add_func("/webkit/webview/create_destroy", test_webkit_web_frame_create_destroy);
    173     g_test_add_func("/webkit/webframe/lifetime", test_webkit_web_frame_lifetime);
    174     g_test_add_func("/webkit/webview/printing", test_webkit_web_frame_printing);
    175     g_test_add_func("/webkit/webview/response", test_webkit_web_frame_response);
    176     return g_test_run ();
    177 }
    178 
    179 #else
    180 int main(int argc, char** argv)
    181 {
    182     g_critical("You will need at least glib-2.16.0 and gtk-2.14.0 to run the unit tests. Doing nothing now.");
    183     return 0;
    184 }
    185 
    186 #endif
    187