From 053bee8b80f4f987718c6633b831a58eb231b599 Mon Sep 17 00:00:00 2001 From: John Layt Date: Sat, 30 Nov 2013 00:00:01 +0100 Subject: [PATCH] QPrinter - Clean up Print Engine Key defaults and tests The PrintEngine keys are not consistently treated across the platforms and are not properly tested. Start the process of making the print engines behave consistently by documenting and testing the current behavior. Ensure all unsupported features return a consistent default value. The auto test for valuePreservation() has been flaky depending on the platform and installed printers so remove it and replace it with more complete testing. If no native printers available then don't test the native engines. Fixes for the individual inconsistent keys will follow. Task-number: QTBUG-26430 Change-Id: Iab914d7e0a5ae4a2cdc24c8645751f0910cf440c Reviewed-by: Lars Knoll --- .../platforms/cocoa/qprintengine_mac.mm | 88 +- src/printsupport/kernel/qprintengine_pdf.cpp | 42 +- src/printsupport/kernel/qprintengine_win.cpp | 83 +- src/printsupport/kernel/qprinter.cpp | 1 - .../kernel/qprinter/tst_qprinter.cpp | 1416 ++++++++++++----- 5 files changed, 1189 insertions(+), 441 deletions(-) diff --git a/src/plugins/platforms/cocoa/qprintengine_mac.mm b/src/plugins/platforms/cocoa/qprintengine_mac.mm index f363b1772f..dd2bd718f6 100644 --- a/src/plugins/platforms/cocoa/qprintengine_mac.mm +++ b/src/plugins/platforms/cocoa/qprintengine_mac.mm @@ -591,20 +591,52 @@ void QMacPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va return; switch (key) { - case PPK_CollateCopies: + + // The following keys are properties or derived values and so cannot be set + case PPK_PageRect: break; + case PPK_PaperRect: + break; + case PPK_PaperSources: + break; + case PPK_SupportsMultipleCopies: + break; + case PPK_SupportedResolutions: + break; + + // The following keys are settings that are unsupported by the Mac PrintEngine case PPK_ColorMode: break; + case PPK_CollateCopies: + // TODO Add support using PMSetCollate / PMGetCollate + break; case PPK_Creator: + // TODO Add value preservation support by using local variable + break; + case PPK_CustomBase: break; case PPK_DocumentName: + // TODO Add support using PMPrintSettingsSetJobName / PMPrintSettingsGetJobName + break; + case PPK_Duplex: + // TODO Add support using PMSetDuplex / PMGetDuplex + break; + case PPK_FontEmbedding: break; case PPK_PageOrder: + // TODO Check if can be supported via Cups Options break; case PPK_PaperSource: + // TODO Check if can be supported via Cups Options + break; + case PPK_PrinterProgram: break; case PPK_SelectionOption: break; + case PPK_WindowsPageSize: + break; + + // The following keys are properties and settings that are supported by the Mac PrintEngine case PPK_Resolution: { PMPrinter printer; UInt32 count; @@ -709,9 +741,7 @@ void QMacPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va d->hasCustomPageMargins = true; break; } - - default: - break; + // No default so that compiler will complain if new keys added and not handled in this engine } } @@ -724,16 +754,59 @@ QVariant QMacPrintEngine::property(PrintEnginePropertyKey key) const return *d->valueCache.find(key); switch (key) { + + // The following keys are settings that are unsupported by the Mac PrintEngine + // Return sensible default values to ensure consistent behavior across platforms case PPK_CollateCopies: + // TODO Add support using PMSetCollate / PMGetCollate ret = false; break; case PPK_ColorMode: ret = QPrinter::Color; break; case PPK_Creator: + ret = QString(); + break; + case PPK_CustomBase: + // Special case, leave null break; case PPK_DocumentName: + // TODO Add support using PMPrintSettingsSetJobName / PMPrintSettingsGetJobName + ret = QString(); break; + case PPK_Duplex: + // TODO Add support using PMSetDuplex / PMGetDuplex + ret = QPrinter::DuplexNone; + break; + case PPK_FontEmbedding: + ret = false; + break; + case PPK_PageOrder: + // TODO Check if can be supported via Cups Options + ret = QPrinter::FirstPageFirst; + break; + case PPK_PaperSource: + // TODO Check if can be supported via Cups Options + ret = QPrinter::Auto; + break; + case PPK_PaperSources: { + // TODO Check if can be supported via Cups Options + QList out; + out << int(QPrinter::Auto); + ret = out; + break; + } + case PPK_PrinterProgram: + ret = QString(); + break; + case PPK_SelectionOption: + ret = QString(); + break; + case PPK_WindowsPageSize: + // Special case, leave null + break; + + // The following keys are properties and settings that are supported by the Mac PrintEngine case PPK_FullPage: ret = d->fullPage; break; @@ -757,10 +830,6 @@ QVariant QMacPrintEngine::property(PrintEnginePropertyKey key) const case PPK_OutputFileName: ret = d->outputFilename; break; - case PPK_PageOrder: - break; - case PPK_PaperSource: - break; case PPK_PageRect: { // PageRect is returned in device pixels QRect r; @@ -855,8 +924,7 @@ QVariant QMacPrintEngine::property(PrintEnginePropertyKey key) const ret = margins; break; } - default: - break; + // No default so that compiler will complain if new keys added and not handled in this engine } return ret; } diff --git a/src/printsupport/kernel/qprintengine_pdf.cpp b/src/printsupport/kernel/qprintengine_pdf.cpp index 7b3c3c1dac..f5c80b4cae 100644 --- a/src/printsupport/kernel/qprintengine_pdf.cpp +++ b/src/printsupport/kernel/qprintengine_pdf.cpp @@ -147,6 +147,28 @@ void QPdfPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va Q_D(QPdfPrintEngine); switch (int(key)) { + + // The following keys are properties or derived values and so cannot be set + case PPK_PageRect: + break; + case PPK_PaperRect: + break; + case PPK_PaperSources: + break; + case PPK_SupportsMultipleCopies: + break; + case PPK_SupportedResolutions: + break; + + // The following keys are settings that are unsupported by the PDF PrintEngine + case PPK_CustomBase: + break; + case PPK_PaperName: + break; + case PPK_WindowsPageSize: + break; + + // The following keys are properties and settings that are supported by the PDF PrintEngine case PPK_CollateCopies: d->collate = value.toBool(); break; @@ -216,8 +238,7 @@ void QPdfPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va d->pageMarginsSet = true; break; } - default: - break; + // No default so that compiler will complain if new keys added and not handled in this engine } } @@ -227,6 +248,20 @@ QVariant QPdfPrintEngine::property(PrintEnginePropertyKey key) const QVariant ret; switch (int(key)) { + + // The following keys are settings that are unsupported by the PDF PrintEngine + // Return sensible default values to ensure consistent behavior across platforms + case PPK_CustomBase: + // Special case, leave null + break; + case PPK_PaperName: + ret = QString(); + break; + case PPK_WindowsPageSize: + // Special case, leave null + break; + + // The following keys are properties and settings that are supported by the PDF PrintEngine case PPK_CollateCopies: ret = d->collate; break; @@ -307,8 +342,7 @@ QVariant QPdfPrintEngine::property(PrintEnginePropertyKey key) const ret = margins; break; } - default: - break; + // No default so that compiler will complain if new keys added and not handled in this engine } return ret; } diff --git a/src/printsupport/kernel/qprintengine_win.cpp b/src/printsupport/kernel/qprintengine_win.cpp index 6dafbb4870..d36b7b87c1 100644 --- a/src/printsupport/kernel/qprintengine_win.cpp +++ b/src/printsupport/kernel/qprintengine_win.cpp @@ -1273,6 +1273,38 @@ void QWin32PrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant & { Q_D(QWin32PrintEngine); switch (key) { + + // The following keys are properties or derived values and so cannot be set + case PPK_PageRect: + break; + case PPK_PaperRect: + break; + case PPK_PaperSources: + break; + case PPK_SupportsMultipleCopies: + break; + case PPK_SupportedResolutions: + break; + + // The following keys are settings that are unsupported by the Windows PrintEngine + case PPK_Creator: + // TODO Add value preservation support by using local variable + break; + case PPK_CustomBase: + break; + case PPK_Duplex: + // TODO Add support using DEVMODE.dmDuplex + break; + case PPK_FontEmbedding: + break; + case PPK_PageOrder: + break; + case PPK_PrinterProgram: + break; + case PPK_SelectionOption: + break; + + // The following keys are properties and settings that are supported by the Windows PrintEngine case PPK_CollateCopies: { if (!d->devMode) @@ -1291,10 +1323,6 @@ void QWin32PrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant & } break; - case PPK_Creator: - - break; - case PPK_DocumentName: if (isActive()) { qWarning("QWin32PrintEngine: Cannot change document name while printing is active"); @@ -1347,6 +1375,7 @@ void QWin32PrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant & setDevModePaperFlags(d->devMode, d->has_custom_paper_size); d->doReinit(); break; + case PPK_PaperName: { if (!d->devMode) @@ -1416,15 +1445,6 @@ void QWin32PrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant & } break; - case PPK_SelectionOption: - - break; - - case PPK_SupportedResolutions: - - break; - - case PPK_WindowsPageSize: if (!d->devMode) break; @@ -1468,9 +1488,9 @@ void QWin32PrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant & d->setPageMargins(left, top, right, bottom); break; } - default: - // Do nothing - break; + + // No default so that compiler will complain if new keys added and not handled in this engine + } } @@ -1480,10 +1500,33 @@ QVariant QWin32PrintEngine::property(PrintEnginePropertyKey key) const QVariant value; switch (key) { + // The following keys are settings that are unsupported by the Windows PrintEngine + // Return sensible default values to ensure consistent behavior across platforms case PPK_CollateCopies: + // TODO Add support using DEVMODE.dmCollate to match setting value = false; break; + case PPK_Creator: + value = QString(); + break; + case PPK_Duplex: + // TODO Add support using DEVMODE.dmDuplex + value = QPrinter::DuplexNone; + break; + case PPK_FontEmbedding: + value = false; + break; + case PPK_PageOrder: + value = QPrinter::FirstPageFirst; + break; + case PPK_PrinterProgram: + value = QString(); + break; + case PPK_SelectionOption: + value = QString(); + break; + // The following keys are properties and settings that are supported by the Windows PrintEngine case PPK_ColorMode: { if (!d->devMode) { @@ -1567,6 +1610,7 @@ QVariant QWin32PrintEngine::property(PrintEnginePropertyKey key) const value = QTransform(1/d->stretch_x, 0, 0, 1/d->stretch_y, 0, 0).mapRect(d->devPaperRect); } break; + case PPK_PaperName: if (!d->devMode) { value = QLatin1String("A4"); @@ -1596,6 +1640,7 @@ QVariant QWin32PrintEngine::property(PrintEnginePropertyKey key) const } } break; + case PPK_PaperSource: if (!d->devMode) { value = QPrinter::Auto; @@ -1670,9 +1715,9 @@ QVariant QWin32PrintEngine::property(PrintEnginePropertyKey key) const value = margins; break; } - default: - // Do nothing - break; + + // No default so that compiler will complain if new keys added and not handled in this engine + } return value; } diff --git a/src/printsupport/kernel/qprinter.cpp b/src/printsupport/kernel/qprinter.cpp index 984b11123e..77fa40f414 100644 --- a/src/printsupport/kernel/qprinter.cpp +++ b/src/printsupport/kernel/qprinter.cpp @@ -226,7 +226,6 @@ void QPrinterPrivate::changeEngines(QPrinter::OutputFormat format, const QPrinte prop = QVariant(q_ptr->copyCount()); else if (key != QPrintEngine::PPK_PrinterName) prop = oldPrintEngine->property(key); - if (prop.isValid()) setProperty(key, prop); } diff --git a/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp b/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp index b8b4c00549..37aa7b2a16 100644 --- a/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp +++ b/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp @@ -88,10 +88,7 @@ public slots: void cleanupTestCase(); #else private slots: - void getSetCheck(); -// Add your testfunctions and testdata create functions here #ifdef Q_OS_WIN - void testPageSize(); void testNonExistentPrinter(); #endif void testPageRectAndPaperRect(); @@ -105,10 +102,8 @@ private slots: void testMulitpleSets(); void testPageMargins_data(); void testPageMargins(); - void changingOutputFormat(); void outputFormatFromSuffix(); void setGetPaperSize(); - void valuePreservation(); void errorReporting(); void testCustomPageSizes(); void customPaperSizeAndMargins_data(); @@ -118,14 +113,41 @@ private slots: #if !defined(QT_NO_COMPLETER) && !defined(QT_NO_FILEDIALOG) void printDialogCompleter(); #endif - - void testCopyCount(); void testCurrentPage(); - void taskQTBUG4497_reusePrinterOnDifferentFiles(); void testPdfTitle(); void testPageMetrics_data(); void testPageMetrics(); + + // 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(); #endif }; @@ -140,98 +162,6 @@ void tst_QPrinter::cleanupTestCase() QSKIP("This test requires printing support"); } #else -// Testing get/set functions -void tst_QPrinter::getSetCheck() -{ - QPrinter obj1; - // OutputFormat QPrinter::outputFormat() - // void QPrinter::setOutputFormat(OutputFormat) - obj1.setOutputFormat(QPrinter::OutputFormat(QPrinter::PdfFormat)); - QCOMPARE(QPrinter::OutputFormat(QPrinter::PdfFormat), obj1.outputFormat()); - - // bool QPrinter::collateCopies() - // void QPrinter::setCollateCopies(bool) - obj1.setCollateCopies(false); - QCOMPARE(false, obj1.collateCopies()); - obj1.setCollateCopies(true); - QCOMPARE(true, obj1.collateCopies()); - - obj1.setColorMode(QPrinter::GrayScale); - QCOMPARE(obj1.colorMode(), QPrinter::GrayScale); - obj1.setColorMode(QPrinter::Color); - QCOMPARE(obj1.colorMode(), QPrinter::Color); - - obj1.setCreator(QString::fromLatin1("RandomQtUser")); - QCOMPARE(obj1.creator(), QString::fromLatin1("RandomQtUser")); - - obj1.setDocName(QString::fromLatin1("RandomQtDocument")); - QCOMPARE(obj1.docName(), QString::fromLatin1("RandomQtDocument")); - - obj1.setDoubleSidedPrinting(true); - QCOMPARE(obj1.doubleSidedPrinting(), true); - obj1.setDoubleSidedPrinting(false); - QCOMPARE(obj1.doubleSidedPrinting(), false); - - obj1.setFromTo(1, 4); - QCOMPARE(obj1.fromPage(), 1); - QCOMPARE(obj1.toPage(), 4); - - obj1.setFullPage(true); - QCOMPARE(obj1.fullPage(), true); - obj1.setFullPage(false); - QCOMPARE(obj1.fullPage(), false); - - obj1.setOrientation(QPrinter::Landscape); - QCOMPARE(obj1.orientation(), QPrinter::Landscape); - obj1.setOrientation(QPrinter::Portrait); - QCOMPARE(obj1.orientation(), QPrinter::Portrait); - - obj1.setOutputFileName(QString::fromLatin1("RandomQtName")); - QCOMPARE(obj1.outputFileName(), QString::fromLatin1("RandomQtName")); - - obj1.setPageOrder(QPrinter::FirstPageFirst); - QCOMPARE(obj1.pageOrder(), QPrinter::FirstPageFirst); - obj1.setPageOrder(QPrinter::LastPageFirst); - QCOMPARE(obj1.pageOrder(), QPrinter::LastPageFirst); - - obj1.setPaperSource(QPrinter::Cassette); - QCOMPARE(obj1.paperSource(), QPrinter::Cassette); - obj1.setPaperSource(QPrinter::Middle); - QCOMPARE(obj1.paperSource(), QPrinter::Middle); - -#ifdef Q_OS_UNIX - obj1.setPrintProgram(QString::fromLatin1("/bin/true")); - QCOMPARE(obj1.printProgram(), QString::fromLatin1("/bin/true")); - - obj1.setPrinterSelectionOption(QString::fromLatin1("--option")); - QCOMPARE(obj1.printerSelectionOption(), QString::fromLatin1("--option")); -#endif - - // bool QPrinter::fontEmbeddingEnabled() - // void QPrinter::setFontEmbeddingEnabled(bool) - obj1.setFontEmbeddingEnabled(false); - QCOMPARE(false, obj1.fontEmbeddingEnabled()); - obj1.setFontEmbeddingEnabled(true); - QCOMPARE(true, obj1.fontEmbeddingEnabled()); - - // PageSize QPrinter::pageSize() - // void QPrinter::setPageSize(PageSize) - obj1.setPageSize(QPrinter::PageSize(QPrinter::A4)); - QCOMPARE(QPrinter::PageSize(QPrinter::A4), obj1.pageSize()); - obj1.setPageSize(QPrinter::PageSize(QPrinter::Letter)); - QCOMPARE(QPrinter::PageSize(QPrinter::Letter), obj1.pageSize()); - obj1.setPageSize(QPrinter::PageSize(QPrinter::Legal)); - QCOMPARE(QPrinter::PageSize(QPrinter::Legal), obj1.pageSize()); - - // PrintRange QPrinter::printRange() - // void QPrinter::setPrintRange(PrintRange) - obj1.setPrintRange(QPrinter::PrintRange(QPrinter::AllPages)); - QCOMPARE(QPrinter::PrintRange(QPrinter::AllPages), obj1.printRange()); - obj1.setPrintRange(QPrinter::PrintRange(QPrinter::Selection)); - QCOMPARE(QPrinter::PrintRange(QPrinter::Selection), obj1.printRange()); - obj1.setPrintRange(QPrinter::PrintRange(QPrinter::PageRange)); - QCOMPARE(QPrinter::PrintRange(QPrinter::PageRange), obj1.printRange()); -} #define MYCOMPARE(a, b) QCOMPARE(QVariant((int)a), QVariant((int)b)) @@ -287,30 +217,6 @@ void tst_QPrinter::testPrintPreviewDialog() QCOMPARE(widget->currentPage(), 1); } -#ifdef Q_OS_WIN -// QPrinter::winPageSize(): Windows only. -void tst_QPrinter::testPageSize() -{ - QPrinter prn; - - prn.setPageSize(QPrinter::Letter); - MYCOMPARE(prn.pageSize(), QPrinter::Letter); - MYCOMPARE(prn.winPageSize(), DMPAPER_LETTER); - - prn.setPageSize(QPrinter::A4); - MYCOMPARE(prn.pageSize(), QPrinter::A4); - MYCOMPARE(prn.winPageSize(), DMPAPER_A4); - - prn.setWinPageSize(DMPAPER_LETTER); - MYCOMPARE(prn.winPageSize(), DMPAPER_LETTER); - MYCOMPARE(prn.pageSize(), QPrinter::Letter); - - prn.setWinPageSize(DMPAPER_A4); - MYCOMPARE(prn.winPageSize(), DMPAPER_A4); - MYCOMPARE(prn.pageSize(), QPrinter::A4); -} -#endif // Q_OS_WIN - void tst_QPrinter::testPageRectAndPaperRect_data() { QTest::addColumn("orientation"); @@ -558,17 +464,6 @@ void tst_QPrinter::testMulitpleSets() QVERIFY(qAbs(paperHeight - widthMMAfter) <= 2); } -void tst_QPrinter::changingOutputFormat() -{ -#if QT_VERSION < 0x050000 - QPrinter p; - p.setOutputFormat(QPrinter::PostScriptFormat); - p.setPageSize(QPrinter::A8); - p.setOutputFormat(QPrinter::PdfFormat); - QCOMPARE(p.pageSize(), QPrinter::A8); -#endif -} - void tst_QPrinter::outputFormatFromSuffix() { if (QPrinterInfo::availablePrinters().size() == 0) @@ -673,271 +568,6 @@ void tst_QPrinter::testPageMargins() QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Cicero]) < tolerance); } -void tst_QPrinter::valuePreservation() -{ - QPrinter::OutputFormat oldFormat = QPrinter::PdfFormat; - QPrinter::OutputFormat newFormat = QPrinter::NativeFormat; // TODO: Correct? - - // Some properties are documented to only be supported by NativeFormat in X11 environment - bool doX11Tests = QGuiApplication::platformName().compare(QLatin1String("xcb"), Qt::CaseInsensitive) == 0; - bool windowsPlatform = QGuiApplication::platformName().compare(QLatin1String("windows"), Qt::CaseInsensitive) == 0; - bool manualSourceSupported = true; - -#ifdef Q_OS_WIN - // QPrinter::supportedPaperSources() is only available on Windows, so just assuming manual is supported on others. - QPrinter printer; - printer.setOutputFormat(newFormat); - QList sources = printer.supportedPaperSources(); - if (!sources.contains(QPrinter::Manual)) { - manualSourceSupported = false; - qWarning() << "Manual paper source not supported by native printer, skipping related test."; - } -#endif // Q_OS_WIN - - // Querying PPK_CollateCopies is hardcoded to return false with Windows native print engine, - // so skip testing that in Windows. - if (!windowsPlatform) { - QPrinter printer; - printer.setOutputFormat(oldFormat); - bool status = printer.collateCopies(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.collateCopies(), status); - - printer.setCollateCopies(!status); - printer.setOutputFormat(newFormat); - QCOMPARE(printer.collateCopies(), !status); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.collateCopies(), !status); - } - { - QPrinter printer; - printer.setOutputFormat(oldFormat); - QPrinter::ColorMode status = printer.colorMode(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.colorMode(), status); - - printer.setColorMode(QPrinter::ColorMode(!status)); - printer.setOutputFormat(newFormat); - QCOMPARE(printer.colorMode(), QPrinter::ColorMode(!status)); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.colorMode(), QPrinter::ColorMode(!status)); - } - if (doX11Tests) { - QPrinter printer; - printer.setOutputFormat(oldFormat); - QString status = printer.creator(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.creator(), status); - - status = QString::fromLatin1("Mr. Test"); - printer.setCreator(status); - printer.setOutputFormat(newFormat); - QCOMPARE(printer.creator(), status); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.creator(), status); - } - { - QPrinter printer; - printer.setOutputFormat(oldFormat); - QString status = printer.docName(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.docName(), status); - - status = QString::fromLatin1("Test document"); - printer.setDocName(status); - printer.setOutputFormat(newFormat); - QCOMPARE(printer.docName(), status); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.docName(), status); - } - if (doX11Tests) { - QPrinter printer; - printer.setOutputFormat(oldFormat); - bool status = printer.doubleSidedPrinting(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.doubleSidedPrinting(), status); - - printer.setDoubleSidedPrinting(!status); - printer.setOutputFormat(newFormat); - QCOMPARE(printer.doubleSidedPrinting(), !status); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.doubleSidedPrinting(), !status); - } - if (doX11Tests) { - QPrinter printer; - printer.setOutputFormat(oldFormat); - bool status = printer.fontEmbeddingEnabled(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.fontEmbeddingEnabled(), status); - - printer.setFontEmbeddingEnabled(!status); - printer.setOutputFormat(newFormat); - QCOMPARE(printer.fontEmbeddingEnabled(), !status); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.fontEmbeddingEnabled(), !status); - } - { - QPrinter printer; - printer.setOutputFormat(oldFormat); - bool status = printer.fullPage(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.fullPage(), status); - - printer.setFullPage(!status); - printer.setOutputFormat(newFormat); - QCOMPARE(printer.fullPage(), !status); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.fullPage(), !status); - } - { - QPrinter printer; - printer.setOutputFormat(oldFormat); - QPrinter::Orientation status = printer.orientation(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.orientation(), status); - - printer.setOrientation(QPrinter::Orientation(!status)); - printer.setOutputFormat(newFormat); - QCOMPARE(printer.orientation(), QPrinter::Orientation(!status)); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.orientation(), QPrinter::Orientation(!status)); - } - { - QPrinter printer; - printer.setOutputFormat(oldFormat); - QString status = printer.outputFileName(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.outputFileName(), status); - - status = QString::fromLatin1("Test file"); - printer.setOutputFileName(status); - printer.setOutputFormat(newFormat); - QCOMPARE(printer.outputFileName(), status); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.outputFileName(), status); - } - if (doX11Tests) { - QPrinter printer; - printer.setOutputFormat(oldFormat); - QPrinter::PageOrder status = printer.pageOrder(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.pageOrder(), status); - - printer.setPageOrder(QPrinter::PageOrder(!status)); - printer.setOutputFormat(newFormat); - QCOMPARE(printer.pageOrder(), QPrinter::PageOrder(!status)); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.pageOrder(), QPrinter::PageOrder(!status)); - } - { - QPrinter printer; - printer.setOutputFormat(oldFormat); - QPrinter::PageSize status = printer.pageSize(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.pageSize(), status); - - printer.setPageSize(QPrinter::B5); - printer.setOutputFormat(newFormat); - QCOMPARE(printer.pageSize(), QPrinter::B5); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.pageSize(), QPrinter::B5); - } - if (manualSourceSupported) { - QPrinter printer; - printer.setOutputFormat(oldFormat); - QPrinter::PaperSource status = printer.paperSource(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.paperSource(), status); - - printer.setPaperSource(QPrinter::Manual); - printer.setOutputFormat(newFormat); - QCOMPARE(printer.paperSource(), QPrinter::Manual); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.paperSource(), QPrinter::Manual); - } - if (doX11Tests) { - QPrinter printer; - printer.setOutputFormat(oldFormat); - QString status = printer.printProgram(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.printProgram(), status); - - status = QString::fromLatin1("/usr/local/bin/lpr"); - printer.setPrintProgram(status); - printer.setOutputFormat(newFormat); - QCOMPARE(printer.printProgram(), status); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.printProgram(), status); - } - { - QPrinter printer; - printer.setOutputFormat(oldFormat); - QPrinter::PrintRange status = printer.printRange(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.printRange(), status); - - printer.setPrintRange(QPrinter::PrintRange(!status)); - printer.setOutputFormat(newFormat); - QCOMPARE(printer.printRange(), QPrinter::PrintRange(!status)); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.printRange(), QPrinter::PrintRange(!status)); - } - { - QPrinter printer; - printer.setOutputFormat(oldFormat); - QString status = printer.printerName(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.printerName(), status); - } - // QPrinter::printerSelectionOption is explicitly documented not to be available on Windows. -#ifndef Q_OS_WIN - { - QPrinter printer; - printer.setOutputFormat(oldFormat); - QString status = printer.printerSelectionOption(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.printerSelectionOption(), status); - - status = QString::fromLatin1("Optional option"); - printer.setPrinterSelectionOption(status); - printer.setOutputFormat(newFormat); - QCOMPARE(printer.printerSelectionOption(), status); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.printerSelectionOption(), status); - } -#endif // Q_OS_WIN - { - QPrinter printer; - printer.setOutputFormat(oldFormat); - int status = printer.resolution(); - printer.setOutputFormat(newFormat); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.resolution(), status); - - printer.setResolution(status-150); - printer.setOutputFormat(newFormat); - QCOMPARE(printer.resolution(), status-150); - printer.setOutputFormat(oldFormat); - QCOMPARE(printer.resolution(), status-150); - } -} - void tst_QPrinter::errorReporting() { QPrinter p; @@ -1057,13 +687,6 @@ void tst_QPrinter::printDialogCompleter() } #endif -void tst_QPrinter::testCopyCount() -{ - QPrinter p; - p.setCopyCount(15); - QCOMPARE(p.copyCount(), 15); -} - static void printPage(QPainter *painter) { painter->setPen(QPen(Qt::black, 4)); @@ -1255,6 +878,985 @@ void tst_QPrinter::customPaperNameSettingByName() } } +// Test QPrintEngine keys and their QPrinter setters/getters + +void tst_QPrinter::testMultipleKeys() +{ + // Tests multiple keys preservation, note are only ones that are consistent across all engines + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Check default values + QCOMPARE(native.fullPage(), false); + QCOMPARE(native.orientation(), QPrinter::Portrait); + QCOMPARE(native.copyCount(), 1); + QCOMPARE(native.collateCopies(), false); + QCOMPARE(native.printRange(), QPrinter::AllPages); + + // Change values + native.setFullPage(true); + native.setOrientation(QPrinter::Landscape); + native.setCopyCount(9); + native.setCollateCopies(true); + native.setPrintRange(QPrinter::CurrentPage); + + // Check changed values + QCOMPARE(native.fullPage(), true); + QCOMPARE(native.orientation(), QPrinter::Landscape); + QCOMPARE(native.copyCount(), 9); + QCOMPARE(native.collateCopies(), true); + QCOMPARE(native.printRange(), QPrinter::CurrentPage); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.fullPage(), true); + QCOMPARE(native.orientation(), QPrinter::Landscape); + QCOMPARE(native.copyCount(), 9); + QCOMPARE(native.collateCopies(), true); + QCOMPARE(native.printRange(), QPrinter::CurrentPage); + + // Change values + native.setFullPage(false); + native.setOrientation(QPrinter::Portrait); + native.setCopyCount(5); + native.setCollateCopies(false); + native.setPrintRange(QPrinter::PageRange); + + // Check changed values + QCOMPARE(native.fullPage(), false); + QCOMPARE(native.orientation(), QPrinter::Portrait); + QCOMPARE(native.copyCount(), 5); + QCOMPARE(native.collateCopies(), false); + QCOMPARE(native.printRange(), QPrinter::PageRange); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::collateCopies() +{ + // collateCopies() / setCollateCopies() / PPK_ColorMode + // PdfFormat: Supported, default false + // NativeFormat, Cups: Supported, default false + // NativeFormat, Win: Part Supported if valid DevMode, can set but always returns false + // NativeFormat, Mac: Unsupported, always false + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.collateCopies(), false); + pdf.setCollateCopies(true); + QCOMPARE(pdf.collateCopies(), true); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + QCOMPARE(native.collateCopies(), false); + + // Test set/get + bool expected = true; + native.setCollateCopies(expected); +#if defined Q_OS_MAC || defined Q_OS_WIN + expected = false; +#endif // Q_OS_MAC + QCOMPARE(native.collateCopies(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.collateCopies(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.collateCopies(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::colorMode() +{ + // colorMode() / setColorMode() / PPK_ColorMode + // PdfFormat: Supported, default QPrinter::Color + // NativeFormat, Cups: Supported, default QPrinter::Color + // NativeFormat, Win: Supported if valid DevMode, otherwise QPrinter::Color + // NativeFormat, Mac: Unsupported, always QPrinter::Color + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.colorMode(), QPrinter::Color); + pdf.setColorMode(QPrinter::GrayScale); + QCOMPARE(pdf.colorMode(), QPrinter::GrayScale); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + // TODO Printer specific, need QPrinterInfo::colorMode() + //QCOMPARE(native.colorMode(), QPrinter::Color); + + // Test set/get + QPrinter::ColorMode expected = QPrinter::GrayScale; + native.setColorMode(expected); +#ifdef Q_OS_MAC + expected = QPrinter::Color; +#endif // Q_OS_MAC + QCOMPARE(native.colorMode(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.colorMode(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.colorMode(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::copyCount() +{ + // copyCount() / setCopyCount() / PPK_CopyCount + // numCopies() / setNumCopies() / PPK_NumberOfCopies + // actualNumCopies() / supportsMultipleCopies() + // PdfFormat: Supported, multiple copies unsupported, default 1 + // NativeFormat, Cups: Supported, multiple copies supported, default 1 + // NativeFormat, Win: Supported, multiple copies supported, default 1 + // NativeFormat, Mac: Supported, multiple copies supported, default 1 + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.supportsMultipleCopies(), false); + QCOMPARE(pdf.copyCount(), 1); + QCOMPARE(pdf.numCopies(), 1); + QCOMPARE(pdf.actualNumCopies(), 1); + pdf.setCopyCount(9); + QCOMPARE(pdf.copyCount(), 9); + QCOMPARE(pdf.numCopies(), 9); + QCOMPARE(pdf.actualNumCopies(), 9); + pdf.setNumCopies(7); + QCOMPARE(pdf.copyCount(), 7); + QCOMPARE(pdf.numCopies(), 7); + QCOMPARE(pdf.actualNumCopies(), 7); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + QCOMPARE(native.supportsMultipleCopies(), true); + QCOMPARE(native.copyCount(), 1); + QCOMPARE(native.numCopies(), 1); + QCOMPARE(native.actualNumCopies(), 1); + + // Test set/get + native.setCopyCount(9); + QCOMPARE(native.copyCount(), 9); + QCOMPARE(native.numCopies(), 1); + QCOMPARE(native.actualNumCopies(), 9); + native.setNumCopies(7); + QCOMPARE(native.copyCount(), 7); + QCOMPARE(native.numCopies(), 1); + QCOMPARE(native.actualNumCopies(), 7); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.copyCount(), 7); + QCOMPARE(native.numCopies(), 7); + QCOMPARE(native.actualNumCopies(), 7); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.copyCount(), 7); + QCOMPARE(native.numCopies(), 1); + QCOMPARE(native.actualNumCopies(), 7); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::creator() +{ + // creator() / setCreator() / PPK_Creator + // PdfFormat: Supported, default QString() + // NativeFormat, Cups: Supported, default QString() + // NativeFormat, Win: Unsupported, always QString() + // NativeFormat, Mac: Unsupported, always QString() + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.creator(), QString()); + pdf.setCreator(QStringLiteral("Test Creator")); + QCOMPARE(pdf.creator(), QStringLiteral("Test Creator")); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + QCOMPARE(native.creator(), QString()); + + // Test set/get + QString expected = QStringLiteral("Test Creator"); + native.setCreator(expected); +#if defined Q_OS_MAC || defined Q_OS_WIN + expected.clear(); +#endif // Q_OS_MAC || Q_OS_WIN + QCOMPARE(native.creator(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.creator(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.creator(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::docName() +{ + // docName() / setDocName() / PPK_DocumentName + // PdfFormat: Supported, default QString() + // NativeFormat, Cups: Supported, default QString() + // NativeFormat, Win: Supported, default "document1" + // NativeFormat, Mac: Unsupported, always QString() + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.docName(), QString()); + pdf.setDocName(QStringLiteral("Test Name")); + QCOMPARE(pdf.docName(), QStringLiteral("Test Name")); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default +#ifdef Q_OS_WIN + QCOMPARE(native.docName(), QString("document1")); +#else + QCOMPARE(native.docName(), QString()); +#endif // Q_OS_WIN + + // Test set/get + QString expected = QStringLiteral("Test Name"); + native.setDocName(expected); +#ifdef Q_OS_MAC + expected.clear(); +#endif // Q_OS_MAC + QCOMPARE(native.docName(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.docName(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.docName(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::duplex() +{ + // duplex()) / setDuplex() / PPK_Duplex + // PdfFormat: Supported, default QPrinter::DuplexNone + // NativeFormat, Cups: Supported, default QPrinter::DuplexNone + // NativeFormat, Win: Unsupported, always QPrinter::DuplexNone + // NativeFormat, Mac: Unsupported, always QPrinter::DuplexNone + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.duplex(), QPrinter::DuplexNone); + pdf.setDuplex(QPrinter::DuplexAuto); + QCOMPARE(pdf.duplex(), QPrinter::DuplexAuto); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + // TODO Printer specific, need QPrinterInfo::duplex() + //QCOMPARE(native.duplex(), QPrinter::DuplexNone); + + // Test set/get + QPrinter::DuplexMode expected = QPrinter::DuplexAuto; + native.setDuplex(expected); +#if defined Q_OS_MAC || defined Q_OS_WIN + expected = QPrinter::DuplexNone; +#endif // Q_OS_MAC || Q_OS_WIN + QCOMPARE(native.duplex(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.duplex(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.duplex(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::doubleSidedPrinting() +{ + // PdfFormat: Supported, default false + // NativeFormat, Cups: Supported, default false + // NativeFormat, Win: Unsupported, always false + // NativeFormat, Mac: Unsupported, always false + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.doubleSidedPrinting(), false); + pdf.setDoubleSidedPrinting(true); + QCOMPARE(pdf.doubleSidedPrinting(), true); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + // TODO Printer specific, need QPrinterInfo::duplex() + //QCOMPARE(native.doubleSidedPrinting(), false); + + // Test set/get + bool expected = true; + native.setDoubleSidedPrinting(expected); +#if defined Q_OS_MAC || defined Q_OS_WIN + expected = false; +#endif // Q_OS_MAC || Q_OS_WIN + QCOMPARE(native.doubleSidedPrinting(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.doubleSidedPrinting(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.doubleSidedPrinting(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::fontEmbedding() +{ + // fontEmbeddingEnabled() / setFontEmbeddingEnabled() / PPK_FontEmbedding + // PdfFormat: Supported, default true + // NativeFormat, Cups: Supported, default true + // NativeFormat, Win: Unsupported, always false + // NativeFormat, Mac: Unsupported, always false + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.fontEmbeddingEnabled(), true); + pdf.setFontEmbeddingEnabled(false); + QCOMPARE(pdf.fontEmbeddingEnabled(), false); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default +#if defined Q_OS_MAC || defined Q_OS_WIN + QCOMPARE(native.fontEmbeddingEnabled(), false); +#else + QCOMPARE(native.fontEmbeddingEnabled(), true); +#endif // Q_OS_MAC || Q_OS_WIN + + // Test set/get + bool expected = true; + native.setFontEmbeddingEnabled(expected); +#if defined Q_OS_MAC || defined Q_OS_WIN + expected = false; +#endif // Q_OS_MAC || Q_OS_WIN + QCOMPARE(native.fontEmbeddingEnabled(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.fontEmbeddingEnabled(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.fontEmbeddingEnabled(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::fullPage() +{ + // fullPage() / setFullPage() / PPK_FullPage + // PdfFormat: Supported, default false + // NativeFormat, Cups: Supported, default false + // NativeFormat, Win: Supported, default false + // NativeFormat, Mac: Supported, default false + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.fullPage(), false); + pdf.setFullPage(true); + QCOMPARE(pdf.fullPage(), true); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + QCOMPARE(native.fullPage(), false); + + // Test set/get + bool expected = true; + native.setFullPage(expected); + QCOMPARE(native.fullPage(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.fullPage(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.fullPage(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::orientation() +{ + // orientation() / setOrientation() / PPK_Orientation + // PdfFormat: Supported, default QPrinter::Portrait + // NativeFormat, Cups: Supported, default QPrinter::Portrait + // NativeFormat, Win: Supported, default QPrinter::Portrait + // NativeFormat, Mac: Supported, default QPrinter::Portrait + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.orientation(), QPrinter::Portrait); + pdf.setOrientation(QPrinter::Landscape); + QCOMPARE(pdf.orientation(), QPrinter::Landscape); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + // TODO Printer specific, need QPrinterInfo::orientation() + //QCOMPARE(native.orientation(), QPrinter::Portrait); + + // Test set/get + QPrinter::Orientation expected = QPrinter::Landscape; + native.setOrientation(expected); + QCOMPARE(native.orientation(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.orientation(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.orientation(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::outputFileName() +{ + // outputFileName() / setOutputFileName() / PPK_OutputFileName + // PdfFormat: Supported, default QString() + // NativeFormat, Cups: Supported, default QString() + // NativeFormat, Win: Supported, default QString() + // NativeFormat, Mac: Supported, default QString() + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.outputFileName(), QString()); + pdf.setOutputFileName(QStringLiteral("Test File")); + QCOMPARE(pdf.outputFileName(), QString("Test File")); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + QCOMPARE(native.outputFileName(), QString()); + + // Test set/get + QString expected = QStringLiteral("Test File"); + native.setOutputFileName(expected); + QCOMPARE(native.outputFileName(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.outputFileName(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.outputFileName(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::pageOrder() +{ + // pageOrder() / setPageOrder() / PPK_PageOrder + // PdfFormat: Supported, default QPrinter::FirstPageFirst + // NativeFormat, Cups: Supported, default QPrinter::FirstPageFirst + // NativeFormat, Win: Unsupported, always QPrinter::FirstPageFirst + // NativeFormat, Mac: Unsupported, always QPrinter::FirstPageFirst + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.pageOrder(), QPrinter::FirstPageFirst); + pdf.setPageOrder(QPrinter::LastPageFirst); + QCOMPARE(pdf.pageOrder(), QPrinter::LastPageFirst); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + QCOMPARE(native.pageOrder(), QPrinter::FirstPageFirst); + + // Test set/get + QPrinter::PageOrder expected = QPrinter::LastPageFirst; + native.setPageOrder(expected); +#if defined Q_OS_MAC || defined Q_OS_WIN + expected = QPrinter::FirstPageFirst; +#endif // Q_OS_MAC || Q_OS_WIN + QCOMPARE(native.pageOrder(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.pageOrder(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.pageOrder(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::pageSize() +{ + // Note PPK_PaperSize == PPK_PageSize + // pageSize() / setPageSize() / PPK_PageSize + // PdfFormat: Supported, defaults to QPrinter::A4 + // NativeFormat, Cups: Supported, defaults to printer default + // NativeFormat, Win: Supported, defaults to printer default + // NativeFormat, Mac: Supported, must be supported size, defaults to printer default + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.pageSize(), QPrinter::A4); + pdf.setPageSize(QPrinter::A1); + QCOMPARE(pdf.pageSize(), QPrinter::A1); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + // TODO Printer specific, need QPrinterInfo::paperSize() + //QCOMPARE(native.pageSize(), QPrinter::A4); + + // Test set/get + QPrinter::PaperSize expected = QPrinter::A4; + QPrinterInfo info = QPrinterInfo::printerInfo(native.printerName()); + foreach (QPrinter::PaperSize supported, info.supportedPaperSizes()) { + if (supported != QPrinter::Custom && supported != native.paperSize()) { + expected = supported; + break; + } + } + native.setPageSize(expected); + QCOMPARE(native.pageSize(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.pageSize(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.pageSize(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::paperSize() +{ + // PPK_PaperSize == PPK_PageSize + // paperSize() / setPaperSize() / PPK_PaperSize + // pageSize() / setPageSize() / PPK_PageSize + // PdfFormat: Supported, defaults to QPrinter::A4 + // NativeFormat, Cups: Supported, defaults to printer default + // NativeFormat, Win: Supported, defaults to printer default + // NativeFormat, Mac: Supported, must be supported size, defaults to printer default + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.paperSize(), QPrinter::A4); + pdf.setPaperSize(QPrinter::A1); + QCOMPARE(pdf.paperSize(), QPrinter::A1); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + // TODO Printer specific, need QPrinterInfo::paperSize() + //QCOMPARE(native.paperSize(), QPrinter::A4); + + // Test set/get + QPrinter::PaperSize expected = QPrinter::A4; + QPrinterInfo info = QPrinterInfo::printerInfo(native.printerName()); + foreach (QPrinter::PaperSize supported, info.supportedPaperSizes()) { + if (supported != QPrinter::Custom && supported != native.paperSize()) { + expected = supported; + break; + } + } + native.setPaperSize(expected); + QCOMPARE(native.paperSize(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.paperSize(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.paperSize(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::paperSource() +{ + // paperSource() / setPaperSource() / PPK_PaperSource + // PdfFormat: Supported, defaults to QPrinter::Auto + // NativeFormat, Cups: Supported, defaults to QPrinter::Auto + // NativeFormat, Win: Supported if valid DevMode and in supportedPaperSources(), otherwise QPrinter::Auto + // NativeFormat, Mac: Unsupported, always QPrinter::Auto + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.paperSource(), QPrinter::Auto); + pdf.setPaperSource(QPrinter::Lower); + QCOMPARE(pdf.paperSource(), QPrinter::Lower); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + // TODO Printer specific, need QPrinterInfo::paperSource() + //QCOMPARE(native.paperSource(), QPrinter::Auto); + + // Test set/get + QPrinter::PaperSource expected = QPrinter::Manual; +#ifdef Q_OS_WIN + expected = QPrinter::Auto; + foreach (QPrinter::PaperSource supported, native.supportedPaperSources()) { + if (supported != QPrinter::Auto) { + expected = supported; + break; + } + } +#endif // Q_OS_WIN + native.setPaperSource(expected); +#ifdef Q_OS_MAC + expected = QPrinter::Auto; +#endif // Q_OS_MAC + QCOMPARE(native.paperSource(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.paperSource(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.paperSource(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::printProgram() +{ + // printProgram() / setPrintProgram() / PPK_PrintProgram + // PdfFormat: Supported, default QString() + // NativeFormat, Cups: Supported, default QString() + // NativeFormat, Win: Unsupported, always QString() + // NativeFormat, Mac: Unsupported, always QString() + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.printProgram(), QString()); + pdf.setPrintProgram(QStringLiteral("/usr/bin/lpr")); + QCOMPARE(pdf.printProgram(), QStringLiteral("/usr/bin/lpr")); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + QCOMPARE(native.printProgram(), QString()); + + // Test set/get + QString expected = QStringLiteral("/usr/bin/lpr"); + native.setPrintProgram(expected); +#if defined Q_OS_MAC || defined Q_OS_WIN + expected.clear(); +#endif // Q_OS_MAC || Q_OS_WIN + QCOMPARE(native.printProgram(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.printProgram(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.printProgram(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::printRange() +{ + // printRange() / setPrintRange() / PPK_PrintRange + // PdfFormat: Supported, default QPrinter::AllPages + // NativeFormat, Cups: Supported, default QPrinter::AllPages + // NativeFormat, Win: Supported, default QPrinter::AllPages + // NativeFormat, Mac: Supported, default QPrinter::AllPages + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.printRange(), QPrinter::AllPages); + pdf.setPrintRange(QPrinter::CurrentPage); + QCOMPARE(pdf.printRange(), QPrinter::CurrentPage); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + QCOMPARE(native.printRange(), QPrinter::AllPages); + + // Test set/get + QPrinter::PrintRange expected = QPrinter::PageRange; + native.setPrintRange(expected); + QCOMPARE(native.printRange(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.printRange(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.printRange(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::printerName() +{ + // printerName() / setPrinterName() / PPK_PrinterName + // PdfFormat: Supported, default QString + // NativeFormat, Cups: Supported, default printer + // NativeFormat, Win: Supported, default printer + // NativeFormat, Mac: Supported, default printer + + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.printerName(), QString()); + if (QPrinterInfo::availablePrinters().size() == 0) { + pdf.setPrinterName(QStringLiteral("Test Printer")); + QCOMPARE(pdf.printerName(), QString()); + QCOMPARE(pdf.outputFormat(), QPrinter::PdfFormat); + } else { + pdf.setPrinterName(QPrinterInfo::defaultPrinter().printerName()); + QCOMPARE(pdf.printerName(), QPrinterInfo::defaultPrinter().printerName()); + QCOMPARE(pdf.outputFormat(), QPrinter::NativeFormat); + } + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + QCOMPARE(native.printerName(), QPrinterInfo::defaultPrinter().printerName()); + + // Test set/get + QString expected = QPrinterInfo::defaultPrinter().printerName(); + foreach (const QPrinterInfo &available, QPrinterInfo::availablePrinters()) { + if (available.printerName() != expected) { + expected = available.printerName(); + break; + } + } + native.setPrinterName(expected); + QCOMPARE(native.printerName(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.printerName(), QString()); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.printerName(), QPrinterInfo::defaultPrinter().printerName()); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::printerSelectionOption() +{ + // printerSelectionOption() / setPrinterSelectionOption() / PPK_SelectionOption + // PdfFormat: Supported + // NativeFormat, Cups: Supported + // NativeFormat, Win: Unsupported, ifdef'd out TODO remove ifdef, always QString() + // NativeFormat, Mac: Unsupported, always QString() + +#ifndef Q_OS_WIN + QPrinter pdf; + pdf.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdf.printerSelectionOption(), QString()); + pdf.setPrinterSelectionOption(QStringLiteral("Optional option")); + QCOMPARE(pdf.printerSelectionOption(), QString("Optional option")); + + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + QCOMPARE(native.printerSelectionOption(), QString()); + + // Test set/get + QString expected = QStringLiteral("Optional option"); + native.setPrinterSelectionOption(expected); +#ifdef Q_OS_MAC + expected.clear(); +#endif // Q_OS_MAC + QCOMPARE(native.printerSelectionOption(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.printerSelectionOption(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.printerSelectionOption(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +#endif // Q_OS_WIN +} + +void tst_QPrinter::resolution() +{ + // resolution() / setResolution() / PPK_Resolution + // PdfFormat: Supported, can be any number, but only 72 returned by supportedResolutions() + // NativeFormat, Cups: Supported, can be any number, but only 72 returned by supportedResolutions() + // NativeFormat, Win: Supported, can be any number, but supportedResolutions() returns valid list + // NativeFormat, Mac: Supported, but can only be value returned by supportedResolutions() + + QPrinter pdfScreen(QPrinter::ScreenResolution); + pdfScreen.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdfScreen.resolution(), 96); + pdfScreen.setResolution(333); + QCOMPARE(pdfScreen.resolution(), 333); + + QPrinter pdfPrinter(QPrinter::PrinterResolution); + pdfPrinter.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdfPrinter.resolution(), 72); + pdfPrinter.setResolution(333); + QCOMPARE(pdfPrinter.resolution(), 333); + + QPrinter pdfHigh(QPrinter::HighResolution); + pdfHigh.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(pdfHigh.resolution(), 1200); + pdfHigh.setResolution(333); + QCOMPARE(pdfHigh.resolution(), 333); + + QPrinter native(QPrinter::HighResolution); + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test default + // TODO Printer specific, need QPrinterInfo::resolution() + //QCOMPARE(native.resolution(), 300); + + // Test set/get + int expected = 333; +#ifdef Q_OS_MAC + expected = native.resolution(); + foreach (int supported, native.supportedResolutions()) { + if (supported != expected) { + expected = supported; + break; + } + } +#endif // Q_OS_MAC + native.setResolution(expected); + QCOMPARE(native.resolution(), expected); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.resolution(), expected); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.resolution(), expected); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +} + +void tst_QPrinter::supportedPaperSources() +{ + // supportedPaperSources() / PPK_PaperSources + // PdfFormat: ifdef'd out TODO remove ifdef + // NativeFormat, Cups: ifdef'd out TODO remove ifdef + // NativeFormat, Win: Supported, defaults to printer default + // NativeFormat, Mac: ifdef'd out TODO remove ifdef + +#ifdef Q_OS_WIN + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + native.supportedPaperSources(); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +#endif // Q_OS_WIN +} + +void tst_QPrinter::supportedResolutions() +{ + // supportedResolutions() / PPK_SupportedResolutions + // PdfFormat: Supported, only returns 72 + // NativeFormat, Cups: Supported, only returns 72 + // NativeFormat, Win: Supported, defaults to printer list + // NativeFormat, Mac: Supported, defaults to printer list + + QList 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: ifdef'd out TODO remove ifdef + // NativeFormat, Cups: ifdef'd out TODO remove ifdef + // NativeFormat, Win: Supported, defaults to printer default + // NativeFormat, Mac: ifdef'd out TODO remove ifdef + +#ifdef Q_OS_WIN + QPrinter native; + if (native.outputFormat() == QPrinter::NativeFormat) { + // Test set/get + native.setPaperSize(QPrinter::A4); + QCOMPARE(native.pageSize(), QPrinter::A4); + QCOMPARE(native.winPageSize(), DMPAPER_A4); + + native.setPaperSize(QPrinter::Letter); + QCOMPARE(native.pageSize(), QPrinter::Letter); + QCOMPARE(native.winPageSize(), DMPAPER_LETTER); + + native.setWinPageSize(DMPAPER_A4); + QCOMPARE(native.pageSize(), QPrinter::A4); + QCOMPARE(native.winPageSize(), DMPAPER_A4); + + native.setWinPageSize(DMPAPER_LETTER); + QCOMPARE(native.pageSize(), QPrinter::Letter); + QCOMPARE(native.winPageSize(), DMPAPER_LETTER); + + // Test value preservation + native.setOutputFormat(QPrinter::PdfFormat); + QCOMPARE(native.pageSize(), QPrinter::Letter); + QEXPECT_FAIL("", "Win paper size doesn't persist over format change", Continue); + QCOMPARE(native.winPageSize(), DMPAPER_LETTER); + native.setOutputFormat(QPrinter::NativeFormat); + QCOMPARE(native.pageSize(), QPrinter::Letter); + QCOMPARE(native.winPageSize(), DMPAPER_LETTER); + } else { + QSKIP("No printers installed, cannot test NativeFormat, please install printers to test"); + } +#endif // Q_OS_WIN +} + +// 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); +} + #endif // QT_NO_PRINTER QTEST_MAIN(tst_QPrinter)