/**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the test suite of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and Digia. For licensing terms and ** conditions see http://qt.digia.com/licensing. For further information ** use the contact form at http://qt.digia.com/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Digia gives you certain additional ** rights. These rights are described in the Digia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef Q_OS_WIN #include #endif QT_FORWARD_DECLARE_CLASS(QPrinter) // Helper class to make sure temp files are cleaned up after test complete class TempFileCleanup { public: TempFileCleanup(const QString &file) : m_file(file) { } ~TempFileCleanup() { QFile::remove(m_file); } private: QString m_file; }; class tst_QPrinter : public QObject { Q_OBJECT public slots: #ifdef QT_NO_PRINTER void initTestCase(); void cleanupTestCase(); #else private slots: void testPageRectAndPaperRect(); void testPageRectAndPaperRect_data(); void testSetOptions(); void testMargins_data(); void testMargins(); void testPageSetupDialog(); void testPrintPreviewDialog(); void testMulitpleSets_data(); void testMulitpleSets(); void testPageMargins_data(); void testPageMargins(); void outputFormatFromSuffix(); void errorReporting(); void testCustomPageSizes(); void customPaperSizeAndMargins_data(); void customPaperSizeAndMargins(); void customPaperNameSettingBySize(); void customPaperNameSettingByName(); #if !defined(QT_NO_COMPLETER) && !defined(QT_NO_FILEDIALOG) void printDialogCompleter(); #endif void testCurrentPage(); void taskQTBUG4497_reusePrinterOnDifferentFiles(); void testPdfTitle(); // Test QPrintEngine keys and their QPrinter setters/getters void testMultipleKeys(); void collateCopies(); void colorMode(); void copyCount(); void creator(); void docName(); void doubleSidedPrinting(); void duplex(); void fontEmbedding(); void fullPage(); void orientation(); void outputFileName(); void pageOrder(); void pageSize(); void paperSize(); void paperSource(); void printerName(); void printerSelectionOption(); void printProgram(); void printRange(); void resolution(); void supportedPaperSources(); void supportedResolutions(); void windowsPageSize(); // Test QPrinter setters/getters for non-QPrintEngine options void outputFormat(); void fromToPage(); void testPageMetrics_data(); void testPageMetrics(); #endif }; #ifdef QT_NO_PRINTER void tst_QPrinter::initTestCase() { QSKIP("This test requires printing support"); } void tst_QPrinter::cleanupTestCase() { QSKIP("This test requires printing support"); } #else #define MYCOMPARE(a, b) QCOMPARE(QVariant((int)a), QVariant((int)b)) void tst_QPrinter::testPageSetupDialog() { // Make sure this doesn't crash at least { QPrinter printer; QPageSetupDialog dialog(&printer); } } // A preview dialog showing 4 pages for testPrintPreviewDialog(). class MyPreviewDialog : public QPrintPreviewDialog { Q_OBJECT public: MyPreviewDialog(QPrinter *p) : QPrintPreviewDialog(p) { connect(this, SIGNAL(paintRequested(QPrinter*)), this, SLOT(slotPaintRequested(QPrinter*))); } public slots: void slotPaintRequested(QPrinter *p); }; void MyPreviewDialog::slotPaintRequested(QPrinter *p) { enum { pageCount = 4 }; QPainter painter; painter.begin(p); for (int i = 0; i < pageCount; ++i) { const QRect f = p->pageRect(QPrinter::DevicePixel).toRect(); painter.fillRect(f, Qt::white); painter.drawText(f.center(), QString::fromLatin1("Page %1").arg(i + 1)); if (i != pageCount - 1) p->newPage(); } painter.end(); } void tst_QPrinter::testPrintPreviewDialog() { // QTBUG-14517: Showing the dialog with Qt::WindowMaximized caused it to switch to // page 2 due to the scrollbar logic (besides testing for crashes). QPrinter printer; MyPreviewDialog dialog(&printer); dialog.setWindowState(Qt::WindowMaximized); dialog.show(); QVERIFY(QTest::qWaitForWindowExposed(&dialog)); QPrintPreviewWidget *widget = dialog.findChild(); QVERIFY(widget); QCOMPARE(widget->currentPage(), 1); } void tst_QPrinter::testPageRectAndPaperRect_data() { QTest::addColumn("orientation"); QTest::addColumn("withPainter"); QTest::addColumn("resolution"); QTest::addColumn("doPaperRect"); // paperrect QTest::newRow("paperRect0") << int(QPrinter::Portrait) << true << 300 << true; QTest::newRow("paperRect1") << int(QPrinter::Portrait) << false << 300 << true; QTest::newRow("paperRect2") << int(QPrinter::Landscape) << true << 300 << true; QTest::newRow("paperRect3") << int(QPrinter::Landscape) << false << 300 << true; QTest::newRow("paperRect4") << int(QPrinter::Portrait) << true << 600 << true; QTest::newRow("paperRect5") << int(QPrinter::Portrait) << false << 600 << true; QTest::newRow("paperRect6") << int(QPrinter::Landscape) << true << 600 << true; QTest::newRow("paperRect7") << int(QPrinter::Landscape) << false << 600 << true; QTest::newRow("paperRect8") << int(QPrinter::Portrait) << true << 1200 << true; QTest::newRow("paperRect9") << int(QPrinter::Portrait) << false << 1200 << true; QTest::newRow("paperRect10") << int(QPrinter::Landscape) << true << 1200 << true; QTest::newRow("paperRect11") << int(QPrinter::Landscape) << false << 1200 << true; // page rect QTest::newRow("pageRect0") << int(QPrinter::Portrait) << true << 300 << false; QTest::newRow("pageRect1") << int(QPrinter::Portrait) << false << 300 << false; QTest::newRow("pageRect2") << int(QPrinter::Landscape) << true << 300 << false; QTest::newRow("pageRect3") << int(QPrinter::Landscape) << false << 300 << false; QTest::newRow("pageRect4") << int(QPrinter::Portrait) << true << 600 << false; QTest::newRow("pageRect5") << int(QPrinter::Portrait) << false << 600 << false; QTest::newRow("pageRect6") << int(QPrinter::Landscape) << true << 600 << false; QTest::newRow("pageRect7") << int(QPrinter::Landscape) << false << 600 << false; QTest::newRow("pageRect8") << int(QPrinter::Portrait) << true << 1200 << false; QTest::newRow("pageRect9") << int(QPrinter::Portrait) << false << 1200 << false; QTest::newRow("pageRect10") << int(QPrinter::Landscape) << true << 1200 << false; QTest::newRow("pageRect11") << int(QPrinter::Landscape) << false << 1200 << false; } void tst_QPrinter::testPageRectAndPaperRect() { QFETCH(bool, withPainter); QFETCH(int, orientation); QFETCH(int, resolution); QFETCH(bool, doPaperRect); QPainter *painter = 0; QPrinter printer(QPrinter::HighResolution); printer.setOrientation(QPrinter::Orientation(orientation)); printer.setOutputFileName("silly"); TempFileCleanup tmpFile("silly"); QRect pageRect = doPaperRect ? printer.paperRect() : printer.pageRect(); float inchesX = float(pageRect.width()) / float(printer.resolution()); float inchesY = float(pageRect.height()) / float(printer.resolution()); printer.setResolution(resolution); if (withPainter) painter = new QPainter(&printer); QRect otherRect = doPaperRect ? printer.paperRect() : printer.pageRect(); float otherInchesX = float(otherRect.width()) / float(printer.resolution()); float otherInchesY = float(otherRect.height()) / float(printer.resolution()); if (painter != 0) delete painter; QVERIFY(qAbs(otherInchesX - inchesX) < 0.01); QVERIFY(qAbs(otherInchesY - inchesY) < 0.01); QVERIFY(printer.orientation() == QPrinter::Portrait || pageRect.width() > pageRect.height()); QVERIFY(printer.orientation() != QPrinter::Portrait || pageRect.width() < pageRect.height()); } void tst_QPrinter::testSetOptions() { QPrinter prn; QPrintDialog dlg(&prn); // Verify default values MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintToFile), true); MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintSelection), false); MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintPageRange), true); dlg.setEnabledOptions(QAbstractPrintDialog::PrintPageRange); MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintToFile), false); MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintSelection), false); MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintPageRange), true); dlg.setEnabledOptions((QAbstractPrintDialog::PrintDialogOptions(QAbstractPrintDialog::PrintSelection | QAbstractPrintDialog::PrintPageRange))); MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintToFile), false); MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintSelection), true); MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintPageRange), true); dlg.setEnabledOptions(QAbstractPrintDialog::PrintSelection); MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintToFile), false); MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintSelection), true); MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintPageRange), false); } void tst_QPrinter::testMargins_data() { QTest::addColumn("orientation"); QTest::addColumn("fullpage"); QTest::addColumn("pagesize"); QTest::addColumn("width"); QTest::addColumn("height"); QTest::addColumn("withPainter"); QTest::newRow("data0") << int(QPrinter::Portrait) << true << int(QPrinter::A4) << 210 << 297 << false; QTest::newRow("data1") << int(QPrinter::Landscape) << true << int(QPrinter::A4) << 297 << 210 << false; QTest::newRow("data2") << int(QPrinter::Landscape) << false << int(QPrinter::A4) << 297 << 210 << false; QTest::newRow("data3") << int(QPrinter::Portrait) << false << int(QPrinter::A4) << 210 << 297 << false; QTest::newRow("data4") << int(QPrinter::Portrait) << true << int(QPrinter::A4) << 210 << 297 << true; QTest::newRow("data5") << int(QPrinter::Landscape) << true << int(QPrinter::A4) << 297 << 210 << true; QTest::newRow("data6") << int(QPrinter::Landscape) << false << int(QPrinter::A4) << 297 << 210 << true; QTest::newRow("data7") << int(QPrinter::Portrait) << false << int(QPrinter::A4) << 210 << 297 << true; } void tst_QPrinter::testMargins() { QFETCH(bool, withPainter); QFETCH(int, orientation); QFETCH(int, pagesize); QFETCH(int, width); QFETCH(int, height); QFETCH(bool, fullpage); Q_UNUSED(width); Q_UNUSED(height); QPrinter printer; QPainter *painter = 0; printer.setOutputFileName("silly"); printer.setOrientation((QPrinter::Orientation)orientation); printer.setFullPage(fullpage); printer.setPageSize((QPrinter::PageSize)pagesize); if (withPainter) painter = new QPainter(&printer); if (painter) delete painter; QFile::remove("silly"); } void tst_QPrinter::testMulitpleSets_data() { QTest::addColumn("resolution"); QTest::addColumn("pageSize"); QTest::addColumn("widthMMAfter"); QTest::addColumn("heightMMAfter"); QTest::newRow("lowRes") << int(QPrinter::ScreenResolution) << int(QPrinter::A4) << 210 << 297; QTest::newRow("lowResLetter") << int(QPrinter::ScreenResolution) << int(QPrinter::Letter) << 216 << 279; QTest::newRow("lowResA5") << int(QPrinter::ScreenResolution) << int(QPrinter::A5) << 148 << 210; QTest::newRow("midRes") << int(QPrinter::PrinterResolution) << int(QPrinter::A4) << 210 << 297; QTest::newRow("midResLetter") << int(QPrinter::PrinterResolution) << int(QPrinter::Letter) << 216 << 279; QTest::newRow("midResA5") << int(QPrinter::PrinterResolution) << int(QPrinter::A5) << 148 << 210; QTest::newRow("highRes") << int(QPrinter::HighResolution) << int(QPrinter::A4) << 210 << 297; QTest::newRow("highResLetter") << int(QPrinter::HighResolution) << int(QPrinter::Letter) << 216 << 279; QTest::newRow("highResA5") << int(QPrinter::HighResolution) << int(QPrinter::A5) << 148 << 210; } static void computePageValue(const QPrinter &printer, int &retWidth, int &retHeight) { const double Inch2MM = 25.4; double width = double(printer.paperRect().width()) / printer.logicalDpiX() * Inch2MM; double height = double(printer.paperRect().height()) / printer.logicalDpiY() * Inch2MM; retWidth = qRound(width); retHeight = qRound(height); } void tst_QPrinter::testMulitpleSets() { // A very simple test, but Mac needs to have its format "validated" if the format is changed // This takes care of that. QFETCH(int, resolution); QFETCH(int, pageSize); QFETCH(int, widthMMAfter); QFETCH(int, heightMMAfter); QPrinter::PrinterMode mode = QPrinter::PrinterMode(resolution); QPrinter::PageSize printerPageSize = QPrinter::PageSize(pageSize); QPrinter printer(mode); printer.setFullPage(true); int paperWidth, paperHeight; //const int Tolerance = 2; computePageValue(printer, paperWidth, paperHeight); printer.setPageSize(printerPageSize); if (printer.pageSize() != printerPageSize) { QSKIP("Current page size is not supported on this printer"); return; } QVERIFY(qAbs(printer.widthMM() - widthMMAfter) <= 2); QVERIFY(qAbs(printer.heightMM() - heightMMAfter) <= 2); computePageValue(printer, paperWidth, paperHeight); QVERIFY(qAbs(paperWidth - widthMMAfter) <= 2); QVERIFY(qAbs(paperHeight - heightMMAfter) <= 2); // Set it again and see if it still works. printer.setPageSize(printerPageSize); QVERIFY(qAbs(printer.widthMM() - widthMMAfter) <= 2); QVERIFY(qAbs(printer.heightMM() - heightMMAfter) <= 2); printer.setOrientation(QPrinter::Landscape); computePageValue(printer, paperWidth, paperHeight); QVERIFY(qAbs(paperWidth - heightMMAfter) <= 2); QVERIFY(qAbs(paperHeight - widthMMAfter) <= 2); } void tst_QPrinter::outputFormatFromSuffix() { if (QPrinterInfo::availablePrinters().size() == 0) QSKIP("No printers available."); QPrinter p; QVERIFY(p.outputFormat() == QPrinter::NativeFormat); p.setOutputFileName("test.pdf"); TempFileCleanup tmpFile("test.pdf"); QVERIFY(p.outputFormat() == QPrinter::PdfFormat); p.setOutputFileName(QString()); QVERIFY(p.outputFormat() == QPrinter::NativeFormat); } void tst_QPrinter::testPageMargins_data() { QTest::addColumn("left"); QTest::addColumn("top"); QTest::addColumn("right"); QTest::addColumn("bottom"); QTest::addColumn("unit"); // Use custom margins that will exceed most printers minimum allowed QTest::newRow("data0") << qreal(25.5) << qreal(26.5) << qreal(27.5) << qreal(28.5) << static_cast(QPrinter::Millimeter); QTest::newRow("data1") << qreal(55.5) << qreal(56.5) << qreal(57.5) << qreal(58.5) << static_cast(QPrinter::Point); QTest::newRow("data2") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast(QPrinter::Inch); QTest::newRow("data3") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast(QPrinter::Pica); QTest::newRow("data4") << qreal(55.5) << qreal(56.5) << qreal(57.5) << qreal(58.5) << static_cast(QPrinter::Didot); QTest::newRow("data5") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast(QPrinter::Cicero); } void tst_QPrinter::testPageMargins() { QPrinter obj1; QFETCH(qreal, left); QFETCH(qreal, top); QFETCH(qreal, right); QFETCH(qreal, bottom); QFETCH(int, unit); QPageLayout layout = QPageLayout(QPageSize(QPageSize::A0), QPageLayout::Portrait, QMarginsF(left, top, right, bottom), QPageLayout::Unit(unit)); qreal nLeft, nTop, nRight, nBottom; obj1.setPageMargins(left, top, right, bottom, QPrinter::Unit(unit)); obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Millimeter); QCOMPARE(nLeft, layout.margins(QPageLayout::Millimeter).left()); QCOMPARE(nRight, layout.margins(QPageLayout::Millimeter).right()); QCOMPARE(nTop, layout.margins(QPageLayout::Millimeter).top()); QCOMPARE(nBottom, layout.margins(QPageLayout::Millimeter).bottom()); obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Point); QCOMPARE(nLeft, layout.margins(QPageLayout::Point).left()); QCOMPARE(nRight, layout.margins(QPageLayout::Point).right()); QCOMPARE(nTop, layout.margins(QPageLayout::Point).top()); QCOMPARE(nBottom, layout.margins(QPageLayout::Point).bottom()); obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Inch); QCOMPARE(nLeft, layout.margins(QPageLayout::Inch).left()); QCOMPARE(nRight, layout.margins(QPageLayout::Inch).right()); QCOMPARE(nTop, layout.margins(QPageLayout::Inch).top()); QCOMPARE(nBottom, layout.margins(QPageLayout::Inch).bottom()); obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Pica); QCOMPARE(nLeft, layout.margins(QPageLayout::Pica).left()); QCOMPARE(nRight, layout.margins(QPageLayout::Pica).right()); QCOMPARE(nTop, layout.margins(QPageLayout::Pica).top()); QCOMPARE(nBottom, layout.margins(QPageLayout::Pica).bottom()); obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Didot); QCOMPARE(nLeft, layout.margins(QPageLayout::Didot).left()); QCOMPARE(nRight, layout.margins(QPageLayout::Didot).right()); QCOMPARE(nTop, layout.margins(QPageLayout::Didot).top()); QCOMPARE(nBottom, layout.margins(QPageLayout::Didot).bottom()); obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Cicero); QCOMPARE(nLeft, layout.margins(QPageLayout::Cicero).left()); QCOMPARE(nRight, layout.margins(QPageLayout::Cicero).right()); QCOMPARE(nTop, layout.margins(QPageLayout::Cicero).top()); QCOMPARE(nBottom, layout.margins(QPageLayout::Cicero).bottom()); } void tst_QPrinter::errorReporting() { QPrinter p; p.setOutputFormat(QPrinter::PdfFormat); QCOMPARE(p.isValid(), true); QPainter painter; #ifndef Q_OS_WIN // not sure how to choose a never-writable file on windows. But its QFile behavior anyway, so lets rely on it failing elsewhere p.setOutputFileName("/foobar/nonwritable.pdf"); QCOMPARE(painter.begin(&p), false); // it should check the output file is writable #endif p.setOutputFileName("test.pdf"); TempFileCleanup tmpFile("test.pdf"); QCOMPARE(painter.begin(&p), true); // it should check the output QCOMPARE(p.isValid(), true); painter.end(); } void tst_QPrinter::testCustomPageSizes() { QPrinter p; QSizeF customSize(8.5, 11.0); p.setPaperSize(customSize, QPrinter::Inch); QSizeF paperSize = p.paperSize(QPrinter::Inch); QCOMPARE(paperSize.width(), customSize.width()); QCOMPARE(paperSize.height(), customSize.height()); QPrinter p2(QPrinter::HighResolution); p2.setPaperSize(customSize, QPrinter::Inch); paperSize = p.paperSize(QPrinter::Inch); QCOMPARE(paperSize.width(), customSize.width()); QCOMPARE(paperSize.height(), customSize.height()); } void tst_QPrinter::customPaperSizeAndMargins_data() { QTest::addColumn("pdf"); QTest::addColumn("before"); QTest::addColumn("left"); QTest::addColumn("top"); QTest::addColumn("right"); QTest::addColumn("bottom"); // Use custom margins that will exceed most printers minimum allowed QTest::newRow("beforeNoPDF") << false << true << qreal(30) << qreal(30) << qreal(30) << qreal(30); QTest::newRow("beforePDF") << true << true << qreal(30) << qreal(30) << qreal(30) << qreal(30); QTest::newRow("afterNoPDF") << false << false << qreal(30) << qreal(30) << qreal(30) << qreal(30); QTest::newRow("afterAfterPDF") << true << false << qreal(30) << qreal(30) << qreal(30) << qreal(30); } void tst_QPrinter::customPaperSizeAndMargins() { QFETCH(bool, pdf); QFETCH(bool, before); QFETCH(qreal, left); QFETCH(qreal, top); QFETCH(qreal, right); QFETCH(qreal, bottom); qreal tolerance = 0.05; qreal getLeft = 0; qreal getRight = 0; qreal getTop = 0; qreal getBottom = 0; // Use a custom page size that most printers should support, A4 is 210x297 // TODO Use print device api when available QSizeF customSize(200.0, 300.0); QPrinter p; if (pdf) p.setOutputFormat(QPrinter::PdfFormat); if (before) p.setPageMargins(left, top, right, bottom, QPrinter::Millimeter); p.setPaperSize(customSize, QPrinter::Millimeter); p.getPageMargins(&getLeft, &getTop, &getRight, &getBottom, QPrinter::Millimeter); if (before) { QVERIFY(fabs(left - getLeft) < tolerance); QVERIFY(fabs(left - getTop) < tolerance); QVERIFY(fabs(left - getRight) < tolerance); QVERIFY(fabs(left - getBottom) < tolerance); } else { p.setPageMargins(left, top, right, bottom, QPrinter::Millimeter); p.getPageMargins(&getLeft, &getTop, &getRight, &getBottom, QPrinter::Millimeter); QVERIFY(fabs(left - getLeft) < tolerance); QVERIFY(fabs(left - getTop) < tolerance); QVERIFY(fabs(left - getRight) < tolerance); QVERIFY(fabs(left - getBottom) < tolerance); } } #if !defined(QT_NO_COMPLETER) && !defined(QT_NO_FILEDIALOG) void tst_QPrinter::printDialogCompleter() { QPrintDialog dialog; dialog.printer()->setOutputFileName("file.pdf"); TempFileCleanup tmpFile("file.pdf"); dialog.setEnabledOptions(QAbstractPrintDialog::PrintToFile); dialog.show(); QTest::qWait(100); QTest::keyClick(&dialog, Qt::Key_Tab); QTest::keyClick(&dialog, 'P'); // The test passes if it doesn't crash. } #endif static void printPage(QPainter *painter) { painter->setPen(QPen(Qt::black, 4)); painter->drawRect(50, 60, 70, 80); } void tst_QPrinter::taskQTBUG4497_reusePrinterOnDifferentFiles() { TempFileCleanup tmpFile1("out1.pdf"); TempFileCleanup tmpFile2("out2.pdf"); QPrinter printer; { printer.setOutputFileName("out1.pdf"); QPainter painter(&printer); printPage(&painter); } { printer.setOutputFileName("out2.pdf"); QPainter painter(&printer); printPage(&painter); } QFile file1("out1.pdf"); QVERIFY(file1.open(QIODevice::ReadOnly)); QFile file2("out2.pdf"); QVERIFY(file2.open(QIODevice::ReadOnly)); while (!file1.atEnd() && !file2.atEnd()) { QByteArray file1Line = file1.readLine(); QByteArray file2Line = file2.readLine(); if (!file1Line.startsWith("%%CreationDate")) QCOMPARE(file1Line, file2Line); } QVERIFY(file1.atEnd()); QVERIFY(file2.atEnd()); } void tst_QPrinter::testCurrentPage() { QPrinter printer; printer.setFromTo(1, 10); // Test set print range printer.setPrintRange(QPrinter::CurrentPage); QCOMPARE(printer.printRange(), QPrinter::CurrentPage); QCOMPARE(printer.fromPage(), 1); QCOMPARE(printer.toPage(), 10); QPrintDialog dialog(&printer); // Test default Current Page option to off QCOMPARE(dialog.isOptionEnabled(QPrintDialog::PrintCurrentPage), false); // Test enable Current Page option dialog.setOption(QPrintDialog::PrintCurrentPage); QCOMPARE(dialog.isOptionEnabled(QPrintDialog::PrintCurrentPage), true); } void tst_QPrinter::testPdfTitle() { // Check the document name is represented correctly in produced pdf { QPainter painter; QPrinter printer; // This string is just the UTF-8 encoding of the string: \()f ø hiragana o const unsigned char titleBuf[]={0x5c, 0x28, 0x29, 0x66, 0xc3, 0xb8, 0xe3, 0x81, 0x8a, 0x00}; const char *title = reinterpret_cast(titleBuf); printer.setOutputFileName("file.pdf"); printer.setDocName(QString::fromUtf8(title)); painter.begin(&printer); painter.end(); } TempFileCleanup tmpFile("file.pdf"); QFile file("file.pdf"); QVERIFY(file.open(QIODevice::ReadOnly)); // The we expect the title to appear in the PDF as: // ASCII('\title (') UTF16(\\\(\)f ø hiragana o) ASCII(')'). // which has the following binary representation const unsigned char expectedBuf[] = { 0x2f, 0x54, 0x69, 0x74, 0x6c, 0x65, 0x20, 0x28, 0xfe, 0xff, 0x00, 0x5c, 0x5c, 0x00, 0x5c, 0x28, 0x00, 0x5c, 0x29, 0x00, 0x66, 0x00, 0xf8, 0x30, 0x4a, 0x29}; const char *expected = reinterpret_cast(expectedBuf); QVERIFY(file.readAll().contains(QByteArray(expected, 26))); } void tst_QPrinter::customPaperNameSettingBySize() { QPrinter printer(QPrinter::HighResolution); QPrinterInfo info(printer); QList sizes = info.supportedPageSizes(); if (sizes.size() == 0) QSKIP("No printers installed on this machine"); for (int i=0; i sizes = info.supportedPageSizes(); if (sizes.size() == 0) QSKIP("No printers installed on this machine"); for (int i=0; i expected; QPrinter pdf; pdf.setOutputFormat(QPrinter::PdfFormat); expected << 72; QCOMPARE(pdf.supportedResolutions(), expected); QPrinter native; if (native.outputFormat() == QPrinter::NativeFormat) { native.supportedResolutions(); } else { QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); } } void tst_QPrinter::windowsPageSize() { // winPageSize() / setWinPageSize() / PPK_WindowsPageSize // PdfFormat: Supported, defaults to printer default // NativeFormat, Cups: Supported, defaults to printer default // NativeFormat, Win: Supported, defaults to printer default // NativeFormat, Mac: Supported, defaults to printer default QPrinter pdf; pdf.setOutputFormat(QPrinter::PdfFormat); QCOMPARE(pdf.winPageSize(), 9); // DMPAPER_A4 pdf.setWinPageSize(1); // DMPAPER_LETTER QCOMPARE(pdf.winPageSize(), 1); QPrinter native; if (native.outputFormat() == QPrinter::NativeFormat) { // Test set/get native.setPaperSize(QPrinter::A4); QCOMPARE(native.pageSize(), QPrinter::A4); QCOMPARE(native.winPageSize(), 9); // DMPAPER_A4 native.setPaperSize(QPrinter::Letter); QCOMPARE(native.pageSize(), QPrinter::Letter); QCOMPARE(native.winPageSize(), 1); // DMPAPER_LETTER native.setWinPageSize(9); // DMPAPER_A4 QCOMPARE(native.pageSize(), QPrinter::A4); QCOMPARE(native.winPageSize(), 9); // DMPAPER_A4 native.setWinPageSize(1); // DMPAPER_LETTER QCOMPARE(native.pageSize(), QPrinter::Letter); QCOMPARE(native.winPageSize(), 1); // DMPAPER_LETTER // Test value preservation native.setOutputFormat(QPrinter::PdfFormat); QCOMPARE(native.pageSize(), QPrinter::Letter); QCOMPARE(native.winPageSize(), 1); // DMPAPER_LETTER native.setOutputFormat(QPrinter::NativeFormat); QCOMPARE(native.pageSize(), QPrinter::Letter); QCOMPARE(native.winPageSize(), 1); // DMPAPER_LETTER } else { QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); } } // Test QPrinter setters/getters for non-QPrintEngine options void tst_QPrinter::outputFormat() { QPrinter printer; if (QPrinterInfo::availablePrinters().size() == 0) { QCOMPARE(printer.outputFormat(), QPrinter::PdfFormat); QCOMPARE(printer.printerName(), QString()); } else { QCOMPARE(printer.outputFormat(), QPrinter::NativeFormat); QCOMPARE(printer.printerName(), QPrinterInfo::defaultPrinter().printerName()); } printer.setOutputFormat(QPrinter::PdfFormat); QCOMPARE(printer.outputFormat(), QPrinter::PdfFormat); QCOMPARE(printer.printerName(), QString()); } void tst_QPrinter::fromToPage() { QPrinter printer; QCOMPARE(printer.fromPage(), 0); QCOMPARE(printer.toPage(), 0); printer.setFromTo(3, 7); QCOMPARE(printer.fromPage(), 3); QCOMPARE(printer.toPage(), 7); } void tst_QPrinter::testPageMetrics_data() { QTest::addColumn("outputFormat"); QTest::addColumn("pageSize"); QTest::addColumn("widthMMf"); QTest::addColumn("heightMMf"); QTest::addColumn("setMargins"); QTest::addColumn("leftMMf"); QTest::addColumn("rightMMf"); QTest::addColumn("topMMf"); QTest::addColumn("bottomMMf"); QTest::newRow("PDF A4") << int(QPrinter::PdfFormat) << int(QPrinter::A4) << 210.0 << 297.0 << false << 0.0 << 0.0 << 0.0 << 0.0; QTest::newRow("PDF A4 Margins") << int(QPrinter::PdfFormat) << int(QPrinter::A4) << 210.0 << 297.0 << true << 20.0 << 30.0 << 40.0 << 50.0; QTest::newRow("Native A4") << int(QPrinter::NativeFormat) << int(QPrinter::A4) << 210.0 << 297.0 << false << 0.0 << 0.0 << 0.0 << 0.0; QTest::newRow("Native A4 Margins") << int(QPrinter::NativeFormat) << int(QPrinter::A4) << 210.0 << 297.0 << true << 20.0 << 30.0 << 40.0 << 50.0; QTest::newRow("PDF Portrait") << int(QPrinter::PdfFormat) << -1 << 200.0 << 300.0 << false << 0.0 << 0.0 << 0.0 << 0.0; QTest::newRow("PDF Portrait Margins") << int(QPrinter::PdfFormat) << -1 << 200.0 << 300.0 << true << 20.0 << 30.0 << 40.0 << 50.0; QTest::newRow("PDF Landscape") << int(QPrinter::PdfFormat) << -1 << 300.0 << 200.0 << false << 0.0 << 0.0 << 0.0 << 0.0; QTest::newRow("PDF Landscape Margins") << int(QPrinter::PdfFormat) << -1 << 300.0 << 200.0 << true << 20.0 << 30.0 << 40.0 << 50.0; QTest::newRow("Native Portrait") << int(QPrinter::NativeFormat) << -1 << 200.0 << 300.0 << false << 0.0 << 0.0 << 0.0 << 0.0; QTest::newRow("Native Portrait Margins") << int(QPrinter::NativeFormat) << -1 << 200.0 << 300.0 << true << 20.0 << 30.0 << 40.0 << 50.0; QTest::newRow("Native Landscape") << int(QPrinter::NativeFormat) << -1 << 300.0 << 200.0 << false << 0.0 << 0.0 << 0.0 << 0.0; QTest::newRow("Native Landscape Margins") << int(QPrinter::NativeFormat) << -1 << 300.0 << 200.0 << true << 20.0 << 30.0 << 40.0 << 50.0; } void tst_QPrinter::testPageMetrics() { QFETCH(int, outputFormat); QFETCH(int, pageSize); QFETCH(qreal, widthMMf); QFETCH(qreal, heightMMf); QFETCH(bool, setMargins); QFETCH(qreal, leftMMf); QFETCH(qreal, rightMMf); QFETCH(qreal, topMMf); QFETCH(qreal, bottomMMf); QSizeF sizeMMf = QSizeF(widthMMf, heightMMf); QPrinter printer; printer.setOutputFormat(QPrinter::OutputFormat(outputFormat)); if (printer.outputFormat() != QPrinter::OutputFormat(outputFormat)) QSKIP("Please install a native printer to run this test"); QCOMPARE(printer.outputFormat(), QPrinter::OutputFormat(outputFormat)); QCOMPARE(printer.orientation(), QPrinter::Portrait); if (setMargins) { // Setup the given margins QPrinter::Margins margins; margins.left = leftMMf; margins.right = rightMMf; margins.top = topMMf; margins.bottom = bottomMMf; printer.setMargins(margins); QCOMPARE(printer.margins().left, leftMMf); QCOMPARE(printer.margins().right, rightMMf); QCOMPARE(printer.margins().top, topMMf); QCOMPARE(printer.margins().bottom, bottomMMf); } // Set the given size, in Portrait mode if (pageSize < 0) { printer.setPageSizeMM(sizeMMf); QCOMPARE(printer.pageSize(), QPrinter::Custom); } else { printer.setPageSize(QPrinter::PageSize(pageSize)); QCOMPARE(printer.pageSize(), QPrinter::PageSize(pageSize)); } QCOMPARE(printer.orientation(), QPrinter::Portrait); if (setMargins) { // Check margins unchanged from page size change QCOMPARE(printer.margins().left, leftMMf); QCOMPARE(printer.margins().right, rightMMf); QCOMPARE(printer.margins().top, topMMf); QCOMPARE(printer.margins().bottom, bottomMMf); } else { // Fetch the default margins for the printer and page size // TODO Check against margins from print device when api added leftMMf = printer.margins().left; rightMMf = printer.margins().right; topMMf = printer.margins().top; bottomMMf = printer.margins().bottom; } // QPagedPaintDevice::pageSizeMM() always returns Portrait QCOMPARE(printer.pageSizeMM(), sizeMMf); // QPrinter::paperSize() always returns set orientation QCOMPARE(printer.paperSize(QPrinter::Millimeter), sizeMMf); // QPagedPaintDevice::widthMM() and heightMM() are paint metrics and always return set orientation QCOMPARE(printer.widthMM(), qRound(widthMMf - leftMMf - rightMMf)); QCOMPARE(printer.heightMM(), qRound(heightMMf - topMMf - bottomMMf)); // QPrinter::paperRect() always returns set orientation QCOMPARE(printer.paperRect(QPrinter::Millimeter), QRectF(0, 0, widthMMf, heightMMf)); // QPrinter::pageRect() always returns set orientation QCOMPARE(printer.pageRect(QPrinter::Millimeter), QRectF(leftMMf, topMMf, widthMMf - leftMMf - rightMMf, heightMMf - topMMf - bottomMMf)); // Now switch to Landscape mode, size should be unchanged, but rect and metrics should change printer.setOrientation(QPrinter::Landscape); if (pageSize < 0) { QCOMPARE(printer.pageSize(), QPrinter::Custom); } else { QCOMPARE(printer.pageSize(), QPrinter::PageSize(pageSize)); } QCOMPARE(printer.orientation(), QPrinter::Landscape); if (setMargins) { // Check margins unchanged from page size change QCOMPARE(printer.margins().left, leftMMf); QCOMPARE(printer.margins().right, rightMMf); QCOMPARE(printer.margins().top, topMMf); QCOMPARE(printer.margins().bottom, bottomMMf); } else { // Fetch the default margins for the printer and page size // TODO Check against margins from print device when api added leftMMf = printer.margins().left; rightMMf = printer.margins().right; topMMf = printer.margins().top; bottomMMf = printer.margins().bottom; } // QPagedPaintDevice::pageSizeMM() always returns Portrait QCOMPARE(printer.pageSizeMM(), sizeMMf); // QPrinter::paperSize() always returns set orientation QCOMPARE(printer.paperSize(QPrinter::Millimeter), sizeMMf.transposed()); // QPagedPaintDevice::widthMM() and heightMM() are paint metrics and always return set orientation QCOMPARE(printer.widthMM(), qRound(heightMMf - leftMMf - rightMMf)); QCOMPARE(printer.heightMM(), qRound(widthMMf - topMMf - bottomMMf)); // QPrinter::paperRect() always returns set orientation QCOMPARE(printer.paperRect(QPrinter::Millimeter), QRectF(0, 0, heightMMf, widthMMf)); // QPrinter::pageRect() always returns set orientation QCOMPARE(printer.pageRect(QPrinter::Millimeter), QRectF(leftMMf, topMMf, heightMMf - leftMMf - rightMMf, widthMMf - topMMf - bottomMMf)); // Now while in Landscape mode, set the size again, results should be the same if (pageSize < 0) { printer.setPageSizeMM(sizeMMf); QCOMPARE(printer.pageSize(), QPrinter::Custom); } else { printer.setPageSize(QPrinter::PageSize(pageSize)); QCOMPARE(printer.pageSize(), QPrinter::PageSize(pageSize)); } QCOMPARE(printer.orientation(), QPrinter::Landscape); if (setMargins) { // Check margins unchanged from page size change QCOMPARE(printer.margins().left, leftMMf); QCOMPARE(printer.margins().right, rightMMf); QCOMPARE(printer.margins().top, topMMf); QCOMPARE(printer.margins().bottom, bottomMMf); } else { // Fetch the default margins for the printer and page size // TODO Check against margins from print device when api added leftMMf = printer.margins().left; rightMMf = printer.margins().right; topMMf = printer.margins().top; bottomMMf = printer.margins().bottom; } // QPagedPaintDevice::pageSizeMM() always returns Portrait QCOMPARE(printer.pageSizeMM(), sizeMMf); // QPrinter::paperSize() always returns set orientation QCOMPARE(printer.paperSize(QPrinter::Millimeter), sizeMMf.transposed()); // QPagedPaintDevice::widthMM() and heightMM() are paint metrics and always return set orientation QCOMPARE(printer.widthMM(), qRound(heightMMf - leftMMf - rightMMf)); QCOMPARE(printer.heightMM(), qRound(widthMMf - topMMf - bottomMMf)); // QPrinter::paperRect() always returns set orientation QCOMPARE(printer.paperRect(QPrinter::Millimeter), QRectF(0, 0, heightMMf, widthMMf)); // QPrinter::pageRect() always returns set orientation QCOMPARE(printer.pageRect(QPrinter::Millimeter), QRectF(leftMMf, topMMf, heightMMf - leftMMf - rightMMf, widthMMf - topMMf - bottomMMf)); } #endif // QT_NO_PRINTER QTEST_MAIN(tst_QPrinter) #include "tst_qprinter.moc"