[GTK] Fix concatenations of string literals that are not C++11-compliant
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / gtk / tests / TestPrinting.cpp
1 /*
2  * Copyright (C) 2012 Igalia S.L.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21 #include "WebViewTest.h"
22 #include <glib/gstdio.h>
23 #include <wtf/gobject/GRefPtr.h>
24
25 #ifdef HAVE_GTK_UNIX_PRINTING
26 #include <gtk/gtkunixprint.h>
27 #endif
28
29 static char* kTempDirectory;
30
31 static void testPrintOperationPrintSettings(WebViewTest* test, gconstpointer)
32 {
33     GRefPtr<WebKitPrintOperation> printOperation = adoptGRef(webkit_print_operation_new(test->m_webView));
34     test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(printOperation.get()));
35
36     g_assert(!webkit_print_operation_get_print_settings(printOperation.get()));
37     g_assert(!webkit_print_operation_get_page_setup(printOperation.get()));
38
39     GRefPtr<GtkPrintSettings> printSettings = adoptGRef(gtk_print_settings_new());
40     test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(printSettings.get()));
41
42     GRefPtr<GtkPageSetup> pageSetup = adoptGRef(gtk_page_setup_new());
43     test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(pageSetup.get()));
44
45     webkit_print_operation_set_print_settings(printOperation.get(), printSettings.get());
46     webkit_print_operation_set_page_setup(printOperation.get(), pageSetup.get());
47
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());
50 }
51
52 static gboolean webViewPrintCallback(WebKitWebView* webView, WebKitPrintOperation* printOperation, WebViewTest* test)
53 {
54     g_assert(webView == test->m_webView);
55
56     g_assert(WEBKIT_IS_PRINT_OPERATION(printOperation));
57     test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(printOperation));
58
59     g_assert(!webkit_print_operation_get_print_settings(printOperation));
60     g_assert(!webkit_print_operation_get_page_setup(printOperation));
61
62     g_main_loop_quit(test->m_mainLoop);
63
64     return TRUE;
65 }
66
67 static void testWebViewPrint(WebViewTest* test, gconstpointer)
68 {
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);
72 }
73
74 #ifdef HAVE_GTK_UNIX_PRINTING
75 class PrintTest: public WebViewTest {
76 public:
77     MAKE_GLIB_TEST_FIXTURE(PrintTest);
78
79     static void printFinishedCallback(WebKitPrintOperation*, PrintTest* test)
80     {
81         g_main_loop_quit(test->m_mainLoop);
82     }
83
84     static void printFailedCallback(WebKitPrintOperation*, GError* error, PrintTest* test)
85     {
86         g_assert(test->m_expectedError);
87         g_assert(error);
88         g_assert(g_error_matches(error, WEBKIT_PRINT_ERROR, test->m_expectedError));
89     }
90
91     PrintTest()
92         : m_expectedError(0)
93     {
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);
98     }
99
100     static gboolean testPrintOperationPrintPrinter(GtkPrinter* printer, gpointer userData)
101     {
102         if (strcmp(gtk_printer_get_name(printer), "Print to File"))
103             return FALSE;
104
105         GtkPrinter** foundPrinter = static_cast<GtkPrinter**>(userData);
106         *foundPrinter = static_cast<GtkPrinter*>(g_object_ref(printer));
107         return TRUE;
108     }
109
110     GtkPrinter* findPrintToFilePrinter()
111     {
112         GtkPrinter* printer = 0;
113         gtk_enumerate_printers(testPrintOperationPrintPrinter, &printer, 0, TRUE);
114         return printer;
115     }
116
117     void waitUntilPrintFinished()
118     {
119         g_main_loop_run(m_mainLoop);
120     }
121
122     GRefPtr<WebKitPrintOperation> m_printOperation;
123     unsigned int m_expectedError;
124 };
125
126 static void testPrintOperationPrint(PrintTest* test, gconstpointer)
127 {
128     test->loadHtml("<html><body>WebKitGTK+ printing test</body></html>", 0);
129     test->waitUntilLoadFinished();
130
131     GRefPtr<GtkPrinter> printer = adoptGRef(test->findPrintToFilePrinter());
132     if (!printer) {
133         g_message("%s", "Cannot test WebKitPrintOperation/print: no suitable printer found");
134         return;
135     }
136
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()));
140
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());
144
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();
148
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");
154
155     g_file_delete(outputFile.get(), 0, 0);
156 }
157
158 static void testPrintOperationErrors(PrintTest* test, gconstpointer)
159 {
160     test->loadHtml("<html><body>WebKitGTK+ printing errors test</body></html>", 0);
161     test->waitUntilLoadFinished();
162
163     GRefPtr<GtkPrinter> printer = adoptGRef(test->findPrintToFilePrinter());
164     if (!printer) {
165         g_message("%s", "Cannot test WebKitPrintOperation/print: no suitable printer found");
166         return;
167     }
168
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();
177
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();
183
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();
190 }
191 #endif // HAVE_GTK_UNIX_PRINTING
192
193 void beforeAll()
194 {
195     kTempDirectory = g_dir_make_tmp("WebKit2Tests-XXXXXX", 0);
196     g_assert(kTempDirectory);
197
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);
203 #endif
204 }
205
206 void afterAll()
207 {
208     g_rmdir(kTempDirectory);
209 }