diff options
Diffstat (limited to 'tests/auto/printsupport/kernel/qprinter')
-rw-r--r-- | tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp | 79 |
1 files changed, 77 insertions, 2 deletions
diff --git a/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp b/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp index 7251cca528..644cd33e5c 100644 --- a/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp +++ b/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp @@ -113,6 +113,8 @@ private slots: void testCustomPageSizes(); void customPaperSizeAndMargins_data(); void customPaperSizeAndMargins(); + void customPaperNameSettingBySize(); + void customPaperNameSettingByName(); #if !defined(QT_NO_COMPLETER) && !defined(QT_NO_FILEDIALOG) void printDialogCompleter(); #endif @@ -967,6 +969,13 @@ void tst_QPrinter::errorReporting() painter.end(); } +static QByteArray msgSizeMismatch(const QSizeF &actual, const QSizeF &expected) +{ + QString result; + QDebug(&result) << "Paper size mismatch" << actual << "!=" << expected; + return result.toLocal8Bit(); +} + void tst_QPrinter::testCustomPageSizes() { QPrinter p; @@ -975,12 +984,16 @@ void tst_QPrinter::testCustomPageSizes() p.setPaperSize(customSize, QPrinter::Inch); QSizeF paperSize = p.paperSize(QPrinter::Inch); - QCOMPARE(paperSize, customSize); + // Due to the different calculations, the sizes may be off by a fraction so we have to check it manually + // instead of relying on QSizeF comparison + QVERIFY2(sqrt(pow(paperSize.width() - customSize.width(), 2.0) + pow(paperSize.height() - customSize.height(), 2.0)) < 0.01, + msgSizeMismatch(paperSize, customSize)); QPrinter p2(QPrinter::HighResolution); p2.setPaperSize(customSize, QPrinter::Inch); paperSize = p.paperSize(QPrinter::Inch); - QCOMPARE(paperSize, customSize); + QVERIFY2(sqrt(pow(paperSize.width() - customSize.width(), 2.0) + pow(paperSize.height() - customSize.height(), 2.0)) < 0.01, + msgSizeMismatch(paperSize, customSize)); } void tst_QPrinter::customPaperSizeAndMargins_data() @@ -1193,6 +1206,68 @@ void tst_QPrinter::testPageMetrics() QCOMPARE(printer.pageSizeMM(), QSizeF(widthMMf, heightMMf)); } +void tst_QPrinter::customPaperNameSettingBySize() +{ +#ifndef Q_OS_WIN + QSKIP("Currently this triggers a problem on non Windows platforms, this will be fixed separately - QTBUG-34521"); +#endif + QPrinter printer(QPrinter::HighResolution); + QPrinterInfo info(printer); + QList<QPair<QString, QSizeF> > sizes = info.supportedSizesWithNames(); + if (sizes.size() == 0) + QSKIP("No printers installed on this machine"); + for (int i=0; i<sizes.size(); i++) { + printer.setPaperSize(sizes.at(i).second, QPrinter::Millimeter); + QCOMPARE(sizes.at(i).second, printer.paperSize(QPrinter::Millimeter)); + // Some printers have the same size under different names which can cause a problem for the test + // So we iterate up to the current position to check + QSizeF paperSize = sizes.at(i).second; + QString paperName = printer.paperName(); + bool paperNameFound = (sizes.at(i).first == paperName); + if (!paperNameFound) { + for (int j=0; j<i; j++) { + if (sizes.at(j).second == paperSize && sizes.at(j).first == paperName) { + paperNameFound = true; + break; + } + } + } + // Fail with the original values + if (!paperNameFound) + QCOMPARE(sizes.at(i).first, printer.paperName()); + } + + // Check setting a custom size after setting a standard one works + QSizeF customSize(200, 200); + printer.setPaperSize(customSize, QPrinter::Millimeter); + QCOMPARE(printer.paperSize(QPrinter::Millimeter), customSize); + QCOMPARE(printer.paperSize(), QPrinter::Custom); + + // Finally check setting a standard size after a custom one works + printer.setPaperSize(sizes.at(0).second, QPrinter::Millimeter); + QCOMPARE(printer.paperName(), sizes.at(0).first); + QCOMPARE(printer.paperSize(QPrinter::Millimeter), sizes.at(0).second); +} + +void tst_QPrinter::customPaperNameSettingByName() +{ +#ifndef Q_OS_WIN + QSKIP("Currently this triggers a problem on non Windows platforms, this will be fixed separately - QTBUG-34521"); +#endif + QPrinter printer(QPrinter::HighResolution); + QPrinterInfo info(printer); + QList<QPair<QString, QSizeF> > sizes = info.supportedSizesWithNames(); + if (sizes.size() == 0) + QSKIP("No printers installed on this machine"); + for (int i=0; i<sizes.size(); i++) { + printer.setPaperName(sizes.at(i).first); + QCOMPARE(sizes.at(i).first, printer.paperName()); + QSizeF paperSize = printer.paperSize(QPrinter::Millimeter); + QVERIFY2(sqrt(pow(sizes.at(i).second.width() - paperSize.width(), 2.0) + pow(sizes.at(i).second.height() - paperSize.height(), 2.0)) < 0.01, + msgSizeMismatch(sizes.at(i).second, paperSize)); + } +} + #endif // QT_NO_PRINTER QTEST_MAIN(tst_QPrinter) |