Changeset 42436 in webkit


Ignore:
Timestamp:
Apr 13, 2009 9:27:06 AM (15 years ago)
Author:
kov@webkit.org
Message:

2009-04-13 Gustavo Noronha Silva <gustavo.noronha@collabora.co.uk>

Reviewed by Holger Freyther.

https://bugs.webkit.org/show_bug.cgi?id=22898
[GTK] need proper API for printing

Added simple printing unit tests.

  • tests/testwebframe.c: (print_requested_cb): (print_timeout): (test_webkit_web_frame_printing): (main):
Location:
trunk/WebKit/gtk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebKit/gtk/ChangeLog

    r42435 r42436  
     12009-04-13  Gustavo Noronha Silva  <gustavo.noronha@collabora.co.uk>
     2
     3        Reviewed by Holger Freyther.
     4
     5        https://bugs.webkit.org/show_bug.cgi?id=22898
     6        [GTK] need proper API for printing
     7
     8        Added simple printing unit tests.
     9
     10        * tests/testwebframe.c:
     11        (print_requested_cb):
     12        (print_timeout):
     13        (test_webkit_web_frame_printing):
     14        (main):
     15
    1162009-04-13  Gustavo Noronha Silva  <gns@gnome.org>
    217
  • trunk/WebKit/gtk/tests/testwebframe.c

    r41864 r42436  
    11/*
    22 * Copyright (C) 2008 Holger Hans Peter Freyther
     3 * Copyright (C) 2009 Collabora Ltd.
    34 *
    45 * This library is free software; you can redistribute it and/or
     
    1819 */
    1920
     21#include <errno.h>
     22#include <unistd.h>
    2023#include <glib.h>
     24#include <glib/gstdio.h>
    2125#include <gtk/gtk.h>
    2226#include <webkit/webkit.h>
     
    6367}
    6468
     69static gboolean print_requested_cb(WebKitWebView* webView, WebKitWebFrame* webFrame, GMainLoop* loop)
     70{
     71    g_object_set_data(G_OBJECT(webView), "signal-handled", GINT_TO_POINTER(TRUE));
     72    g_main_loop_quit(loop);
     73    return TRUE;
     74}
     75
     76static void print_timeout(GMainLoop* loop)
     77{
     78    if (g_main_loop_is_running(loop))
     79        g_main_loop_quit(loop);
     80}
     81
     82static void test_webkit_web_frame_printing(void)
     83{
     84    WebKitWebView* webView;
     85
     86    webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
     87    g_object_ref_sink(webView);
     88    g_assert_cmpint(G_OBJECT(webView)->ref_count, ==, 1);
     89
     90    webkit_web_view_load_string(webView,
     91                                "<html><body><h1>WebKitGTK+!</h1></body></html>",
     92                                "text/html",
     93                                "utf-8",
     94                                "file://");
     95
     96    GMainLoop* loop = g_main_loop_new(NULL, TRUE);
     97
     98    // Does javascript print() work correctly?
     99    g_signal_connect(webView, "print-requested",
     100                     G_CALLBACK(print_requested_cb),
     101                     loop);
     102
     103    g_object_set_data(G_OBJECT(webView), "signal-handled", GINT_TO_POINTER(FALSE));
     104    webkit_web_view_execute_script (webView, "print();");
     105
     106    // Give javascriptcore some time to process the print request, but
     107    // prepare a timeout to avoid it running forever in case the signal is
     108    // never emitted.
     109    g_timeout_add(1000, (GSourceFunc)print_timeout, loop);
     110    g_main_loop_run(loop);
     111
     112    g_assert_cmpint(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(webView), "signal-handled")), ==, TRUE);
     113
     114    // Does printing directly to a file?
     115    GError *error = NULL;
     116    gchar* temporaryFilename = NULL;
     117    gint fd = g_file_open_tmp ("webkit-testwebframe-XXXXXX", &temporaryFilename, &error);
     118    close(fd);
     119
     120    if (error) {
     121        g_critical("Failed to open a temporary file for writing: %s.", error->message);
     122        g_error_free(error);
     123        goto cleanup;
     124    }
     125
     126    // We delete the file, so that we can easily figure out that the
     127    // file got printed;
     128    if (g_unlink(temporaryFilename) == -1) {
     129        g_warning("Failed to delete the temporary file: %s.\nThis may cause the test to be bogus.", g_strerror(errno));
     130    }
     131
     132    WebKitWebFrame* webFrame = webkit_web_view_get_main_frame(webView);
     133    GtkPrintOperation* operation = gtk_print_operation_new();
     134    GtkPrintOperationAction action = GTK_PRINT_OPERATION_ACTION_EXPORT;
     135    GtkPrintOperationResult result;
     136
     137    gtk_print_operation_set_export_filename(operation, temporaryFilename);
     138    result = webkit_web_frame_print_full (webFrame, operation, action, NULL);
     139
     140    g_assert_cmpint(result, ==, GTK_PRINT_OPERATION_RESULT_APPLY);
     141    g_assert_cmpint(g_file_test(temporaryFilename, G_FILE_TEST_IS_REGULAR), ==, TRUE);
     142
     143    g_unlink(temporaryFilename);
     144    g_object_unref(operation);
     145cleanup:
     146    g_object_unref(webView);
     147    g_free(temporaryFilename);
     148}
     149
    65150int main(int argc, char** argv)
    66151{
     
    71156    g_test_add_func("/webkit/webview/create_destroy", test_webkit_web_frame_create_destroy);
    72157    g_test_add_func("/webkit/webframe/lifetime", test_webkit_web_frame_lifetime);
     158    g_test_add_func("/webkit/webview/printing", test_webkit_web_frame_printing);
    73159    return g_test_run ();
    74160}
Note: See TracChangeset for help on using the changeset viewer.