1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
42
#include <QtTest/QtTest>
45
#include <qpagesetupdialog.h>
47
#include <qprintdialog.h>
48
#include <qprinterinfo.h>
51
#include <qprintengine.h>
60
QT_FORWARD_DECLARE_CLASS(QPrinter)
62
// Helper class to make sure temp files are cleaned up after test complete
66
TempFileCleanup(const QString &file)
73
QFile::remove(m_file);
79
class tst_QPrinter : public QObject
86
void cleanupTestCase();
90
// Add your testfunctions and testdata create functions here
93
void testNonExistentPrinter();
95
void testPageRectAndPaperRect();
96
void testPageRectAndPaperRect_data();
97
void testSetOptions();
98
void testMargins_data();
100
void testPageSetupDialog();
101
void testMulitpleSets_data();
102
void testMulitpleSets();
103
void testPageMargins_data();
104
void testPageMargins();
105
void changingOutputFormat();
106
void outputFormatFromSuffix();
107
void setGetPaperSize();
108
void valuePreservation();
109
void errorReporting();
110
void testCustomPageSizes();
111
void customPaperSizeAndMargins_data();
112
void customPaperSizeAndMargins();
113
#if !defined(QT_NO_COMPLETER) && !defined(QT_NO_FILEDIALOG)
114
void printDialogCompleter();
117
void testCopyCount();
118
void testCurrentPage();
120
void taskQTBUG4497_reusePrinterOnDifferentFiles();
126
void tst_QPrinter::initTestCase()
128
QSKIP("This test requires printing support");
131
void tst_QPrinter::cleanupTestCase()
133
QSKIP("This test requires printing support");
136
// Testing get/set functions
137
void tst_QPrinter::getSetCheck()
140
// OutputFormat QPrinter::outputFormat()
141
// void QPrinter::setOutputFormat(OutputFormat)
142
obj1.setOutputFormat(QPrinter::OutputFormat(QPrinter::PdfFormat));
143
QCOMPARE(QPrinter::OutputFormat(QPrinter::PdfFormat), obj1.outputFormat());
145
// bool QPrinter::collateCopies()
146
// void QPrinter::setCollateCopies(bool)
147
obj1.setCollateCopies(false);
148
QCOMPARE(false, obj1.collateCopies());
149
obj1.setCollateCopies(true);
150
QCOMPARE(true, obj1.collateCopies());
152
obj1.setColorMode(QPrinter::GrayScale);
153
QCOMPARE(obj1.colorMode(), QPrinter::GrayScale);
154
obj1.setColorMode(QPrinter::Color);
155
QCOMPARE(obj1.colorMode(), QPrinter::Color);
157
obj1.setCreator(QString::fromLatin1("RandomQtUser"));
158
QCOMPARE(obj1.creator(), QString::fromLatin1("RandomQtUser"));
160
obj1.setDocName(QString::fromLatin1("RandomQtDocument"));
161
QCOMPARE(obj1.docName(), QString::fromLatin1("RandomQtDocument"));
163
obj1.setDoubleSidedPrinting(true);
164
QCOMPARE(obj1.doubleSidedPrinting(), true);
165
obj1.setDoubleSidedPrinting(false);
166
QCOMPARE(obj1.doubleSidedPrinting(), false);
168
obj1.setFromTo(1, 4);
169
QCOMPARE(obj1.fromPage(), 1);
170
QCOMPARE(obj1.toPage(), 4);
172
obj1.setFullPage(true);
173
QCOMPARE(obj1.fullPage(), true);
174
obj1.setFullPage(false);
175
QCOMPARE(obj1.fullPage(), false);
177
obj1.setOrientation(QPrinter::Landscape);
178
QCOMPARE(obj1.orientation(), QPrinter::Landscape);
179
obj1.setOrientation(QPrinter::Portrait);
180
QCOMPARE(obj1.orientation(), QPrinter::Portrait);
182
obj1.setOutputFileName(QString::fromLatin1("RandomQtName"));
183
QCOMPARE(obj1.outputFileName(), QString::fromLatin1("RandomQtName"));
185
obj1.setPageOrder(QPrinter::FirstPageFirst);
186
QCOMPARE(obj1.pageOrder(), QPrinter::FirstPageFirst);
187
obj1.setPageOrder(QPrinter::LastPageFirst);
188
QCOMPARE(obj1.pageOrder(), QPrinter::LastPageFirst);
190
obj1.setPaperSource(QPrinter::Cassette);
191
QCOMPARE(obj1.paperSource(), QPrinter::Cassette);
192
obj1.setPaperSource(QPrinter::Middle);
193
QCOMPARE(obj1.paperSource(), QPrinter::Middle);
196
obj1.setPrintProgram(QString::fromLatin1("/bin/true"));
197
QCOMPARE(obj1.printProgram(), QString::fromLatin1("/bin/true"));
199
obj1.setPrinterSelectionOption(QString::fromLatin1("--option"));
200
QCOMPARE(obj1.printerSelectionOption(), QString::fromLatin1("--option"));
203
obj1.setPrinterName(QString::fromLatin1("myPrinter"));
204
QCOMPARE(obj1.printerName(), QString::fromLatin1("myPrinter"));
206
// bool QPrinter::fontEmbeddingEnabled()
207
// void QPrinter::setFontEmbeddingEnabled(bool)
208
obj1.setFontEmbeddingEnabled(false);
209
QCOMPARE(false, obj1.fontEmbeddingEnabled());
210
obj1.setFontEmbeddingEnabled(true);
211
QCOMPARE(true, obj1.fontEmbeddingEnabled());
213
// PageSize QPrinter::pageSize()
214
// void QPrinter::setPageSize(PageSize)
215
obj1.setPageSize(QPrinter::PageSize(QPrinter::A4));
216
QCOMPARE(QPrinter::PageSize(QPrinter::A4), obj1.pageSize());
217
obj1.setPageSize(QPrinter::PageSize(QPrinter::Letter));
218
QCOMPARE(QPrinter::PageSize(QPrinter::Letter), obj1.pageSize());
219
obj1.setPageSize(QPrinter::PageSize(QPrinter::Legal));
220
QCOMPARE(QPrinter::PageSize(QPrinter::Legal), obj1.pageSize());
222
// PrintRange QPrinter::printRange()
223
// void QPrinter::setPrintRange(PrintRange)
224
obj1.setPrintRange(QPrinter::PrintRange(QPrinter::AllPages));
225
QCOMPARE(QPrinter::PrintRange(QPrinter::AllPages), obj1.printRange());
226
obj1.setPrintRange(QPrinter::PrintRange(QPrinter::Selection));
227
QCOMPARE(QPrinter::PrintRange(QPrinter::Selection), obj1.printRange());
228
obj1.setPrintRange(QPrinter::PrintRange(QPrinter::PageRange));
229
QCOMPARE(QPrinter::PrintRange(QPrinter::PageRange), obj1.printRange());
232
#define MYCOMPARE(a, b) QCOMPARE(QVariant((int)a), QVariant((int)b))
234
void tst_QPrinter::testPageSetupDialog()
236
// Make sure this doesn't crash at least
239
QPageSetupDialog dialog(&printer);
244
// QPrinter::winPageSize(): Windows only.
245
void tst_QPrinter::testPageSize()
249
prn.setPageSize(QPrinter::Letter);
250
MYCOMPARE(prn.pageSize(), QPrinter::Letter);
251
MYCOMPARE(prn.winPageSize(), DMPAPER_LETTER);
253
prn.setPageSize(QPrinter::A4);
254
MYCOMPARE(prn.pageSize(), QPrinter::A4);
255
MYCOMPARE(prn.winPageSize(), DMPAPER_A4);
257
prn.setWinPageSize(DMPAPER_LETTER);
258
MYCOMPARE(prn.winPageSize(), DMPAPER_LETTER);
259
MYCOMPARE(prn.pageSize(), QPrinter::Letter);
261
prn.setWinPageSize(DMPAPER_A4);
262
MYCOMPARE(prn.winPageSize(), DMPAPER_A4);
263
MYCOMPARE(prn.pageSize(), QPrinter::A4);
267
void tst_QPrinter::testPageRectAndPaperRect_data()
269
QTest::addColumn<int>("orientation");
270
QTest::addColumn<bool>("withPainter");
271
QTest::addColumn<int>("resolution");
272
QTest::addColumn<bool>("doPaperRect");
275
QTest::newRow("paperRect0") << int(QPrinter::Portrait) << true << 300 << true;
276
QTest::newRow("paperRect1") << int(QPrinter::Portrait) << false << 300 << true;
277
QTest::newRow("paperRect2") << int(QPrinter::Landscape) << true << 300 << true;
278
QTest::newRow("paperRect3") << int(QPrinter::Landscape) << false << 300 << true;
279
QTest::newRow("paperRect4") << int(QPrinter::Portrait) << true << 600 << true;
280
QTest::newRow("paperRect5") << int(QPrinter::Portrait) << false << 600 << true;
281
QTest::newRow("paperRect6") << int(QPrinter::Landscape) << true << 600 << true;
282
QTest::newRow("paperRect7") << int(QPrinter::Landscape) << false << 600 << true;
283
QTest::newRow("paperRect8") << int(QPrinter::Portrait) << true << 1200 << true;
284
QTest::newRow("paperRect9") << int(QPrinter::Portrait) << false << 1200 << true;
285
QTest::newRow("paperRect10") << int(QPrinter::Landscape) << true << 1200 << true;
286
QTest::newRow("paperRect11") << int(QPrinter::Landscape) << false << 1200 << true;
289
QTest::newRow("pageRect0") << int(QPrinter::Portrait) << true << 300 << false;
290
QTest::newRow("pageRect1") << int(QPrinter::Portrait) << false << 300 << false;
291
QTest::newRow("pageRect2") << int(QPrinter::Landscape) << true << 300 << false;
292
QTest::newRow("pageRect3") << int(QPrinter::Landscape) << false << 300 << false;
293
QTest::newRow("pageRect4") << int(QPrinter::Portrait) << true << 600 << false;
294
QTest::newRow("pageRect5") << int(QPrinter::Portrait) << false << 600 << false;
295
QTest::newRow("pageRect6") << int(QPrinter::Landscape) << true << 600 << false;
296
QTest::newRow("pageRect7") << int(QPrinter::Landscape) << false << 600 << false;
297
QTest::newRow("pageRect8") << int(QPrinter::Portrait) << true << 1200 << false;
298
QTest::newRow("pageRect9") << int(QPrinter::Portrait) << false << 1200 << false;
299
QTest::newRow("pageRect10") << int(QPrinter::Landscape) << true << 1200 << false;
300
QTest::newRow("pageRect11") << int(QPrinter::Landscape) << false << 1200 << false;
303
void tst_QPrinter::testPageRectAndPaperRect()
305
QFETCH(bool, withPainter);
306
QFETCH(int, orientation);
307
QFETCH(int, resolution);
308
QFETCH(bool, doPaperRect);
310
QPainter *painter = 0;
311
QPrinter printer(QPrinter::HighResolution);
312
printer.setOrientation(QPrinter::Orientation(orientation));
313
printer.setOutputFileName("silly");
314
TempFileCleanup tmpFile("silly");
316
QRect pageRect = doPaperRect ? printer.paperRect() : printer.pageRect();
317
float inchesX = float(pageRect.width()) / float(printer.resolution());
318
float inchesY = float(pageRect.height()) / float(printer.resolution());
319
printer.setResolution(resolution);
321
painter = new QPainter(&printer);
323
QRect otherRect = doPaperRect ? printer.paperRect() : printer.pageRect();
324
float otherInchesX = float(otherRect.width()) / float(printer.resolution());
325
float otherInchesY = float(otherRect.height()) / float(printer.resolution());
329
QVERIFY(qAbs(otherInchesX - inchesX) < 0.01);
330
QVERIFY(qAbs(otherInchesY - inchesY) < 0.01);
332
QVERIFY(printer.orientation() == QPrinter::Portrait || pageRect.width() > pageRect.height());
333
QVERIFY(printer.orientation() != QPrinter::Portrait || pageRect.width() < pageRect.height());
336
void tst_QPrinter::testSetOptions()
339
QPrintDialog dlg(&prn);
341
// Verify default values
342
MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintToFile), true);
343
MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintSelection), false);
344
MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintPageRange), true);
346
dlg.setEnabledOptions(QAbstractPrintDialog::PrintPageRange);
347
MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintToFile), false);
348
MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintSelection), false);
349
MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintPageRange), true);
351
dlg.setEnabledOptions((QAbstractPrintDialog::PrintDialogOptions(QAbstractPrintDialog::PrintSelection
352
| QAbstractPrintDialog::PrintPageRange)));
353
MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintToFile), false);
354
MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintSelection), true);
355
MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintPageRange), true);
357
dlg.setEnabledOptions(QAbstractPrintDialog::PrintSelection);
358
MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintToFile), false);
359
MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintSelection), true);
360
MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintPageRange), false);
363
void tst_QPrinter::testMargins_data()
365
QTest::addColumn<int>("orientation");
366
QTest::addColumn<bool>("fullpage");
367
QTest::addColumn<int>("pagesize");
368
QTest::addColumn<int>("width");
369
QTest::addColumn<int>("height");
370
QTest::addColumn<bool>("withPainter");
372
QTest::newRow("data0") << int(QPrinter::Portrait) << true << int(QPrinter::A4) << 210 << 297 << false;
373
QTest::newRow("data1") << int(QPrinter::Landscape) << true << int(QPrinter::A4) << 297 << 210 << false;
374
QTest::newRow("data2") << int(QPrinter::Landscape) << false << int(QPrinter::A4) << 297 << 210 << false;
375
QTest::newRow("data3") << int(QPrinter::Portrait) << false << int(QPrinter::A4) << 210 << 297 << false;
376
QTest::newRow("data4") << int(QPrinter::Portrait) << true << int(QPrinter::A4) << 210 << 297 << true;
377
QTest::newRow("data5") << int(QPrinter::Landscape) << true << int(QPrinter::A4) << 297 << 210 << true;
378
QTest::newRow("data6") << int(QPrinter::Landscape) << false << int(QPrinter::A4) << 297 << 210 << true;
379
QTest::newRow("data7") << int(QPrinter::Portrait) << false << int(QPrinter::A4) << 210 << 297 << true;
382
void tst_QPrinter::testMargins()
384
QFETCH(bool, withPainter);
385
QFETCH(int, orientation);
386
QFETCH(int, pagesize);
389
QFETCH(bool, fullpage);
393
QPainter *painter = 0;
394
printer.setOutputFileName("silly");
395
printer.setOrientation((QPrinter::Orientation)orientation);
396
printer.setFullPage(fullpage);
397
printer.setPageSize((QPrinter::PageSize)pagesize);
399
painter = new QPainter(&printer);
403
QFile::remove("silly");
407
// QPrinter::testNonExistentPrinter() is not relevant for this platform
408
void tst_QPrinter::testNonExistentPrinter()
413
// Make sure it doesn't crash on setting or getting properties
414
printer.setPrinterName("some non existing printer");
415
printer.setPageSize(QPrinter::A4);
416
printer.setOrientation(QPrinter::Portrait);
417
printer.setFullPage(true);
419
printer.orientation();
421
printer.setCopyCount(1);
422
printer.printerName();
425
QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmWidth), 0);
426
QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmHeight), 0);
427
QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmWidthMM), 0);
428
QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmHeightMM), 0);
429
QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmNumColors), 0);
430
QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmDepth), 0);
431
QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmDpiX), 0);
432
QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmDpiY), 0);
433
QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmPhysicalDpiX), 0);
434
QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmPhysicalDpiY), 0);
436
QVERIFY(!painter.begin(&printer));
440
void tst_QPrinter::testMulitpleSets_data()
442
QTest::addColumn<int>("resolution");
443
QTest::addColumn<int>("pageSize");
444
QTest::addColumn<int>("widthMMAfter");
445
QTest::addColumn<int>("heightMMAfter");
448
QTest::newRow("lowRes") << int(QPrinter::ScreenResolution) << int(QPrinter::A4) << 210 << 297;
449
QTest::newRow("lowResLetter") << int(QPrinter::ScreenResolution) << int(QPrinter::Letter) << 216 << 279;
450
QTest::newRow("lowResA5") << int(QPrinter::ScreenResolution) << int(QPrinter::A5) << 148 << 210;
451
QTest::newRow("midRes") << int(QPrinter::PrinterResolution) << int(QPrinter::A4) << 210 << 297;
452
QTest::newRow("midResLetter") << int(QPrinter::PrinterResolution) << int(QPrinter::Letter) << 216 << 279;
453
QTest::newRow("midResA5") << int(QPrinter::PrinterResolution) << int(QPrinter::A5) << 148 << 210;
454
QTest::newRow("highRes") << int(QPrinter::HighResolution) << int(QPrinter::A4) << 210 << 297;
455
QTest::newRow("highResLetter") << int(QPrinter::HighResolution) << int(QPrinter::Letter) << 216 << 279;
456
QTest::newRow("highResA5") << int(QPrinter::HighResolution) << int(QPrinter::A5) << 148 << 210;
459
static void computePageValue(const QPrinter &printer, int &retWidth, int &retHeight)
461
const double Inch2MM = 25.4;
463
double width = double(printer.paperRect().width()) / printer.logicalDpiX() * Inch2MM;
464
double height = double(printer.paperRect().height()) / printer.logicalDpiY() * Inch2MM;
465
retWidth = qRound(width);
466
retHeight = qRound(height);
469
void tst_QPrinter::testMulitpleSets()
471
// A very simple test, but Mac needs to have its format "validated" if the format is changed
472
// This takes care of that.
473
QFETCH(int, resolution);
474
QFETCH(int, pageSize);
475
QFETCH(int, widthMMAfter);
476
QFETCH(int, heightMMAfter);
479
QPrinter::PrinterMode mode = QPrinter::PrinterMode(resolution);
480
QPrinter::PageSize printerPageSize = QPrinter::PageSize(pageSize);
481
QPrinter printer(mode);
482
printer.setFullPage(true);
484
int paperWidth, paperHeight;
485
//const int Tolerance = 2;
487
computePageValue(printer, paperWidth, paperHeight);
488
printer.setPageSize(printerPageSize);
490
if (printer.pageSize() != printerPageSize) {
491
QSKIP("Current page size is not supported on this printer");
495
QVERIFY(qAbs(printer.widthMM() - widthMMAfter) <= 2);
496
QVERIFY(qAbs(printer.heightMM() - heightMMAfter) <= 2);
498
computePageValue(printer, paperWidth, paperHeight);
500
QVERIFY(qAbs(paperWidth - widthMMAfter) <= 2);
501
QVERIFY(qAbs(paperHeight - heightMMAfter) <= 2);
503
// Set it again and see if it still works.
504
printer.setPageSize(printerPageSize);
505
QVERIFY(qAbs(printer.widthMM() - widthMMAfter) <= 2);
506
QVERIFY(qAbs(printer.heightMM() - heightMMAfter) <= 2);
508
printer.setOrientation(QPrinter::Landscape);
509
computePageValue(printer, paperWidth, paperHeight);
510
QVERIFY(qAbs(paperWidth - heightMMAfter) <= 2);
511
QVERIFY(qAbs(paperHeight - widthMMAfter) <= 2);
514
void tst_QPrinter::changingOutputFormat()
516
#if QT_VERSION < 0x050000
518
p.setOutputFormat(QPrinter::PostScriptFormat);
519
p.setPageSize(QPrinter::A8);
520
p.setOutputFormat(QPrinter::PdfFormat);
521
QCOMPARE(p.pageSize(), QPrinter::A8);
525
void tst_QPrinter::outputFormatFromSuffix()
527
if (QPrinterInfo::availablePrinters().size() == 0)
528
QSKIP("No printers available.");
530
QVERIFY(p.outputFormat() == QPrinter::NativeFormat);
531
p.setOutputFileName("test.pdf");
532
TempFileCleanup tmpFile("test.pdf");
533
QVERIFY(p.outputFormat() == QPrinter::PdfFormat);
534
p.setOutputFileName(QString());
535
QVERIFY(p.outputFormat() == QPrinter::NativeFormat);
538
void tst_QPrinter::setGetPaperSize()
541
p.setOutputFormat(QPrinter::PdfFormat);
542
QSizeF size(500, 10);
543
p.setPaperSize(size, QPrinter::Millimeter);
544
QCOMPARE(p.paperSize(QPrinter::Millimeter), size);
545
QSizeF ptSize = p.paperSize(QPrinter::Point);
546
//qDebug() << ptSize;
547
QVERIFY(qAbs(ptSize.width() - size.width() * (72/25.4)) < 1E-4);
548
QVERIFY(qAbs(ptSize.height() - size.height() * (72/25.4)) < 1E-4);
551
void tst_QPrinter::testPageMargins_data()
553
QTest::addColumn<qreal>("left");
554
QTest::addColumn<qreal>("top");
555
QTest::addColumn<qreal>("right");
556
QTest::addColumn<qreal>("bottom");
557
QTest::addColumn<int>("unit");
559
QTest::newRow("data0") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast<int>(QPrinter::Millimeter);
560
QTest::newRow("data1") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast<int>(QPrinter::Point);
561
QTest::newRow("data2") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast<int>(QPrinter::Inch);
562
QTest::newRow("data3") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast<int>(QPrinter::Pica);
563
QTest::newRow("data4") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast<int>(QPrinter::Didot);
564
QTest::newRow("data5") << qreal(5.5) << qreal(6.5) << qreal(7.5) << qreal(8.5) << static_cast<int>(QPrinter::Cicero);
567
void tst_QPrinter::testPageMargins()
571
qreal toMillimeters[6];
572
toMillimeters[QPrinter::Millimeter] = 1;
573
toMillimeters[QPrinter::Point] = 0.352777778;
574
toMillimeters[QPrinter::Inch] = 25.4;
575
toMillimeters[QPrinter::Pica] = 4.23333333;
576
toMillimeters[QPrinter::Didot] = 0.376;
577
toMillimeters[QPrinter::Cicero] = 4.51166667;
581
QFETCH(qreal, right);
582
QFETCH(qreal, bottom);
585
qreal nLeft, nTop, nRight, nBottom;
587
obj1.setPageMargins(left, top, right, bottom, static_cast<QPrinter::Unit>(unit));
589
qreal tolerance = 0.05;
591
obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Millimeter);
592
QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Millimeter]) < tolerance);
593
QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Millimeter]) < tolerance);
594
QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Millimeter]) < tolerance);
595
QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Millimeter]) < tolerance);
597
obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Point);
598
QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Point]) < tolerance);
599
QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Point]) < tolerance);
600
QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Point]) < tolerance);
601
QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Point]) < tolerance);
603
obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Inch);
604
QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Inch]) < tolerance);
605
QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Inch]) < tolerance);
606
QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Inch]) < tolerance);
607
QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Inch]) < tolerance);
609
obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Pica);
610
QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Pica]) < tolerance);
611
QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Pica]) < tolerance);
612
QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Pica]) < tolerance);
613
QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Pica]) < tolerance);
615
obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Didot);
616
QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Didot]) < tolerance);
617
QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Didot]) < tolerance);
618
QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Didot]) < tolerance);
619
QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Didot]) < tolerance);
621
obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Cicero);
622
QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Cicero]) < tolerance);
623
QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Cicero]) < tolerance);
624
QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Cicero]) < tolerance);
625
QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Cicero]) < tolerance);
628
void tst_QPrinter::valuePreservation()
630
QPrinter::OutputFormat oldFormat = QPrinter::PdfFormat;
631
QPrinter::OutputFormat newFormat = QPrinter::NativeFormat; // TODO: Correct?
633
// Some properties are documented to only be supported by NativeFormat in X11 environment
634
bool doX11Tests = QGuiApplication::platformName().compare(QLatin1String("xcb"), Qt::CaseInsensitive) == 0;
635
bool windowsPlatform = QGuiApplication::platformName().compare(QLatin1String("windows"), Qt::CaseInsensitive) == 0;
636
bool manualSourceSupported = true;
639
// QPrinter::supportedPaperSources() is only available on Windows, so just assuming manual is supported on others.
641
printer.setOutputFormat(newFormat);
642
QList<QPrinter::PaperSource> sources = printer.supportedPaperSources();
643
if (!sources.contains(QPrinter::Manual)) {
644
manualSourceSupported = false;
645
qWarning() << "Manual paper source not supported by native printer, skipping related test.";
649
// Querying PPK_CollateCopies is hardcoded to return false with Windows native print engine,
650
// so skip testing that in Windows.
651
if (!windowsPlatform) {
653
printer.setOutputFormat(oldFormat);
654
bool status = printer.collateCopies();
655
printer.setOutputFormat(newFormat);
656
printer.setOutputFormat(oldFormat);
657
QCOMPARE(printer.collateCopies(), status);
659
printer.setCollateCopies(!status);
660
printer.setOutputFormat(newFormat);
662
QEXPECT_FAIL("","QTBUG-26430", Abort);
664
QCOMPARE(printer.collateCopies(), !status);
665
printer.setOutputFormat(oldFormat);
666
QCOMPARE(printer.collateCopies(), !status);
670
printer.setOutputFormat(oldFormat);
671
QPrinter::ColorMode status = printer.colorMode();
672
printer.setOutputFormat(newFormat);
673
printer.setOutputFormat(oldFormat);
674
QCOMPARE(printer.colorMode(), status);
676
printer.setColorMode(QPrinter::ColorMode(!status));
677
printer.setOutputFormat(newFormat);
678
QCOMPARE(printer.colorMode(), QPrinter::ColorMode(!status));
679
printer.setOutputFormat(oldFormat);
680
QCOMPARE(printer.colorMode(), QPrinter::ColorMode(!status));
684
printer.setOutputFormat(oldFormat);
685
QString status = printer.creator();
686
printer.setOutputFormat(newFormat);
687
printer.setOutputFormat(oldFormat);
688
QCOMPARE(printer.creator(), status);
690
status = QString::fromLatin1("Mr. Test");
691
printer.setCreator(status);
692
printer.setOutputFormat(newFormat);
693
QCOMPARE(printer.creator(), status);
694
printer.setOutputFormat(oldFormat);
695
QCOMPARE(printer.creator(), status);
699
printer.setOutputFormat(oldFormat);
700
QString status = printer.docName();
701
printer.setOutputFormat(newFormat);
702
printer.setOutputFormat(oldFormat);
703
QCOMPARE(printer.docName(), status);
705
status = QString::fromLatin1("Test document");
706
printer.setDocName(status);
707
printer.setOutputFormat(newFormat);
708
QCOMPARE(printer.docName(), status);
709
printer.setOutputFormat(oldFormat);
710
QCOMPARE(printer.docName(), status);
714
printer.setOutputFormat(oldFormat);
715
bool status = printer.doubleSidedPrinting();
716
printer.setOutputFormat(newFormat);
717
printer.setOutputFormat(oldFormat);
718
QCOMPARE(printer.doubleSidedPrinting(), status);
720
printer.setDoubleSidedPrinting(!status);
721
printer.setOutputFormat(newFormat);
722
QCOMPARE(printer.doubleSidedPrinting(), !status);
723
printer.setOutputFormat(oldFormat);
724
QCOMPARE(printer.doubleSidedPrinting(), !status);
728
printer.setOutputFormat(oldFormat);
729
bool status = printer.fontEmbeddingEnabled();
730
printer.setOutputFormat(newFormat);
731
printer.setOutputFormat(oldFormat);
732
QCOMPARE(printer.fontEmbeddingEnabled(), status);
734
printer.setFontEmbeddingEnabled(!status);
735
printer.setOutputFormat(newFormat);
736
QCOMPARE(printer.fontEmbeddingEnabled(), !status);
737
printer.setOutputFormat(oldFormat);
738
QCOMPARE(printer.fontEmbeddingEnabled(), !status);
742
printer.setOutputFormat(oldFormat);
743
bool status = printer.fullPage();
744
printer.setOutputFormat(newFormat);
745
printer.setOutputFormat(oldFormat);
746
QCOMPARE(printer.fullPage(), status);
748
printer.setFullPage(!status);
749
printer.setOutputFormat(newFormat);
750
QCOMPARE(printer.fullPage(), !status);
751
printer.setOutputFormat(oldFormat);
752
QCOMPARE(printer.fullPage(), !status);
756
printer.setOutputFormat(oldFormat);
757
QPrinter::Orientation status = printer.orientation();
758
printer.setOutputFormat(newFormat);
759
printer.setOutputFormat(oldFormat);
760
QCOMPARE(printer.orientation(), status);
762
printer.setOrientation(QPrinter::Orientation(!status));
763
printer.setOutputFormat(newFormat);
764
QCOMPARE(printer.orientation(), QPrinter::Orientation(!status));
765
printer.setOutputFormat(oldFormat);
766
QCOMPARE(printer.orientation(), QPrinter::Orientation(!status));
770
printer.setOutputFormat(oldFormat);
771
QString status = printer.outputFileName();
772
printer.setOutputFormat(newFormat);
773
printer.setOutputFormat(oldFormat);
774
QCOMPARE(printer.outputFileName(), status);
776
status = QString::fromLatin1("Test file");
777
printer.setOutputFileName(status);
778
printer.setOutputFormat(newFormat);
779
QCOMPARE(printer.outputFileName(), status);
780
printer.setOutputFormat(oldFormat);
781
QCOMPARE(printer.outputFileName(), status);
785
printer.setOutputFormat(oldFormat);
786
QPrinter::PageOrder status = printer.pageOrder();
787
printer.setOutputFormat(newFormat);
788
printer.setOutputFormat(oldFormat);
789
QCOMPARE(printer.pageOrder(), status);
791
printer.setPageOrder(QPrinter::PageOrder(!status));
792
printer.setOutputFormat(newFormat);
793
QCOMPARE(printer.pageOrder(), QPrinter::PageOrder(!status));
794
printer.setOutputFormat(oldFormat);
795
QCOMPARE(printer.pageOrder(), QPrinter::PageOrder(!status));
799
printer.setOutputFormat(oldFormat);
800
QPrinter::PageSize status = printer.pageSize();
801
printer.setOutputFormat(newFormat);
802
printer.setOutputFormat(oldFormat);
803
QCOMPARE(printer.pageSize(), status);
805
printer.setPageSize(QPrinter::B5);
806
printer.setOutputFormat(newFormat);
807
QCOMPARE(printer.pageSize(), QPrinter::B5);
808
printer.setOutputFormat(oldFormat);
809
QCOMPARE(printer.pageSize(), QPrinter::B5);
811
if (manualSourceSupported) {
813
printer.setOutputFormat(oldFormat);
814
QPrinter::PaperSource status = printer.paperSource();
815
printer.setOutputFormat(newFormat);
816
printer.setOutputFormat(oldFormat);
817
QCOMPARE(printer.paperSource(), status);
819
printer.setPaperSource(QPrinter::Manual);
820
printer.setOutputFormat(newFormat);
821
QCOMPARE(printer.paperSource(), QPrinter::Manual);
822
printer.setOutputFormat(oldFormat);
823
QCOMPARE(printer.paperSource(), QPrinter::Manual);
827
printer.setOutputFormat(oldFormat);
828
QString status = printer.printProgram();
829
printer.setOutputFormat(newFormat);
830
printer.setOutputFormat(oldFormat);
831
QCOMPARE(printer.printProgram(), status);
833
status = QString::fromLatin1("/usr/local/bin/lpr");
834
printer.setPrintProgram(status);
835
printer.setOutputFormat(newFormat);
836
QCOMPARE(printer.printProgram(), status);
837
printer.setOutputFormat(oldFormat);
838
QCOMPARE(printer.printProgram(), status);
842
printer.setOutputFormat(oldFormat);
843
QPrinter::PrintRange status = printer.printRange();
844
printer.setOutputFormat(newFormat);
845
printer.setOutputFormat(oldFormat);
846
QCOMPARE(printer.printRange(), status);
848
printer.setPrintRange(QPrinter::PrintRange(!status));
849
printer.setOutputFormat(newFormat);
850
QCOMPARE(printer.printRange(), QPrinter::PrintRange(!status));
851
printer.setOutputFormat(oldFormat);
852
QCOMPARE(printer.printRange(), QPrinter::PrintRange(!status));
856
printer.setOutputFormat(oldFormat);
857
QString status = printer.printerName();
858
printer.setOutputFormat(newFormat);
859
printer.setOutputFormat(oldFormat);
860
QCOMPARE(printer.printerName(), status);
862
status = QString::fromLatin1("SuperDuperPrinter");
863
printer.setPrinterName(status);
864
printer.setOutputFormat(newFormat);
865
QCOMPARE(printer.printerName(), status);
866
printer.setOutputFormat(oldFormat);
867
QCOMPARE(printer.printerName(), status);
869
// QPrinter::printerSelectionOption is explicitly documented not to be available on Windows.
873
printer.setOutputFormat(oldFormat);
874
QString status = printer.printerSelectionOption();
875
printer.setOutputFormat(newFormat);
876
printer.setOutputFormat(oldFormat);
877
QCOMPARE(printer.printerSelectionOption(), status);
879
status = QString::fromLatin1("Optional option");
880
printer.setPrinterSelectionOption(status);
881
printer.setOutputFormat(newFormat);
882
QCOMPARE(printer.printerSelectionOption(), status);
883
printer.setOutputFormat(oldFormat);
884
QCOMPARE(printer.printerSelectionOption(), status);
889
printer.setOutputFormat(oldFormat);
890
int status = printer.resolution();
891
printer.setOutputFormat(newFormat);
892
printer.setOutputFormat(oldFormat);
893
QCOMPARE(printer.resolution(), status);
895
printer.setResolution(status-150);
896
printer.setOutputFormat(newFormat);
897
QCOMPARE(printer.resolution(), status-150);
898
printer.setOutputFormat(oldFormat);
899
QCOMPARE(printer.resolution(), status-150);
903
void tst_QPrinter::errorReporting()
906
p.setOutputFormat(QPrinter::PdfFormat);
907
QCOMPARE(p.isValid(), true);
910
// not sure how to choose a never-writable file on windows. But its QFile behavior anyway, so lets rely on it failing elsewhere
911
p.setOutputFileName("/foobar/nonwritable.pdf");
912
QCOMPARE(painter.begin(&p), false); // it should check the output file is writable
914
p.setOutputFileName("test.pdf");
915
TempFileCleanup tmpFile("test.pdf");
916
QCOMPARE(painter.begin(&p), true); // it should check the output
917
QCOMPARE(p.isValid(), true);
921
void tst_QPrinter::testCustomPageSizes()
925
QSizeF customSize(8.5, 11.0);
926
p.setPaperSize(customSize, QPrinter::Inch);
928
QSizeF paperSize = p.paperSize(QPrinter::Inch);
929
QCOMPARE(paperSize, customSize);
931
QPrinter p2(QPrinter::HighResolution);
932
p2.setPaperSize(customSize, QPrinter::Inch);
933
paperSize = p.paperSize(QPrinter::Inch);
934
QCOMPARE(paperSize, customSize);
937
void tst_QPrinter::customPaperSizeAndMargins_data()
939
QTest::addColumn<bool>("pdf");
940
QTest::addColumn<bool>("before");
941
QTest::addColumn<qreal>("left");
942
QTest::addColumn<qreal>("top");
943
QTest::addColumn<qreal>("right");
944
QTest::addColumn<qreal>("bottom");
946
QTest::newRow("beforeNoPDF") << false << true << qreal(2) << qreal(2) << qreal(2) << qreal(2);
947
QTest::newRow("beforePDF") << true << true << qreal(2) << qreal(2) << qreal(2) << qreal(2);
948
QTest::newRow("afterNoPDF") << false << false << qreal(2) << qreal(2) << qreal(2) << qreal(2);
949
QTest::newRow("afterAfterPDF") << true << false << qreal(2) << qreal(2) << qreal(2) << qreal(2);
952
void tst_QPrinter::customPaperSizeAndMargins()
955
QFETCH(bool, before);
958
QFETCH(qreal, right);
959
QFETCH(qreal, bottom);
961
qreal tolerance = 0.05;
966
QSizeF customSize(8.5, 11.0);
970
p.setOutputFormat(QPrinter::PdfFormat);
972
p.setPageMargins(left, top, right, bottom, QPrinter::Millimeter);
973
p.setPaperSize(customSize, QPrinter::Millimeter);
974
p.getPageMargins(&getLeft, &getTop, &getRight, &getBottom, QPrinter::Millimeter);
976
QVERIFY(fabs(left - getLeft) < tolerance);
977
QVERIFY(fabs(left - getTop) < tolerance);
978
QVERIFY(fabs(left - getRight) < tolerance);
979
QVERIFY(fabs(left - getBottom) < tolerance);
981
QVERIFY(getLeft == 0);
982
QVERIFY(getTop == 0);
983
QVERIFY(getRight == 0);
984
QVERIFY(getBottom == 0);
985
p.setPageMargins(left, top, right, bottom, QPrinter::Millimeter);
986
p.getPageMargins(&getLeft, &getTop, &getRight, &getBottom, QPrinter::Millimeter);
987
QVERIFY(fabs(left - getLeft) < tolerance);
988
QVERIFY(fabs(left - getTop) < tolerance);
989
QVERIFY(fabs(left - getRight) < tolerance);
990
QVERIFY(fabs(left - getBottom) < tolerance);
994
#if !defined(QT_NO_COMPLETER) && !defined(QT_NO_FILEDIALOG)
995
void tst_QPrinter::printDialogCompleter()
998
dialog.printer()->setOutputFileName("file.pdf");
999
TempFileCleanup tmpFile("file.pdf");
1000
dialog.setEnabledOptions(QAbstractPrintDialog::PrintToFile);
1005
QTest::keyClick(&dialog, Qt::Key_Tab);
1006
QTest::keyClick(&dialog, 'P');
1007
// The test passes if it doesn't crash.
1011
void tst_QPrinter::testCopyCount()
1015
QCOMPARE(p.copyCount(), 15);
1018
static void printPage(QPainter *painter)
1020
painter->setPen(QPen(Qt::black, 4));
1021
painter->drawRect(50, 60, 70, 80);
1024
void tst_QPrinter::taskQTBUG4497_reusePrinterOnDifferentFiles()
1026
TempFileCleanup tmpFile1("out1.pdf");
1027
TempFileCleanup tmpFile2("out2.pdf");
1032
printer.setOutputFileName("out1.pdf");
1033
QPainter painter(&printer);
1034
printPage(&painter);
1039
printer.setOutputFileName("out2.pdf");
1040
QPainter painter(&printer);
1041
printPage(&painter);
1044
QFile file1("out1.pdf");
1045
QVERIFY(file1.open(QIODevice::ReadOnly));
1047
QFile file2("out2.pdf");
1048
QVERIFY(file2.open(QIODevice::ReadOnly));
1050
while (!file1.atEnd() && !file2.atEnd()) {
1051
QByteArray file1Line = file1.readLine();
1052
QByteArray file2Line = file2.readLine();
1054
if (!file1Line.startsWith("%%CreationDate"))
1055
QCOMPARE(file1Line, file2Line);
1058
QVERIFY(file1.atEnd());
1059
QVERIFY(file2.atEnd());
1062
void tst_QPrinter::testCurrentPage()
1065
printer.setFromTo(1, 10);
1067
// Test set print range
1068
printer.setPrintRange(QPrinter::CurrentPage);
1069
QCOMPARE(printer.printRange(), QPrinter::CurrentPage);
1070
QCOMPARE(printer.fromPage(), 1);
1071
QCOMPARE(printer.toPage(), 10);
1073
QPrintDialog dialog(&printer);
1075
// Test default Current Page option to off
1076
QCOMPARE(dialog.isOptionEnabled(QPrintDialog::PrintCurrentPage), false);
1078
// Test enable Current Page option
1079
dialog.setOption(QPrintDialog::PrintCurrentPage);
1080
QCOMPARE(dialog.isOptionEnabled(QPrintDialog::PrintCurrentPage), true);
1084
void tst_QPrinter::testPdfTitle()
1086
// Check the document name is represented correctly in produced pdf
1090
// This string is just the UTF-8 encoding of the string: \()f ø hiragana o
1091
const unsigned char titleBuf[]={0x5c, 0x28, 0x29, 0x66, 0xc3, 0xb8, 0xe3, 0x81, 0x8a, 0x00};
1092
const char *title = reinterpret_cast<const char*>(titleBuf);
1093
printer.setOutputFileName("file.pdf");
1094
printer.setDocName(QString::fromUtf8(title));
1095
painter.begin(&printer);
1098
TempFileCleanup tmpFile("file.pdf");
1099
QFile file("file.pdf");
1100
QVERIFY(file.open(QIODevice::ReadOnly));
1101
// The we expect the title to appear in the PDF as:
1102
// ASCII('\title (') UTF16(\\\(\)f ø hiragana o) ASCII(')').
1103
// which has the following binary representation
1104
const unsigned char expectedBuf[] = {
1105
0x2f, 0x54, 0x69, 0x74, 0x6c, 0x65, 0x20, 0x28, 0xfe,
1106
0xff, 0x00, 0x5c, 0x5c, 0x00, 0x5c, 0x28, 0x00, 0x5c,
1107
0x29, 0x00, 0x66, 0x00, 0xf8, 0x30, 0x4a, 0x29};
1108
const char *expected = reinterpret_cast<const char*>(expectedBuf);
1109
QVERIFY(file.readAll().contains(QByteArray(expected, 26)));
1111
#endif // QT_NO_PRINTER
1113
QTEST_MAIN(tst_QPrinter)
1114
#include "tst_qprinter.moc"