summaryrefslogtreecommitdiffstats
path: root/tests/auto/printsupport/kernel/qprinterinfo/tst_qprinterinfo.cpp
blob: 9416224440104b0aaf4ed44f9aeeea5a27f0959b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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.
**
** GNU Lesser General Public License Usage
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** 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.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest/QtTest>
#include <QtGlobal>
#include <QtAlgorithms>
#include <QtPrintSupport/qprinterinfo.h>

#ifdef Q_OS_UNIX
#  include <unistd.h>
#  include <sys/types.h>
#  include <sys/wait.h>
#endif


class tst_QPrinterInfo : public QObject
{
    Q_OBJECT

public slots:
#ifdef QT_NO_PRINTER
    void initTestCase();
    void cleanupTestCase();
#else
private slots:
#ifndef Q_OS_WIN32
    void testForDefaultPrinter();
    void testForPrinters();
#endif
    void testForPaperSizes();
    void testConstructors();
    void testAssignment();
    void namedPrinter();

private:
    QString getDefaultPrinterFromSystem();
    QStringList getPrintersFromSystem();

#ifdef Q_OS_UNIX
    QString getOutputFromCommand(const QStringList& command);
#endif // Q_OS_UNIX
#endif
};


#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
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;
    command << "lpstat" << "-d";
    QString output = getOutputFromCommand(command);

    QRegExp noDefaultReg("[^:]*no .*default");
    int pos = noDefaultReg.indexIn(output);
    if (pos >= 0) {
        return QString();
    }

    QRegExp defaultReg("default.*: *([a-zA-Z0-9_-]+)");
    defaultReg.indexIn(output);
    printer = defaultReg.cap(1);
#endif // Q_OS_UNIX

    return printer;
}

QStringList tst_QPrinterInfo::getPrintersFromSystem()
{
    QStringList ans;

#ifdef Q_OS_WIN32
    // TODO "cscript c:\windows\system32\prnmngr.vbs -l"
#endif // Q_OS_WIN32
#ifdef Q_OS_UNIX
    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;
        }
    }
#endif // Q_OS_UNIX

    return ans;
}

#ifdef Q_OS_UNIX
// 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);
    }
}
#endif

// Windows test support not yet implemented
#ifndef Q_OS_WIN32
void tst_QPrinterInfo::testForDefaultPrinter()
{
    QString testPrinter = getDefaultPrinterFromSystem();
    QString defaultPrinter = QPrinterInfo::defaultPrinter().printerName();
    QString availablePrinter;
    int availablePrinterDefaults = 0;

    QList<QPrinterInfo> list = QPrinterInfo::availablePrinters();
    for (int c = 0; c < list.size(); ++c) {
        if (list[c].isDefault()) {
            availablePrinter = list.at(c).printerName();
            ++availablePrinterDefaults;
        }
    }

    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);
}
#endif

// Windows test support not yet implemented
#ifndef Q_OS_WIN32
void tst_QPrinterInfo::testForPrinters()
{
    QStringList testPrinters = getPrintersFromSystem();

    QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
    QStringList qtPrinters;
    for (int i = 0; i < printers.size(); ++i)
        qtPrinters.append(printers.at(i).printerName());

    qSort(testPrinters);
    qSort(qtPrinters);

    qDebug() << "Test believes Available Printers                              = " << testPrinters;
    qDebug() << "QPrinterInfo::availablePrinters() believes Available Printers = " << qtPrinters;

    QCOMPARE(qtPrinters.size(), testPrinters.size());

    for (int i = 0; i < testPrinters.size(); ++i)
        QCOMPARE(qtPrinters.at(i), testPrinters.at(i));
}
#endif

void tst_QPrinterInfo::testForPaperSizes()
{
    // 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.

    QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
    for (int i = 0; i < printers.size(); ++i)
        qDebug() << "Printer: " << printers.at(i).printerName() << " Paper Sizes: " << printers.at(i).supportedPaperSizes();
}

void tst_QPrinterInfo::testConstructors()
{
    QPrinterInfo null;
    QCOMPARE(null.printerName(), QString());
    QVERIFY(null.isNull());

    QPrinterInfo null2(null);
    QVERIFY(null2.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());
    }
}

void tst_QPrinterInfo::testAssignment()
{
    QPrinterInfo null;
    QVERIFY(null.isNull());
    QPrinterInfo null2;
    null2 = null;
    QVERIFY(null2.isNull());

    QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();

    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());
        QCOMPARE(copy.description(),         printers.at(i).description());
        QCOMPARE(copy.location(),            printers.at(i).location());
        QCOMPARE(copy.makeAndModel(),        printers.at(i).makeAndModel());
        QCOMPARE(copy.supportedPaperSizes(), printers.at(i).supportedPaperSizes());
    }
}

void tst_QPrinterInfo::namedPrinter()
{
    QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();

    QStringList printerNames;

    foreach (const QPrinterInfo &pi, printers) {
        QPrinterInfo pi2 = QPrinterInfo::printerInfo(pi.printerName());
        qDebug() << "Printer: " << pi2.printerName() << " : " << pi2.description() << " : "
                 << pi2.location() << " : " << pi2.makeAndModel() << " : "
                 << pi2.isNull() << " : " << pi2.isDefault();
        QCOMPARE(pi2.printerName(),         pi.printerName());
        QCOMPARE(pi2.description(),         pi.description());
        QCOMPARE(pi2.location(),            pi.location());
        QCOMPARE(pi2.makeAndModel(),        pi.makeAndModel());
        QCOMPARE(pi2.supportedPaperSizes(), pi.supportedPaperSizes());
        QCOMPARE(pi2.isNull(),              pi.isNull());
        QCOMPARE(pi2.isDefault(),           pi.isDefault());
    }
}
#endif // QT_NO_PRINTER

QTEST_MAIN(tst_QPrinterInfo)
#include "tst_qprinterinfo.moc"