~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

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