1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtTest/QtTest>
30
31#include <qprinter.h>
32#include <qpagesetupdialog.h>
33#include <qpainter.h>
34#include <qprintdialog.h>
35#include <qprintpreviewdialog.h>
36#include <qprintpreviewwidget.h>
37#include <qprinterinfo.h>
38#include <qvariant.h>
39#include <qpainter.h>
40#include <qprintengine.h>
41#include <qpagelayout.h>
42#include <qsharedpointer.h>
43#include <qtemporarydir.h>
44
45#include <math.h>
46
47#ifdef Q_OS_WIN
48#include <windows.h>
49#endif
50
51#if QT_CONFIG(printer)
52typedef QSharedPointer<QPrinter> PrinterPtr;
53
54Q_DECLARE_METATYPE(PrinterPtr)
55Q_DECLARE_METATYPE(QPrinter::Orientation)
56Q_DECLARE_METATYPE(QPrinter::PageSize)
57#endif // printer
58
59static int fileNumber = 0;
60
61class tst_QPrinter : public QObject
62{
63 Q_OBJECT
64
65private slots:
66 void initTestCase();
67#if QT_CONFIG(printer)
68 void testPageRectAndPaperRect();
69 void testPageRectAndPaperRect_data();
70 void testSetOptions();
71 void testMargins_data();
72 void testMargins();
73 void testPageSetupDialog();
74 void testPrintPreviewDialog();
75 void testMulitpleSets_data();
76 void testMulitpleSets();
77 void testPageMargins_data();
78 void testPageMargins();
79 void outputFormatFromSuffix();
80 void errorReporting();
81 void testCustomPageSizes();
82 void customPaperSizeAndMargins_data();
83 void customPaperSizeAndMargins();
84 void customPaperNameSettingBySize();
85 void customPaperNameSettingByName();
86#if QT_CONFIG(completer) && QT_CONFIG(filedialog)
87 void printDialogCompleter();
88#endif
89 void testCurrentPage();
90 void taskQTBUG4497_reusePrinterOnDifferentFiles();
91 void testPdfTitle();
92
93 // Test QPrintEngine keys and their QPrinter setters/getters
94 void testMultipleKeys();
95 void collateCopies();
96 void colorMode();
97 void copyCount();
98 void creator();
99 void docName();
100 void doubleSidedPrinting();
101 void duplex();
102 void fontEmbedding();
103 void fullPage();
104 void orientation();
105 void outputFileName();
106 void pageOrder();
107 void pageSize();
108 void paperSize();
109 void paperSource();
110 void printerName();
111 void printerSelectionOption();
112 void printProgram();
113 void printRange();
114 void resolution();
115 void supportedPaperSources();
116 void supportedResolutions();
117 void windowsPageSize();
118
119 // Test QPrinter setters/getters for non-QPrintEngine options
120 void outputFormat();
121 void fromToPage();
122
123 void testPageMetrics_data();
124 void testPageMetrics();
125 void reusePageMetrics();
126#endif
127private:
128 QString testFileName(const QString &prefix, const QString &suffix);
129 QString testPdfFileName(const QString &prefix) { return testFileName(prefix, QStringLiteral("pdf")); }
130
131 QTemporaryDir m_tempDir;
132};
133
134void tst_QPrinter::initTestCase()
135{
136#if !QT_CONFIG(printer)
137 QSKIP("This test requires printing support");
138#endif
139 QVERIFY2(m_tempDir.isValid(), qPrintable(m_tempDir.errorString()));
140}
141
142#if QT_CONFIG(printer)
143
144#define MYCOMPARE(a, b) QCOMPARE(QVariant((int)a), QVariant((int)b))
145
146void tst_QPrinter::testPageSetupDialog()
147{
148 // Make sure this doesn't crash at least
149 {
150 QPrinter printer;
151 QPageSetupDialog dialog(&printer);
152 }
153}
154
155// A preview dialog showing 4 pages for testPrintPreviewDialog().
156
157class MyPreviewDialog : public QPrintPreviewDialog {
158 Q_OBJECT
159public:
160 MyPreviewDialog(QPrinter *p) : QPrintPreviewDialog(p)
161 {
162 connect(sender: this, SIGNAL(paintRequested(QPrinter*)), receiver: this, SLOT(slotPaintRequested(QPrinter*)));
163 }
164
165public slots:
166 void slotPaintRequested(QPrinter *p);
167};
168
169void MyPreviewDialog::slotPaintRequested(QPrinter *p)
170{
171 enum { pageCount = 4 };
172 QPainter painter;
173 painter.begin(p);
174 for (int i = 0; i < pageCount; ++i) {
175 const QRect f = p->pageRect(QPrinter::DevicePixel).toRect();
176 painter.fillRect(r: f, c: Qt::white);
177 painter.drawText(p: f.center(), s: QString::fromLatin1(str: "Page %1").arg(a: i + 1));
178 if (i != pageCount - 1)
179 p->newPage();
180 }
181 painter.end();
182}
183
184void tst_QPrinter::testPrintPreviewDialog()
185{
186 // QTBUG-14517: Showing the dialog with Qt::WindowMaximized caused it to switch to
187 // page 2 due to the scrollbar logic (besides testing for crashes).
188 QPrinter printer;
189 MyPreviewDialog dialog(&printer);
190 dialog.setWindowState(Qt::WindowMaximized);
191 dialog.show();
192 QVERIFY(QTest::qWaitForWindowExposed(&dialog));
193 QPrintPreviewWidget *widget = dialog.findChild<QPrintPreviewWidget *>();
194 QVERIFY(widget);
195 QCOMPARE(widget->currentPage(), 1);
196}
197
198void tst_QPrinter::testPageRectAndPaperRect_data()
199{
200 QTest::addColumn<PrinterPtr>(name: "printer");
201 QTest::addColumn<QPrinter::Orientation>(name: "orientation");
202 QTest::addColumn<bool>(name: "withPainter");
203 QTest::addColumn<int>(name: "resolution");
204 QTest::addColumn<bool>(name: "doPaperRect");
205
206 const PrinterPtr printer(new QPrinter(QPrinter::HighResolution));
207 // paperrect
208 QTest::newRow(dataTag: "paperRect0") << printer << QPrinter::Portrait << true << 300 << true;
209 QTest::newRow(dataTag: "paperRect1") << printer << QPrinter::Portrait << false << 300 << true;
210 QTest::newRow(dataTag: "paperRect2") << printer << QPrinter::Landscape << true << 300 << true;
211 QTest::newRow(dataTag: "paperRect3") << printer << QPrinter::Landscape << false << 300 << true;
212 QTest::newRow(dataTag: "paperRect4") << printer << QPrinter::Portrait << true << 600 << true;
213 QTest::newRow(dataTag: "paperRect5") << printer << QPrinter::Portrait << false << 600 << true;
214 QTest::newRow(dataTag: "paperRect6") << printer << QPrinter::Landscape << true << 600 << true;
215 QTest::newRow(dataTag: "paperRect7") << printer << QPrinter::Landscape << false << 600 << true;
216 QTest::newRow(dataTag: "paperRect8") << printer << QPrinter::Portrait << true << 1200 << true;
217 QTest::newRow(dataTag: "paperRect9") << printer << QPrinter::Portrait << false << 1200 << true;
218 QTest::newRow(dataTag: "paperRect10") << printer << QPrinter::Landscape << true << 1200 << true;
219 QTest::newRow(dataTag: "paperRect11") << printer << QPrinter::Landscape << false << 1200 << true;
220
221 // page rect
222 QTest::newRow(dataTag: "pageRect0") << printer << QPrinter::Portrait << true << 300 << false;
223 QTest::newRow(dataTag: "pageRect1") << printer << QPrinter::Portrait << false << 300 << false;
224 QTest::newRow(dataTag: "pageRect2") << printer << QPrinter::Landscape << true << 300 << false;
225 QTest::newRow(dataTag: "pageRect3") << printer << QPrinter::Landscape << false << 300 << false;
226 QTest::newRow(dataTag: "pageRect4") << printer << QPrinter::Portrait << true << 600 << false;
227 QTest::newRow(dataTag: "pageRect5") << printer << QPrinter::Portrait << false << 600 << false;
228 QTest::newRow(dataTag: "pageRect6") << printer << QPrinter::Landscape << true << 600 << false;
229 QTest::newRow(dataTag: "pageRect7") << printer << QPrinter::Landscape << false << 600 << false;
230 QTest::newRow(dataTag: "pageRect8") << printer << QPrinter::Portrait << true << 1200 << false;
231 QTest::newRow(dataTag: "pageRect9") << printer << QPrinter::Portrait << false << 1200 << false;
232 QTest::newRow(dataTag: "pageRect10") << printer << QPrinter::Landscape << true << 1200 << false;
233 QTest::newRow(dataTag: "pageRect11") << printer << QPrinter::Landscape << false << 1200 << false;
234}
235
236void tst_QPrinter::testPageRectAndPaperRect()
237{
238 QFETCH(PrinterPtr, printer);
239 QFETCH(bool, withPainter);
240 QFETCH(QPrinter::Orientation, orientation);
241 QFETCH(int, resolution);
242 QFETCH(bool, doPaperRect);
243
244 QPainter *painter = 0;
245 printer->setOrientation(orientation);
246 printer->setOutputFileName(testFileName(prefix: QLatin1String("silly"), suffix: QString()));
247
248 QRect pageRect = doPaperRect ? printer->paperRect() : printer->pageRect();
249 float inchesX = float(pageRect.width()) / float(printer->resolution());
250 float inchesY = float(pageRect.height()) / float(printer->resolution());
251 printer->setResolution(resolution);
252 if (withPainter)
253 painter = new QPainter(printer.data());
254
255 QRect otherRect = doPaperRect ? printer->paperRect() : printer->pageRect();
256 float otherInchesX = float(otherRect.width()) / float(printer->resolution());
257 float otherInchesY = float(otherRect.height()) / float(printer->resolution());
258 if (painter != 0)
259 delete painter;
260
261 QVERIFY(qAbs(otherInchesX - inchesX) < 0.01);
262 QVERIFY(qAbs(otherInchesY - inchesY) < 0.01);
263
264 QVERIFY(printer->orientation() == QPrinter::Portrait || pageRect.width() > pageRect.height());
265 QVERIFY(printer->orientation() != QPrinter::Portrait || pageRect.width() < pageRect.height());
266}
267
268void tst_QPrinter::testSetOptions()
269{
270 QPrinter prn;
271 QPrintDialog dlg(&prn);
272
273 // Verify default values
274 MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintToFile), true);
275 MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintSelection), false);
276 MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintPageRange), true);
277
278 dlg.setEnabledOptions(QAbstractPrintDialog::PrintPageRange);
279 MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintToFile), false);
280 MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintSelection), false);
281 MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintPageRange), true);
282
283 dlg.setEnabledOptions((QAbstractPrintDialog::PrintDialogOptions(QAbstractPrintDialog::PrintSelection
284 | QAbstractPrintDialog::PrintPageRange)));
285 MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintToFile), false);
286 MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintSelection), true);
287 MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintPageRange), true);
288
289 dlg.setEnabledOptions(QAbstractPrintDialog::PrintSelection);
290 MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintToFile), false);
291 MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintSelection), true);
292 MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintPageRange), false);
293}
294
295void tst_QPrinter::testMargins_data()
296{
297 QTest::addColumn<PrinterPtr>(name: "printer");
298 QTest::addColumn<QPrinter::Orientation>(name: "orientation");
299 QTest::addColumn<bool>(name: "fullpage");
300 QTest::addColumn<QPrinter::PageSize>(name: "pagesize");
301 QTest::addColumn<bool>(name: "withPainter");
302
303 const PrinterPtr printer(new QPrinter);
304 QTest::newRow(dataTag: "data0") << printer << QPrinter::Portrait << true << QPrinter::A4 << false;
305 QTest::newRow(dataTag: "data1") << printer << QPrinter::Landscape << true << QPrinter::A4 << false;
306 QTest::newRow(dataTag: "data2") << printer << QPrinter::Landscape << false << QPrinter::A4 << false;
307 QTest::newRow(dataTag: "data3") << printer << QPrinter::Portrait << false << QPrinter::A4 << false;
308 QTest::newRow(dataTag: "data4") << printer << QPrinter::Portrait << true << QPrinter::A4 << true;
309 QTest::newRow(dataTag: "data5") << printer << QPrinter::Landscape << true << QPrinter::A4 << true;
310 QTest::newRow(dataTag: "data6") << printer << QPrinter::Landscape << false << QPrinter::A4 << true;
311 QTest::newRow(dataTag: "data7") << printer << QPrinter::Portrait << false << QPrinter::A4 << true;
312}
313
314void tst_QPrinter::testMargins()
315{
316 QFETCH(PrinterPtr, printer);
317 QFETCH(bool, withPainter);
318 QFETCH(QPrinter::Orientation, orientation);
319 QFETCH(QPrinter::PageSize, pagesize);
320 QFETCH(bool, fullpage);
321 QPainter *painter = 0;
322 printer->setOutputFileName(testFileName(prefix: QLatin1String("silly"), suffix: QString()));
323 printer->setOrientation(orientation);
324 printer->setFullPage(fullpage);
325 printer->setPageSize(pagesize);
326 if (withPainter)
327 painter = new QPainter(printer.data());
328
329 if (painter)
330 delete painter;
331}
332
333void tst_QPrinter::testMulitpleSets_data()
334{
335 QTest::addColumn<int>(name: "resolution");
336 QTest::addColumn<int>(name: "pageSize");
337 QTest::addColumn<int>(name: "widthMMAfter");
338 QTest::addColumn<int>(name: "heightMMAfter");
339
340
341 QTest::newRow(dataTag: "lowRes") << int(QPrinter::ScreenResolution) << int(QPrinter::A4) << 210 << 297;
342 QTest::newRow(dataTag: "lowResLetter") << int(QPrinter::ScreenResolution) << int(QPrinter::Letter) << 216 << 279;
343 QTest::newRow(dataTag: "lowResA5") << int(QPrinter::ScreenResolution) << int(QPrinter::A5) << 148 << 210;
344 QTest::newRow(dataTag: "midRes") << int(QPrinter::PrinterResolution) << int(QPrinter::A4) << 210 << 297;
345 QTest::newRow(dataTag: "midResLetter") << int(QPrinter::PrinterResolution) << int(QPrinter::Letter) << 216 << 279;
346 QTest::newRow(dataTag: "midResA5") << int(QPrinter::PrinterResolution) << int(QPrinter::A5) << 148 << 210;
347 QTest::newRow(dataTag: "highRes") << int(QPrinter::HighResolution) << int(QPrinter::A4) << 210 << 297;
348 QTest::newRow(dataTag: "highResLetter") << int(QPrinter::HighResolution) << int(QPrinter::Letter) << 216 << 279;
349 QTest::newRow(dataTag: "highResA5") << int(QPrinter::HighResolution) << int(QPrinter::A5) << 148 << 210;
350}
351
352static void computePageValue(const QPrinter &printer, int &retWidth, int &retHeight)
353{
354 const double Inch2MM = 25.4;
355
356 double width = double(printer.paperRect().width()) / printer.logicalDpiX() * Inch2MM;
357 double height = double(printer.paperRect().height()) / printer.logicalDpiY() * Inch2MM;
358 retWidth = qRound(d: width);
359 retHeight = qRound(d: height);
360}
361
362void tst_QPrinter::testMulitpleSets()
363{
364 // A very simple test, but Mac needs to have its format "validated" if the format is changed
365 // This takes care of that.
366 QFETCH(int, resolution);
367 QFETCH(int, pageSize);
368 QFETCH(int, widthMMAfter);
369 QFETCH(int, heightMMAfter);
370
371
372 QPrinter::PrinterMode mode = QPrinter::PrinterMode(resolution);
373 QPrinter::PageSize printerPageSize = QPrinter::PageSize(pageSize);
374 QPrinter printer(mode);
375 printer.setFullPage(true);
376
377 int paperWidth, paperHeight;
378 //const int Tolerance = 2;
379
380 computePageValue(printer, retWidth&: paperWidth, retHeight&: paperHeight);
381 printer.setPageSize(printerPageSize);
382
383 if (printer.pageSize() != printerPageSize) {
384 QSKIP("Current page size is not supported on this printer");
385 return;
386 }
387
388 QVERIFY(qAbs(printer.widthMM() - widthMMAfter) <= 2);
389 QVERIFY(qAbs(printer.heightMM() - heightMMAfter) <= 2);
390
391 computePageValue(printer, retWidth&: paperWidth, retHeight&: paperHeight);
392
393 QVERIFY(qAbs(paperWidth - widthMMAfter) <= 2);
394 QVERIFY(qAbs(paperHeight - heightMMAfter) <= 2);
395
396 // Set it again and see if it still works.
397 printer.setPageSize(printerPageSize);
398 QVERIFY(qAbs(printer.widthMM() - widthMMAfter) <= 2);
399 QVERIFY(qAbs(printer.heightMM() - heightMMAfter) <= 2);
400
401 printer.setOrientation(QPrinter::Landscape);
402 computePageValue(printer, retWidth&: paperWidth, retHeight&: paperHeight);
403 QVERIFY(qAbs(paperWidth - heightMMAfter) <= 2);
404 QVERIFY(qAbs(paperHeight - widthMMAfter) <= 2);
405}
406
407void tst_QPrinter::outputFormatFromSuffix()
408{
409 if (QPrinterInfo::availablePrinters().size() == 0)
410 QSKIP("No printers available.");
411 QPrinter p;
412 QCOMPARE(p.outputFormat(), QPrinter::NativeFormat);
413 p.setOutputFileName(testPdfFileName(prefix: QLatin1String("test")));
414 QCOMPARE(p.outputFormat(), QPrinter::PdfFormat);
415 p.setOutputFileName(QString());
416 QCOMPARE(p.outputFormat(), QPrinter::NativeFormat);
417}
418
419void tst_QPrinter::testPageMargins_data()
420{
421 QTest::addColumn<qreal>(name: "left");
422 QTest::addColumn<qreal>(name: "top");
423 QTest::addColumn<qreal>(name: "right");
424 QTest::addColumn<qreal>(name: "bottom");
425 QTest::addColumn<int>(name: "unit");
426
427 // Use custom margins that will exceed most printers minimum allowed
428 QTest::newRow(dataTag: "data0") << qreal(25.5) << qreal(26.5) << qreal(27.5) << qreal(28.5) << static_cast<int>(QPrinter::Millimeter);
429 QTest::newRow(dataTag: "data1") << qreal(55.5) << qreal(56.5) << qreal(57.5) << qreal(58.5) << static_cast<int>(QPrinter::Point);
430 QTest::newRow(dataTag: "data2") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast<int>(QPrinter::Inch);
431 QTest::newRow(dataTag: "data3") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast<int>(QPrinter::Pica);
432 QTest::newRow(dataTag: "data4") << qreal(55.5) << qreal(56.5) << qreal(57.5) << qreal(58.5) << static_cast<int>(QPrinter::Didot);
433 QTest::newRow(dataTag: "data5") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast<int>(QPrinter::Cicero);
434}
435
436void tst_QPrinter::testPageMargins()
437{
438 QPrinter obj1;
439
440 QFETCH(qreal, left);
441 QFETCH(qreal, top);
442 QFETCH(qreal, right);
443 QFETCH(qreal, bottom);
444 QFETCH(int, unit);
445
446 QPageLayout layout = QPageLayout(QPageSize(QPageSize::A0), QPageLayout::Portrait,
447 QMarginsF(left, top, right, bottom), QPageLayout::Unit(unit));
448
449 qreal nLeft, nTop, nRight, nBottom;
450
451 obj1.setPageMargins(left, top, right, bottom, unit: QPrinter::Unit(unit));
452
453 obj1.getPageMargins(left: &nLeft, top: &nTop, right: &nRight, bottom: &nBottom, unit: QPrinter::Millimeter);
454 QCOMPARE(nLeft, layout.margins(QPageLayout::Millimeter).left());
455 QCOMPARE(nRight, layout.margins(QPageLayout::Millimeter).right());
456 QCOMPARE(nTop, layout.margins(QPageLayout::Millimeter).top());
457 QCOMPARE(nBottom, layout.margins(QPageLayout::Millimeter).bottom());
458
459 obj1.getPageMargins(left: &nLeft, top: &nTop, right: &nRight, bottom: &nBottom, unit: QPrinter::Point);
460 QCOMPARE(nLeft, layout.margins(QPageLayout::Point).left());
461 QCOMPARE(nRight, layout.margins(QPageLayout::Point).right());
462 QCOMPARE(nTop, layout.margins(QPageLayout::Point).top());
463 QCOMPARE(nBottom, layout.margins(QPageLayout::Point).bottom());
464
465 obj1.getPageMargins(left: &nLeft, top: &nTop, right: &nRight, bottom: &nBottom, unit: QPrinter::Inch);
466 QCOMPARE(nLeft, layout.margins(QPageLayout::Inch).left());
467 QCOMPARE(nRight, layout.margins(QPageLayout::Inch).right());
468 QCOMPARE(nTop, layout.margins(QPageLayout::Inch).top());
469 QCOMPARE(nBottom, layout.margins(QPageLayout::Inch).bottom());
470
471 obj1.getPageMargins(left: &nLeft, top: &nTop, right: &nRight, bottom: &nBottom, unit: QPrinter::Pica);
472 QCOMPARE(nLeft, layout.margins(QPageLayout::Pica).left());
473 QCOMPARE(nRight, layout.margins(QPageLayout::Pica).right());
474 QCOMPARE(nTop, layout.margins(QPageLayout::Pica).top());
475 QCOMPARE(nBottom, layout.margins(QPageLayout::Pica).bottom());
476
477 obj1.getPageMargins(left: &nLeft, top: &nTop, right: &nRight, bottom: &nBottom, unit: QPrinter::Didot);
478 QCOMPARE(nLeft, layout.margins(QPageLayout::Didot).left());
479 QCOMPARE(nRight, layout.margins(QPageLayout::Didot).right());
480 QCOMPARE(nTop, layout.margins(QPageLayout::Didot).top());
481 QCOMPARE(nBottom, layout.margins(QPageLayout::Didot).bottom());
482
483 obj1.getPageMargins(left: &nLeft, top: &nTop, right: &nRight, bottom: &nBottom, unit: QPrinter::Cicero);
484 QCOMPARE(nLeft, layout.margins(QPageLayout::Cicero).left());
485 QCOMPARE(nRight, layout.margins(QPageLayout::Cicero).right());
486 QCOMPARE(nTop, layout.margins(QPageLayout::Cicero).top());
487 QCOMPARE(nBottom, layout.margins(QPageLayout::Cicero).bottom());
488}
489
490void tst_QPrinter::errorReporting()
491{
492 QPrinter p;
493 p.setOutputFormat(QPrinter::PdfFormat);
494 QCOMPARE(p.isValid(), true);
495 QPainter painter;
496#ifndef Q_OS_WIN
497 // not sure how to choose a never-writable file on windows. But its QFile behavior anyway, so lets rely on it failing elsewhere
498 p.setOutputFileName("/foobar/nonwritable.pdf");
499 QCOMPARE(painter.begin(&p), false); // it should check the output file is writable
500#endif
501 p.setOutputFileName(testPdfFileName(prefix: QLatin1String("test")));
502 QCOMPARE(painter.begin(&p), true); // it should check the output
503 QCOMPARE(p.isValid(), true);
504 painter.end();
505}
506
507void tst_QPrinter::testCustomPageSizes()
508{
509 QPrinter p;
510
511 QSizeF customSize(7.0, 11.0);
512 p.setPaperSize(paperSize: customSize, unit: QPrinter::Inch);
513
514 QSizeF paperSize = p.paperSize(unit: QPrinter::Inch);
515 QCOMPARE(paperSize.width(), customSize.width());
516 QCOMPARE(paperSize.height(), customSize.height());
517
518 QPrinter p2(QPrinter::HighResolution);
519 p2.setPaperSize(paperSize: customSize, unit: QPrinter::Inch);
520 paperSize = p.paperSize(unit: QPrinter::Inch);
521 QCOMPARE(paperSize.width(), customSize.width());
522 QCOMPARE(paperSize.height(), customSize.height());
523
524 const QSizeF sizeInPixels = p.paperSize(unit: QPrinter::DevicePixel);
525 QPrinter p3;
526 p3.setPaperSize(paperSize: sizeInPixels, unit: QPrinter::DevicePixel);
527 paperSize = p3.paperSize(unit: QPrinter::Inch);
528 QCOMPARE(paperSize.width(), customSize.width());
529 QCOMPARE(paperSize.height(), customSize.height());
530 QPageSize pageSize = p3.pageLayout().pageSize();
531 QCOMPARE(pageSize.key(), QString("Custom.504x792"));
532 QCOMPARE(pageSize.name(), QString("Custom (504pt x 792pt)"));
533}
534
535void tst_QPrinter::customPaperSizeAndMargins_data()
536{
537 QTest::addColumn<bool>(name: "pdf");
538 QTest::addColumn<bool>(name: "before");
539 QTest::addColumn<qreal>(name: "left");
540 QTest::addColumn<qreal>(name: "top");
541 QTest::addColumn<qreal>(name: "right");
542 QTest::addColumn<qreal>(name: "bottom");
543
544 // Use custom margins that will exceed most printers minimum allowed
545 QTest::newRow(dataTag: "beforeNoPDF") << false << true << qreal(30) << qreal(30) << qreal(30) << qreal(30);
546 QTest::newRow(dataTag: "beforePDF") << true << true << qreal(30) << qreal(30) << qreal(30) << qreal(30);
547 QTest::newRow(dataTag: "afterNoPDF") << false << false << qreal(30) << qreal(30) << qreal(30) << qreal(30);
548 QTest::newRow(dataTag: "afterAfterPDF") << true << false << qreal(30) << qreal(30) << qreal(30) << qreal(30);
549}
550
551void tst_QPrinter::customPaperSizeAndMargins()
552{
553 QFETCH(bool, pdf);
554 QFETCH(bool, before);
555 QFETCH(qreal, left);
556 QFETCH(qreal, top);
557 QFETCH(qreal, right);
558 QFETCH(qreal, bottom);
559
560 qreal tolerance = 0.05;
561 qreal getLeft = 0;
562 qreal getRight = 0;
563 qreal getTop = 0;
564 qreal getBottom = 0;
565 // Use a custom page size that most printers should support, A4 is 210x297
566 // TODO Use print device api when available
567 QSizeF customSize(200.0, 300.0);
568
569 QPrinter p;
570 if (pdf)
571 p.setOutputFormat(QPrinter::PdfFormat);
572 if (before)
573 p.setPageMargins(left, top, right, bottom, unit: QPrinter::Millimeter);
574 p.setPaperSize(paperSize: customSize, unit: QPrinter::Millimeter);
575 p.getPageMargins(left: &getLeft, top: &getTop, right: &getRight, bottom: &getBottom, unit: QPrinter::Millimeter);
576 if (before) {
577 QVERIFY(fabs(left - getLeft) < tolerance);
578 QVERIFY(fabs(left - getTop) < tolerance);
579 QVERIFY(fabs(left - getRight) < tolerance);
580 QVERIFY(fabs(left - getBottom) < tolerance);
581 } else {
582 p.setPageMargins(left, top, right, bottom, unit: QPrinter::Millimeter);
583 p.getPageMargins(left: &getLeft, top: &getTop, right: &getRight, bottom: &getBottom, unit: QPrinter::Millimeter);
584 QVERIFY(fabs(left - getLeft) < tolerance);
585 QVERIFY(fabs(left - getTop) < tolerance);
586 QVERIFY(fabs(left - getRight) < tolerance);
587 QVERIFY(fabs(left - getBottom) < tolerance);
588 }
589}
590
591#if QT_CONFIG(completer) && QT_CONFIG(filedialog)
592void tst_QPrinter::printDialogCompleter()
593{
594 QPrintDialog dialog;
595 dialog.printer()->setOutputFileName(testPdfFileName(prefix: QLatin1String("file")));
596#if defined(Q_OS_WIN) || defined(Q_OS_DARWIN)
597 if (dialog.printer()->outputFormat() != QPrinter::NativeFormat)
598 QSKIP("Dialog cannot be used with non-native formats");
599#endif
600 dialog.setEnabledOptions(QAbstractPrintDialog::PrintToFile);
601 dialog.show();
602
603 QVERIFY(QTest::qWaitForWindowActive(&dialog));
604
605 QTest::keyClick(widget: &dialog, key: Qt::Key_Tab);
606 QTest::keyClick(widget: &dialog, key: 'P');
607 // The test passes if it doesn't crash.
608}
609#endif
610
611static void printPage(QPainter *painter)
612{
613 painter->setPen(QPen(Qt::black, 4));
614 painter->drawRect(x: 50, y: 60, w: 70, h: 80);
615}
616
617void tst_QPrinter::taskQTBUG4497_reusePrinterOnDifferentFiles()
618{
619 const QString fileName1 = testPdfFileName(prefix: QLatin1String("out1_"));
620 const QString fileName2 = testPdfFileName(prefix: QLatin1String("out2_"));
621
622 QPrinter printer;
623 {
624
625 printer.setOutputFileName(fileName1);
626 QPainter painter(&printer);
627 printPage(painter: &painter);
628
629 }
630 {
631
632 printer.setOutputFileName(fileName2);
633 QPainter painter(&printer);
634 printPage(painter: &painter);
635
636 }
637 QFile file1(fileName1);
638 QVERIFY(file1.open(QIODevice::ReadOnly));
639
640 QFile file2(fileName2);
641 QVERIFY(file2.open(QIODevice::ReadOnly));
642
643 while (!file1.atEnd() && !file2.atEnd()) {
644 QByteArray file1Line = file1.readLine();
645 QByteArray file2Line = file2.readLine();
646
647 if (!file1Line.contains(c: "CreationDate"))
648 QCOMPARE(file1Line, file2Line);
649 }
650
651 QVERIFY(file1.atEnd());
652 QVERIFY(file2.atEnd());
653}
654
655void tst_QPrinter::testCurrentPage()
656{
657 QPrinter printer;
658 printer.setFromTo(fromPage: 1, toPage: 10);
659
660 // Test set print range
661 printer.setPrintRange(QPrinter::CurrentPage);
662 QCOMPARE(printer.printRange(), QPrinter::CurrentPage);
663 QCOMPARE(printer.fromPage(), 1);
664 QCOMPARE(printer.toPage(), 10);
665
666 QPrintDialog dialog(&printer);
667
668 // Test default Current Page option to off
669 QCOMPARE(dialog.isOptionEnabled(QPrintDialog::PrintCurrentPage), false);
670
671 // Test enable Current Page option
672 dialog.setOption(option: QPrintDialog::PrintCurrentPage);
673 QCOMPARE(dialog.isOptionEnabled(QPrintDialog::PrintCurrentPage), true);
674
675}
676
677void tst_QPrinter::testPdfTitle()
678{
679 const QString fileName = testPdfFileName(prefix: QLatin1String("file"));
680
681 // Check the document name is represented correctly in produced pdf
682 {
683 QPainter painter;
684 QPrinter printer;
685 // This string is just the UTF-8 encoding of the string: \()f &oslash; hiragana o
686 const unsigned char titleBuf[]={0x5c, 0x28, 0x29, 0x66, 0xc3, 0xb8, 0xe3, 0x81, 0x8a, 0x00};
687 const char *title = reinterpret_cast<const char*>(titleBuf);
688 printer.setOutputFileName(fileName);
689 printer.setDocName(QString::fromUtf8(str: title));
690 painter.begin(&printer);
691 painter.end();
692 }
693 QFile file(fileName);
694 QVERIFY(file.open(QIODevice::ReadOnly));
695 // The we expect the title to appear in the PDF as:
696 // ASCII('\title (') UTF16(\\\(\)f &oslash; hiragana o) ASCII(')').
697 // which has the following binary representation
698 const unsigned char expectedBuf[] = {
699 0x2f, 0x54, 0x69, 0x74, 0x6c, 0x65, 0x20, 0x28, 0xfe,
700 0xff, 0x00, 0x5c, 0x5c, 0x00, 0x5c, 0x28, 0x00, 0x5c,
701 0x29, 0x00, 0x66, 0x00, 0xf8, 0x30, 0x4a, 0x29};
702 const char *expected = reinterpret_cast<const char*>(expectedBuf);
703 QVERIFY(file.readAll().contains(QByteArray(expected, 26)));
704}
705
706void tst_QPrinter::customPaperNameSettingBySize()
707{
708 QPrinter printer(QPrinter::HighResolution);
709 QPrinterInfo info(printer);
710 QList<QPageSize> sizes = info.supportedPageSizes();
711 if (sizes.size() == 0)
712 QSKIP("No printers installed on this machine");
713 for (int i=0; i<sizes.size(); i++) {
714 printer.setPaperSize(paperSize: sizes.at(i).size(units: QPageSize::Millimeter), unit: QPrinter::Millimeter);
715 QCOMPARE(sizes.at(i).size(QPageSize::Millimeter), printer.paperSize(QPrinter::Millimeter));
716 // Some printers have the same size under different names which can cause a problem for the test
717 // So we look at all the other sizes to see if one also matches as we don't know which order they are in
718 QSizeF paperSize = sizes.at(i).size(units: QPageSize::Millimeter);
719 QString paperName = printer.paperName();
720 bool paperNameFound = (sizes.at(i).name() == paperName);
721 if (!paperNameFound) {
722 for (int j = 0; j < sizes.size(); ++j) {
723 if (j != i
724 && sizes.at(i: j).size(units: QPageSize::Millimeter) == paperSize
725 && sizes.at(i: j).name() == paperName) {
726 paperNameFound = true;
727 break;
728 }
729 }
730 }
731 // Fail with the original values
732 if (!paperNameFound) {
733 qDebug() << "supportedPageSizes() = " << sizes;
734 QEXPECT_FAIL("", "Paper Name mismatch: please report this failure at bugreports.qt.io", Continue);
735 QCOMPARE(sizes.at(i).name(), printer.paperName());
736 }
737 }
738
739 // Check setting a custom size after setting a standard one works
740 QSizeF customSize(200, 300);
741 printer.setPaperSize(paperSize: customSize, unit: QPrinter::Millimeter);
742 QCOMPARE(printer.paperSize(QPrinter::Millimeter), customSize);
743 QCOMPARE(printer.paperSize(), QPrinter::Custom);
744
745 // Finally check setting a standard size after a custom one works
746 printer.setPaperSize(paperSize: sizes.at(i: 0).size(units: QPageSize::Millimeter), unit: QPrinter::Millimeter);
747 QCOMPARE(printer.paperName(), sizes.at(0).name());
748 QCOMPARE(printer.paperSize(QPrinter::Millimeter), sizes.at(0).size(QPageSize::Millimeter));
749}
750
751void tst_QPrinter::customPaperNameSettingByName()
752{
753 QPrinter printer(QPrinter::HighResolution);
754 QPrinterInfo info(printer);
755 QList<QPageSize> sizes = info.supportedPageSizes();
756 if (sizes.size() == 0)
757 QSKIP("No printers installed on this machine");
758 for (int i=0; i<sizes.size(); i++) {
759 printer.setPaperName(sizes.at(i).name());
760 QCOMPARE(sizes.at(i).name(), printer.paperName());
761 QCOMPARE(sizes.at(i).size(QPageSize::Millimeter), printer.paperSize(QPrinter::Millimeter));
762 }
763}
764
765// Test QPrintEngine keys and their QPrinter setters/getters
766
767void tst_QPrinter::testMultipleKeys()
768{
769 // Tests multiple keys preservation, note are only ones that are consistent across all engines
770
771 QPrinter native;
772 if (native.outputFormat() == QPrinter::NativeFormat) {
773 // Check default values
774 QCOMPARE(native.fullPage(), false);
775 QCOMPARE(native.orientation(), QPrinter::Portrait);
776 QCOMPARE(native.copyCount(), 1);
777 QCOMPARE(native.collateCopies(), true);
778 QCOMPARE(native.printRange(), QPrinter::AllPages);
779
780 // Change values
781 native.setFullPage(true);
782 native.setOrientation(QPrinter::Landscape);
783 native.setCopyCount(9);
784 native.setCollateCopies(false);
785 native.setPrintRange(QPrinter::CurrentPage);
786
787 // Check changed values
788 QCOMPARE(native.fullPage(), true);
789 QCOMPARE(native.orientation(), QPrinter::Landscape);
790 QCOMPARE(native.copyCount(), 9);
791 QCOMPARE(native.collateCopies(), false);
792 QCOMPARE(native.printRange(), QPrinter::CurrentPage);
793
794 // Test value preservation
795 native.setOutputFormat(QPrinter::PdfFormat);
796 QCOMPARE(native.fullPage(), true);
797 QCOMPARE(native.orientation(), QPrinter::Landscape);
798 QCOMPARE(native.copyCount(), 9);
799 QCOMPARE(native.collateCopies(), false);
800 QCOMPARE(native.printRange(), QPrinter::CurrentPage);
801
802 // Change values
803 native.setFullPage(false);
804 native.setOrientation(QPrinter::Portrait);
805 native.setCopyCount(5);
806 native.setCollateCopies(true);
807 native.setPrintRange(QPrinter::PageRange);
808
809 // Check changed values
810 QCOMPARE(native.fullPage(), false);
811 QCOMPARE(native.orientation(), QPrinter::Portrait);
812 QCOMPARE(native.copyCount(), 5);
813 QCOMPARE(native.collateCopies(), true);
814 QCOMPARE(native.printRange(), QPrinter::PageRange);
815 } else {
816 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
817 }
818}
819
820void tst_QPrinter::collateCopies()
821{
822 // collateCopies() / setCollateCopies() / PPK_ColorMode
823 // PdfFormat: Supported, default true
824 // NativeFormat, Cups: Supported, default true
825 // NativeFormat, Win: Supported, default true
826 // NativeFormat, Mac: Supported, default true
827
828 QPrinter pdf;
829 pdf.setOutputFormat(QPrinter::PdfFormat);
830 QCOMPARE(pdf.collateCopies(), true);
831 pdf.setCollateCopies(false);
832 QCOMPARE(pdf.collateCopies(), false);
833
834 QPrinter native;
835 if (native.outputFormat() == QPrinter::NativeFormat) {
836 // Test default
837 QCOMPARE(native.collateCopies(), true);
838
839 // Test set/get
840 bool expected = false;
841 native.setCollateCopies(expected);
842 QCOMPARE(native.collateCopies(), expected);
843
844 // Test value preservation
845 native.setOutputFormat(QPrinter::PdfFormat);
846 QCOMPARE(native.collateCopies(), expected);
847 native.setOutputFormat(QPrinter::NativeFormat);
848 QCOMPARE(native.collateCopies(), expected);
849 } else {
850 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
851 }
852}
853
854void tst_QPrinter::colorMode()
855{
856 // colorMode() / setColorMode() / PPK_ColorMode
857 // PdfFormat: Supported, default QPrinter::Color
858 // NativeFormat, Cups: Supported, default QPrinter::Color
859 // NativeFormat, Win: Supported if valid DevMode, otherwise QPrinter::Color
860 // NativeFormat, Mac: Unsupported, always QPrinter::Color
861
862 QPrinter pdf;
863 pdf.setOutputFormat(QPrinter::PdfFormat);
864 QCOMPARE(pdf.colorMode(), QPrinter::Color);
865 pdf.setColorMode(QPrinter::GrayScale);
866 QCOMPARE(pdf.colorMode(), QPrinter::GrayScale);
867
868 QPrinter native;
869 if (native.outputFormat() == QPrinter::NativeFormat) {
870 // Test default
871 // TODO Printer specific, need QPrinterInfo::colorMode()
872 //QCOMPARE(native.colorMode(), QPrinter::Color);
873
874 // Test set/get
875 QPrinter::ColorMode expected = QPrinter::GrayScale;
876 native.setColorMode(expected);
877#ifdef Q_OS_MAC
878 expected = QPrinter::Color;
879#endif // Q_OS_MAC
880 QCOMPARE(native.colorMode(), expected);
881
882 // Test value preservation
883 native.setOutputFormat(QPrinter::PdfFormat);
884 QCOMPARE(native.colorMode(), expected);
885 native.setOutputFormat(QPrinter::NativeFormat);
886 QCOMPARE(native.colorMode(), expected);
887 } else {
888 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
889 }
890}
891
892void tst_QPrinter::copyCount()
893{
894 // copyCount() / setCopyCount() / PPK_CopyCount
895 // numCopies() / setNumCopies() / PPK_NumberOfCopies
896 // actualNumCopies() / supportsMultipleCopies()
897 // PdfFormat: Supported, multiple copies unsupported, default 1
898 // NativeFormat, Cups: Supported, multiple copies supported, default 1
899 // NativeFormat, Win: Supported, multiple copies supported, default 1
900 // NativeFormat, Mac: Supported, multiple copies supported, default 1
901
902 QPrinter pdf;
903 pdf.setOutputFormat(QPrinter::PdfFormat);
904 QCOMPARE(pdf.supportsMultipleCopies(), false);
905 QCOMPARE(pdf.copyCount(), 1);
906 QCOMPARE(pdf.numCopies(), 1);
907 QCOMPARE(pdf.actualNumCopies(), 1);
908 pdf.setCopyCount(9);
909 QCOMPARE(pdf.copyCount(), 9);
910 QCOMPARE(pdf.numCopies(), 9);
911 QCOMPARE(pdf.actualNumCopies(), 9);
912 pdf.setNumCopies(7);
913 QCOMPARE(pdf.copyCount(), 7);
914 QCOMPARE(pdf.numCopies(), 7);
915 QCOMPARE(pdf.actualNumCopies(), 7);
916
917 QPrinter native;
918 if (native.outputFormat() == QPrinter::NativeFormat) {
919 // Test default
920 QCOMPARE(native.supportsMultipleCopies(), true);
921 QCOMPARE(native.copyCount(), 1);
922 QCOMPARE(native.numCopies(), 1);
923 QCOMPARE(native.actualNumCopies(), 1);
924
925 // Test set/get
926 native.setCopyCount(9);
927 QCOMPARE(native.copyCount(), 9);
928 QCOMPARE(native.numCopies(), 1);
929 QCOMPARE(native.actualNumCopies(), 9);
930 native.setNumCopies(7);
931 QCOMPARE(native.copyCount(), 7);
932 QCOMPARE(native.numCopies(), 1);
933 QCOMPARE(native.actualNumCopies(), 7);
934
935 // Test value preservation
936 native.setOutputFormat(QPrinter::PdfFormat);
937 QCOMPARE(native.copyCount(), 7);
938 QCOMPARE(native.numCopies(), 7);
939 QCOMPARE(native.actualNumCopies(), 7);
940 native.setOutputFormat(QPrinter::NativeFormat);
941 QCOMPARE(native.copyCount(), 7);
942 QCOMPARE(native.numCopies(), 1);
943 QCOMPARE(native.actualNumCopies(), 7);
944 } else {
945 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
946 }
947}
948
949void tst_QPrinter::creator()
950{
951 // creator() / setCreator() / PPK_Creator
952 // PdfFormat: Supported, default QString()
953 // NativeFormat, Cups: Supported, default QString()
954 // NativeFormat, Win: Supported, default QString()
955 // NativeFormat, Mac: Supported, default QString()
956
957 QPrinter pdf;
958 pdf.setOutputFormat(QPrinter::PdfFormat);
959 QCOMPARE(pdf.creator(), QString());
960 pdf.setCreator(QStringLiteral("Test Creator"));
961 QCOMPARE(pdf.creator(), QStringLiteral("Test Creator"));
962
963 QPrinter native;
964 if (native.outputFormat() == QPrinter::NativeFormat) {
965 // Test default
966 QCOMPARE(native.creator(), QString());
967
968 // Test set/get
969 QString expected = QStringLiteral("Test Creator");
970 native.setCreator(expected);
971 QCOMPARE(native.creator(), expected);
972
973 // Test value preservation
974 native.setOutputFormat(QPrinter::PdfFormat);
975 QCOMPARE(native.creator(), expected);
976 native.setOutputFormat(QPrinter::NativeFormat);
977 QCOMPARE(native.creator(), expected);
978 } else {
979 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
980 }
981}
982
983void tst_QPrinter::docName()
984{
985 // docName() / setDocName() / PPK_DocumentName
986 // PdfFormat: Supported, default QString()
987 // NativeFormat, Cups: Supported, default QString()
988 // NativeFormat, Win: Supported, default QString()
989 // NativeFormat, Mac: Supported, default QString()
990
991 QPrinter pdf;
992 pdf.setOutputFormat(QPrinter::PdfFormat);
993 QCOMPARE(pdf.docName(), QString());
994 pdf.setDocName(QStringLiteral("Test Name"));
995 QCOMPARE(pdf.docName(), QStringLiteral("Test Name"));
996
997 QPrinter native;
998 if (native.outputFormat() == QPrinter::NativeFormat) {
999 // Test default
1000 QCOMPARE(native.docName(), QString());
1001
1002 // Test set/get
1003 QString expected = QStringLiteral("Test Name");
1004 native.setDocName(expected);
1005 QCOMPARE(native.docName(), expected);
1006
1007 // Test value preservation
1008 native.setOutputFormat(QPrinter::PdfFormat);
1009 QCOMPARE(native.docName(), expected);
1010 native.setOutputFormat(QPrinter::NativeFormat);
1011 QCOMPARE(native.docName(), expected);
1012 } else {
1013 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1014 }
1015}
1016
1017void tst_QPrinter::duplex()
1018{
1019 // duplex()) / setDuplex() / PPK_Duplex
1020 // PdfFormat: Supported, default QPrinter::DuplexNone
1021 // NativeFormat, Cups: Supported, default to printer default
1022 // NativeFormat, Win: Supported, default to printer default
1023 // NativeFormat, Mac: Supported, default to printer default
1024
1025 QPrinter pdf;
1026 pdf.setOutputFormat(QPrinter::PdfFormat);
1027 QCOMPARE(pdf.duplex(), QPrinter::DuplexNone);
1028 pdf.setDuplex(QPrinter::DuplexAuto);
1029 QCOMPARE(pdf.duplex(), QPrinter::DuplexNone); // pdf doesn't have the concept of duplex
1030
1031 QPrinter native;
1032 if (native.outputFormat() == QPrinter::NativeFormat) {
1033 // Test default
1034 QPrinterInfo printerInfo = QPrinterInfo::defaultPrinter();
1035 QPrinter::DuplexMode expected = printerInfo.defaultDuplexMode();
1036 QCOMPARE(native.duplex(), expected);
1037 // Test set/get (skipping Auto as that will return something different)
1038 foreach (QPrinter::DuplexMode mode, printerInfo.supportedDuplexModes()) {
1039 if (mode != expected && mode != QPrinter::DuplexAuto) {
1040 expected = mode;
1041 break;
1042 }
1043 }
1044 native.setDuplex(expected);
1045 QCOMPARE(native.duplex(), expected);
1046
1047 // Test value preservation
1048 native.setOutputFormat(QPrinter::PdfFormat);
1049 QCOMPARE(native.duplex(), expected);
1050 native.setOutputFormat(QPrinter::NativeFormat);
1051 QCOMPARE(native.duplex(), expected);
1052
1053 // Test setting invalid option
1054 if (!printerInfo.supportedDuplexModes().contains(t: QPrinter::DuplexLongSide)) {
1055 native.setDuplex(QPrinter::DuplexLongSide);
1056 QCOMPARE(native.duplex(), expected);
1057 }
1058 } else {
1059 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1060 }
1061}
1062
1063void tst_QPrinter::doubleSidedPrinting()
1064{
1065 // PdfFormat: Supported, default false
1066 // NativeFormat, Cups: Supported, default to printer default
1067 // NativeFormat, Win: Supported, default to printer default
1068 // NativeFormat, Mac: Supported, default to printer default
1069
1070 QPrinter pdf;
1071 pdf.setOutputFormat(QPrinter::PdfFormat);
1072 QCOMPARE(pdf.doubleSidedPrinting(), false);
1073 pdf.setDoubleSidedPrinting(true);
1074 QCOMPARE(pdf.doubleSidedPrinting(), false); // pdf doesn't have the concept of duplex
1075
1076 QPrinter native;
1077 if (native.outputFormat() == QPrinter::NativeFormat) {
1078 // Test default
1079 QPrinterInfo printerInfo(native);
1080 bool expected = (printerInfo.defaultDuplexMode() != QPrinter::DuplexNone);
1081 QCOMPARE(native.doubleSidedPrinting(), expected);
1082
1083 // Test set/get, changing the expected value if possible
1084 expected = expected ? false : (printerInfo.supportedDuplexModes().count() > 1);
1085 native.setDoubleSidedPrinting(expected);
1086 QCOMPARE(native.doubleSidedPrinting(), expected);
1087
1088 // Test value preservation
1089 native.setOutputFormat(QPrinter::PdfFormat);
1090 QCOMPARE(native.doubleSidedPrinting(), expected);
1091 native.setOutputFormat(QPrinter::NativeFormat);
1092 QCOMPARE(native.doubleSidedPrinting(), expected);
1093 } else {
1094 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1095 }
1096}
1097
1098void tst_QPrinter::fontEmbedding()
1099{
1100 // fontEmbeddingEnabled() / setFontEmbeddingEnabled() / PPK_FontEmbedding
1101 // PdfFormat: Supported, default true
1102 // NativeFormat: Supported, default true
1103
1104 QPrinter pdf;
1105 pdf.setOutputFormat(QPrinter::PdfFormat);
1106 QCOMPARE(pdf.fontEmbeddingEnabled(), true);
1107 pdf.setFontEmbeddingEnabled(false);
1108 QCOMPARE(pdf.fontEmbeddingEnabled(), false);
1109
1110 QPrinter native;
1111 if (native.outputFormat() == QPrinter::NativeFormat) {
1112 // Test default
1113 QCOMPARE(native.fontEmbeddingEnabled(), true);
1114
1115 // Test set/get
1116 native.setFontEmbeddingEnabled(true);
1117 QCOMPARE(native.fontEmbeddingEnabled(), true);
1118
1119 // Test value preservation
1120 native.setOutputFormat(QPrinter::PdfFormat);
1121 QCOMPARE(native.fontEmbeddingEnabled(), true);
1122 native.setOutputFormat(QPrinter::NativeFormat);
1123 QCOMPARE(native.fontEmbeddingEnabled(), true);
1124 } else {
1125 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1126 }
1127}
1128
1129void tst_QPrinter::fullPage()
1130{
1131 // fullPage() / setFullPage() / PPK_FullPage
1132 // PdfFormat: Supported, default false
1133 // NativeFormat, Cups: Supported, default false
1134 // NativeFormat, Win: Supported, default false
1135 // NativeFormat, Mac: Supported, default false
1136
1137 QPrinter pdf;
1138 pdf.setOutputFormat(QPrinter::PdfFormat);
1139 QCOMPARE(pdf.fullPage(), false);
1140 pdf.setFullPage(true);
1141 QCOMPARE(pdf.fullPage(), true);
1142 pdf.setFullPage(false);
1143 QCOMPARE(pdf.fullPage(), false);
1144
1145 QPrinter native;
1146 if (native.outputFormat() == QPrinter::NativeFormat) {
1147 // Test default
1148 QCOMPARE(native.fullPage(), false);
1149
1150 // Test set/get
1151 bool expected = true;
1152 native.setFullPage(expected);
1153 QCOMPARE(native.fullPage(), expected);
1154
1155 // Test value preservation
1156 native.setOutputFormat(QPrinter::PdfFormat);
1157 QCOMPARE(native.fullPage(), expected);
1158 native.setOutputFormat(QPrinter::NativeFormat);
1159 QCOMPARE(native.fullPage(), expected);
1160
1161 // Test set/get
1162 expected = false;
1163 native.setFullPage(expected);
1164 QCOMPARE(native.fullPage(), expected);
1165
1166 // Test value preservation
1167 native.setOutputFormat(QPrinter::PdfFormat);
1168 QCOMPARE(native.fullPage(), expected);
1169 native.setOutputFormat(QPrinter::NativeFormat);
1170 QCOMPARE(native.fullPage(), expected);
1171 } else {
1172 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1173 }
1174}
1175
1176void tst_QPrinter::orientation()
1177{
1178 // orientation() / setOrientation() / PPK_Orientation
1179 // PdfFormat: Supported, default QPrinter::Portrait
1180 // NativeFormat, Cups: Supported, default QPrinter::Portrait
1181 // NativeFormat, Win: Supported, default QPrinter::Portrait
1182 // NativeFormat, Mac: Supported, default QPrinter::Portrait
1183
1184 QPrinter pdf;
1185 pdf.setOutputFormat(QPrinter::PdfFormat);
1186 QCOMPARE(pdf.orientation(), QPrinter::Portrait);
1187 pdf.setOrientation(QPrinter::Landscape);
1188 QCOMPARE(pdf.orientation(), QPrinter::Landscape);
1189
1190 QPrinter native;
1191 if (native.outputFormat() == QPrinter::NativeFormat) {
1192 // Test default
1193 // TODO Printer specific, need QPrinterInfo::orientation()
1194 //QCOMPARE(native.orientation(), QPrinter::Portrait);
1195
1196 // Test set/get
1197 QPrinter::Orientation expected = QPrinter::Landscape;
1198 native.setOrientation(expected);
1199 QCOMPARE(native.orientation(), expected);
1200
1201 // Test value preservation
1202 native.setOutputFormat(QPrinter::PdfFormat);
1203 QCOMPARE(native.orientation(), expected);
1204 native.setOutputFormat(QPrinter::NativeFormat);
1205 QCOMPARE(native.orientation(), expected);
1206
1207 // Test set/get
1208 expected = QPrinter::Portrait;
1209 native.setOrientation(expected);
1210 QCOMPARE(native.orientation(), expected);
1211
1212 // Test value preservation
1213 native.setOutputFormat(QPrinter::PdfFormat);
1214 QCOMPARE(native.orientation(), expected);
1215 native.setOutputFormat(QPrinter::NativeFormat);
1216 QCOMPARE(native.orientation(), expected);
1217 } else {
1218 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1219 }
1220}
1221
1222void tst_QPrinter::outputFileName()
1223{
1224 // outputFileName() / setOutputFileName() / PPK_OutputFileName
1225 // PdfFormat: Supported, default QString()
1226 // NativeFormat, Cups: Supported, default QString()
1227 // NativeFormat, Win: Supported, default QString()
1228 // NativeFormat, Mac: Supported, default QString()
1229
1230 QPrinter pdf;
1231 pdf.setOutputFormat(QPrinter::PdfFormat);
1232 QCOMPARE(pdf.outputFileName(), QString());
1233 const QString fileName = testFileName(QStringLiteral("Test File"), suffix: QString());
1234 pdf.setOutputFileName(fileName);
1235 QCOMPARE(pdf.outputFileName(), fileName);
1236
1237 QPrinter native;
1238 if (native.outputFormat() == QPrinter::NativeFormat) {
1239 // Test default
1240 QCOMPARE(native.outputFileName(), QString());
1241
1242 // Test set/get
1243 QString expected = fileName;
1244 native.setOutputFileName(expected);
1245 QCOMPARE(native.outputFileName(), expected);
1246
1247 // Test value preservation
1248 native.setOutputFormat(QPrinter::PdfFormat);
1249 QCOMPARE(native.outputFileName(), expected);
1250 native.setOutputFormat(QPrinter::NativeFormat);
1251 QCOMPARE(native.outputFileName(), expected);
1252 } else {
1253 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1254 }
1255}
1256
1257void tst_QPrinter::pageOrder()
1258{
1259 // pageOrder() / setPageOrder() / PPK_PageOrder
1260 // PdfFormat: Supported, default QPrinter::FirstPageFirst
1261 // NativeFormat, Cups: Supported, default QPrinter::FirstPageFirst
1262 // NativeFormat, Win: Unsupported, always QPrinter::FirstPageFirst
1263 // NativeFormat, Mac: Unsupported, always QPrinter::FirstPageFirst
1264
1265 QPrinter pdf;
1266 pdf.setOutputFormat(QPrinter::PdfFormat);
1267 QCOMPARE(pdf.pageOrder(), QPrinter::FirstPageFirst);
1268 pdf.setPageOrder(QPrinter::LastPageFirst);
1269 QCOMPARE(pdf.pageOrder(), QPrinter::LastPageFirst);
1270
1271 QPrinter native;
1272 if (native.outputFormat() == QPrinter::NativeFormat) {
1273 // Test default
1274 QCOMPARE(native.pageOrder(), QPrinter::FirstPageFirst);
1275
1276 // Test set/get
1277 QPrinter::PageOrder expected = QPrinter::LastPageFirst;
1278 native.setPageOrder(expected);
1279#if defined Q_OS_MAC || defined Q_OS_WIN
1280 expected = QPrinter::FirstPageFirst;
1281#endif // Q_OS_MAC || Q_OS_WIN
1282 QCOMPARE(native.pageOrder(), expected);
1283
1284 // Test value preservation
1285 native.setOutputFormat(QPrinter::PdfFormat);
1286 QCOMPARE(native.pageOrder(), expected);
1287 native.setOutputFormat(QPrinter::NativeFormat);
1288 QCOMPARE(native.pageOrder(), expected);
1289 } else {
1290 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1291 }
1292}
1293
1294void tst_QPrinter::pageSize()
1295{
1296 // Note PPK_PaperSize == PPK_PageSize
1297 // pageSize() / setPageSize() / PPK_PageSize
1298 // PdfFormat: Supported, defaults to QPrinter::A4
1299 // NativeFormat, Cups: Supported, defaults to printer default
1300 // NativeFormat, Win: Supported, defaults to printer default
1301 // NativeFormat, Mac: Supported, must be supported size, defaults to printer default
1302
1303 QPrinter pdf;
1304 pdf.setOutputFormat(QPrinter::PdfFormat);
1305 QCOMPARE(pdf.pageSize(), QPrinter::A4);
1306 pdf.setPageSize(QPrinter::A1);
1307 QCOMPARE(pdf.pageSize(), QPrinter::A1);
1308
1309 QPrinter native;
1310 if (native.outputFormat() == QPrinter::NativeFormat) {
1311 // Test default
1312 // TODO Printer specific, need QPrinterInfo::paperSize()
1313 //QCOMPARE(native.pageSize(), QPrinter::A4);
1314
1315 // Test set/get
1316 QPrinter::PaperSize expected = QPrinter::A4;
1317 QPrinterInfo info = QPrinterInfo::printerInfo(printerName: native.printerName());
1318 const auto &pageSizes = info.supportedPageSizes();
1319 for (const auto &pageSize : pageSizes) {
1320 const QPrinter::PaperSize supported = QPrinter::PaperSize(pageSize.id());
1321 if (supported != QPrinter::Custom && supported != native.paperSize()) {
1322 expected = supported;
1323 break;
1324 }
1325 }
1326 native.setPageSize(expected);
1327 QCOMPARE(native.pageSize(), expected);
1328
1329 // Test value preservation
1330 native.setOutputFormat(QPrinter::PdfFormat);
1331 QCOMPARE(native.pageSize(), expected);
1332 native.setOutputFormat(QPrinter::NativeFormat);
1333 QCOMPARE(native.pageSize(), expected);
1334 } else {
1335 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1336 }
1337}
1338
1339void tst_QPrinter::paperSize()
1340{
1341 // PPK_PaperSize == PPK_PageSize
1342 // paperSize() / setPaperSize() / PPK_PaperSize
1343 // pageSize() / setPageSize() / PPK_PageSize
1344 // PdfFormat: Supported, defaults to QPrinter::A4
1345 // NativeFormat, Cups: Supported, defaults to printer default
1346 // NativeFormat, Win: Supported, defaults to printer default
1347 // NativeFormat, Mac: Supported, must be supported size, defaults to printer default
1348
1349 QPrinter pdf;
1350 pdf.setOutputFormat(QPrinter::PdfFormat);
1351 QCOMPARE(pdf.paperSize(), QPrinter::A4);
1352 pdf.setPaperSize(QPrinter::A1);
1353 QCOMPARE(pdf.paperSize(), QPrinter::A1);
1354
1355 QPrinter native;
1356 if (native.outputFormat() == QPrinter::NativeFormat) {
1357 // Test default
1358 // TODO Printer specific, need QPrinterInfo::paperSize()
1359 //QCOMPARE(native.paperSize(), QPrinter::A4);
1360
1361 // Test set/get
1362 QPrinter::PaperSize expected = QPrinter::A4;
1363 QPrinterInfo info = QPrinterInfo::printerInfo(printerName: native.printerName());
1364 const auto &pageSizes = info.supportedPageSizes();
1365 for (const auto &pageSize : pageSizes) {
1366 const QPrinter::PaperSize supported = QPrinter::PaperSize(pageSize.id());
1367 if (supported != QPrinter::Custom && supported != native.paperSize()) {
1368 expected = supported;
1369 break;
1370 }
1371 }
1372 native.setPaperSize(expected);
1373 QCOMPARE(native.paperSize(), expected);
1374
1375 // Test value preservation
1376 native.setOutputFormat(QPrinter::PdfFormat);
1377 QCOMPARE(native.paperSize(), expected);
1378 native.setOutputFormat(QPrinter::NativeFormat);
1379 QCOMPARE(native.paperSize(), expected);
1380 } else {
1381 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1382 }
1383}
1384
1385void tst_QPrinter::paperSource()
1386{
1387 // paperSource() / setPaperSource() / PPK_PaperSource
1388 // PdfFormat: Supported, defaults to QPrinter::Auto
1389 // NativeFormat, Cups: Supported, defaults to QPrinter::Auto
1390 // NativeFormat, Win: Supported if valid DevMode and in supportedPaperSources(), otherwise QPrinter::Auto
1391 // NativeFormat, Mac: Unsupported, always QPrinter::Auto
1392
1393 QPrinter pdf;
1394 pdf.setOutputFormat(QPrinter::PdfFormat);
1395 QCOMPARE(pdf.paperSource(), QPrinter::Auto);
1396 pdf.setPaperSource(QPrinter::Lower);
1397 QCOMPARE(pdf.paperSource(), QPrinter::Lower);
1398
1399 QPrinter native;
1400 if (native.outputFormat() == QPrinter::NativeFormat) {
1401 // Test default
1402 // TODO Printer specific, need QPrinterInfo::paperSource()
1403 //QCOMPARE(native.paperSource(), QPrinter::Auto);
1404
1405 // Test set/get
1406 QPrinter::PaperSource expected = QPrinter::Manual;
1407#ifdef Q_OS_WIN
1408 expected = QPrinter::Auto;
1409 foreach (QPrinter::PaperSource supported, native.supportedPaperSources()) {
1410 if (supported != QPrinter::Auto) {
1411 expected = supported;
1412 break;
1413 }
1414 }
1415#endif // Q_OS_WIN
1416 native.setPaperSource(expected);
1417#ifdef Q_OS_MAC
1418 expected = QPrinter::Auto;
1419#endif // Q_OS_MAC
1420 QCOMPARE(native.paperSource(), expected);
1421
1422 // Test value preservation
1423 native.setOutputFormat(QPrinter::PdfFormat);
1424 QCOMPARE(native.paperSource(), expected);
1425 native.setOutputFormat(QPrinter::NativeFormat);
1426 QCOMPARE(native.paperSource(), expected);
1427 } else {
1428 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1429 }
1430}
1431
1432void tst_QPrinter::printProgram()
1433{
1434 // printProgram() / setPrintProgram() / PPK_PrintProgram
1435 // PdfFormat: Supported, default QString()
1436 // NativeFormat, Cups: Supported, default QString()
1437 // NativeFormat, Win: Unsupported, always QString()
1438 // NativeFormat, Mac: Unsupported, always QString()
1439
1440 QPrinter pdf;
1441 pdf.setOutputFormat(QPrinter::PdfFormat);
1442 QCOMPARE(pdf.printProgram(), QString());
1443 pdf.setPrintProgram(QStringLiteral("/usr/bin/lpr"));
1444 QCOMPARE(pdf.printProgram(), QStringLiteral("/usr/bin/lpr"));
1445
1446 QPrinter native;
1447 if (native.outputFormat() == QPrinter::NativeFormat) {
1448 // Test default
1449 QCOMPARE(native.printProgram(), QString());
1450
1451 // Test set/get
1452 QString expected = QStringLiteral("/usr/bin/lpr");
1453 native.setPrintProgram(expected);
1454#if defined Q_OS_MAC || defined Q_OS_WIN
1455 expected.clear();
1456#endif // Q_OS_MAC || Q_OS_WIN
1457 QCOMPARE(native.printProgram(), expected);
1458
1459 // Test value preservation
1460 native.setOutputFormat(QPrinter::PdfFormat);
1461 QCOMPARE(native.printProgram(), expected);
1462 native.setOutputFormat(QPrinter::NativeFormat);
1463 QCOMPARE(native.printProgram(), expected);
1464 } else {
1465 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1466 }
1467}
1468
1469void tst_QPrinter::printRange()
1470{
1471 // printRange() / setPrintRange() / PPK_PrintRange
1472 // PdfFormat: Supported, default QPrinter::AllPages
1473 // NativeFormat, Cups: Supported, default QPrinter::AllPages
1474 // NativeFormat, Win: Supported, default QPrinter::AllPages
1475 // NativeFormat, Mac: Supported, default QPrinter::AllPages
1476
1477 QPrinter pdf;
1478 pdf.setOutputFormat(QPrinter::PdfFormat);
1479 QCOMPARE(pdf.printRange(), QPrinter::AllPages);
1480 pdf.setPrintRange(QPrinter::CurrentPage);
1481 QCOMPARE(pdf.printRange(), QPrinter::CurrentPage);
1482
1483 QPrinter native;
1484 if (native.outputFormat() == QPrinter::NativeFormat) {
1485 // Test default
1486 QCOMPARE(native.printRange(), QPrinter::AllPages);
1487
1488 // Test set/get
1489 QPrinter::PrintRange expected = QPrinter::PageRange;
1490 native.setPrintRange(expected);
1491 QCOMPARE(native.printRange(), expected);
1492
1493 // Test value preservation
1494 native.setOutputFormat(QPrinter::PdfFormat);
1495 QCOMPARE(native.printRange(), expected);
1496 native.setOutputFormat(QPrinter::NativeFormat);
1497 QCOMPARE(native.printRange(), expected);
1498 } else {
1499 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1500 }
1501}
1502
1503void tst_QPrinter::printerName()
1504{
1505 // printerName() / setPrinterName() / PPK_PrinterName
1506 // PdfFormat: Supported, default QString
1507 // NativeFormat, Cups: Supported, default printer
1508 // NativeFormat, Win: Supported, default printer
1509 // NativeFormat, Mac: Supported, default printer
1510
1511 QPrinter pdf;
1512 pdf.setOutputFormat(QPrinter::PdfFormat);
1513 QCOMPARE(pdf.printerName(), QString());
1514 if (QPrinterInfo::availablePrinters().size() == 0) {
1515 pdf.setPrinterName(QStringLiteral("Test Printer"));
1516 QCOMPARE(pdf.printerName(), QString());
1517 QCOMPARE(pdf.outputFormat(), QPrinter::PdfFormat);
1518 } else {
1519 pdf.setPrinterName(QPrinterInfo::defaultPrinter().printerName());
1520 QCOMPARE(pdf.printerName(), QPrinterInfo::defaultPrinter().printerName());
1521 QCOMPARE(pdf.outputFormat(), QPrinter::NativeFormat);
1522 }
1523
1524 QPrinter native;
1525 if (native.outputFormat() == QPrinter::NativeFormat) {
1526 // Test default
1527 QCOMPARE(native.printerName(), QPrinterInfo::defaultPrinter().printerName());
1528
1529 // Test set/get
1530 QString expected = QPrinterInfo::defaultPrinter().printerName();
1531 foreach (const QPrinterInfo &available, QPrinterInfo::availablePrinters()) {
1532 if (available.printerName() != expected) {
1533 expected = available.printerName();
1534 break;
1535 }
1536 }
1537 native.setPrinterName(expected);
1538 QCOMPARE(native.printerName(), expected);
1539
1540 // Test value preservation
1541 native.setOutputFormat(QPrinter::PdfFormat);
1542 QCOMPARE(native.printerName(), QString());
1543 native.setOutputFormat(QPrinter::NativeFormat);
1544 QCOMPARE(native.printerName(), QPrinterInfo::defaultPrinter().printerName());
1545 } else {
1546 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1547 }
1548}
1549
1550void tst_QPrinter::printerSelectionOption()
1551{
1552 // printerSelectionOption() / setPrinterSelectionOption() / PPK_SelectionOption
1553 // PdfFormat: Supported
1554 // NativeFormat, Cups: Supported
1555 // NativeFormat, Win: Unsupported, always QString()
1556 // NativeFormat, Mac: Unsupported, always QString()
1557
1558 QPrinter pdf;
1559 pdf.setOutputFormat(QPrinter::PdfFormat);
1560 QCOMPARE(pdf.printerSelectionOption(), QString());
1561 pdf.setPrinterSelectionOption(QStringLiteral("Optional option"));
1562 QCOMPARE(pdf.printerSelectionOption(), QString("Optional option"));
1563
1564 QPrinter native;
1565 if (native.outputFormat() == QPrinter::NativeFormat) {
1566 // Test default
1567 QCOMPARE(native.printerSelectionOption(), QString());
1568
1569 // Test set/get
1570 QString expected = QStringLiteral("Optional option");
1571 native.setPrinterSelectionOption(expected);
1572#if defined Q_OS_MAC || defined Q_OS_WIN
1573 expected.clear();
1574#endif // Q_OS_MAC || Q_OS_WIN
1575 QCOMPARE(native.printerSelectionOption(), expected);
1576
1577 // Test value preservation
1578 native.setOutputFormat(QPrinter::PdfFormat);
1579 QCOMPARE(native.printerSelectionOption(), expected);
1580 native.setOutputFormat(QPrinter::NativeFormat);
1581 QCOMPARE(native.printerSelectionOption(), expected);
1582 } else {
1583 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1584 }
1585}
1586
1587void tst_QPrinter::resolution()
1588{
1589 // resolution() / setResolution() / PPK_Resolution
1590 // PdfFormat: Supported, can be any number, but only 72 returned by supportedResolutions()
1591 // NativeFormat, Cups: Supported, can be any number, but only 72 returned by supportedResolutions()
1592 // NativeFormat, Win: Supported, can be any number, but supportedResolutions() returns valid list
1593 // NativeFormat, Mac: Supported, but can only be value returned by supportedResolutions()
1594
1595 QPrinter pdfScreen(QPrinter::ScreenResolution);
1596 pdfScreen.setOutputFormat(QPrinter::PdfFormat);
1597 QCOMPARE(pdfScreen.resolution(), 96);
1598 pdfScreen.setResolution(333);
1599 QCOMPARE(pdfScreen.resolution(), 333);
1600
1601 QPrinter pdfPrinter(QPrinter::PrinterResolution);
1602 pdfPrinter.setOutputFormat(QPrinter::PdfFormat);
1603 QCOMPARE(pdfPrinter.resolution(), 72);
1604 pdfPrinter.setResolution(333);
1605 QCOMPARE(pdfPrinter.resolution(), 333);
1606
1607 QPrinter pdfHigh(QPrinter::HighResolution);
1608 pdfHigh.setOutputFormat(QPrinter::PdfFormat);
1609 QCOMPARE(pdfHigh.resolution(), 1200);
1610 pdfHigh.setResolution(333);
1611 QCOMPARE(pdfHigh.resolution(), 333);
1612
1613 QPrinter native(QPrinter::HighResolution);
1614 if (native.outputFormat() == QPrinter::NativeFormat) {
1615 // Test default
1616 // TODO Printer specific, need QPrinterInfo::resolution()
1617 //QCOMPARE(native.resolution(), 300);
1618
1619 // Test set/get
1620 int expected = 333;
1621#ifdef Q_OS_MAC
1622 // QMacPrintEngine chooses the closest supported resolution.
1623 const QList<int> all_supported = native.supportedResolutions();
1624 foreach (int supported, all_supported) {
1625 // Test setting a supported resolution
1626 int requested = supported;
1627 native.setResolution(requested);
1628 QCOMPARE(native.resolution(), requested);
1629
1630 // Test setting an unsupported resolution
1631 do {
1632 requested += 5;
1633 } while (all_supported.contains(requested));
1634 native.setResolution(requested);
1635 int result = native.resolution();
1636 QVERIFY(all_supported.contains(result));
1637 QVERIFY(qAbs(result - requested) <= qAbs(supported - requested));
1638 }
1639
1640 expected = native.resolution();
1641#endif // Q_OS_MAC
1642 native.setResolution(expected);
1643 QCOMPARE(native.resolution(), expected);
1644
1645 // Test value preservation
1646 native.setOutputFormat(QPrinter::PdfFormat);
1647 QCOMPARE(native.resolution(), expected);
1648 native.setOutputFormat(QPrinter::NativeFormat);
1649 QCOMPARE(native.resolution(), expected);
1650 } else {
1651 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1652 }
1653}
1654
1655void tst_QPrinter::supportedPaperSources()
1656{
1657 // supportedPaperSources() / PPK_PaperSources
1658 // PdfFormat: ifdef'd out TODO remove ifdef
1659 // NativeFormat, Cups: ifdef'd out TODO remove ifdef
1660 // NativeFormat, Win: Supported, defaults to printer default
1661 // NativeFormat, Mac: ifdef'd out TODO remove ifdef
1662
1663#ifdef Q_OS_WIN
1664 QPrinter native;
1665 if (native.outputFormat() == QPrinter::NativeFormat) {
1666 native.supportedPaperSources();
1667 } else {
1668 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1669 }
1670#endif // Q_OS_WIN
1671}
1672
1673void tst_QPrinter::supportedResolutions()
1674{
1675 // supportedResolutions() / PPK_SupportedResolutions
1676 // PdfFormat: Supported, only returns 72
1677 // NativeFormat, Cups: Supported, only returns 72
1678 // NativeFormat, Win: Supported, defaults to printer list
1679 // NativeFormat, Mac: Supported, defaults to printer list
1680
1681 QList<int> expected;
1682
1683 QPrinter pdf;
1684 pdf.setOutputFormat(QPrinter::PdfFormat);
1685 expected << 72;
1686 QCOMPARE(pdf.supportedResolutions(), expected);
1687
1688 QPrinter native;
1689 if (native.outputFormat() == QPrinter::NativeFormat) {
1690 native.supportedResolutions();
1691 } else {
1692 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1693 }
1694}
1695
1696void tst_QPrinter::windowsPageSize()
1697{
1698 // winPageSize() / setWinPageSize() / PPK_WindowsPageSize
1699 // PdfFormat: Supported, defaults to printer default
1700 // NativeFormat, Cups: Supported, defaults to printer default
1701 // NativeFormat, Win: Supported, defaults to printer default
1702 // NativeFormat, Mac: Supported, defaults to printer default
1703
1704 QPrinter pdf;
1705 pdf.setOutputFormat(QPrinter::PdfFormat);
1706 QCOMPARE(pdf.winPageSize(), 9); // DMPAPER_A4
1707 pdf.setWinPageSize(1); // DMPAPER_LETTER
1708 QCOMPARE(pdf.winPageSize(), 1);
1709
1710 QPrinter native;
1711 if (native.outputFormat() == QPrinter::NativeFormat) {
1712 // Test set/get
1713 native.setPaperSize(QPrinter::A4);
1714 QCOMPARE(native.pageSize(), QPrinter::A4);
1715 QCOMPARE(native.winPageSize(), 9); // DMPAPER_A4
1716
1717 native.setPaperSize(QPrinter::Letter);
1718 QCOMPARE(native.pageSize(), QPrinter::Letter);
1719 QCOMPARE(native.winPageSize(), 1); // DMPAPER_LETTER
1720
1721 native.setWinPageSize(9); // DMPAPER_A4
1722 QCOMPARE(native.pageSize(), QPrinter::A4);
1723 QCOMPARE(native.winPageSize(), 9); // DMPAPER_A4
1724
1725 native.setWinPageSize(1); // DMPAPER_LETTER
1726 QCOMPARE(native.pageSize(), QPrinter::Letter);
1727 QCOMPARE(native.winPageSize(), 1); // DMPAPER_LETTER
1728
1729 // Test value preservation
1730 native.setOutputFormat(QPrinter::PdfFormat);
1731 QCOMPARE(native.pageSize(), QPrinter::Letter);
1732 QCOMPARE(native.winPageSize(), 1); // DMPAPER_LETTER
1733 native.setOutputFormat(QPrinter::NativeFormat);
1734 QCOMPARE(native.pageSize(), QPrinter::Letter);
1735 QCOMPARE(native.winPageSize(), 1); // DMPAPER_LETTER
1736 } else {
1737 QSKIP("No printers installed, cannot test NativeFormat, please install printers to test");
1738 }
1739}
1740
1741// Test QPrinter setters/getters for non-QPrintEngine options
1742
1743void tst_QPrinter::outputFormat()
1744{
1745 QPrinter printer;
1746 if (QPrinterInfo::availablePrinters().size() == 0) {
1747 QCOMPARE(printer.outputFormat(), QPrinter::PdfFormat);
1748 QCOMPARE(printer.printerName(), QString());
1749 } else {
1750 QCOMPARE(printer.outputFormat(), QPrinter::NativeFormat);
1751 QCOMPARE(printer.printerName(), QPrinterInfo::defaultPrinter().printerName());
1752 }
1753
1754 printer.setOutputFormat(QPrinter::PdfFormat);
1755 QCOMPARE(printer.outputFormat(), QPrinter::PdfFormat);
1756 QCOMPARE(printer.printerName(), QString());
1757}
1758
1759void tst_QPrinter::fromToPage()
1760{
1761 QPrinter printer;
1762 QCOMPARE(printer.fromPage(), 0);
1763 QCOMPARE(printer.toPage(), 0);
1764 printer.setFromTo(fromPage: 3, toPage: 7);
1765 QCOMPARE(printer.fromPage(), 3);
1766 QCOMPARE(printer.toPage(), 7);
1767}
1768
1769void tst_QPrinter::testPageMetrics_data()
1770{
1771 QTest::addColumn<int>(name: "outputFormat");
1772 QTest::addColumn<int>(name: "pageSize");
1773 QTest::addColumn<qreal>(name: "widthMMf");
1774 QTest::addColumn<qreal>(name: "heightMMf");
1775 QTest::addColumn<bool>(name: "setMargins");
1776 QTest::addColumn<qreal>(name: "leftMMf");
1777 QTest::addColumn<qreal>(name: "rightMMf");
1778 QTest::addColumn<qreal>(name: "topMMf");
1779 QTest::addColumn<qreal>(name: "bottomMMf");
1780
1781 QTest::newRow(dataTag: "PDF A4") << int(QPrinter::PdfFormat) << int(QPrinter::A4) << 210.0 << 297.0 << false << 0.0 << 0.0 << 0.0 << 0.0;
1782 QTest::newRow(dataTag: "PDF A4 Margins") << int(QPrinter::PdfFormat) << int(QPrinter::A4) << 210.0 << 297.0 << true << 20.0 << 30.0 << 40.0 << 50.0;
1783 QTest::newRow(dataTag: "Native A4") << int(QPrinter::NativeFormat) << int(QPrinter::A4) << 210.0 << 297.0 << false << 0.0 << 0.0 << 0.0 << 0.0;
1784 QTest::newRow(dataTag: "Native A4 Margins") << int(QPrinter::NativeFormat) << int(QPrinter::A4) << 210.0 << 297.0 << true << 20.0 << 30.0 << 40.0 << 50.0;
1785
1786 QTest::newRow(dataTag: "PDF Portrait") << int(QPrinter::PdfFormat) << -1 << 200.0 << 300.0 << false << 0.0 << 0.0 << 0.0 << 0.0;
1787 QTest::newRow(dataTag: "PDF Portrait Margins") << int(QPrinter::PdfFormat) << -1 << 200.0 << 300.0 << true << 20.0 << 30.0 << 40.0 << 50.0;
1788 QTest::newRow(dataTag: "PDF Landscape") << int(QPrinter::PdfFormat) << -1 << 300.0 << 200.0 << false << 0.0 << 0.0 << 0.0 << 0.0;
1789 QTest::newRow(dataTag: "PDF Landscape Margins") << int(QPrinter::PdfFormat) << -1 << 300.0 << 200.0 << true << 20.0 << 30.0 << 40.0 << 50.0;
1790 QTest::newRow(dataTag: "Native Portrait") << int(QPrinter::NativeFormat) << -1 << 200.0 << 300.0 << false << 0.0 << 0.0 << 0.0 << 0.0;
1791 QTest::newRow(dataTag: "Native Portrait Margins") << int(QPrinter::NativeFormat) << -1 << 200.0 << 300.0 << true << 20.0 << 30.0 << 40.0 << 50.0;
1792 QTest::newRow(dataTag: "Native Landscape") << int(QPrinter::NativeFormat) << -1 << 300.0 << 200.0 << false << 0.0 << 0.0 << 0.0 << 0.0;
1793 QTest::newRow(dataTag: "Native Landscape Margins") << int(QPrinter::NativeFormat) << -1 << 300.0 << 200.0 << true << 20.0 << 30.0 << 40.0 << 50.0;
1794}
1795
1796void tst_QPrinter::testPageMetrics()
1797{
1798 QFETCH(int, outputFormat);
1799 QFETCH(int, pageSize);
1800 QFETCH(qreal, widthMMf);
1801 QFETCH(qreal, heightMMf);
1802 QFETCH(bool, setMargins);
1803 QFETCH(qreal, leftMMf);
1804 QFETCH(qreal, rightMMf);
1805 QFETCH(qreal, topMMf);
1806 QFETCH(qreal, bottomMMf);
1807
1808 QSizeF sizeMMf = QSizeF(widthMMf, heightMMf);
1809
1810 QPrinter printer;
1811 printer.setOutputFormat(QPrinter::OutputFormat(outputFormat));
1812 if (printer.outputFormat() != QPrinter::OutputFormat(outputFormat))
1813 QSKIP("Please install a native printer to run this test");
1814 QCOMPARE(printer.outputFormat(), QPrinter::OutputFormat(outputFormat));
1815 QCOMPARE(printer.orientation(), QPrinter::Portrait);
1816
1817 if (setMargins) {
1818 // Setup the given margins
1819 QPrinter::Margins margins;
1820 margins.left = leftMMf;
1821 margins.right = rightMMf;
1822 margins.top = topMMf;
1823 margins.bottom = bottomMMf;
1824 printer.setMargins(margins);
1825 QCOMPARE(printer.margins().left, leftMMf);
1826 QCOMPARE(printer.margins().right, rightMMf);
1827 QCOMPARE(printer.margins().top, topMMf);
1828 QCOMPARE(printer.margins().bottom, bottomMMf);
1829 }
1830
1831
1832 // Set the given size, in Portrait mode
1833 if (pageSize < 0) {
1834 printer.setPageSizeMM(sizeMMf);
1835 QCOMPARE(printer.pageSize(), QPrinter::Custom);
1836 } else {
1837 printer.setPageSize(QPrinter::PageSize(pageSize));
1838 QCOMPARE(printer.pageSize(), QPrinter::PageSize(pageSize));
1839 }
1840 QCOMPARE(printer.orientation(), QPrinter::Portrait);
1841 if (setMargins) {
1842 // Check margins unchanged from page size change
1843 QCOMPARE(printer.margins().left, leftMMf);
1844 QCOMPARE(printer.margins().right, rightMMf);
1845 QCOMPARE(printer.margins().top, topMMf);
1846 QCOMPARE(printer.margins().bottom, bottomMMf);
1847 } else {
1848 // Fetch the default margins for the printer and page size
1849 // TODO Check against margins from print device when api added
1850 leftMMf = printer.margins().left;
1851 rightMMf = printer.margins().right;
1852 topMMf = printer.margins().top;
1853 bottomMMf = printer.margins().bottom;
1854 }
1855
1856 // QPagedPaintDevice::pageSizeMM() always returns Portrait
1857 QCOMPARE(printer.pageSizeMM(), sizeMMf);
1858
1859 // QPrinter::paperSize() always returns set orientation
1860 QCOMPARE(printer.paperSize(QPrinter::Millimeter), sizeMMf);
1861
1862 // QPagedPaintDevice::widthMM() and heightMM() are paint metrics and always return set orientation
1863 QCOMPARE(printer.widthMM(), qRound(widthMMf - leftMMf - rightMMf));
1864 QCOMPARE(printer.heightMM(), qRound(heightMMf - topMMf - bottomMMf));
1865
1866 // QPrinter::paperRect() always returns set orientation
1867 QCOMPARE(printer.paperRect(QPrinter::Millimeter), QRectF(0, 0, widthMMf, heightMMf));
1868
1869 // QPrinter::pageRect() always returns set orientation
1870 QCOMPARE(printer.pageRect(QPrinter::Millimeter), QRectF(leftMMf, topMMf, widthMMf - leftMMf - rightMMf, heightMMf - topMMf - bottomMMf));
1871
1872
1873 // Now switch to Landscape mode, size should be unchanged, but rect and metrics should change
1874 printer.setOrientation(QPrinter::Landscape);
1875 if (pageSize < 0) {
1876 QCOMPARE(printer.pageSize(), QPrinter::Custom);
1877 } else {
1878 QCOMPARE(printer.pageSize(), QPrinter::PageSize(pageSize));
1879 }
1880 QCOMPARE(printer.orientation(), QPrinter::Landscape);
1881 if (setMargins) {
1882 // Check margins unchanged from page size change
1883 QCOMPARE(printer.margins().left, leftMMf);
1884 QCOMPARE(printer.margins().right, rightMMf);
1885 QCOMPARE(printer.margins().top, topMMf);
1886 QCOMPARE(printer.margins().bottom, bottomMMf);
1887 } else {
1888 // Fetch the default margins for the printer and page size
1889 // TODO Check against margins from print device when api added
1890 leftMMf = printer.margins().left;
1891 rightMMf = printer.margins().right;
1892 topMMf = printer.margins().top;
1893 bottomMMf = printer.margins().bottom;
1894 }
1895
1896 // QPagedPaintDevice::pageSizeMM() always returns Portrait
1897 QCOMPARE(printer.pageSizeMM(), sizeMMf);
1898
1899 // QPrinter::paperSize() always returns set orientation
1900 QCOMPARE(printer.paperSize(QPrinter::Millimeter), sizeMMf.transposed());
1901
1902 // QPagedPaintDevice::widthMM() and heightMM() are paint metrics and always return set orientation
1903 QCOMPARE(printer.widthMM(), qRound(heightMMf - leftMMf - rightMMf));
1904 QCOMPARE(printer.heightMM(), qRound(widthMMf - topMMf - bottomMMf));
1905
1906 // QPrinter::paperRect() always returns set orientation
1907 QCOMPARE(printer.paperRect(QPrinter::Millimeter), QRectF(0, 0, heightMMf, widthMMf));
1908
1909 // QPrinter::pageRect() always returns set orientation
1910 QCOMPARE(printer.pageRect(QPrinter::Millimeter), QRectF(leftMMf, topMMf, heightMMf - leftMMf - rightMMf, widthMMf - topMMf - bottomMMf));
1911
1912
1913 // Now while in Landscape mode, set the size again, results should be the same
1914 if (pageSize < 0) {
1915 printer.setPageSizeMM(sizeMMf);
1916 QCOMPARE(printer.pageSize(), QPrinter::Custom);
1917 } else {
1918 printer.setPageSize(QPrinter::PageSize(pageSize));
1919 QCOMPARE(printer.pageSize(), QPrinter::PageSize(pageSize));
1920 }
1921 QCOMPARE(printer.orientation(), QPrinter::Landscape);
1922 if (setMargins) {
1923 // Check margins unchanged from page size change
1924 QCOMPARE(printer.margins().left, leftMMf);
1925 QCOMPARE(printer.margins().right, rightMMf);
1926 QCOMPARE(printer.margins().top, topMMf);
1927 QCOMPARE(printer.margins().bottom, bottomMMf);
1928 } else {
1929 // Fetch the default margins for the printer and page size
1930 // TODO Check against margins from print device when api added
1931 leftMMf = printer.margins().left;
1932 rightMMf = printer.margins().right;
1933 topMMf = printer.margins().top;
1934 bottomMMf = printer.margins().bottom;
1935 }
1936
1937 // QPagedPaintDevice::pageSizeMM() always returns Portrait
1938 QCOMPARE(printer.pageSizeMM(), sizeMMf);
1939
1940 // QPrinter::paperSize() always returns set orientation
1941 QCOMPARE(printer.paperSize(QPrinter::Millimeter), sizeMMf.transposed());
1942
1943 // QPagedPaintDevice::widthMM() and heightMM() are paint metrics and always return set orientation
1944 QCOMPARE(printer.widthMM(), qRound(heightMMf - leftMMf - rightMMf));
1945 QCOMPARE(printer.heightMM(), qRound(widthMMf - topMMf - bottomMMf));
1946
1947 // QPrinter::paperRect() always returns set orientation
1948 QCOMPARE(printer.paperRect(QPrinter::Millimeter), QRectF(0, 0, heightMMf, widthMMf));
1949
1950 // QPrinter::pageRect() always returns set orientation
1951 QCOMPARE(printer.pageRect(QPrinter::Millimeter), QRectF(leftMMf, topMMf, heightMMf - leftMMf - rightMMf, widthMMf - topMMf - bottomMMf));
1952}
1953
1954QString tst_QPrinter::testFileName(const QString &prefix, const QString &suffix)
1955{
1956 QString result = m_tempDir.path() + QLatin1Char('/') + prefix
1957 + QString::number(fileNumber++);
1958 if (!suffix.isEmpty())
1959 result += QLatin1Char('.') + suffix;
1960 return result;
1961}
1962
1963void tst_QPrinter::reusePageMetrics()
1964{
1965 QList<QPrinterInfo> availablePrinters = QPrinterInfo::availablePrinters();
1966 if (availablePrinters.size() < 2)
1967 QSKIP("Not enough printers to do this test with, need at least 2 setup");
1968 QPrinter defaultP;
1969 QPrinterInfo info(defaultP);
1970 QString otherPrinterName;
1971 for (QPrinterInfo i : qAsConst(t&: availablePrinters)) {
1972 if (i.printerName() != defaultP.printerName()) {
1973 otherPrinterName = i.printerName();
1974 break;
1975 }
1976 }
1977 QPrinter otherP(QPrinterInfo::printerInfo(printerName: otherPrinterName));
1978 QList<QPageSize> defaultPageSizes = info.supportedPageSizes();
1979 QList<QPageSize> otherPageSizes = QPrinterInfo(otherP).supportedPageSizes();
1980 QPageSize unavailableSizeToSet;
1981 for (QPageSize s : qAsConst(t&: defaultPageSizes)) {
1982 bool found = false;
1983 for (QPageSize os : qAsConst(t&: otherPageSizes)) {
1984 if (os.isEquivalentTo(other: s)) {
1985 found = true;
1986 break;
1987 }
1988 }
1989 const QPageSize tmpSize(s.size(units: QPageSize::Point), QPageSize::Point);
1990 if (!tmpSize.name().startsWith(s: "Custom"))
1991 found = true;
1992 if (!found) {
1993 unavailableSizeToSet = s;
1994 break;
1995 }
1996 }
1997 if (!unavailableSizeToSet.isValid())
1998 QSKIP("Could not find a size that was not available on the non default printer. The test "
1999 "requires this");
2000 defaultP.setPageSize(unavailableSizeToSet);
2001 defaultP.setPrinterName(otherP.printerName());
2002 QVERIFY(defaultP.pageLayout().pageSize().isEquivalentTo(unavailableSizeToSet));
2003 QVERIFY(defaultP.pageLayout().pageSize().name() != unavailableSizeToSet.name());
2004 QCOMPARE(defaultP.pageLayout().pageSize().sizePoints(), unavailableSizeToSet.sizePoints());
2005}
2006
2007#endif // QT_CONFIG(printer)
2008
2009QTEST_MAIN(tst_QPrinter)
2010#include "tst_qprinter.moc"
2011

source code of qtbase/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp