2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2013-01-02 11:13:29 +00:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-09-19 12:28:29 +00:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the test suite of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
2012-09-19 12:28:29 +00:00
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2011-04-27 10:05:43 +00:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
2011-05-24 09:34:08 +00:00
|
|
|
** GNU General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3.0 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU General Public License version 3.0 requirements will be
|
|
|
|
** met: http://www.gnu.org/copyleft/gpl.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
2012-01-24 06:17:24 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <QtTest/QtTest>
|
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QtAlgorithms>
|
2012-05-11 20:58:15 +00:00
|
|
|
#include <QtPrintSupport/qprinterinfo.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/wait.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
class tst_QPrinterInfo : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
2012-09-07 12:59:15 +00:00
|
|
|
public slots:
|
|
|
|
#ifdef QT_NO_PRINTER
|
|
|
|
void initTestCase();
|
|
|
|
void cleanupTestCase();
|
|
|
|
#else
|
2011-04-27 10:05:43 +00:00
|
|
|
private slots:
|
2012-09-07 12:59:15 +00:00
|
|
|
#ifndef Q_OS_WIN32
|
2011-04-27 10:05:43 +00:00
|
|
|
void testForDefaultPrinter();
|
|
|
|
void testForPrinters();
|
2012-09-07 12:59:15 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
void testForPaperSizes();
|
|
|
|
void testConstructors();
|
|
|
|
void testAssignment();
|
2012-05-14 19:17:16 +00:00
|
|
|
void namedPrinter();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
QString getDefaultPrinterFromSystem();
|
|
|
|
QStringList getPrintersFromSystem();
|
|
|
|
|
2012-05-11 20:58:15 +00:00
|
|
|
#ifdef Q_OS_UNIX
|
2011-04-27 10:05:43 +00:00
|
|
|
QString getOutputFromCommand(const QStringList& command);
|
2012-05-11 20:58:15 +00:00
|
|
|
#endif // Q_OS_UNIX
|
2012-09-07 12:59:15 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
2012-09-07 12:59:15 +00:00
|
|
|
|
|
|
|
#ifdef QT_NO_PRINTER
|
|
|
|
void tst_QPrinterInfo::initTestCase()
|
|
|
|
{
|
|
|
|
QSKIP("This test requires printing support");
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QPrinterInfo::cleanupTestCase()
|
|
|
|
{
|
|
|
|
QSKIP("This test requires printing support");
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2011-04-27 10:05:43 +00:00
|
|
|
QString tst_QPrinterInfo::getDefaultPrinterFromSystem()
|
|
|
|
{
|
2012-05-11 20:58:15 +00:00
|
|
|
QString printer;
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
// TODO "cscript c:\windows\system32\prnmngr.vbs -g"
|
|
|
|
#endif // Q_OS_WIN32
|
|
|
|
#ifdef Q_OS_UNIX
|
2011-04-27 10:05:43 +00:00
|
|
|
QStringList command;
|
|
|
|
command << "lpstat" << "-d";
|
|
|
|
QString output = getOutputFromCommand(command);
|
|
|
|
|
|
|
|
QRegExp noDefaultReg("[^:]*no .*default");
|
|
|
|
int pos = noDefaultReg.indexIn(output);
|
|
|
|
if (pos >= 0) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2012-02-21 02:52:15 +00:00
|
|
|
QRegExp defaultReg("default.*: *([a-zA-Z0-9_-]+)");
|
2011-04-27 10:05:43 +00:00
|
|
|
defaultReg.indexIn(output);
|
2012-05-11 20:58:15 +00:00
|
|
|
printer = defaultReg.cap(1);
|
|
|
|
#endif // Q_OS_UNIX
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
return printer;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList tst_QPrinterInfo::getPrintersFromSystem()
|
|
|
|
{
|
|
|
|
QStringList ans;
|
|
|
|
|
2012-05-11 20:58:15 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
// TODO "cscript c:\windows\system32\prnmngr.vbs -l"
|
|
|
|
#endif // Q_OS_WIN32
|
|
|
|
#ifdef Q_OS_UNIX
|
2011-04-27 10:05:43 +00:00
|
|
|
QStringList command;
|
|
|
|
command << "lpstat" << "-p";
|
|
|
|
QString output = getOutputFromCommand(command);
|
|
|
|
QStringList list = output.split(QChar::fromLatin1('\n'));
|
|
|
|
|
|
|
|
QRegExp reg("^[Pp]rinter ([.a-zA-Z0-9-_@]+)");
|
|
|
|
for (int c = 0; c < list.size(); ++c) {
|
|
|
|
if (reg.indexIn(list[c]) >= 0) {
|
|
|
|
QString printer = reg.cap(1);
|
|
|
|
ans << printer;
|
|
|
|
}
|
|
|
|
}
|
2012-05-11 20:58:15 +00:00
|
|
|
#endif // Q_OS_UNIX
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
return ans;
|
|
|
|
}
|
|
|
|
|
2012-05-11 20:58:15 +00:00
|
|
|
#ifdef Q_OS_UNIX
|
2011-04-27 10:05:43 +00:00
|
|
|
// This function does roughly the same as the `command substitution` in
|
|
|
|
// the shell.
|
|
|
|
QString tst_QPrinterInfo::getOutputFromCommand(const QStringList& command)
|
|
|
|
{
|
|
|
|
// The command execution does nothing on non-unix systems.
|
|
|
|
int pid;
|
|
|
|
int status = 0;
|
|
|
|
int pipePtr[2];
|
|
|
|
|
|
|
|
// Create a pipe that is shared between parent and child process.
|
|
|
|
if (pipe(pipePtr) < 0) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
close(pipePtr[0]);
|
|
|
|
close(pipePtr[1]);
|
|
|
|
return QString();
|
|
|
|
} else if (pid == 0) {
|
|
|
|
// In child.
|
|
|
|
// Close the reading end.
|
|
|
|
close(pipePtr[0]);
|
|
|
|
// Redirect stdout to the pipe.
|
|
|
|
if (dup2(pipePtr[1], 1) < 0) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char** argv = new char*[command.size()+1];
|
|
|
|
for (int c = 0; c < command.size(); ++c) {
|
|
|
|
argv[c] = new char[command[c].size()+1];
|
|
|
|
strcpy(argv[c], command[c].toLatin1().data());
|
|
|
|
}
|
|
|
|
argv[command.size()] = NULL;
|
|
|
|
execvp(argv[0], argv);
|
|
|
|
// Shouldn't get here, but it's possible if command is not found.
|
|
|
|
close(pipePtr[1]);
|
|
|
|
close(1);
|
|
|
|
for (int c = 0; c < command.size(); ++c) {
|
|
|
|
delete [] argv[c];
|
|
|
|
}
|
|
|
|
delete [] argv;
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
// In parent.
|
|
|
|
// Close the writing end.
|
|
|
|
close(pipePtr[1]);
|
|
|
|
|
|
|
|
QFile pipeRead;
|
|
|
|
if (!pipeRead.open(pipePtr[0], QIODevice::ReadOnly)) {
|
|
|
|
close(pipePtr[0]);
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
QByteArray array;
|
|
|
|
array = pipeRead.readAll();
|
|
|
|
pipeRead.close();
|
|
|
|
close(pipePtr[0]);
|
|
|
|
wait(&status);
|
|
|
|
return QString(array);
|
|
|
|
}
|
|
|
|
}
|
2012-05-11 20:58:15 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-09-07 12:59:15 +00:00
|
|
|
// Windows test support not yet implemented
|
|
|
|
#ifndef Q_OS_WIN32
|
2011-04-27 10:05:43 +00:00
|
|
|
void tst_QPrinterInfo::testForDefaultPrinter()
|
|
|
|
{
|
2012-05-11 20:58:15 +00:00
|
|
|
QString testPrinter = getDefaultPrinterFromSystem();
|
|
|
|
QString defaultPrinter = QPrinterInfo::defaultPrinter().printerName();
|
|
|
|
QString availablePrinter;
|
|
|
|
int availablePrinterDefaults = 0;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QList<QPrinterInfo> list = QPrinterInfo::availablePrinters();
|
|
|
|
for (int c = 0; c < list.size(); ++c) {
|
|
|
|
if (list[c].isDefault()) {
|
2012-05-11 20:58:15 +00:00
|
|
|
availablePrinter = list.at(c).printerName();
|
|
|
|
++availablePrinterDefaults;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-11 20:58:15 +00:00
|
|
|
qDebug() << "Test believes Default Printer = " << testPrinter;
|
|
|
|
qDebug() << "QPrinterInfo::defaultPrinter() believes Default Printer = " << defaultPrinter;
|
|
|
|
qDebug() << "QPrinterInfo::availablePrinters() believes Default Printer = " << availablePrinter;
|
|
|
|
|
|
|
|
QCOMPARE(testPrinter, defaultPrinter);
|
|
|
|
QCOMPARE(testPrinter, availablePrinter);
|
|
|
|
if (!availablePrinter.isEmpty())
|
|
|
|
QCOMPARE(availablePrinterDefaults, 1);
|
2011-11-15 01:36:18 +00:00
|
|
|
}
|
2012-09-07 12:59:15 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-09-07 12:59:15 +00:00
|
|
|
// Windows test support not yet implemented
|
|
|
|
#ifndef Q_OS_WIN32
|
2011-04-27 10:05:43 +00:00
|
|
|
void tst_QPrinterInfo::testForPrinters()
|
|
|
|
{
|
2012-05-11 20:58:15 +00:00
|
|
|
QStringList testPrinters = getPrintersFromSystem();
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
|
2012-05-11 20:58:15 +00:00
|
|
|
QStringList qtPrinters;
|
|
|
|
for (int i = 0; i < printers.size(); ++i)
|
|
|
|
qtPrinters.append(printers.at(i).printerName());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-05-11 20:58:15 +00:00
|
|
|
qSort(testPrinters);
|
|
|
|
qSort(qtPrinters);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-05-11 20:58:15 +00:00
|
|
|
qDebug() << "Test believes Available Printers = " << testPrinters;
|
|
|
|
qDebug() << "QPrinterInfo::availablePrinters() believes Available Printers = " << qtPrinters;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-05-11 20:58:15 +00:00
|
|
|
QCOMPARE(qtPrinters.size(), testPrinters.size());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-05-11 20:58:15 +00:00
|
|
|
for (int i = 0; i < testPrinters.size(); ++i)
|
|
|
|
QCOMPARE(qtPrinters.at(i), testPrinters.at(i));
|
2011-11-15 01:36:18 +00:00
|
|
|
}
|
2012-09-07 12:59:15 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
void tst_QPrinterInfo::testForPaperSizes()
|
|
|
|
{
|
2012-05-11 20:58:15 +00:00
|
|
|
// TODO Old PaperSize test dependent on physical printer installed, new generic test required
|
|
|
|
// In the meantime just exercise the code path and print-out for inspection.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
|
2012-05-11 20:58:15 +00:00
|
|
|
for (int i = 0; i < printers.size(); ++i)
|
|
|
|
qDebug() << "Printer: " << printers.at(i).printerName() << " Paper Sizes: " << printers.at(i).supportedPaperSizes();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QPrinterInfo::testConstructors()
|
|
|
|
{
|
2012-05-11 20:58:15 +00:00
|
|
|
QPrinterInfo null;
|
|
|
|
QCOMPARE(null.printerName(), QString());
|
|
|
|
QVERIFY(null.isNull());
|
|
|
|
|
|
|
|
QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
|
|
|
|
|
|
|
|
for (int i = 0; i < printers.size(); ++i) {
|
|
|
|
QPrinterInfo copy1(printers.at(i));
|
|
|
|
QCOMPARE(copy1.printerName(), printers.at(i).printerName());
|
|
|
|
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());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QPrinterInfo::testAssignment()
|
|
|
|
{
|
2012-05-11 20:58:15 +00:00
|
|
|
QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-05-11 20:58:15 +00:00
|
|
|
for (int i = 0; i < printers.size(); ++i) {
|
|
|
|
QPrinterInfo copy;
|
|
|
|
copy = printers.at(i);
|
|
|
|
QCOMPARE(copy.printerName(), printers.at(i).printerName());
|
|
|
|
QCOMPARE(copy.isNull(), printers.at(i).isNull());
|
|
|
|
QCOMPARE(copy.isDefault(), printers.at(i).isDefault());
|
2012-05-14 20:02:13 +00:00
|
|
|
QCOMPARE(copy.description(), printers.at(i).description());
|
|
|
|
QCOMPARE(copy.location(), printers.at(i).location());
|
|
|
|
QCOMPARE(copy.makeAndModel(), printers.at(i).makeAndModel());
|
2012-05-11 20:58:15 +00:00
|
|
|
QCOMPARE(copy.supportedPaperSizes(), printers.at(i).supportedPaperSizes());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-14 19:17:16 +00:00
|
|
|
void tst_QPrinterInfo::namedPrinter()
|
|
|
|
{
|
|
|
|
QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
|
|
|
|
|
|
|
|
QStringList printerNames;
|
|
|
|
|
|
|
|
foreach (const QPrinterInfo &pi, printers) {
|
|
|
|
QPrinterInfo pi2 = QPrinterInfo::printerInfo(pi.printerName());
|
2012-05-14 20:02:13 +00:00
|
|
|
qDebug() << "Printer: " << pi2.printerName() << " : " << pi2.description() << " : "
|
|
|
|
<< pi2.location() << " : " << pi2.makeAndModel() << " : "
|
2012-05-14 19:17:16 +00:00
|
|
|
<< pi2.isNull() << " : " << pi2.isDefault();
|
|
|
|
QCOMPARE(pi2.printerName(), pi.printerName());
|
2012-05-14 20:02:13 +00:00
|
|
|
QCOMPARE(pi2.description(), pi.description());
|
|
|
|
QCOMPARE(pi2.location(), pi.location());
|
|
|
|
QCOMPARE(pi2.makeAndModel(), pi.makeAndModel());
|
2012-05-14 19:17:16 +00:00
|
|
|
QCOMPARE(pi2.supportedPaperSizes(), pi.supportedPaperSizes());
|
|
|
|
QCOMPARE(pi2.isNull(), pi.isNull());
|
|
|
|
QCOMPARE(pi2.isDefault(), pi.isDefault());
|
|
|
|
}
|
|
|
|
}
|
2012-09-07 12:59:15 +00:00
|
|
|
#endif // QT_NO_PRINTER
|
2012-05-14 19:17:16 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QTEST_MAIN(tst_QPrinterInfo)
|
|
|
|
#include "tst_qprinterinfo.moc"
|