tests/auto/qprinter/tst_qprinter.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 
       
    45 
       
    46 
       
    47 #include <qprinter.h>
       
    48 #include <qpagesetupdialog.h>
       
    49 #include <qpainter.h>
       
    50 #include <qprintdialog.h>
       
    51 #include <qprinterinfo.h>
       
    52 #ifdef QT3_SUPPORT
       
    53 #include <q3paintdevicemetrics.h>
       
    54 #endif
       
    55 #include <qvariant.h>
       
    56 #include <qpainter.h>
       
    57 #include <qprintengine.h>
       
    58 
       
    59 #include <math.h>
       
    60 
       
    61 #ifdef Q_WS_WIN
       
    62 #include <windows.h>
       
    63 #endif
       
    64 
       
    65 Q_DECLARE_METATYPE(QRect)
       
    66 
       
    67 
       
    68 
       
    69 //TESTED_CLASS=
       
    70 //TESTED_FILES=
       
    71 
       
    72 class tst_QPrinter : public QObject
       
    73 {
       
    74     Q_OBJECT
       
    75 
       
    76 public:
       
    77     tst_QPrinter();
       
    78     virtual ~tst_QPrinter();
       
    79 
       
    80 
       
    81 public slots:
       
    82     void initTestCase();
       
    83     void cleanupTestCase();
       
    84     void init();
       
    85     void cleanup();
       
    86 private slots:
       
    87     void getSetCheck();
       
    88 // Add your testfunctions and testdata create functions here
       
    89     void testPageSize();
       
    90     void testPageRectAndPaperRect();
       
    91     void testPageRectAndPaperRect_data();
       
    92     void testSetOptions();
       
    93     void testMargins_data();
       
    94     void testMargins();
       
    95     void testNonExistentPrinter();
       
    96     void testPageSetupDialog();
       
    97     void testMulitpleSets_data();
       
    98     void testMulitpleSets();
       
    99     void testPageMargins_data();
       
   100     void testPageMargins();
       
   101     void changingOutputFormat();
       
   102     void outputFormatFromSuffix();
       
   103     void setGetPaperSize();
       
   104     void valuePreservation();
       
   105     void errorReporting();
       
   106     void testCustomPageSizes();
       
   107     void printDialogCompleter();
       
   108 
       
   109     void testActualNumCopies();
       
   110 
       
   111     void taskQTBUG4497_reusePrinterOnDifferentFiles();
       
   112 
       
   113 private:
       
   114 };
       
   115 
       
   116 // Testing get/set functions
       
   117 void tst_QPrinter::getSetCheck()
       
   118 {
       
   119     QPrinter obj1;
       
   120     // OutputFormat QPrinter::outputFormat()
       
   121     // void QPrinter::setOutputFormat(OutputFormat)
       
   122     obj1.setOutputFormat(QPrinter::OutputFormat(QPrinter::PdfFormat));
       
   123     QCOMPARE(QPrinter::OutputFormat(QPrinter::PdfFormat), obj1.outputFormat());
       
   124 
       
   125     // bool QPrinter::collateCopies()
       
   126     // void QPrinter::setCollateCopies(bool)
       
   127     obj1.setCollateCopies(false);
       
   128     QCOMPARE(false, obj1.collateCopies());
       
   129     obj1.setCollateCopies(true);
       
   130     QCOMPARE(true, obj1.collateCopies());
       
   131 
       
   132     obj1.setColorMode(QPrinter::GrayScale);
       
   133     QCOMPARE(obj1.colorMode(), QPrinter::GrayScale);
       
   134     obj1.setColorMode(QPrinter::Color);
       
   135     QCOMPARE(obj1.colorMode(), QPrinter::Color);
       
   136 
       
   137     obj1.setCreator(QString::fromLatin1("RandomQtUser"));
       
   138     QCOMPARE(obj1.creator(), QString::fromLatin1("RandomQtUser"));
       
   139 
       
   140     obj1.setDocName(QString::fromLatin1("RandomQtDocument"));
       
   141     QCOMPARE(obj1.docName(), QString::fromLatin1("RandomQtDocument"));
       
   142 
       
   143     obj1.setDoubleSidedPrinting(true);
       
   144     QCOMPARE(obj1.doubleSidedPrinting(), true);
       
   145     obj1.setDoubleSidedPrinting(false);
       
   146     QCOMPARE(obj1.doubleSidedPrinting(), false);
       
   147 
       
   148     obj1.setFromTo(1, 4);
       
   149     QCOMPARE(obj1.fromPage(), 1);
       
   150     QCOMPARE(obj1.toPage(), 4);
       
   151 
       
   152     obj1.setFullPage(true);
       
   153     QCOMPARE(obj1.fullPage(), true);
       
   154     obj1.setFullPage(false);
       
   155     QCOMPARE(obj1.fullPage(), false);
       
   156 
       
   157     obj1.setOrientation(QPrinter::Landscape);
       
   158     QCOMPARE(obj1.orientation(), QPrinter::Landscape);
       
   159     obj1.setOrientation(QPrinter::Portrait);
       
   160     QCOMPARE(obj1.orientation(), QPrinter::Portrait);
       
   161 
       
   162     obj1.setOutputFileName(QString::fromLatin1("RandomQtName"));
       
   163     QCOMPARE(obj1.outputFileName(), QString::fromLatin1("RandomQtName"));
       
   164 
       
   165     obj1.setPageOrder(QPrinter::FirstPageFirst);
       
   166     QCOMPARE(obj1.pageOrder(), QPrinter::FirstPageFirst);
       
   167     obj1.setPageOrder(QPrinter::LastPageFirst);
       
   168     QCOMPARE(obj1.pageOrder(), QPrinter::LastPageFirst);
       
   169 
       
   170     obj1.setPaperSource(QPrinter::Cassette);
       
   171     QCOMPARE(obj1.paperSource(), QPrinter::Cassette);
       
   172     obj1.setPaperSource(QPrinter::Middle);
       
   173     QCOMPARE(obj1.paperSource(), QPrinter::Middle);
       
   174 
       
   175 #ifdef Q_OS_UNIX
       
   176     obj1.setPrintProgram(QString::fromLatin1("/bin/true"));
       
   177     QCOMPARE(obj1.printProgram(), QString::fromLatin1("/bin/true"));
       
   178 
       
   179     obj1.setPrinterSelectionOption(QString::fromLatin1("--option"));
       
   180     QCOMPARE(obj1.printerSelectionOption(), QString::fromLatin1("--option"));
       
   181 #endif
       
   182 
       
   183     obj1.setPrinterName(QString::fromLatin1("myPrinter"));
       
   184     QCOMPARE(obj1.printerName(), QString::fromLatin1("myPrinter"));
       
   185 
       
   186     // bool QPrinter::fontEmbeddingEnabled()
       
   187     // void QPrinter::setFontEmbeddingEnabled(bool)
       
   188     obj1.setFontEmbeddingEnabled(false);
       
   189     QCOMPARE(false, obj1.fontEmbeddingEnabled());
       
   190     obj1.setFontEmbeddingEnabled(true);
       
   191     QCOMPARE(true, obj1.fontEmbeddingEnabled());
       
   192 
       
   193     // PageSize QPrinter::pageSize()
       
   194     // void QPrinter::setPageSize(PageSize)
       
   195     obj1.setPageSize(QPrinter::PageSize(QPrinter::A4));
       
   196     QCOMPARE(QPrinter::PageSize(QPrinter::A4), obj1.pageSize());
       
   197     obj1.setPageSize(QPrinter::PageSize(QPrinter::Letter));
       
   198     QCOMPARE(QPrinter::PageSize(QPrinter::Letter), obj1.pageSize());
       
   199     obj1.setPageSize(QPrinter::PageSize(QPrinter::Legal));
       
   200     QCOMPARE(QPrinter::PageSize(QPrinter::Legal), obj1.pageSize());
       
   201 
       
   202     // PrintRange QPrinter::printRange()
       
   203     // void QPrinter::setPrintRange(PrintRange)
       
   204     obj1.setPrintRange(QPrinter::PrintRange(QPrinter::AllPages));
       
   205     QCOMPARE(QPrinter::PrintRange(QPrinter::AllPages), obj1.printRange());
       
   206     obj1.setPrintRange(QPrinter::PrintRange(QPrinter::Selection));
       
   207     QCOMPARE(QPrinter::PrintRange(QPrinter::Selection), obj1.printRange());
       
   208     obj1.setPrintRange(QPrinter::PrintRange(QPrinter::PageRange));
       
   209     QCOMPARE(QPrinter::PrintRange(QPrinter::PageRange), obj1.printRange());
       
   210 }
       
   211 
       
   212 tst_QPrinter::tst_QPrinter()
       
   213 {
       
   214 }
       
   215 
       
   216 tst_QPrinter::~tst_QPrinter()
       
   217 {
       
   218 
       
   219 }
       
   220 
       
   221 // initTestCase will be executed once before the first testfunction is executed.
       
   222 void tst_QPrinter::initTestCase()
       
   223 {
       
   224 // TODO: Add testcase generic initialization code here.
       
   225 // suggestion:
       
   226 //    testWidget = new QPrinter(0,"testWidget");
       
   227 //    testWidget->setFixedSize(200, 200);
       
   228 //    qApp->setMainWidget(testWidget);
       
   229 //    testWidget->show();
       
   230 }
       
   231 
       
   232 // cleanupTestCase will be executed once after the last testfunction is executed.
       
   233 void tst_QPrinter::cleanupTestCase()
       
   234 {
       
   235 // TODO: Add testcase generic cleanup code here.
       
   236 // suggestion:
       
   237 //    testWidget->hide();
       
   238 //    qApp->setMainWidget(0);
       
   239 //    delete testWidget;
       
   240 }
       
   241 
       
   242 // init() will be executed immediately before each testfunction is run.
       
   243 void tst_QPrinter::init()
       
   244 {
       
   245 // TODO: Add testfunction specific initialization code here.
       
   246 }
       
   247 
       
   248 // cleanup() will be executed immediately after each testfunction is run.
       
   249 void tst_QPrinter::cleanup()
       
   250 {
       
   251 // TODO: Add testfunction specific cleanup code here.
       
   252 }
       
   253 
       
   254 #define MYCOMPARE(a, b) QCOMPARE(QVariant((int)a), QVariant((int)b))
       
   255 
       
   256 void tst_QPrinter::testPageSetupDialog()
       
   257 {
       
   258     // Make sure this doesn't crash at least
       
   259     {
       
   260         QPrinter printer;
       
   261         QPageSetupDialog dialog(&printer);
       
   262     }
       
   263 }
       
   264 
       
   265 void tst_QPrinter::testPageSize()
       
   266 {
       
   267 #if defined (Q_WS_WIN)
       
   268     QPrinter prn;
       
   269 
       
   270     prn.setPageSize(QPrinter::Letter);
       
   271     MYCOMPARE(prn.pageSize(), QPrinter::Letter);
       
   272     MYCOMPARE(prn.winPageSize(), DMPAPER_LETTER);
       
   273 
       
   274     prn.setPageSize(QPrinter::A4);
       
   275     MYCOMPARE(prn.pageSize(), QPrinter::A4);
       
   276     MYCOMPARE(prn.winPageSize(), DMPAPER_A4);
       
   277 
       
   278     prn.setWinPageSize(DMPAPER_LETTER);
       
   279     MYCOMPARE(prn.winPageSize(), DMPAPER_LETTER);
       
   280     MYCOMPARE(prn.pageSize(), QPrinter::Letter);
       
   281 
       
   282     prn.setWinPageSize(DMPAPER_A4);
       
   283     MYCOMPARE(prn.winPageSize(), DMPAPER_A4);
       
   284     MYCOMPARE(prn.pageSize(), QPrinter::A4);
       
   285 #else
       
   286     QSKIP("QPrinter::winPageSize() does not exist for nonwindows platforms", SkipAll);
       
   287 #endif
       
   288 }
       
   289 
       
   290 void tst_QPrinter::testPageRectAndPaperRect_data()
       
   291 {
       
   292     QTest::addColumn<int>("orientation");
       
   293     QTest::addColumn<bool>("withPainter");
       
   294     QTest::addColumn<int>("resolution");
       
   295     QTest::addColumn<bool>("doPaperRect");
       
   296 
       
   297     // paperrect
       
   298     QTest::newRow("paperRect0") << int(QPrinter::Portrait) << true << 300 << true;
       
   299     QTest::newRow("paperRect1") << int(QPrinter::Portrait) << false << 300 << true;
       
   300     QTest::newRow("paperRect2") << int(QPrinter::Landscape) << true << 300 << true;
       
   301     QTest::newRow("paperRect3") << int(QPrinter::Landscape) << false << 300 << true;
       
   302     QTest::newRow("paperRect4") << int(QPrinter::Portrait) << true << 600 << true;
       
   303     QTest::newRow("paperRect5") << int(QPrinter::Portrait) << false << 600 << true;
       
   304     QTest::newRow("paperRect6") << int(QPrinter::Landscape) << true << 600 << true;
       
   305     QTest::newRow("paperRect7") << int(QPrinter::Landscape) << false << 600 << true;
       
   306     QTest::newRow("paperRect8") << int(QPrinter::Portrait) << true << 1200 << true;
       
   307     QTest::newRow("paperRect9") << int(QPrinter::Portrait) << false << 1200 << true;
       
   308     QTest::newRow("paperRect10") << int(QPrinter::Landscape) << true << 1200 << true;
       
   309     QTest::newRow("paperRect11") << int(QPrinter::Landscape) << false << 1200 << true;
       
   310 
       
   311     // page rect
       
   312     QTest::newRow("pageRect0") << int(QPrinter::Portrait) << true << 300 << false;
       
   313     QTest::newRow("pageRect1") << int(QPrinter::Portrait) << false << 300 << false;
       
   314     QTest::newRow("pageRect2") << int(QPrinter::Landscape) << true << 300 << false;
       
   315     QTest::newRow("pageRect3") << int(QPrinter::Landscape) << false << 300 << false;
       
   316     QTest::newRow("pageRect4") << int(QPrinter::Portrait) << true << 600 << false;
       
   317     QTest::newRow("pageRect5") << int(QPrinter::Portrait) << false << 600 << false;
       
   318     QTest::newRow("pageRect6") << int(QPrinter::Landscape) << true << 600 << false;
       
   319     QTest::newRow("pageRect7") << int(QPrinter::Landscape) << false << 600 << false;
       
   320     QTest::newRow("pageRect8") << int(QPrinter::Portrait) << true << 1200 << false;
       
   321     QTest::newRow("pageRect9") << int(QPrinter::Portrait) << false << 1200 << false;
       
   322     QTest::newRow("pageRect10") << int(QPrinter::Landscape) << true << 1200 << false;
       
   323     QTest::newRow("pageRect11") << int(QPrinter::Landscape) << false << 1200 << false;
       
   324 }
       
   325 
       
   326 void tst_QPrinter::testPageRectAndPaperRect()
       
   327 {
       
   328     QFETCH(bool,  withPainter);
       
   329     QFETCH(int,  orientation);
       
   330     QFETCH(int, resolution);
       
   331     QFETCH(bool, doPaperRect);
       
   332 
       
   333     QPainter *painter = 0;
       
   334     QPrinter printer(QPrinter::HighResolution);
       
   335     printer.setOrientation(QPrinter::Orientation(orientation));
       
   336     printer.setOutputFileName("silly");
       
   337 
       
   338     QRect pageRect = doPaperRect ? printer.paperRect() : printer.pageRect();
       
   339     float inchesX = float(pageRect.width()) / float(printer.resolution());
       
   340     float inchesY = float(pageRect.height()) / float(printer.resolution());
       
   341     printer.setResolution(resolution);
       
   342     if (withPainter)
       
   343         painter = new QPainter(&printer);
       
   344 
       
   345     QRect otherRect = doPaperRect ? printer.paperRect() : printer.pageRect();
       
   346     float otherInchesX = float(otherRect.width()) / float(printer.resolution());
       
   347     float otherInchesY = float(otherRect.height()) / float(printer.resolution());
       
   348     if (painter != 0)
       
   349         delete painter;
       
   350 
       
   351     QVERIFY(qAbs(otherInchesX - inchesX) < 0.01);
       
   352     QVERIFY(qAbs(otherInchesY - inchesY) < 0.01);
       
   353 
       
   354     QVERIFY(printer.orientation() == QPrinter::Portrait || pageRect.width() > pageRect.height());
       
   355     QVERIFY(printer.orientation() != QPrinter::Portrait || pageRect.width() < pageRect.height());
       
   356 }
       
   357 
       
   358 void tst_QPrinter::testSetOptions()
       
   359 {
       
   360     QPrinter prn;
       
   361     QPrintDialog dlg(&prn);
       
   362 
       
   363     // Verify default values
       
   364     MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintToFile), TRUE);
       
   365     MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintSelection), FALSE);
       
   366     MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintPageRange), TRUE);
       
   367 
       
   368     dlg.setEnabledOptions(QAbstractPrintDialog::PrintPageRange);
       
   369     MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintToFile), FALSE);
       
   370     MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintSelection), FALSE);
       
   371     MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintPageRange), TRUE);
       
   372 
       
   373     dlg.setEnabledOptions((QAbstractPrintDialog::PrintDialogOptions(QAbstractPrintDialog::PrintSelection
       
   374                                                                     | QAbstractPrintDialog::PrintPageRange)));
       
   375     MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintToFile), FALSE);
       
   376     MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintSelection), TRUE);
       
   377     MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintPageRange), TRUE);
       
   378 
       
   379     dlg.setEnabledOptions(QAbstractPrintDialog::PrintSelection);
       
   380     MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintToFile), FALSE);
       
   381     MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintSelection), TRUE);
       
   382     MYCOMPARE(dlg.isOptionEnabled(QAbstractPrintDialog::PrintPageRange), FALSE);
       
   383 }
       
   384 
       
   385 void tst_QPrinter::testMargins_data()
       
   386 {
       
   387     QTest::addColumn<int>("orientation");
       
   388     QTest::addColumn<bool>("fullpage");
       
   389     QTest::addColumn<int>("pagesize");
       
   390     QTest::addColumn<int>("width");
       
   391     QTest::addColumn<int>("height");
       
   392     QTest::addColumn<bool>("withPainter");
       
   393 
       
   394     QTest::newRow("data0") << int(QPrinter::Portrait) << true << int(QPrinter::A4) << 210 << 297 << false;
       
   395     QTest::newRow("data1") << int(QPrinter::Landscape) << true << int(QPrinter::A4) << 297 << 210 << false;
       
   396     QTest::newRow("data2") << int(QPrinter::Landscape) << false << int(QPrinter::A4) << 297 << 210 << false;
       
   397     QTest::newRow("data3") << int(QPrinter::Portrait) << false << int(QPrinter::A4) << 210 << 297 << false;
       
   398     QTest::newRow("data4") << int(QPrinter::Portrait) << true << int(QPrinter::A4) << 210 << 297 << true;
       
   399     QTest::newRow("data5") << int(QPrinter::Landscape) << true << int(QPrinter::A4) << 297 << 210 << true;
       
   400     QTest::newRow("data6") << int(QPrinter::Landscape) << false << int(QPrinter::A4) << 297 << 210 << true;
       
   401     QTest::newRow("data7") << int(QPrinter::Portrait) << false << int(QPrinter::A4) << 210 << 297 << true;
       
   402 }
       
   403 
       
   404 void tst_QPrinter::testMargins()
       
   405 {
       
   406     QFETCH(bool,  withPainter);
       
   407     QFETCH(int,  orientation);
       
   408     QFETCH(int,  pagesize);
       
   409     QFETCH(int,  width);
       
   410     QFETCH(int,  height);
       
   411     QFETCH(bool, fullpage);
       
   412     QPrinter printer;
       
   413     QPainter *painter = 0;
       
   414     printer.setOutputFileName("silly");
       
   415     printer.setOrientation((QPrinter::Orientation)orientation);
       
   416     printer.setFullPage(fullpage);
       
   417     printer.setPageSize((QPrinter::PageSize)pagesize);
       
   418     if (withPainter)
       
   419 	painter = new QPainter(&printer);
       
   420 
       
   421 #ifdef QT3_SUPPORT
       
   422     Q3PaintDeviceMetrics metrics(&printer);
       
   423     int pwidth = metrics.width();
       
   424     int pheight = metrics.height();
       
   425 
       
   426     if (orientation == QPrinter::Portrait) {
       
   427         QVERIFY(pheight >= pwidth);
       
   428     } else {
       
   429         QVERIFY(pwidth >= pheight);
       
   430     }
       
   431 
       
   432     if (fullpage) {
       
   433         QCOMPARE(metrics.widthMM(), width);
       
   434         QCOMPARE(metrics.heightMM(), height);
       
   435     }
       
   436 #endif
       
   437 
       
   438     if (painter)
       
   439         delete painter;
       
   440 }
       
   441 
       
   442 void tst_QPrinter::testNonExistentPrinter()
       
   443 {
       
   444 #if defined(Q_WS_X11) || defined(Q_WS_QWS) || defined(Q_OS_MAC)
       
   445     QSKIP("QPrinter::testNonExistentPrinter() is not relevant for X11/Embedded/Mac", SkipAll);
       
   446 #else
       
   447     QPrinter printer;
       
   448     QPainter painter;
       
   449 
       
   450     // Make sure it doesn't crash on setting or getting properties
       
   451     printer.setPrinterName("some non existing printer");
       
   452     printer.setPageSize(QPrinter::A4);
       
   453     printer.setOrientation(QPrinter::Portrait);
       
   454     printer.setFullPage(true);
       
   455     printer.pageSize();
       
   456     printer.orientation();
       
   457     printer.fullPage();
       
   458     printer.setNumCopies(1);
       
   459     printer.printerName();
       
   460 
       
   461     // nor metrics
       
   462     QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmWidth), 0);
       
   463     QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmHeight), 0);
       
   464     QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmWidthMM), 0);
       
   465     QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmHeightMM), 0);
       
   466     QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmNumColors), 0);
       
   467     QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmDepth), 0);
       
   468     QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmDpiX), 0);
       
   469     QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmDpiY), 0);
       
   470     QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmPhysicalDpiX), 0);
       
   471     QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmPhysicalDpiY), 0);
       
   472 
       
   473     QVERIFY(!painter.begin(&printer));
       
   474 #endif
       
   475 }
       
   476 
       
   477 
       
   478 void tst_QPrinter::testMulitpleSets_data()
       
   479 {
       
   480     QTest::addColumn<int>("resolution");
       
   481     QTest::addColumn<int>("pageSize");
       
   482     QTest::addColumn<int>("widthMMAfter");
       
   483     QTest::addColumn<int>("heightMMAfter");
       
   484 
       
   485 
       
   486     QTest::newRow("lowRes") << int(QPrinter::ScreenResolution) << int(QPrinter::A4) << 210 << 297;
       
   487     QTest::newRow("lowResLetter") << int(QPrinter::ScreenResolution) << int(QPrinter::Letter) << 216 << 279;
       
   488     QTest::newRow("lowResA5") << int(QPrinter::ScreenResolution) << int(QPrinter::A5) << 148 << 210;
       
   489     QTest::newRow("midRes") << int(QPrinter::PrinterResolution) << int(QPrinter::A4) << 210 << 297;
       
   490     QTest::newRow("midResLetter") << int(QPrinter::PrinterResolution) << int(QPrinter::Letter) << 216 << 279;
       
   491     QTest::newRow("midResA5") << int(QPrinter::PrinterResolution) << int(QPrinter::A5) << 148 << 210;
       
   492     QTest::newRow("highRes") << int(QPrinter::HighResolution) << int(QPrinter::A4) << 210 << 297;
       
   493     QTest::newRow("highResLetter") << int(QPrinter::HighResolution) << int(QPrinter::Letter) << 216 << 279;
       
   494     QTest::newRow("highResA5") << int(QPrinter::HighResolution) << int(QPrinter::A5) << 148 << 210;
       
   495 }
       
   496 
       
   497 static void computePageValue(const QPrinter &printer, int &retWidth, int &retHeight)
       
   498 {
       
   499     const double Inch2MM = 25.4;
       
   500 
       
   501     double width = double(printer.paperRect().width()) / printer.logicalDpiX() * Inch2MM;
       
   502     double height = double(printer.paperRect().height()) / printer.logicalDpiY() * Inch2MM;
       
   503     retWidth = qRound(width);
       
   504     retHeight = qRound(height);
       
   505 }
       
   506 
       
   507 void tst_QPrinter::testMulitpleSets()
       
   508 {
       
   509     // A very simple test, but Mac needs to have its format "validated" if the format is changed
       
   510     // This takes care of that.
       
   511     QFETCH(int, resolution);
       
   512     QFETCH(int, pageSize);
       
   513     QFETCH(int, widthMMAfter);
       
   514     QFETCH(int, heightMMAfter);
       
   515 
       
   516 
       
   517     QPrinter::PrinterMode mode = QPrinter::PrinterMode(resolution);
       
   518     QPrinter::PageSize printerPageSize = QPrinter::PageSize(pageSize);
       
   519     QPrinter printer(mode);
       
   520     printer.setFullPage(true);
       
   521 
       
   522     int paperWidth, paperHeight;
       
   523     //const int Tolerance = 2;
       
   524 
       
   525     computePageValue(printer, paperWidth, paperHeight);
       
   526     printer.setPageSize(printerPageSize);
       
   527 
       
   528     if (printer.pageSize() != printerPageSize) {
       
   529         QSKIP("Current page size is not supported on this printer", SkipSingle);
       
   530         return;
       
   531     }
       
   532 
       
   533     QCOMPARE(printer.widthMM(), widthMMAfter);
       
   534     QCOMPARE(printer.heightMM(), heightMMAfter);
       
   535 
       
   536     computePageValue(printer, paperWidth, paperHeight);
       
   537 
       
   538     QVERIFY(qAbs(paperWidth - widthMMAfter) <= 2);
       
   539     QVERIFY(qAbs(paperHeight - heightMMAfter) <= 2);
       
   540 
       
   541     // Set it again and see if it still works.
       
   542     printer.setPageSize(printerPageSize);
       
   543     QCOMPARE(printer.widthMM(), widthMMAfter);
       
   544     QCOMPARE(printer.heightMM(), heightMMAfter);
       
   545 
       
   546     printer.setOrientation(QPrinter::Landscape);
       
   547     computePageValue(printer, paperWidth, paperHeight);
       
   548     QVERIFY(qAbs(paperWidth - heightMMAfter) <= 2);
       
   549     QVERIFY(qAbs(paperHeight - widthMMAfter) <= 2);
       
   550 }
       
   551 
       
   552 void tst_QPrinter::changingOutputFormat()
       
   553 {
       
   554     QPrinter p;
       
   555     p.setOutputFormat(QPrinter::PostScriptFormat);
       
   556     p.setPageSize(QPrinter::A8);
       
   557     p.setOutputFormat(QPrinter::PdfFormat);
       
   558     QCOMPARE(p.pageSize(), QPrinter::A8);
       
   559 }
       
   560 
       
   561 void tst_QPrinter::outputFormatFromSuffix()
       
   562 {
       
   563     if (QPrinterInfo::availablePrinters().size() == 0)
       
   564         QSKIP("No printers available.", SkipAll);
       
   565     QPrinter p;
       
   566     QVERIFY(p.outputFormat() == QPrinter::NativeFormat);
       
   567     p.setOutputFileName("test.ps");
       
   568     QVERIFY(p.outputFormat() == QPrinter::PostScriptFormat);
       
   569     p.setOutputFileName("test.pdf");
       
   570     QVERIFY(p.outputFormat() == QPrinter::PdfFormat);
       
   571     p.setOutputFileName(QString());
       
   572     QVERIFY(p.outputFormat() == QPrinter::NativeFormat);
       
   573 }
       
   574 
       
   575 void tst_QPrinter::setGetPaperSize()
       
   576 {
       
   577     QPrinter p;
       
   578     p.setOutputFormat(QPrinter::PdfFormat);
       
   579     QSizeF size(500, 10);
       
   580     p.setPaperSize(size, QPrinter::Millimeter);
       
   581     QCOMPARE(p.paperSize(QPrinter::Millimeter), size);
       
   582     QSizeF ptSize = p.paperSize(QPrinter::Point);
       
   583     //qDebug() << ptSize;
       
   584     QVERIFY(qAbs(ptSize.width() - size.width() * (72/25.4)) < 1E-4);
       
   585     QVERIFY(qAbs(ptSize.height() - size.height() * (72/25.4)) < 1E-4);
       
   586 }
       
   587 
       
   588 void tst_QPrinter::testPageMargins_data()
       
   589 {
       
   590     QTest::addColumn<qreal>("left");
       
   591     QTest::addColumn<qreal>("top");
       
   592     QTest::addColumn<qreal>("right");
       
   593     QTest::addColumn<qreal>("bottom");
       
   594     QTest::addColumn<int>("unit");
       
   595 
       
   596     QTest::newRow("data0") << 5.5 << 6.5 << 7.5 << 8.5 << static_cast<int>(QPrinter::Millimeter);
       
   597     QTest::newRow("data1") << 5.5 << 6.5 << 7.5 << 8.5 << static_cast<int>(QPrinter::Point);
       
   598     QTest::newRow("data2") << 5.5 << 6.5 << 7.5 << 8.5 << static_cast<int>(QPrinter::Inch);
       
   599     QTest::newRow("data3") << 5.5 << 6.5 << 7.5 << 8.5 << static_cast<int>(QPrinter::Pica);
       
   600     QTest::newRow("data4") << 5.5 << 6.5 << 7.5 << 8.5 << static_cast<int>(QPrinter::Didot);
       
   601     QTest::newRow("data5") << 5.5 << 6.5 << 7.5 << 8.5 << static_cast<int>(QPrinter::Cicero);
       
   602 }
       
   603 
       
   604 void tst_QPrinter::testPageMargins()
       
   605 {
       
   606     QPrinter obj1;
       
   607 
       
   608     qreal toMillimeters[6];
       
   609     toMillimeters[QPrinter::Millimeter] = 1;
       
   610     toMillimeters[QPrinter::Point] = 0.352777778;
       
   611     toMillimeters[QPrinter::Inch] = 25.4;
       
   612     toMillimeters[QPrinter::Pica] = 4.23333333;
       
   613     toMillimeters[QPrinter::Didot] = 0.376;
       
   614     toMillimeters[QPrinter::Cicero] = 4.51166667;
       
   615 
       
   616     QFETCH(qreal, left);
       
   617     QFETCH(qreal, top);
       
   618     QFETCH(qreal, right);
       
   619     QFETCH(qreal, bottom);
       
   620     QFETCH(int, unit);
       
   621 
       
   622     qreal nLeft, nTop, nRight, nBottom;
       
   623 
       
   624     obj1.setPageMargins(left, top, right, bottom, static_cast<QPrinter::Unit>(unit));
       
   625 
       
   626     qreal tolerance = 0.05;
       
   627 
       
   628     obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Millimeter);
       
   629     QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Millimeter]) < tolerance);
       
   630     QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Millimeter]) < tolerance);
       
   631     QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Millimeter]) < tolerance);
       
   632     QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Millimeter]) < tolerance);
       
   633 
       
   634     obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Point);
       
   635     QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Point]) < tolerance);
       
   636     QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Point]) < tolerance);
       
   637     QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Point]) < tolerance);
       
   638     QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Point]) < tolerance);
       
   639 
       
   640     obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Inch);
       
   641     QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Inch]) < tolerance);
       
   642     QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Inch]) < tolerance);
       
   643     QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Inch]) < tolerance);
       
   644     QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Inch]) < tolerance);
       
   645 
       
   646     obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Pica);
       
   647     QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Pica]) < tolerance);
       
   648     QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Pica]) < tolerance);
       
   649     QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Pica]) < tolerance);
       
   650     QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Pica]) < tolerance);
       
   651 
       
   652     obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Didot);
       
   653     QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Didot]) < tolerance);
       
   654     QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Didot]) < tolerance);
       
   655     QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Didot]) < tolerance);
       
   656     QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Didot]) < tolerance);
       
   657 
       
   658     obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Cicero);
       
   659     QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Cicero]) < tolerance);
       
   660     QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Cicero]) < tolerance);
       
   661     QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Cicero]) < tolerance);
       
   662     QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Cicero]) < tolerance);
       
   663 }
       
   664 
       
   665 void tst_QPrinter::valuePreservation()
       
   666 {
       
   667     QPrinter::OutputFormat oldFormat = QPrinter::PdfFormat;
       
   668     QPrinter::OutputFormat newFormat = QPrinter::PostScriptFormat;
       
   669 
       
   670     {
       
   671         QPrinter printer;
       
   672         printer.setOutputFormat(oldFormat);
       
   673         bool status = printer.collateCopies();
       
   674         printer.setOutputFormat(newFormat);
       
   675         printer.setOutputFormat(oldFormat);
       
   676         QCOMPARE(printer.collateCopies(), status);
       
   677 
       
   678         printer.setCollateCopies(!status);
       
   679         printer.setOutputFormat(newFormat);
       
   680         QCOMPARE(printer.collateCopies(), !status);
       
   681         printer.setOutputFormat(oldFormat);
       
   682         QCOMPARE(printer.collateCopies(), !status);
       
   683     }
       
   684     {
       
   685         QPrinter printer;
       
   686         printer.setOutputFormat(oldFormat);
       
   687         QPrinter::ColorMode status = printer.colorMode();
       
   688         printer.setOutputFormat(newFormat);
       
   689         printer.setOutputFormat(oldFormat);
       
   690         QCOMPARE(printer.colorMode(), status);
       
   691 
       
   692         printer.setColorMode(QPrinter::ColorMode(!status));
       
   693         printer.setOutputFormat(newFormat);
       
   694         QCOMPARE(printer.colorMode(), QPrinter::ColorMode(!status));
       
   695         printer.setOutputFormat(oldFormat);
       
   696         QCOMPARE(printer.colorMode(), QPrinter::ColorMode(!status));
       
   697     }
       
   698     {
       
   699         QPrinter printer;
       
   700         printer.setOutputFormat(oldFormat);
       
   701         QString status = printer.creator();
       
   702         printer.setOutputFormat(newFormat);
       
   703         printer.setOutputFormat(oldFormat);
       
   704         QCOMPARE(printer.creator(), status);
       
   705 
       
   706         status = QString::fromLatin1("Mr. Test");
       
   707         printer.setCreator(status);
       
   708         printer.setOutputFormat(newFormat);
       
   709         QCOMPARE(printer.creator(), status);
       
   710         printer.setOutputFormat(oldFormat);
       
   711         QCOMPARE(printer.creator(), status);
       
   712     }
       
   713     {
       
   714         QPrinter printer;
       
   715         printer.setOutputFormat(oldFormat);
       
   716         QString status = printer.docName();
       
   717         printer.setOutputFormat(newFormat);
       
   718         printer.setOutputFormat(oldFormat);
       
   719         QCOMPARE(printer.docName(), status);
       
   720 
       
   721         status = QString::fromLatin1("Test document");
       
   722         printer.setDocName(status);
       
   723         printer.setOutputFormat(newFormat);
       
   724         QCOMPARE(printer.docName(), status);
       
   725         printer.setOutputFormat(oldFormat);
       
   726         QCOMPARE(printer.docName(), status);
       
   727     }
       
   728     {
       
   729         QPrinter printer;
       
   730         printer.setOutputFormat(oldFormat);
       
   731         bool status = printer.doubleSidedPrinting();
       
   732         printer.setOutputFormat(newFormat);
       
   733         printer.setOutputFormat(oldFormat);
       
   734         QCOMPARE(printer.doubleSidedPrinting(), status);
       
   735 
       
   736         printer.setDoubleSidedPrinting(!status);
       
   737         printer.setOutputFormat(newFormat);
       
   738         QCOMPARE(printer.doubleSidedPrinting(), !status);
       
   739         printer.setOutputFormat(oldFormat);
       
   740         QCOMPARE(printer.doubleSidedPrinting(), !status);
       
   741     }
       
   742     {
       
   743         QPrinter printer;
       
   744         printer.setOutputFormat(oldFormat);
       
   745         bool status = printer.fontEmbeddingEnabled();
       
   746         printer.setOutputFormat(newFormat);
       
   747         printer.setOutputFormat(oldFormat);
       
   748         QCOMPARE(printer.fontEmbeddingEnabled(), status);
       
   749 
       
   750         printer.setFontEmbeddingEnabled(!status);
       
   751         printer.setOutputFormat(newFormat);
       
   752         QCOMPARE(printer.fontEmbeddingEnabled(), !status);
       
   753         printer.setOutputFormat(oldFormat);
       
   754         QCOMPARE(printer.fontEmbeddingEnabled(), !status);
       
   755     }
       
   756     {
       
   757         QPrinter printer;
       
   758         printer.setOutputFormat(oldFormat);
       
   759         bool status = printer.fullPage();
       
   760         printer.setOutputFormat(newFormat);
       
   761         printer.setOutputFormat(oldFormat);
       
   762         QCOMPARE(printer.fullPage(), status);
       
   763 
       
   764         printer.setFullPage(!status);
       
   765         printer.setOutputFormat(newFormat);
       
   766         QCOMPARE(printer.fullPage(), !status);
       
   767         printer.setOutputFormat(oldFormat);
       
   768         QCOMPARE(printer.fullPage(), !status);
       
   769     }
       
   770     {
       
   771         QPrinter printer;
       
   772         printer.setOutputFormat(oldFormat);
       
   773         QPrinter::Orientation status = printer.orientation();
       
   774         printer.setOutputFormat(newFormat);
       
   775         printer.setOutputFormat(oldFormat);
       
   776         QCOMPARE(printer.orientation(), status);
       
   777 
       
   778         printer.setOrientation(QPrinter::Orientation(!status));
       
   779         printer.setOutputFormat(newFormat);
       
   780         QCOMPARE(printer.orientation(), QPrinter::Orientation(!status));
       
   781         printer.setOutputFormat(oldFormat);
       
   782         QCOMPARE(printer.orientation(), QPrinter::Orientation(!status));
       
   783     }
       
   784     {
       
   785         QPrinter printer;
       
   786         printer.setOutputFormat(oldFormat);
       
   787         QString status = printer.outputFileName();
       
   788         printer.setOutputFormat(newFormat);
       
   789         printer.setOutputFormat(oldFormat);
       
   790         QCOMPARE(printer.outputFileName(), status);
       
   791 
       
   792         status = QString::fromLatin1("Test file");
       
   793         printer.setOutputFileName(status);
       
   794         printer.setOutputFormat(newFormat);
       
   795         QCOMPARE(printer.outputFileName(), status);
       
   796         printer.setOutputFormat(oldFormat);
       
   797         QCOMPARE(printer.outputFileName(), status);
       
   798     }
       
   799     {
       
   800         QPrinter printer;
       
   801         printer.setOutputFormat(oldFormat);
       
   802         QPrinter::PageOrder status = printer.pageOrder();
       
   803         printer.setOutputFormat(newFormat);
       
   804         printer.setOutputFormat(oldFormat);
       
   805         QCOMPARE(printer.pageOrder(), status);
       
   806 
       
   807         printer.setPageOrder(QPrinter::PageOrder(!status));
       
   808         printer.setOutputFormat(newFormat);
       
   809         QCOMPARE(printer.pageOrder(), QPrinter::PageOrder(!status));
       
   810         printer.setOutputFormat(oldFormat);
       
   811         QCOMPARE(printer.pageOrder(), QPrinter::PageOrder(!status));
       
   812     }
       
   813     {
       
   814         QPrinter printer;
       
   815         printer.setOutputFormat(oldFormat);
       
   816         QPrinter::PageSize status = printer.pageSize();
       
   817         printer.setOutputFormat(newFormat);
       
   818         printer.setOutputFormat(oldFormat);
       
   819         QCOMPARE(printer.pageSize(), status);
       
   820 
       
   821         printer.setPageSize(QPrinter::B5);
       
   822         printer.setOutputFormat(newFormat);
       
   823         QCOMPARE(printer.pageSize(), QPrinter::B5);
       
   824         printer.setOutputFormat(oldFormat);
       
   825         QCOMPARE(printer.pageSize(), QPrinter::B5);
       
   826     }
       
   827     {
       
   828         QPrinter printer;
       
   829         printer.setOutputFormat(oldFormat);
       
   830         QPrinter::PaperSource status = printer.paperSource();
       
   831         printer.setOutputFormat(newFormat);
       
   832         printer.setOutputFormat(oldFormat);
       
   833         QCOMPARE(printer.paperSource(), status);
       
   834 
       
   835         printer.setPaperSource(QPrinter::Manual);
       
   836         printer.setOutputFormat(newFormat);
       
   837         QCOMPARE(printer.paperSource(), QPrinter::Manual);
       
   838         printer.setOutputFormat(oldFormat);
       
   839         QCOMPARE(printer.paperSource(), QPrinter::Manual);
       
   840     }
       
   841     {
       
   842         QPrinter printer;
       
   843         printer.setOutputFormat(oldFormat);
       
   844         QString status = printer.printProgram();
       
   845         printer.setOutputFormat(newFormat);
       
   846         printer.setOutputFormat(oldFormat);
       
   847         QCOMPARE(printer.printProgram(), status);
       
   848 
       
   849         status = QString::fromLatin1("/usr/local/bin/lpr");
       
   850         printer.setPrintProgram(status);
       
   851         printer.setOutputFormat(newFormat);
       
   852         QCOMPARE(printer.printProgram(), status);
       
   853         printer.setOutputFormat(oldFormat);
       
   854         QCOMPARE(printer.printProgram(), status);
       
   855     }
       
   856     {
       
   857         QPrinter printer;
       
   858         printer.setOutputFormat(oldFormat);
       
   859         QPrinter::PrintRange status = printer.printRange();
       
   860         printer.setOutputFormat(newFormat);
       
   861         printer.setOutputFormat(oldFormat);
       
   862         QCOMPARE(printer.printRange(), status);
       
   863 
       
   864         printer.setPrintRange(QPrinter::PrintRange(!status));
       
   865         printer.setOutputFormat(newFormat);
       
   866         QCOMPARE(printer.printRange(), QPrinter::PrintRange(!status));
       
   867         printer.setOutputFormat(oldFormat);
       
   868         QCOMPARE(printer.printRange(), QPrinter::PrintRange(!status));
       
   869     }
       
   870     {
       
   871         QPrinter printer;
       
   872         printer.setOutputFormat(oldFormat);
       
   873         QString status = printer.printerName();
       
   874         printer.setOutputFormat(newFormat);
       
   875         printer.setOutputFormat(oldFormat);
       
   876         QCOMPARE(printer.printerName(), status);
       
   877 
       
   878         status = QString::fromLatin1("SuperDuperPrinter");
       
   879         printer.setPrinterName(status);
       
   880         printer.setOutputFormat(newFormat);
       
   881         QCOMPARE(printer.printerName(), status);
       
   882         printer.setOutputFormat(oldFormat);
       
   883         QCOMPARE(printer.printerName(), status);
       
   884     }
       
   885 #ifndef Q_OS_WIN
       
   886     {
       
   887         QPrinter printer;
       
   888         printer.setOutputFormat(oldFormat);
       
   889         QString status = printer.printerSelectionOption();
       
   890         printer.setOutputFormat(newFormat);
       
   891         printer.setOutputFormat(oldFormat);
       
   892         QCOMPARE(printer.printerSelectionOption(), status);
       
   893 
       
   894         status = QString::fromLatin1("Optional option");
       
   895         printer.setPrinterSelectionOption(status);
       
   896         printer.setOutputFormat(newFormat);
       
   897         QCOMPARE(printer.printerSelectionOption(), status);
       
   898         printer.setOutputFormat(oldFormat);
       
   899         QCOMPARE(printer.printerSelectionOption(), status);
       
   900     }
       
   901 #endif
       
   902     {
       
   903         QPrinter printer;
       
   904         printer.setOutputFormat(oldFormat);
       
   905         int status = printer.resolution();
       
   906         printer.setOutputFormat(newFormat);
       
   907         printer.setOutputFormat(oldFormat);
       
   908         QCOMPARE(printer.resolution(), status);
       
   909 
       
   910         printer.setResolution(status-150);
       
   911         printer.setOutputFormat(newFormat);
       
   912         QCOMPARE(printer.resolution(), status-150);
       
   913         printer.setOutputFormat(oldFormat);
       
   914         QCOMPARE(printer.resolution(), status-150);
       
   915     }
       
   916 }
       
   917 
       
   918 void tst_QPrinter::errorReporting()
       
   919 {
       
   920     QPrinter p;
       
   921     p.setOutputFormat(QPrinter::PdfFormat);
       
   922     QCOMPARE(p.isValid(), true);
       
   923     QPainter painter;
       
   924 #ifndef Q_WS_WIN
       
   925     // not sure how to choose a never-writable file on windows.  But its QFile behavior anyway, so lets rely on it failing elsewhere
       
   926     p.setOutputFileName("/foobar/nonwritable.pdf");
       
   927     QCOMPARE(painter.begin(&p), false); // it should check the output file is writable
       
   928 #endif
       
   929     p.setOutputFileName("test.pdf");
       
   930     QCOMPARE(painter.begin(&p), true); // it should check the output
       
   931     QCOMPARE(p.isValid(), true);
       
   932     painter.end();
       
   933 }
       
   934 
       
   935 void tst_QPrinter::testCustomPageSizes()
       
   936 {
       
   937     QPrinter p;
       
   938 
       
   939     QSizeF customSize(8.5, 11.0);
       
   940     p.setPaperSize(customSize, QPrinter::Inch);
       
   941 
       
   942     QSizeF paperSize = p.paperSize(QPrinter::Inch);
       
   943     QCOMPARE(paperSize, customSize);
       
   944 
       
   945     QPrinter p2(QPrinter::HighResolution);
       
   946     p2.setPaperSize(customSize, QPrinter::Inch);
       
   947     paperSize = p.paperSize(QPrinter::Inch);
       
   948     QCOMPARE(paperSize, customSize);
       
   949 }
       
   950 
       
   951 void tst_QPrinter::printDialogCompleter()
       
   952 {
       
   953 #if defined(QT_NO_COMPLETER) || defined(QT_NO_FILEDIALOG)
       
   954     QSKIP("QT_NO_COMPLETER || QT_NO_FILEDIALOG: Auto-complete turned off in QPrinterDialog.", QTest::SkipAll);
       
   955 #else
       
   956     QPrintDialog dialog;
       
   957     dialog.printer()->setOutputFileName("file.pdf");
       
   958     dialog.setEnabledOptions(QAbstractPrintDialog::PrintToFile);
       
   959     dialog.show();
       
   960 
       
   961     QTest::qWait(100);
       
   962 
       
   963     QTest::keyClick(&dialog, Qt::Key_Tab);
       
   964     QTest::keyClick(&dialog, 'P');
       
   965     // The test passes if it doesn't crash.
       
   966 #endif
       
   967 }
       
   968 
       
   969 void tst_QPrinter::testActualNumCopies()
       
   970 {
       
   971     QPrinter p;
       
   972     p.setNumCopies(15);
       
   973     QCOMPARE(p.actualNumCopies(), 15);
       
   974 }
       
   975 
       
   976 static void printPage(QPainter *painter)
       
   977 {
       
   978     painter->setPen(QPen(Qt::black, 4));
       
   979     painter->drawRect(50, 60, 70, 80);
       
   980 }
       
   981 
       
   982 void tst_QPrinter::taskQTBUG4497_reusePrinterOnDifferentFiles()
       
   983 {
       
   984     QPrinter printer;
       
   985     {
       
   986 
       
   987         printer.setOutputFileName("out1.ps");
       
   988         QPainter painter(&printer);
       
   989         printPage(&painter);
       
   990 
       
   991     }
       
   992     {
       
   993 
       
   994         printer.setOutputFileName("out2.ps");
       
   995         QPainter painter(&printer);
       
   996         printPage(&painter);
       
   997 
       
   998     }
       
   999     QFile file1("out1.ps");
       
  1000     QVERIFY(file1.open(QIODevice::ReadOnly));
       
  1001 
       
  1002     QFile file2("out2.ps");
       
  1003     QVERIFY(file2.open(QIODevice::ReadOnly));
       
  1004 
       
  1005     QCOMPARE(file1.readAll(), file2.readAll());
       
  1006 }
       
  1007 
       
  1008 QTEST_MAIN(tst_QPrinter)
       
  1009 #include "tst_qprinter.moc"