QtPrinterInfo: Improve QPrinterInfo Testing

Much of the current QPrinterInfo tests fail due to being dependent on
specific physical or network printers being attached.  This change
removes all printer specific tests and replaces them with generic
tests that will use whatever printers are installed.

Note if no printers are installed then the tests will still pass. A
later change will add virtual printers to test returned results are
correct.

Windows test code is also required and will come later.

This does not yet remove the "insignificant" status from the test,
further improvements and code fixes are still required.

Change-Id: I60802445924edb126aadf78337a8cb6f2f3b3d37
Reviewed-by: John Layt <jlayt@kde.org>
This commit is contained in:
John Layt 2012-05-11 21:58:15 +01:00 committed by Qt by Nokia
parent c8028844ee
commit 3dffbe8c81
1 changed files with 87 additions and 162 deletions

View File

@ -43,8 +43,7 @@
#include <QtTest/QtTest> #include <QtTest/QtTest>
#include <QtGlobal> #include <QtGlobal>
#include <QtAlgorithms> #include <QtAlgorithms>
#include <QtNetwork/QHostInfo> #include <QtPrintSupport/qprinterinfo.h>
#include <qprinterinfo.h>
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
# include <unistd.h> # include <unistd.h>
@ -58,10 +57,9 @@ class tst_QPrinterInfo : public QObject
{ {
Q_OBJECT Q_OBJECT
#ifdef QT_NO_PRINTER
public slots: public slots:
void initTestCase(); void initTestCase();
#else #ifndef QT_NO_PRINTER
private slots: private slots:
void testForDefaultPrinter(); void testForDefaultPrinter();
void testForPrinters(); void testForPrinters();
@ -74,20 +72,28 @@ private:
QString getDefaultPrinterFromSystem(); QString getDefaultPrinterFromSystem();
QStringList getPrintersFromSystem(); QStringList getPrintersFromSystem();
#ifdef Q_OS_UNIX
QString getOutputFromCommand(const QStringList& command); QString getOutputFromCommand(const QStringList& command);
#endif #endif // Q_OS_UNIX
#endif // QT_NO_PRINTER
}; };
#ifdef QT_NO_PRINTER
void tst_QPrinterInfo::initTestCase() void tst_QPrinterInfo::initTestCase()
{ {
#ifdef QT_NO_PRINTER
QSKIP("This test requires printing support"); QSKIP("This test requires printing support");
#endif // QT_NO_PRINTER
} }
#else #ifndef QT_NO_PRINTER
QString tst_QPrinterInfo::getDefaultPrinterFromSystem() QString tst_QPrinterInfo::getDefaultPrinterFromSystem()
{ {
QString printer;
#ifdef Q_OS_WIN32
// TODO "cscript c:\windows\system32\prnmngr.vbs -g"
#endif // Q_OS_WIN32
#ifdef Q_OS_UNIX
QStringList command; QStringList command;
command << "lpstat" << "-d"; command << "lpstat" << "-d";
QString output = getOutputFromCommand(command); QString output = getOutputFromCommand(command);
@ -100,7 +106,9 @@ QString tst_QPrinterInfo::getDefaultPrinterFromSystem()
QRegExp defaultReg("default.*: *([a-zA-Z0-9_-]+)"); QRegExp defaultReg("default.*: *([a-zA-Z0-9_-]+)");
defaultReg.indexIn(output); defaultReg.indexIn(output);
QString printer = defaultReg.cap(1); printer = defaultReg.cap(1);
#endif // Q_OS_UNIX
return printer; return printer;
} }
@ -108,6 +116,10 @@ QStringList tst_QPrinterInfo::getPrintersFromSystem()
{ {
QStringList ans; QStringList ans;
#ifdef Q_OS_WIN32
// TODO "cscript c:\windows\system32\prnmngr.vbs -l"
#endif // Q_OS_WIN32
#ifdef Q_OS_UNIX
QStringList command; QStringList command;
command << "lpstat" << "-p"; command << "lpstat" << "-p";
QString output = getOutputFromCommand(command); QString output = getOutputFromCommand(command);
@ -120,16 +132,17 @@ QStringList tst_QPrinterInfo::getPrintersFromSystem()
ans << printer; ans << printer;
} }
} }
#endif // Q_OS_UNIX
return ans; return ans;
} }
#ifdef Q_OS_UNIX
// This function does roughly the same as the `command substitution` in // This function does roughly the same as the `command substitution` in
// the shell. // the shell.
QString tst_QPrinterInfo::getOutputFromCommand(const QStringList& command) QString tst_QPrinterInfo::getOutputFromCommand(const QStringList& command)
{ {
// The command execution does nothing on non-unix systems. // The command execution does nothing on non-unix systems.
#ifdef Q_OS_UNIX
int pid; int pid;
int status = 0; int status = 0;
int pipePtr[2]; int pipePtr[2];
@ -184,193 +197,106 @@ QString tst_QPrinterInfo::getOutputFromCommand(const QStringList& command)
wait(&status); wait(&status);
return QString(array); return QString(array);
} }
#else
Q_UNUSED(command)
return QString();
#endif
} }
#endif
void tst_QPrinterInfo::testForDefaultPrinter() void tst_QPrinterInfo::testForDefaultPrinter()
{ {
#if defined(Q_OS_UNIX) || defined(Q_OS_WIN32) #ifdef Q_OS_WIN32
# ifdef Q_OS_WIN32 QSKIP("Windows test support not yet implemented");
if (QHostInfo::localHostName() == "fantomet" || QHostInfo::localHostName() == "bobo") { #endif // Q_OS_WIN32
QWARN("Test is hardcoded to \"fantomet\" and \"bobo\" on Windows and may fail"); QString testPrinter = getDefaultPrinterFromSystem();
} else { QString defaultPrinter = QPrinterInfo::defaultPrinter().printerName();
QSKIP("Test is hardcoded to \"fantomet\" and \"bobo\" on Windows"); QString availablePrinter;
} int availablePrinterDefaults = 0;
QString defSysPrinter;
if (QHostInfo::localHostName() == "fantomet") {
defSysPrinter = "Yacc (Lexmark Optra T610 PS3)";
} else if (QHostInfo::localHostName() == "bobo") {
defSysPrinter = "press";
}
# else
QString defSysPrinter = getDefaultPrinterFromSystem();
# endif
if (defSysPrinter == "")
QSKIP("No default printer available");
QList<QPrinterInfo> list = QPrinterInfo::availablePrinters(); QList<QPrinterInfo> list = QPrinterInfo::availablePrinters();
bool found = false;
for (int c = 0; c < list.size(); ++c) { for (int c = 0; c < list.size(); ++c) {
if (list[c].isDefault()) { if (list[c].isDefault()) {
QVERIFY(list.at(c).printerName() == defSysPrinter); availablePrinter = list.at(c).printerName();
QVERIFY(!list.at(c).isNull()); ++availablePrinterDefaults;
found = true;
} else {
QVERIFY(list.at(c).printerName() != defSysPrinter);
QVERIFY(!list.at(c).isNull());
} }
} }
if (!found && defSysPrinter != "") QFAIL("No default printer reported by Qt, although there is one"); qDebug() << "Test believes Default Printer = " << testPrinter;
#else qDebug() << "QPrinterInfo::defaultPrinter() believes Default Printer = " << defaultPrinter;
QSKIP("Test doesn't work on non-Unix"); qDebug() << "QPrinterInfo::availablePrinters() believes Default Printer = " << availablePrinter;
#endif
QCOMPARE(testPrinter, defaultPrinter);
QCOMPARE(testPrinter, availablePrinter);
if (!availablePrinter.isEmpty())
QCOMPARE(availablePrinterDefaults, 1);
} }
void tst_QPrinterInfo::testForPrinters() void tst_QPrinterInfo::testForPrinters()
{ {
#if defined(Q_OS_UNIX) || defined(Q_OS_WIN32) #ifdef Q_OS_WIN32
# ifdef Q_OS_WIN32 QSKIP("Windows test support not yet implemented");
if (QHostInfo::localHostName() == "fantomet" || QHostInfo::localHostName() == "bobo") { #endif // Q_OS_WIN32
QWARN("Test is hardcoded to \"fantomet\" and \"bobo\" on Windows and may fail"); QStringList testPrinters = getPrintersFromSystem();
} else {
QSKIP("Test is hardcoded to \"fantomet\" and \"bobo\" on Windows");
}
QStringList sysPrinters;
if (QHostInfo::localHostName() == "fantomet") {
sysPrinters
<< "Press"
<< "Canon PS-IPU Color Laser Copier v52.3"
<< "EPSON EPL-N4000 PS3"
<< "Kroksleiven"
<< "Lexmark Optra Color 1200 PS"
<< "Yacc (Lexmark Optra T610 PCL)"
<< "Yacc (Lexmark Optra T610 PS3)"
;
} else if (QHostInfo::localHostName() == "bobo") {
sysPrinters
<< "press"
<< "finnmarka"
<< "nordmarka"
;
}
# else
QStringList sysPrinters = getPrintersFromSystem();
# endif
QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters(); QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
QStringList qtPrinters;
for (int i = 0; i < printers.size(); ++i)
qtPrinters.append(printers.at(i).printerName());
// QCOMPARE(printers.size(), sysPrinters.size()); qSort(testPrinters);
qSort(qtPrinters);
QHash<QString, bool> qtPrinters; qDebug() << "Test believes Available Printers = " << testPrinters;
qDebug() << "QPrinterInfo::availablePrinters() believes Available Printers = " << qtPrinters;
for (int j = 0; j < printers.size(); ++j) { QCOMPARE(qtPrinters.size(), testPrinters.size());
qtPrinters.insert(printers.at(j).printerName(), !printers.at(j).isNull());
}
for (int i = 0; i < sysPrinters.size(); ++i) { for (int i = 0; i < testPrinters.size(); ++i)
if (!qtPrinters.value(sysPrinters.at(i))) { QCOMPARE(qtPrinters.at(i), testPrinters.at(i));
qDebug() << "Available printers: " << qtPrinters;
QFAIL(qPrintable(QString("Printer '%1' reported by system, but not reported by Qt").arg(sysPrinters.at(i))));
}
}
#else
QSKIP("Test doesn't work on non-Unix");
#endif
} }
void tst_QPrinterInfo::testForPaperSizes() void tst_QPrinterInfo::testForPaperSizes()
{ {
QSKIP("PaperSize feature doesn't work on Windows, fails on Mac, and is unstable on Linux"); // TODO Old PaperSize test dependent on physical printer installed, new generic test required
// This test is based on common printers found at the Oslo // In the meantime just exercise the code path and print-out for inspection.
// office. It is likely to be skipped or fail for other locations.
QStringList hardPrinters;
hardPrinters << "Finnmarka" << "Huldra";
QList<QList<QPrinter::PaperSize> > hardSizes;
hardSizes
<< QList<QPrinter::PaperSize>()
<< QList<QPrinter::PaperSize>()
;
hardSizes[0] // Finnmarka
<< QPrinter::Letter
<< QPrinter::A4
<< QPrinter::A3
<< QPrinter::A5
<< QPrinter::B4
<< QPrinter::B5
<< QPrinter::Custom // COM10
<< QPrinter::Custom // C5
<< QPrinter::Custom // DL
<< QPrinter::Custom // Monarch
<< QPrinter::Executive
<< QPrinter::Custom // Foolscap
<< QPrinter::Custom // ISO B5
<< QPrinter::Ledger
<< QPrinter::Legal
<< QPrinter::Custom // Japanese Post Card
<< QPrinter::Custom // Invoice
;
hardSizes[1] // Huldra
<< QPrinter::Custom // Not listed at http://localhost:631/, name "Custom"
<< QPrinter::Letter
<< QPrinter::A4
<< QPrinter::A5
<< QPrinter::A6
<< QPrinter::B5
<< QPrinter::Custom // #5 1/2 Envelope
<< QPrinter::Custom // 6x9 Envelope
<< QPrinter::Custom // #10 Envelope
<< QPrinter::Custom // A7 Envelope
<< QPrinter::Custom // C5 Envelope
<< QPrinter::Custom // DL Envelope
<< QPrinter::Custom // Monarch Envelope
<< QPrinter::Custom // #6 3/4 Envelope
<< QPrinter::Executive
<< QPrinter::Custom // US Folio
<< QPrinter::Custom // Index Card
<< QPrinter::Custom // ISO B5
<< QPrinter::Legal
<< QPrinter::Custom // Statement
;
QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters(); QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
for (int i = 0; i < printers.size(); ++i) { for (int i = 0; i < printers.size(); ++i)
for (int j = 0; j < hardPrinters.size(); ++j) { qDebug() << "Printer: " << printers.at(i).printerName() << " Paper Sizes: " << printers.at(i).supportedPaperSizes();
if (printers[i].printerName() == hardPrinters[j]) {
QList<QPrinter::PaperSize> sizes = printers[i].supportedPaperSizes();
qSort(sizes);
qSort(hardSizes[j]);
QCOMPARE(sizes, hardSizes[j]);
}
}
}
} }
void tst_QPrinterInfo::testConstructors() void tst_QPrinterInfo::testConstructors()
{ {
QList<QPrinterInfo> prns(QPrinterInfo::availablePrinters()); QPrinterInfo null;
QCOMPARE(null.printerName(), QString());
QVERIFY(null.isNull());
for (int c = 0; c < prns.size(); ++c) { QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
QList<QPrinter::PaperSize> list1, list2;
list1 = prns[c].supportedPaperSizes(); for (int i = 0; i < printers.size(); ++i) {
QPrinter pr(prns[c]); QPrinterInfo copy1(printers.at(i));
list2 = QPrinterInfo(pr).supportedPaperSizes(); QCOMPARE(copy1.printerName(), printers.at(i).printerName());
QCOMPARE(list2, list1); QCOMPARE(copy1.isNull(), printers.at(i).isNull());
QCOMPARE(copy1.isDefault(), printers.at(i).isDefault());
QCOMPARE(copy1.supportedPaperSizes(), printers.at(i).supportedPaperSizes());
QPrinter printer(printers.at(i));
QPrinterInfo copy2(printer);
QCOMPARE(copy2.printerName(), printers.at(i).printerName());
QCOMPARE(copy2.isNull(), printers.at(i).isNull());
QCOMPARE(copy2.isDefault(), printers.at(i).isDefault());
QCOMPARE(copy2.supportedPaperSizes(), printers.at(i).supportedPaperSizes());
} }
} }
void tst_QPrinterInfo::testAssignment() void tst_QPrinterInfo::testAssignment()
{ {
QList<QPrinterInfo> prns(QPrinterInfo::availablePrinters()); QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
for (int c = 0; c < prns.size(); ++c) { for (int i = 0; i < printers.size(); ++i) {
QPrinterInfo pi = QPrinterInfo::defaultPrinter(); QPrinterInfo copy;
pi = prns[c]; copy = printers.at(i);
QCOMPARE(pi.printerName(), prns[c].printerName()); QCOMPARE(copy.printerName(), printers.at(i).printerName());
QCOMPARE(pi.supportedPaperSizes(), prns[c].supportedPaperSizes()); QCOMPARE(copy.isNull(), printers.at(i).isNull());
QCOMPARE(copy.isDefault(), printers.at(i).isDefault());
QCOMPARE(copy.supportedPaperSizes(), printers.at(i).supportedPaperSizes());
} }
} }
@ -391,8 +317,7 @@ void tst_QPrinterInfo::namedPrinter()
} }
} }
#endif // QT_NO_PRINTER
#endif
QTEST_MAIN(tst_QPrinterInfo) QTEST_MAIN(tst_QPrinterInfo)
#include "tst_qprinterinfo.moc" #include "tst_qprinterinfo.moc"