diff options
Diffstat (limited to 'tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp')
-rw-r--r-- | tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp | 193 |
1 files changed, 92 insertions, 101 deletions
diff --git a/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp b/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp index 16bb0b7c3e..cc32e73b9c 100644 --- a/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp +++ b/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp @@ -39,6 +39,7 @@ #include <qpainter.h> #include <qprintengine.h> #include <qpagelayout.h> +#include <qsharedpointer.h> #include <qtemporarydir.h> #include <math.h> @@ -47,25 +48,15 @@ #include <windows.h> #endif +#if QT_CONFIG(printer) +typedef QSharedPointer<QPrinter> PrinterPtr; -QT_FORWARD_DECLARE_CLASS(QPrinter) +Q_DECLARE_METATYPE(PrinterPtr) +Q_DECLARE_METATYPE(QPrinter::Orientation) +Q_DECLARE_METATYPE(QPrinter::PageSize) +#endif // printer -// 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; -}; +static int fileNumber = 0; class tst_QPrinter : public QObject { @@ -133,6 +124,9 @@ private slots: void testPageMetrics(); #endif private: + QString testFileName(const QString &prefix, const QString &suffix); + QString testPdfFileName(const QString &prefix) { return testFileName(prefix, QStringLiteral("pdf")); } + QTemporaryDir m_tempDir; }; @@ -202,72 +196,72 @@ void tst_QPrinter::testPrintPreviewDialog() void tst_QPrinter::testPageRectAndPaperRect_data() { - QTest::addColumn<int>("orientation"); + QTest::addColumn<PrinterPtr>("printer"); + QTest::addColumn<QPrinter::Orientation>("orientation"); QTest::addColumn<bool>("withPainter"); QTest::addColumn<int>("resolution"); QTest::addColumn<bool>("doPaperRect"); + const PrinterPtr printer(new QPrinter(QPrinter::HighResolution)); // 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; + QTest::newRow("paperRect0") << printer << QPrinter::Portrait << true << 300 << true; + QTest::newRow("paperRect1") << printer << QPrinter::Portrait << false << 300 << true; + QTest::newRow("paperRect2") << printer << QPrinter::Landscape << true << 300 << true; + QTest::newRow("paperRect3") << printer << QPrinter::Landscape << false << 300 << true; + QTest::newRow("paperRect4") << printer << QPrinter::Portrait << true << 600 << true; + QTest::newRow("paperRect5") << printer << QPrinter::Portrait << false << 600 << true; + QTest::newRow("paperRect6") << printer << QPrinter::Landscape << true << 600 << true; + QTest::newRow("paperRect7") << printer << QPrinter::Landscape << false << 600 << true; + QTest::newRow("paperRect8") << printer << QPrinter::Portrait << true << 1200 << true; + QTest::newRow("paperRect9") << printer << QPrinter::Portrait << false << 1200 << true; + QTest::newRow("paperRect10") << printer << QPrinter::Landscape << true << 1200 << true; + QTest::newRow("paperRect11") << printer << 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; + QTest::newRow("pageRect0") << printer << QPrinter::Portrait << true << 300 << false; + QTest::newRow("pageRect1") << printer << QPrinter::Portrait << false << 300 << false; + QTest::newRow("pageRect2") << printer << QPrinter::Landscape << true << 300 << false; + QTest::newRow("pageRect3") << printer << QPrinter::Landscape << false << 300 << false; + QTest::newRow("pageRect4") << printer << QPrinter::Portrait << true << 600 << false; + QTest::newRow("pageRect5") << printer << QPrinter::Portrait << false << 600 << false; + QTest::newRow("pageRect6") << printer << QPrinter::Landscape << true << 600 << false; + QTest::newRow("pageRect7") << printer << QPrinter::Landscape << false << 600 << false; + QTest::newRow("pageRect8") << printer << QPrinter::Portrait << true << 1200 << false; + QTest::newRow("pageRect9") << printer << QPrinter::Portrait << false << 1200 << false; + QTest::newRow("pageRect10") << printer << QPrinter::Landscape << true << 1200 << false; + QTest::newRow("pageRect11") << printer << QPrinter::Landscape << false << 1200 << false; } void tst_QPrinter::testPageRectAndPaperRect() { + QFETCH(PrinterPtr, printer); QFETCH(bool, withPainter); - QFETCH(int, orientation); + QFETCH(QPrinter::Orientation, orientation); QFETCH(int, resolution); QFETCH(bool, doPaperRect); QPainter *painter = 0; - QPrinter printer(QPrinter::HighResolution); - printer.setOrientation(QPrinter::Orientation(orientation)); - const QString fileName = m_tempDir.path() + QLatin1String("/silly"); - printer.setOutputFileName(fileName); - TempFileCleanup tmpFile(fileName); - - 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); + printer->setOrientation(orientation); + printer->setOutputFileName(testFileName(QLatin1String("silly"), QString())); + + 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); + painter = new QPainter(printer.data()); - QRect otherRect = doPaperRect ? printer.paperRect() : printer.pageRect(); - float otherInchesX = float(otherRect.width()) / float(printer.resolution()); - float otherInchesY = float(otherRect.height()) / float(printer.resolution()); + 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()); + QVERIFY(printer->orientation() == QPrinter::Portrait || pageRect.width() > pageRect.height()); + QVERIFY(printer->orientation() != QPrinter::Portrait || pageRect.width() < pageRect.height()); } void tst_QPrinter::testSetOptions() @@ -299,45 +293,40 @@ void tst_QPrinter::testSetOptions() void tst_QPrinter::testMargins_data() { - QTest::addColumn<int>("orientation"); + QTest::addColumn<PrinterPtr>("printer"); + QTest::addColumn<QPrinter::Orientation>("orientation"); QTest::addColumn<bool>("fullpage"); - QTest::addColumn<int>("pagesize"); - QTest::addColumn<int>("width"); - QTest::addColumn<int>("height"); + QTest::addColumn<QPrinter::PageSize>("pagesize"); QTest::addColumn<bool>("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; + const PrinterPtr printer(new QPrinter); + QTest::newRow("data0") << printer << QPrinter::Portrait << true << QPrinter::A4 << false; + QTest::newRow("data1") << printer << QPrinter::Landscape << true << QPrinter::A4 << false; + QTest::newRow("data2") << printer << QPrinter::Landscape << false << QPrinter::A4 << false; + QTest::newRow("data3") << printer << QPrinter::Portrait << false << QPrinter::A4 << false; + QTest::newRow("data4") << printer << QPrinter::Portrait << true << QPrinter::A4 << true; + QTest::newRow("data5") << printer << QPrinter::Landscape << true << QPrinter::A4 << true; + QTest::newRow("data6") << printer << QPrinter::Landscape << false << QPrinter::A4 << true; + QTest::newRow("data7") << printer << QPrinter::Portrait << false << QPrinter::A4 << true; } void tst_QPrinter::testMargins() { + QFETCH(PrinterPtr, printer); QFETCH(bool, withPainter); - QFETCH(int, orientation); - QFETCH(int, pagesize); - QFETCH(int, width); - QFETCH(int, height); + QFETCH(QPrinter::Orientation, orientation); + QFETCH(QPrinter::PageSize, pagesize); 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); + printer->setOutputFileName(testFileName(QLatin1String("silly"), QString())); + printer->setOrientation(orientation); + printer->setFullPage(fullpage); + printer->setPageSize(pagesize); if (withPainter) - painter = new QPainter(&printer); + painter = new QPainter(printer.data()); if (painter) delete painter; - QFile::remove("silly"); } void tst_QPrinter::testMulitpleSets_data() @@ -420,9 +409,7 @@ void tst_QPrinter::outputFormatFromSuffix() QSKIP("No printers available."); QPrinter p; QCOMPARE(p.outputFormat(), QPrinter::NativeFormat); - const QString fileName = m_tempDir.path() + QLatin1String("/test.pdf"); - p.setOutputFileName(fileName); - TempFileCleanup tmpFile(fileName); + p.setOutputFileName(testPdfFileName(QLatin1String("test"))); QCOMPARE(p.outputFormat(), QPrinter::PdfFormat); p.setOutputFileName(QString()); QCOMPARE(p.outputFormat(), QPrinter::NativeFormat); @@ -510,9 +497,7 @@ void tst_QPrinter::errorReporting() p.setOutputFileName("/foobar/nonwritable.pdf"); QCOMPARE(painter.begin(&p), false); // it should check the output file is writable #endif - const QString fileName = m_tempDir.path() + QLatin1String("/test.pdf"); - p.setOutputFileName(fileName); - TempFileCleanup tmpFile(fileName); + p.setOutputFileName(testPdfFileName(QLatin1String("test"))); QCOMPARE(painter.begin(&p), true); // it should check the output QCOMPARE(p.isValid(), true); painter.end(); @@ -606,8 +591,7 @@ void tst_QPrinter::customPaperSizeAndMargins() void tst_QPrinter::printDialogCompleter() { QPrintDialog dialog; - dialog.printer()->setOutputFileName("file.pdf"); - TempFileCleanup tmpFile("file.pdf"); + dialog.printer()->setOutputFileName(testPdfFileName(QLatin1String("file"))); dialog.setEnabledOptions(QAbstractPrintDialog::PrintToFile); dialog.show(); @@ -627,10 +611,8 @@ static void printPage(QPainter *painter) void tst_QPrinter::taskQTBUG4497_reusePrinterOnDifferentFiles() { - const QString fileName1 = m_tempDir.path() + QLatin1String("/out1.pdf"); - const QString fileName2 = m_tempDir.path() + QLatin1String("/out2.pdf"); - TempFileCleanup tmpFile1(fileName1); - TempFileCleanup tmpFile2(fileName2); + const QString fileName1 = testPdfFileName(QLatin1String("out1_")); + const QString fileName2 = testPdfFileName(QLatin1String("out2_")); QPrinter printer; { @@ -689,7 +671,7 @@ void tst_QPrinter::testCurrentPage() void tst_QPrinter::testPdfTitle() { - const QString fileName = m_tempDir.path() + QLatin1String("/file.pdf"); + const QString fileName = testPdfFileName(QLatin1String("file")); // Check the document name is represented correctly in produced pdf { @@ -703,7 +685,6 @@ void tst_QPrinter::testPdfTitle() painter.begin(&printer); painter.end(); } - TempFileCleanup tmpFile(fileName); QFile file(fileName); QVERIFY(file.open(QIODevice::ReadOnly)); // The we expect the title to appear in the PDF as: @@ -1244,8 +1225,9 @@ void tst_QPrinter::outputFileName() QPrinter pdf; pdf.setOutputFormat(QPrinter::PdfFormat); QCOMPARE(pdf.outputFileName(), QString()); - pdf.setOutputFileName(QStringLiteral("Test File")); - QCOMPARE(pdf.outputFileName(), QString("Test File")); + const QString fileName = testFileName(QStringLiteral("Test File"), QString()); + pdf.setOutputFileName(fileName); + QCOMPARE(pdf.outputFileName(), fileName); QPrinter native; if (native.outputFormat() == QPrinter::NativeFormat) { @@ -1253,7 +1235,7 @@ void tst_QPrinter::outputFileName() QCOMPARE(native.outputFileName(), QString()); // Test set/get - QString expected = QStringLiteral("Test File"); + QString expected = fileName; native.setOutputFileName(expected); QCOMPARE(native.outputFileName(), expected); @@ -1960,6 +1942,15 @@ void tst_QPrinter::testPageMetrics() QCOMPARE(printer.pageRect(QPrinter::Millimeter), QRectF(leftMMf, topMMf, heightMMf - leftMMf - rightMMf, widthMMf - topMMf - bottomMMf)); } +QString tst_QPrinter::testFileName(const QString &prefix, const QString &suffix) +{ + QString result = m_tempDir.path() + QLatin1Char('/') + prefix + + QString::number(fileNumber++); + if (!suffix.isEmpty()) + result += QLatin1Char('.') + suffix; + return result; +} + #endif // QT_CONFIG(printer) QTEST_MAIN(tst_QPrinter) |