tests/auto/qprinter/tst_qprinter.cpp
changeset 30 5dc02b23752f
parent 29 b72c6db6890b
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
    62 #include <windows.h>
    62 #include <windows.h>
    63 #endif
    63 #endif
    64 
    64 
    65 Q_DECLARE_METATYPE(QRect)
    65 Q_DECLARE_METATYPE(QRect)
    66 
    66 
    67 QT_FORWARD_DECLARE_CLASS(QPrinter)
    67 
    68 
    68 
    69 //TESTED_CLASS=
    69 //TESTED_CLASS=
    70 //TESTED_FILES=
    70 //TESTED_FILES=
    71 
       
    72 #ifndef QT_NO_PRINTER
       
    73 
    71 
    74 class tst_QPrinter : public QObject
    72 class tst_QPrinter : public QObject
    75 {
    73 {
    76     Q_OBJECT
    74     Q_OBJECT
    77 
    75 
   106     void valuePreservation();
   104     void valuePreservation();
   107     void errorReporting();
   105     void errorReporting();
   108     void testCustomPageSizes();
   106     void testCustomPageSizes();
   109     void printDialogCompleter();
   107     void printDialogCompleter();
   110 
   108 
   111     void testActualNumCopies();
   109     void testCopyCount();
       
   110     void testCurrentPage();
   112 
   111 
   113     void taskQTBUG4497_reusePrinterOnDifferentFiles();
   112     void taskQTBUG4497_reusePrinterOnDifferentFiles();
       
   113     void testPdfTitle();
   114 
   114 
   115 private:
   115 private:
   116 };
   116 };
   117 
   117 
   118 // Testing get/set functions
   118 // Testing get/set functions
   215 {
   215 {
   216 }
   216 }
   217 
   217 
   218 tst_QPrinter::~tst_QPrinter()
   218 tst_QPrinter::~tst_QPrinter()
   219 {
   219 {
       
   220 
   220 }
   221 }
   221 
   222 
   222 // initTestCase will be executed once before the first testfunction is executed.
   223 // initTestCase will be executed once before the first testfunction is executed.
   223 void tst_QPrinter::initTestCase()
   224 void tst_QPrinter::initTestCase()
   224 {
   225 {
   415     printer.setOutputFileName("silly");
   416     printer.setOutputFileName("silly");
   416     printer.setOrientation((QPrinter::Orientation)orientation);
   417     printer.setOrientation((QPrinter::Orientation)orientation);
   417     printer.setFullPage(fullpage);
   418     printer.setFullPage(fullpage);
   418     printer.setPageSize((QPrinter::PageSize)pagesize);
   419     printer.setPageSize((QPrinter::PageSize)pagesize);
   419     if (withPainter)
   420     if (withPainter)
   420 	painter = new QPainter(&printer);
   421         painter = new QPainter(&printer);
   421 
   422 
   422 #ifdef QT3_SUPPORT
   423 #ifdef QT3_SUPPORT
   423     Q3PaintDeviceMetrics metrics(&printer);
   424     Q3PaintDeviceMetrics metrics(&printer);
   424     int pwidth = metrics.width();
   425     int pwidth = metrics.width();
   425     int pheight = metrics.height();
   426     int pheight = metrics.height();
   454     printer.setOrientation(QPrinter::Portrait);
   455     printer.setOrientation(QPrinter::Portrait);
   455     printer.setFullPage(true);
   456     printer.setFullPage(true);
   456     printer.pageSize();
   457     printer.pageSize();
   457     printer.orientation();
   458     printer.orientation();
   458     printer.fullPage();
   459     printer.fullPage();
   459     printer.setNumCopies(1);
   460     printer.setCopyCount(1);
   460     printer.printerName();
   461     printer.printerName();
   461 
   462 
   462     // nor metrics
   463     // nor metrics
   463     QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmWidth), 0);
   464     QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmWidth), 0);
   464     QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmHeight), 0);
   465     QCOMPARE(printer.printEngine()->metric(QPaintDevice::PdmHeight), 0);
   965     QTest::keyClick(&dialog, 'P');
   966     QTest::keyClick(&dialog, 'P');
   966     // The test passes if it doesn't crash.
   967     // The test passes if it doesn't crash.
   967 #endif
   968 #endif
   968 }
   969 }
   969 
   970 
   970 void tst_QPrinter::testActualNumCopies()
   971 void tst_QPrinter::testCopyCount()
   971 {
   972 {
   972     QPrinter p;
   973     QPrinter p;
   973     p.setNumCopies(15);
   974     p.setCopyCount(15);
   974     QCOMPARE(p.actualNumCopies(), 15);
   975     QCOMPARE(p.copyCount(), 15);
   975 }
   976 }
   976 
   977 
   977 static void printPage(QPainter *painter)
   978 static void printPage(QPainter *painter)
   978 {
   979 {
   979     painter->setPen(QPen(Qt::black, 4));
   980     painter->setPen(QPen(Qt::black, 4));
  1004     QVERIFY(file2.open(QIODevice::ReadOnly));
  1005     QVERIFY(file2.open(QIODevice::ReadOnly));
  1005 
  1006 
  1006     QCOMPARE(file1.readAll(), file2.readAll());
  1007     QCOMPARE(file1.readAll(), file2.readAll());
  1007 }
  1008 }
  1008 
  1009 
       
  1010 void tst_QPrinter::testCurrentPage()
       
  1011 {
       
  1012     QPrinter printer;
       
  1013     printer.setFromTo(1, 10);
       
  1014 
       
  1015     // Test set print range
       
  1016     printer.setPrintRange(QPrinter::CurrentPage);
       
  1017     QCOMPARE(printer.printRange(), QPrinter::CurrentPage);
       
  1018     QCOMPARE(printer.fromPage(), 1);
       
  1019     QCOMPARE(printer.toPage(), 10);
       
  1020 
       
  1021     QPrintDialog dialog(&printer);
       
  1022 
       
  1023     // Test default Current Page option to off
       
  1024     QCOMPARE(dialog.isOptionEnabled(QPrintDialog::PrintCurrentPage), false);
       
  1025 
       
  1026     // Test enable Current Page option
       
  1027     dialog.setOption(QPrintDialog::PrintCurrentPage);
       
  1028     QCOMPARE(dialog.isOptionEnabled(QPrintDialog::PrintCurrentPage), true);
       
  1029 
       
  1030 }
       
  1031 
       
  1032 void tst_QPrinter::testPdfTitle()
       
  1033 {
       
  1034     // Check the document name is represented correctly in produced pdf
       
  1035     {
       
  1036         QPainter painter;
       
  1037         QPrinter printer;
       
  1038         // This string is just the UTF-8 encoding of the string: \()f &oslash; hiragana o
       
  1039         const char title[]={0x5c, 0x28, 0x29, 0x66, 0xc3, 0xb8, 0xe3, 0x81, 0x8a, 0x00};
       
  1040         printer.setOutputFileName("file.pdf");
       
  1041         printer.setDocName(QString::fromUtf8(title));
       
  1042         painter.begin(&printer);
       
  1043         painter.end();
       
  1044     }
       
  1045     QFile file("file.pdf");
       
  1046     QVERIFY(file.open(QIODevice::ReadOnly));
       
  1047     // The we expect the title to appear in the PDF as:
       
  1048     // ASCII('\title (') UTF16(\\\(\)f &oslash; hiragana o) ASCII(')').
       
  1049     // which has the following binary representation
       
  1050     const char expected[] = {
       
  1051         0x2f, 0x54, 0x69, 0x74, 0x6c, 0x65, 0x20, 0x28, 0xfe,
       
  1052         0xff, 0x00, 0x5c, 0x5c, 0x00, 0x5c, 0x28, 0x00, 0x5c,
       
  1053         0x29, 0x00, 0x66, 0x00, 0xf8, 0x30, 0x4a, 0x29};
       
  1054     QVERIFY(file.readAll().contains(QByteArray(expected, 26)));
       
  1055 }
       
  1056 
  1009 QTEST_MAIN(tst_QPrinter)
  1057 QTEST_MAIN(tst_QPrinter)
  1010 #include "tst_qprinter.moc"
  1058 #include "tst_qprinter.moc"
  1011 
       
  1012 #else //QT_NO_PRINTER
       
  1013 
       
  1014 QTEST_NOOP_MAIN
       
  1015 
       
  1016 #endif //QT_NO_PRINTER