QPrinter - Add page metric tests

Add more page metrics tests to expose the many bugs and inconsistencies
in the page layout handling.  No platform or pdf backend passes all
these tests so they are skipped for now, but following commits will use
the tests to ensure the re-write of the page layout handling fixes the
many problems in a consistent way for all backends.

Change-Id: Ic5b16aa08dfe2bd9a9191662fd2bbe6b913e15c9
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
Reviewed-by: Andy Shaw <andy.shaw@digia.com>
This commit is contained in:
John Layt 2014-01-26 14:21:59 +01:00 committed by The Qt Project
parent cf56e066ad
commit ad0163d088
2 changed files with 230 additions and 66 deletions

View File

@ -1024,6 +1024,9 @@ void QPrinter::setPageSize(PageSize newPageSize)
Sets the paper size based on \a paperSize in \a unit.
Note that the paper size is defined in a portrait layout, regardless of
what the current printer orientation is set to.
\sa paperSize()
*/
@ -1036,8 +1039,11 @@ void QPrinter::setPaperSize(const QSizeF &paperSize, QPrinter::Unit unit)
}
/*!
\reimp
*/
\reimp
Note that the page size is defined in a portrait layout, regardless of
what the current printer orientation is set to.
*/
void QPrinter::setPageSizeMM(const QSizeF &size)
{
Q_D(QPrinter);
@ -1054,6 +1060,8 @@ void QPrinter::setPageSizeMM(const QSizeF &size)
Returns the paper size in \a unit.
Note that the returned size reflects the current paper orientation.
\sa setPaperSize()
*/

View File

@ -103,7 +103,6 @@ private slots:
void testPageMargins_data();
void testPageMargins();
void outputFormatFromSuffix();
void setGetPaperSize();
void errorReporting();
void testCustomPageSizes();
void customPaperSizeAndMargins_data();
@ -116,8 +115,6 @@ private slots:
void testCurrentPage();
void taskQTBUG4497_reusePrinterOnDifferentFiles();
void testPdfTitle();
void testPageMetrics_data();
void testPageMetrics();
// Test QPrintEngine keys and their QPrinter setters/getters
void testMultipleKeys();
@ -148,6 +145,9 @@ private slots:
// Test QPrinter setters/getters for non-QPrintEngine options
void outputFormat();
void fromToPage();
void testPageMetrics_data();
void testPageMetrics();
#endif
};
@ -477,20 +477,6 @@ void tst_QPrinter::outputFormatFromSuffix()
QVERIFY(p.outputFormat() == QPrinter::NativeFormat);
}
void tst_QPrinter::setGetPaperSize()
{
QPrinter p;
p.setOutputFormat(QPrinter::PdfFormat);
QSizeF size(500, 10);
p.setPaperSize(size, QPrinter::Millimeter);
QCOMPARE(p.paperSize(QPrinter::Millimeter), size);
QCOMPARE(p.pageSizeMM(), size);
QSizeF ptSize = p.paperSize(QPrinter::Point);
//qDebug() << ptSize;
QVERIFY(qAbs(ptSize.width() - size.width() * (72/25.4)) < 1E-4);
QVERIFY(qAbs(ptSize.height() - size.height() * (72/25.4)) < 1E-4);
}
void tst_QPrinter::testPageMargins_data()
{
QTest::addColumn<qreal>("left");
@ -499,11 +485,12 @@ void tst_QPrinter::testPageMargins_data()
QTest::addColumn<qreal>("bottom");
QTest::addColumn<int>("unit");
QTest::newRow("data0") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast<int>(QPrinter::Millimeter);
QTest::newRow("data1") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast<int>(QPrinter::Point);
// 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<int>(QPrinter::Millimeter);
QTest::newRow("data1") << qreal(55.5) << qreal(56.5) << qreal(57.5) << qreal(58.5) << static_cast<int>(QPrinter::Point);
QTest::newRow("data2") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast<int>(QPrinter::Inch);
QTest::newRow("data3") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast<int>(QPrinter::Pica);
QTest::newRow("data4") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast<int>(QPrinter::Didot);
QTest::newRow("data4") << qreal(55.5) << qreal(56.5) << qreal(57.5) << qreal(58.5) << static_cast<int>(QPrinter::Didot);
QTest::newRow("data5") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast<int>(QPrinter::Cicero);
}
@ -622,10 +609,11 @@ void tst_QPrinter::customPaperSizeAndMargins_data()
QTest::addColumn<qreal>("right");
QTest::addColumn<qreal>("bottom");
QTest::newRow("beforeNoPDF") << false << true << qreal(2) << qreal(2) << qreal(2) << qreal(2);
QTest::newRow("beforePDF") << true << true << qreal(2) << qreal(2) << qreal(2) << qreal(2);
QTest::newRow("afterNoPDF") << false << false << qreal(2) << qreal(2) << qreal(2) << qreal(2);
QTest::newRow("afterAfterPDF") << true << false << qreal(2) << qreal(2) << qreal(2) << qreal(2);
// 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()
@ -642,7 +630,9 @@ void tst_QPrinter::customPaperSizeAndMargins()
qreal getRight = 0;
qreal getTop = 0;
qreal getBottom = 0;
QSizeF customSize(8.5, 11.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)
@ -657,10 +647,6 @@ void tst_QPrinter::customPaperSizeAndMargins()
QVERIFY(fabs(left - getRight) < tolerance);
QVERIFY(fabs(left - getBottom) < tolerance);
} else {
QVERIFY(getLeft == 0);
QVERIFY(getTop == 0);
QVERIFY(getRight == 0);
QVERIFY(getBottom == 0);
p.setPageMargins(left, top, right, bottom, QPrinter::Millimeter);
p.getPageMargins(&getLeft, &getTop, &getRight, &getBottom, QPrinter::Millimeter);
QVERIFY(fabs(left - getLeft) < tolerance);
@ -781,41 +767,6 @@ void tst_QPrinter::testPdfTitle()
QVERIFY(file.readAll().contains(QByteArray(expected, 26)));
}
void tst_QPrinter::testPageMetrics_data()
{
QTest::addColumn<int>("pageSize");
QTest::addColumn<int>("widthMM");
QTest::addColumn<int>("heightMM");
QTest::addColumn<float>("widthMMf");
QTest::addColumn<float>("heightMMf");
QTest::newRow("A4") << int(QPrinter::A4) << 210 << 297 << 210.0f << 297.0f;
QTest::newRow("A5") << int(QPrinter::A5) << 148 << 210 << 148.0f << 210.0f;
QTest::newRow("Letter") << int(QPrinter::Letter) << 216 << 279 << 215.9f << 279.4f;
}
void tst_QPrinter::testPageMetrics()
{
QFETCH(int, pageSize);
QFETCH(int, widthMM);
QFETCH(int, heightMM);
QFETCH(float, widthMMf);
QFETCH(float, heightMMf);
QPrinter printer(QPrinter::HighResolution);
printer.setFullPage(true);
printer.setPageSize(QPrinter::PageSize(pageSize));
if (printer.pageSize() != pageSize) {
QSKIP("Current page size is not supported on this printer");
return;
}
QCOMPARE(printer.widthMM(), int(widthMM));
QCOMPARE(printer.heightMM(), int(heightMM));
QCOMPARE(printer.pageSizeMM(), QSizeF(widthMMf, heightMMf));
}
void tst_QPrinter::customPaperNameSettingBySize()
{
#ifndef Q_OS_WIN
@ -1861,6 +1812,211 @@ void tst_QPrinter::fromToPage()
QCOMPARE(printer.toPage(), 7);
}
void tst_QPrinter::testPageMetrics_data()
{
QTest::addColumn<int>("outputFormat");
QTest::addColumn<int>("pageSize");
QTest::addColumn<qreal>("widthMMf");
QTest::addColumn<qreal>("heightMMf");
QTest::addColumn<bool>("setMargins");
QTest::addColumn<qreal>("leftMMf");
QTest::addColumn<qreal>("rightMMf");
QTest::addColumn<qreal>("topMMf");
QTest::addColumn<qreal>("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()
{
QSKIP("Skip tests until new backends pass");
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
QEXPECT_FAIL("", "Rect calculation lacks required precision", Continue);
QCOMPARE(printer.paperRect(QPrinter::Millimeter), QRectF(0, 0, widthMMf, heightMMf));
QCOMPARE(qRound(printer.paperRect(QPrinter::Millimeter).width()), qRound(widthMMf));
QCOMPARE(qRound(printer.paperRect(QPrinter::Millimeter).height()), qRound(heightMMf));
// QPrinter::pageRect() always returns set orientation
QEXPECT_FAIL("", "Rect calculation lacks required precision", Continue);
QCOMPARE(printer.pageRect(QPrinter::Millimeter), QRectF(leftMMf, topMMf, widthMMf - leftMMf - rightMMf, heightMMf - topMMf - bottomMMf));
QCOMPARE(qRound(printer.pageRect(QPrinter::Millimeter).width()), qRound(widthMMf - leftMMf - rightMMf));
QCOMPARE(qRound(printer.pageRect(QPrinter::Millimeter).height()), qRound(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
QEXPECT_FAIL("", "Rect calculation lacks required precision", Continue);
QCOMPARE(printer.paperRect(QPrinter::Millimeter), QRectF(0, 0, heightMMf, widthMMf));
QCOMPARE(qRound(printer.paperRect(QPrinter::Millimeter).width()), qRound(heightMMf));
QCOMPARE(qRound(printer.paperRect(QPrinter::Millimeter).height()), qRound(widthMMf));
// QPrinter::pageRect() always returns set orientation
QEXPECT_FAIL("", "Rect calculation lacks required precision", Continue);
QCOMPARE(printer.pageRect(QPrinter::Millimeter), QRectF(leftMMf, topMMf, heightMMf - leftMMf - rightMMf, widthMMf - topMMf - bottomMMf));
QCOMPARE(qRound(printer.pageRect(QPrinter::Millimeter).width()), qRound(heightMMf - leftMMf - rightMMf));
QCOMPARE(qRound(printer.pageRect(QPrinter::Millimeter).height()), qRound(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
QEXPECT_FAIL("", "Rect calculation lacks required precision", Continue);
QCOMPARE(printer.paperRect(QPrinter::Millimeter), QRectF(0, 0, heightMMf, widthMMf));
QCOMPARE(qRound(printer.paperRect(QPrinter::Millimeter).width()), qRound(heightMMf));
QCOMPARE(qRound(printer.paperRect(QPrinter::Millimeter).height()), qRound(widthMMf));
// QPrinter::pageRect() always returns set orientation
QEXPECT_FAIL("", "Rect calculation lacks required precision", Continue);
QCOMPARE(printer.pageRect(QPrinter::Millimeter), QRectF(leftMMf, topMMf, heightMMf - leftMMf - rightMMf, widthMMf - topMMf - bottomMMf));
QCOMPARE(qRound(printer.pageRect(QPrinter::Millimeter).width()), qRound(heightMMf - leftMMf - rightMMf));
QCOMPARE(qRound(printer.pageRect(QPrinter::Millimeter).height()), qRound(widthMMf - topMMf - bottomMMf));
}
#endif // QT_NO_PRINTER
QTEST_MAIN(tst_QPrinter)