2 * Copyright (C) 2012 Igalia S.L.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2,1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "WebViewTest.h"
22 #include <glib/gstdio.h>
23 #include <wtf/gobject/GRefPtr.h>
25 #ifdef HAVE_GTK_UNIX_PRINTING
26 #include <gtk/gtkunixprint.h>
29 static char* kTempDirectory;
31 static void testPrintOperationPrintSettings(WebViewTest* test, gconstpointer)
33 GRefPtr<WebKitPrintOperation> printOperation = adoptGRef(webkit_print_operation_new(test->m_webView));
34 test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(printOperation.get()));
36 g_assert(!webkit_print_operation_get_print_settings(printOperation.get()));
37 g_assert(!webkit_print_operation_get_page_setup(printOperation.get()));
39 GRefPtr<GtkPrintSettings> printSettings = adoptGRef(gtk_print_settings_new());
40 test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(printSettings.get()));
42 GRefPtr<GtkPageSetup> pageSetup = adoptGRef(gtk_page_setup_new());
43 test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(pageSetup.get()));
45 webkit_print_operation_set_print_settings(printOperation.get(), printSettings.get());
46 webkit_print_operation_set_page_setup(printOperation.get(), pageSetup.get());
48 g_assert(webkit_print_operation_get_print_settings(printOperation.get()) == printSettings.get());
49 g_assert(webkit_print_operation_get_page_setup(printOperation.get()) == pageSetup.get());
52 static gboolean webViewPrintCallback(WebKitWebView* webView, WebKitPrintOperation* printOperation, WebViewTest* test)
54 g_assert(webView == test->m_webView);
56 g_assert(WEBKIT_IS_PRINT_OPERATION(printOperation));
57 test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(printOperation));
59 g_assert(!webkit_print_operation_get_print_settings(printOperation));
60 g_assert(!webkit_print_operation_get_page_setup(printOperation));
62 g_main_loop_quit(test->m_mainLoop);
67 static void testWebViewPrint(WebViewTest* test, gconstpointer)
69 g_signal_connect(test->m_webView, "print", G_CALLBACK(webViewPrintCallback), test);
70 test->loadHtml("<html><body onLoad=\"print();\">WebKitGTK+ printing test</body></html>", 0);
71 g_main_loop_run(test->m_mainLoop);
74 #ifdef HAVE_GTK_UNIX_PRINTING
75 class PrintTest: public WebViewTest {
77 MAKE_GLIB_TEST_FIXTURE(PrintTest);
79 static void printFinishedCallback(WebKitPrintOperation*, PrintTest* test)
81 g_main_loop_quit(test->m_mainLoop);
84 static void printFailedCallback(WebKitPrintOperation*, GError* error, PrintTest* test)
86 g_assert(test->m_expectedError);
88 g_assert(g_error_matches(error, WEBKIT_PRINT_ERROR, test->m_expectedError));
94 m_printOperation = adoptGRef(webkit_print_operation_new(m_webView));
95 assertObjectIsDeletedWhenTestFinishes(G_OBJECT(m_printOperation.get()));
96 g_signal_connect(m_printOperation.get(), "finished", G_CALLBACK(printFinishedCallback), this);
97 g_signal_connect(m_printOperation.get(), "failed", G_CALLBACK(printFailedCallback), this);
100 static gboolean testPrintOperationPrintPrinter(GtkPrinter* printer, gpointer userData)
102 if (strcmp(gtk_printer_get_name(printer), "Print to File"))
105 GtkPrinter** foundPrinter = static_cast<GtkPrinter**>(userData);
106 *foundPrinter = static_cast<GtkPrinter*>(g_object_ref(printer));
110 GtkPrinter* findPrintToFilePrinter()
112 GtkPrinter* printer = 0;
113 gtk_enumerate_printers(testPrintOperationPrintPrinter, &printer, 0, TRUE);
117 void waitUntilPrintFinished()
119 g_main_loop_run(m_mainLoop);
122 GRefPtr<WebKitPrintOperation> m_printOperation;
123 unsigned int m_expectedError;
126 static void testPrintOperationPrint(PrintTest* test, gconstpointer)
128 test->loadHtml("<html><body>WebKitGTK+ printing test</body></html>", 0);
129 test->waitUntilLoadFinished();
131 GRefPtr<GtkPrinter> printer = adoptGRef(test->findPrintToFilePrinter());
133 g_message("%s", "Cannot test WebKitPrintOperation/print: no suitable printer found");
137 GOwnPtr<char> outputFilename(g_build_filename(kTempDirectory, "webkit-print.pdf", NULL));
138 GRefPtr<GFile> outputFile = adoptGRef(g_file_new_for_path(outputFilename.get()));
139 GOwnPtr<char> outputURI(g_file_get_uri(outputFile.get()));
141 GRefPtr<GtkPrintSettings> printSettings = adoptGRef(gtk_print_settings_new());
142 gtk_print_settings_set_printer(printSettings.get(), gtk_printer_get_name(printer.get()));
143 gtk_print_settings_set(printSettings.get(), GTK_PRINT_SETTINGS_OUTPUT_URI, outputURI.get());
145 webkit_print_operation_set_print_settings(test->m_printOperation.get(), printSettings.get());
146 webkit_print_operation_print(test->m_printOperation.get());
147 test->waitUntilPrintFinished();
149 GRefPtr<GFileInfo> fileInfo = adoptGRef(g_file_query_info(outputFile.get(), G_FILE_ATTRIBUTE_STANDARD_SIZE "," G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
150 static_cast<GFileQueryInfoFlags>(0), 0, 0));
151 g_assert(fileInfo.get());
152 g_assert_cmpint(g_file_info_get_size(fileInfo.get()), >, 0);
153 g_assert_cmpstr(g_file_info_get_content_type(fileInfo.get()), ==, "application/pdf");
155 g_file_delete(outputFile.get(), 0, 0);
158 static void testPrintOperationErrors(PrintTest* test, gconstpointer)
160 test->loadHtml("<html><body>WebKitGTK+ printing errors test</body></html>", 0);
161 test->waitUntilLoadFinished();
163 GRefPtr<GtkPrinter> printer = adoptGRef(test->findPrintToFilePrinter());
165 g_message("%s", "Cannot test WebKitPrintOperation/print: no suitable printer found");
169 // General Error: invalid filename.
170 test->m_expectedError = WEBKIT_PRINT_ERROR_GENERAL;
171 GRefPtr<GtkPrintSettings> printSettings = adoptGRef(gtk_print_settings_new());
172 gtk_print_settings_set_printer(printSettings.get(), gtk_printer_get_name(printer.get()));
173 gtk_print_settings_set(printSettings.get(), GTK_PRINT_SETTINGS_OUTPUT_URI, "file:///foo/bar");
174 webkit_print_operation_set_print_settings(test->m_printOperation.get(), printSettings.get());
175 webkit_print_operation_print(test->m_printOperation.get());
176 test->waitUntilPrintFinished();
178 // Printer not found error.
179 test->m_expectedError = WEBKIT_PRINT_ERROR_PRINTER_NOT_FOUND;
180 gtk_print_settings_set_printer(printSettings.get(), "The fake WebKit printer");
181 webkit_print_operation_print(test->m_printOperation.get());
182 test->waitUntilPrintFinished();
184 // No pages to print: print even pages for a single page document.
185 test->m_expectedError = WEBKIT_PRINT_ERROR_INVALID_PAGE_RANGE;
186 gtk_print_settings_set_printer(printSettings.get(), gtk_printer_get_name(printer.get()));
187 gtk_print_settings_set_page_set(printSettings.get(), GTK_PAGE_SET_EVEN);
188 webkit_print_operation_print(test->m_printOperation.get());
189 test->waitUntilPrintFinished();
191 #endif // HAVE_GTK_UNIX_PRINTING
195 kTempDirectory = g_dir_make_tmp("WebKit2Tests-XXXXXX", 0);
196 g_assert(kTempDirectory);
198 WebViewTest::add("WebKitPrintOperation", "printing-settings", testPrintOperationPrintSettings);
199 WebViewTest::add("WebKitWebView", "print", testWebViewPrint);
200 #ifdef HAVE_GTK_UNIX_PRINTING
201 PrintTest::add("WebKitPrintOperation", "print", testPrintOperationPrint);
202 PrintTest::add("WebKitPrintOperation", "print-errors", testPrintOperationErrors);
208 g_rmdir(kTempDirectory);