diff options
Diffstat (limited to 'tests/auto/pdf')
19 files changed, 1166 insertions, 0 deletions
diff --git a/tests/auto/pdf/pdf.pro b/tests/auto/pdf/pdf.pro new file mode 100644 index 000000000..a2b3fcff2 --- /dev/null +++ b/tests/auto/pdf/pdf.pro @@ -0,0 +1,8 @@ +TEMPLATE = subdirs + +SUBDIRS = \ + qpdfbookmarkmodel \ + qpdfpagenavigation \ + qpdfpagerenderer + +qtHaveModule(printsupport): SUBDIRS += qpdfdocument diff --git a/tests/auto/pdf/qpdfbookmarkmodel/pdf-sample.bookmarks.pdf b/tests/auto/pdf/qpdfbookmarkmodel/pdf-sample.bookmarks.pdf Binary files differnew file mode 100644 index 000000000..bd27c18b6 --- /dev/null +++ b/tests/auto/pdf/qpdfbookmarkmodel/pdf-sample.bookmarks.pdf diff --git a/tests/auto/pdf/qpdfbookmarkmodel/pdf-sample.bookmarks_pages.pdf b/tests/auto/pdf/qpdfbookmarkmodel/pdf-sample.bookmarks_pages.pdf Binary files differnew file mode 100644 index 000000000..c4e1aa36e --- /dev/null +++ b/tests/auto/pdf/qpdfbookmarkmodel/pdf-sample.bookmarks_pages.pdf diff --git a/tests/auto/pdf/qpdfbookmarkmodel/qpdfbookmarkmodel.pro b/tests/auto/pdf/qpdfbookmarkmodel/qpdfbookmarkmodel.pro new file mode 100644 index 000000000..11a010637 --- /dev/null +++ b/tests/auto/pdf/qpdfbookmarkmodel/qpdfbookmarkmodel.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +TARGET = tst_qpdfbookmarkmodel +QT += pdf testlib network +macos:CONFIG -= app_bundle +SOURCES += tst_qpdfbookmarkmodel.cpp diff --git a/tests/auto/pdf/qpdfbookmarkmodel/tst_qpdfbookmarkmodel.cpp b/tests/auto/pdf/qpdfbookmarkmodel/tst_qpdfbookmarkmodel.cpp new file mode 100644 index 000000000..fddc98011 --- /dev/null +++ b/tests/auto/pdf/qpdfbookmarkmodel/tst_qpdfbookmarkmodel.cpp @@ -0,0 +1,287 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <QPdfDocument> +#include <QPdfBookmarkModel> + +class tst_QPdfBookmarkModel: public QObject +{ + Q_OBJECT + +public: + tst_QPdfBookmarkModel() + { + qRegisterMetaType<QPdfDocument::Status>(); + } + +private slots: + void emptyModel(); + void setEmptyDocument(); + void setEmptyDocumentAndLoad(); + void setLoadedDocument(); + void unloadDocument(); + void testTreeStructure(); + void testListStructure(); + void testPageNumberRole(); +}; + +void tst_QPdfBookmarkModel::emptyModel() +{ + QPdfBookmarkModel model; + + QVERIFY(!model.document()); + QCOMPARE(model.structureMode(), QPdfBookmarkModel::TreeMode); + QCOMPARE(model.rowCount(), 0); + QCOMPARE(model.columnCount(), 1); + QCOMPARE(model.index(0, 0).isValid(), false); +} + +void tst_QPdfBookmarkModel::setEmptyDocument() +{ + QPdfDocument document; + QPdfBookmarkModel model; + + model.setDocument(&document); + + QCOMPARE(model.document(), &document); + QCOMPARE(model.structureMode(), QPdfBookmarkModel::TreeMode); + QCOMPARE(model.rowCount(), 0); + QCOMPARE(model.columnCount(), 1); + QCOMPARE(model.index(0, 0).isValid(), false); +} + +void tst_QPdfBookmarkModel::setEmptyDocumentAndLoad() +{ + QPdfDocument document; + QPdfBookmarkModel model; + + model.setDocument(&document); + + QSignalSpy modelAboutToBeResetSpy(&model, SIGNAL(modelAboutToBeReset())); + QSignalSpy modelResetSpy(&model, SIGNAL(modelReset())); + + QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.bookmarks.pdf")), QPdfDocument::NoError); + + QCOMPARE(modelAboutToBeResetSpy.count(), 1); + QCOMPARE(modelResetSpy.count(), 1); + + QCOMPARE(model.rowCount(), 3); +} + +void tst_QPdfBookmarkModel::setLoadedDocument() +{ + QPdfDocument document; + QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.bookmarks.pdf")), QPdfDocument::NoError); + + QPdfBookmarkModel model; + + QSignalSpy modelAboutToBeResetSpy(&model, SIGNAL(modelAboutToBeReset())); + QSignalSpy modelResetSpy(&model, SIGNAL(modelReset())); + + model.setDocument(&document); + + QCOMPARE(modelAboutToBeResetSpy.count(), 1); + QCOMPARE(modelResetSpy.count(), 1); + + QCOMPARE(model.rowCount(), 3); +} + +void tst_QPdfBookmarkModel::unloadDocument() +{ + QPdfDocument document; + QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.bookmarks.pdf")), QPdfDocument::NoError); + + QPdfBookmarkModel model; + model.setDocument(&document); + + QCOMPARE(model.rowCount(), 3); + + QSignalSpy modelAboutToBeResetSpy(&model, SIGNAL(modelAboutToBeReset())); + QSignalSpy modelResetSpy(&model, SIGNAL(modelReset())); + + document.close(); + + QCOMPARE(modelAboutToBeResetSpy.count(), 1); + QCOMPARE(modelResetSpy.count(), 1); + + QCOMPARE(model.rowCount(), 0); +} + +void tst_QPdfBookmarkModel::testTreeStructure() +{ + QPdfDocument document; + QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.bookmarks.pdf")), QPdfDocument::NoError); + + QPdfBookmarkModel model; + model.setDocument(&document); + + QCOMPARE(model.rowCount(), 3); + + const QModelIndex index1 = model.index(0, 0); + QCOMPARE(index1.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 1")); + QCOMPARE(index1.data(QPdfBookmarkModel::LevelRole).toInt(), 0); + QCOMPARE(model.rowCount(index1), 2); + + const QModelIndex index1_1 = model.index(0, 0, index1); + QCOMPARE(index1_1.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 1.1")); + QCOMPARE(index1_1.data(QPdfBookmarkModel::LevelRole).toInt(), 1); + QCOMPARE(model.rowCount(index1_1), 0); + + const QModelIndex index1_2 = model.index(1, 0, index1); + QCOMPARE(index1_2.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 1.2")); + QCOMPARE(index1_2.data(QPdfBookmarkModel::LevelRole).toInt(), 1); + QCOMPARE(model.rowCount(index1_2), 0); + + const QModelIndex index2 = model.index(1, 0); + QCOMPARE(index2.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 2")); + QCOMPARE(index2.data(QPdfBookmarkModel::LevelRole).toInt(), 0); + QCOMPARE(model.rowCount(index2), 2); + + const QModelIndex index2_1 = model.index(0, 0, index2); + QCOMPARE(index2_1.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 2.1")); + QCOMPARE(index2_1.data(QPdfBookmarkModel::LevelRole).toInt(), 1); + QCOMPARE(model.rowCount(index2_1), 1); + + const QModelIndex index2_1_1 = model.index(0, 0, index2_1); + QCOMPARE(index2_1_1.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 2.1.1")); + QCOMPARE(index2_1_1.data(QPdfBookmarkModel::LevelRole).toInt(), 2); + QCOMPARE(model.rowCount(index2_1_1), 0); + + const QModelIndex index2_2 = model.index(1, 0, index2); + QCOMPARE(index2_2.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 2.2")); + QCOMPARE(index2_2.data(QPdfBookmarkModel::LevelRole).toInt(), 1); + QCOMPARE(model.rowCount(index2_2), 0); + + const QModelIndex index3 = model.index(2, 0); + QCOMPARE(index3.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 3")); + QCOMPARE(index3.data(QPdfBookmarkModel::LevelRole).toInt(), 0); + QCOMPARE(model.rowCount(index3), 0); + + const QModelIndex index4 = model.index(3, 0); + QCOMPARE(index4, QModelIndex()); +} + +void tst_QPdfBookmarkModel::testListStructure() +{ + QPdfDocument document; + QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.bookmarks.pdf")), QPdfDocument::NoError); + + QPdfBookmarkModel model; + model.setDocument(&document); + + QSignalSpy modelAboutToBeResetSpy(&model, SIGNAL(modelAboutToBeReset())); + QSignalSpy modelResetSpy(&model, SIGNAL(modelReset())); + + model.setStructureMode(QPdfBookmarkModel::ListMode); + + QCOMPARE(modelAboutToBeResetSpy.count(), 1); + QCOMPARE(modelResetSpy.count(), 1); + + QCOMPARE(model.rowCount(), 8); + + const QModelIndex index1 = model.index(0, 0); + QCOMPARE(index1.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 1")); + QCOMPARE(index1.data(QPdfBookmarkModel::LevelRole).toInt(), 0); + QCOMPARE(model.rowCount(index1), 0); + + const QModelIndex index1_1 = model.index(1, 0); + QCOMPARE(index1_1.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 1.1")); + QCOMPARE(index1_1.data(QPdfBookmarkModel::LevelRole).toInt(), 1); + QCOMPARE(model.rowCount(index1_1), 0); + + const QModelIndex index1_2 = model.index(2, 0); + QCOMPARE(index1_2.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 1.2")); + QCOMPARE(index1_2.data(QPdfBookmarkModel::LevelRole).toInt(), 1); + QCOMPARE(model.rowCount(index1_2), 0); + + const QModelIndex index2 = model.index(3, 0); + QCOMPARE(index2.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 2")); + QCOMPARE(index2.data(QPdfBookmarkModel::LevelRole).toInt(), 0); + QCOMPARE(model.rowCount(index2), 0); + + const QModelIndex index2_1 = model.index(4, 0); + QCOMPARE(index2_1.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 2.1")); + QCOMPARE(index2_1.data(QPdfBookmarkModel::LevelRole).toInt(), 1); + QCOMPARE(model.rowCount(index2_1), 0); + + const QModelIndex index2_1_1 = model.index(5, 0); + QCOMPARE(index2_1_1.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 2.1.1")); + QCOMPARE(index2_1_1.data(QPdfBookmarkModel::LevelRole).toInt(), 2); + QCOMPARE(model.rowCount(index2_1_1), 0); + + const QModelIndex index2_2 = model.index(6, 0); + QCOMPARE(index2_2.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 2.2")); + QCOMPARE(index2_2.data(QPdfBookmarkModel::LevelRole).toInt(), 1); + QCOMPARE(model.rowCount(index2_2), 0); + + const QModelIndex index3 = model.index(7, 0); + QCOMPARE(index3.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 3")); + QCOMPARE(index3.data(QPdfBookmarkModel::LevelRole).toInt(), 0); + QCOMPARE(model.rowCount(index3), 0); + + const QModelIndex index4 = model.index(8, 0); + QCOMPARE(index4, QModelIndex()); +} + +void tst_QPdfBookmarkModel::testPageNumberRole() +{ + QPdfDocument document; + QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.bookmarks_pages.pdf")), QPdfDocument::NoError); + + QPdfBookmarkModel model; + model.setDocument(&document); + + QCOMPARE(model.rowCount(), 3); + + const QModelIndex index1 = model.index(0, 0); + QCOMPARE(index1.data(QPdfBookmarkModel::PageNumberRole).toInt(), 0); + + const QModelIndex index2 = model.index(1, 0); + QCOMPARE(index2.data(QPdfBookmarkModel::PageNumberRole).toInt(), 1); + + const QModelIndex index2_1 = model.index(0, 0, index2); + QCOMPARE(index2_1.data(QPdfBookmarkModel::PageNumberRole).toInt(), 1); + + const QModelIndex index3 = model.index(2, 0); + QCOMPARE(index3.data(QPdfBookmarkModel::PageNumberRole).toInt(), 2); +} + +QTEST_MAIN(tst_QPdfBookmarkModel) + +#include "tst_qpdfbookmarkmodel.moc" diff --git a/tests/auto/pdf/qpdfdocument/BLACKLIST b/tests/auto/pdf/qpdfdocument/BLACKLIST new file mode 100644 index 000000000..b8db556d6 --- /dev/null +++ b/tests/auto/pdf/qpdfdocument/BLACKLIST @@ -0,0 +1,6 @@ +[password] +* + +[passwordClearedOnClose] +* + diff --git a/tests/auto/pdf/qpdfdocument/pdf-sample.metadata.pdf b/tests/auto/pdf/qpdfdocument/pdf-sample.metadata.pdf Binary files differnew file mode 100644 index 000000000..c3350ba5f --- /dev/null +++ b/tests/auto/pdf/qpdfdocument/pdf-sample.metadata.pdf diff --git a/tests/auto/pdf/qpdfdocument/pdf-sample.protected.pdf b/tests/auto/pdf/qpdfdocument/pdf-sample.protected.pdf Binary files differnew file mode 100644 index 000000000..d76fdd1a6 --- /dev/null +++ b/tests/auto/pdf/qpdfdocument/pdf-sample.protected.pdf diff --git a/tests/auto/pdf/qpdfdocument/qpdfdocument.pro b/tests/auto/pdf/qpdfdocument/qpdfdocument.pro new file mode 100644 index 000000000..8382a25e3 --- /dev/null +++ b/tests/auto/pdf/qpdfdocument/qpdfdocument.pro @@ -0,0 +1,6 @@ +CONFIG += testcase +TARGET = tst_qpdfdocument +QT += pdf printsupport testlib network +macx:CONFIG -= app_bundle +SOURCES += tst_qpdfdocument.cpp + diff --git a/tests/auto/pdf/qpdfdocument/tst_qpdfdocument.cpp b/tests/auto/pdf/qpdfdocument/tst_qpdfdocument.cpp new file mode 100644 index 000000000..29b85fc89 --- /dev/null +++ b/tests/auto/pdf/qpdfdocument/tst_qpdfdocument.cpp @@ -0,0 +1,386 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <QPainter> +#include <QPdfDocument> +#include <QPrinter> +#include <QTemporaryFile> +#include <QNetworkAccessManager> +#include <QNetworkRequest> +#include <QNetworkReply> + +class tst_QPdfDocument: public QObject +{ + Q_OBJECT + +public: + tst_QPdfDocument() + { + qRegisterMetaType<QPdfDocument::Status>(); + } + +private slots: + void pageCount(); + void loadFromIODevice(); + void loadAsync(); + void password(); + void close(); + void loadAfterClose(); + void closeOnDestroy(); + void status(); + void passwordClearedOnClose(); + void metaData(); +}; + +struct TemporaryPdf: public QTemporaryFile +{ + TemporaryPdf(); + QPageLayout pageLayout; +}; + + +TemporaryPdf::TemporaryPdf() +{ + open(); + pageLayout = QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF()); + + { + QPrinter printer; + printer.setOutputFormat(QPrinter::PdfFormat); + printer.setOutputFileName(fileName()); + printer.setPageLayout(pageLayout); + + { + QPainter painter(&printer); + painter.drawText(100, 100, QStringLiteral("Hello Page 1")); + printer.newPage(); + painter.drawText(100, 100, QStringLiteral("Hello Page 2")); + } + } + + seek(0); +} + +void tst_QPdfDocument::pageCount() +{ + TemporaryPdf tempPdf; + + QPdfDocument doc; + QSignalSpy pageCountChangedSpy(&doc, SIGNAL(pageCountChanged(int))); + + QCOMPARE(doc.pageCount(), 0); + QCOMPARE(doc.load(tempPdf.fileName()), QPdfDocument::NoError); + QCOMPARE(doc.pageCount(), 2); + QCOMPARE(pageCountChangedSpy.count(), 1); + QCOMPARE(pageCountChangedSpy[0][0].toInt(), doc.pageCount()); + + QCOMPARE(doc.pageSize(0).toSize(), tempPdf.pageLayout.fullRectPoints().size()); +} + +void tst_QPdfDocument::loadFromIODevice() +{ + TemporaryPdf tempPdf; + QPdfDocument doc; + QSignalSpy statusChangedSpy(&doc, SIGNAL(statusChanged(QPdfDocument::Status))); + QSignalSpy pageCountChangedSpy(&doc, SIGNAL(pageCountChanged(int))); + doc.load(&tempPdf); + QCOMPARE(statusChangedSpy.count(), 2); + QCOMPARE(statusChangedSpy[0][0].value<QPdfDocument::Status>(), QPdfDocument::Loading); + QCOMPARE(statusChangedSpy[1][0].value<QPdfDocument::Status>(), QPdfDocument::Ready); + QCOMPARE(doc.error(), QPdfDocument::NoError); + QCOMPARE(doc.pageCount(), 2); + QCOMPARE(pageCountChangedSpy.count(), 1); + QCOMPARE(pageCountChangedSpy[0][0].toInt(), doc.pageCount()); +} + +void tst_QPdfDocument::loadAsync() +{ + TemporaryPdf tempPdf; + + QNetworkAccessManager nam; + + QUrl url = QUrl::fromLocalFile(tempPdf.fileName()); + QScopedPointer<QNetworkReply> reply(nam.get(QNetworkRequest(url))); + + QPdfDocument doc; + QSignalSpy statusChangedSpy(&doc, SIGNAL(statusChanged(QPdfDocument::Status))); + QSignalSpy pageCountChangedSpy(&doc, SIGNAL(pageCountChanged(int))); + + doc.load(reply.data()); + + QCOMPARE(statusChangedSpy.count(), 2); + QCOMPARE(statusChangedSpy[0][0].value<QPdfDocument::Status>(), QPdfDocument::Loading); + QCOMPARE(statusChangedSpy[1][0].value<QPdfDocument::Status>(), QPdfDocument::Ready); + QCOMPARE(doc.pageCount(), 2); + QCOMPARE(pageCountChangedSpy.count(), 1); + QCOMPARE(pageCountChangedSpy[0][0].toInt(), doc.pageCount()); +} + +void tst_QPdfDocument::password() +{ + QPdfDocument doc; + QSignalSpy passwordChangedSpy(&doc, SIGNAL(passwordChanged())); + + QCOMPARE(doc.pageCount(), 0); + QCOMPARE(doc.load(QFINDTESTDATA("pdf-sample.protected.pdf")), QPdfDocument::IncorrectPasswordError); + QCOMPARE(passwordChangedSpy.count(), 0); + doc.setPassword(QStringLiteral("WrongPassword")); + QCOMPARE(passwordChangedSpy.count(), 1); + QCOMPARE(doc.load(QFINDTESTDATA("pdf-sample.protected.pdf")), QPdfDocument::IncorrectPasswordError); + QCOMPARE(doc.status(), QPdfDocument::Error); + doc.setPassword(QStringLiteral("Qt")); + QCOMPARE(passwordChangedSpy.count(), 2); + QCOMPARE(doc.load(QFINDTESTDATA("pdf-sample.protected.pdf")), QPdfDocument::NoError); + QCOMPARE(doc.pageCount(), 1); +} + +void tst_QPdfDocument::close() +{ + TemporaryPdf tempPdf; + QPdfDocument doc; + + QSignalSpy statusChangedSpy(&doc, SIGNAL(statusChanged(QPdfDocument::Status))); + QSignalSpy pageCountChangedSpy(&doc, SIGNAL(pageCountChanged(int))); + + doc.load(&tempPdf); + + QCOMPARE(statusChangedSpy.count(), 2); + QCOMPARE(statusChangedSpy[0][0].value<QPdfDocument::Status>(), QPdfDocument::Loading); + QCOMPARE(statusChangedSpy[1][0].value<QPdfDocument::Status>(), QPdfDocument::Ready); + QCOMPARE(pageCountChangedSpy.count(), 1); + QCOMPARE(pageCountChangedSpy[0][0].toInt(), doc.pageCount()); + + statusChangedSpy.clear(); + pageCountChangedSpy.clear(); + + doc.close(); + QCOMPARE(statusChangedSpy.count(), 2); + QCOMPARE(statusChangedSpy[0][0].value<QPdfDocument::Status>(), QPdfDocument::Unloading); + QCOMPARE(statusChangedSpy[1][0].value<QPdfDocument::Status>(), QPdfDocument::Null); + QCOMPARE(doc.pageCount(), 0); + QCOMPARE(pageCountChangedSpy.count(), 1); + QCOMPARE(pageCountChangedSpy[0][0].toInt(), doc.pageCount()); +} + +void tst_QPdfDocument::loadAfterClose() +{ + TemporaryPdf tempPdf; + QPdfDocument doc; + + QSignalSpy statusChangedSpy(&doc, SIGNAL(statusChanged(QPdfDocument::Status))); + QSignalSpy pageCountChangedSpy(&doc, SIGNAL(pageCountChanged(int))); + + doc.load(&tempPdf); + QCOMPARE(statusChangedSpy.count(), 2); + QCOMPARE(statusChangedSpy[0][0].value<QPdfDocument::Status>(), QPdfDocument::Loading); + QCOMPARE(statusChangedSpy[1][0].value<QPdfDocument::Status>(), QPdfDocument::Ready); + QCOMPARE(pageCountChangedSpy.count(), 1); + QCOMPARE(pageCountChangedSpy[0][0].toInt(), doc.pageCount()); + statusChangedSpy.clear(); + pageCountChangedSpy.clear(); + + doc.close(); + QCOMPARE(statusChangedSpy.count(), 2); + QCOMPARE(statusChangedSpy[0][0].value<QPdfDocument::Status>(), QPdfDocument::Unloading); + QCOMPARE(statusChangedSpy[1][0].value<QPdfDocument::Status>(), QPdfDocument::Null); + QCOMPARE(pageCountChangedSpy.count(), 1); + QCOMPARE(pageCountChangedSpy[0][0].toInt(), doc.pageCount()); + statusChangedSpy.clear(); + pageCountChangedSpy.clear(); + + doc.load(&tempPdf); + QCOMPARE(statusChangedSpy.count(), 2); + QCOMPARE(statusChangedSpy[0][0].value<QPdfDocument::Status>(), QPdfDocument::Loading); + QCOMPARE(statusChangedSpy[1][0].value<QPdfDocument::Status>(), QPdfDocument::Ready); + QCOMPARE(doc.error(), QPdfDocument::NoError); + QCOMPARE(doc.pageCount(), 2); + QCOMPARE(pageCountChangedSpy.count(), 1); + QCOMPARE(pageCountChangedSpy[0][0].toInt(), doc.pageCount()); +} + +void tst_QPdfDocument::closeOnDestroy() +{ + TemporaryPdf tempPdf; + + // deleting an open document should automatically close it + { + QPdfDocument *doc = new QPdfDocument; + + doc->load(&tempPdf); + + QSignalSpy statusChangedSpy(doc, SIGNAL(statusChanged(QPdfDocument::Status))); + QSignalSpy pageCountChangedSpy(doc, SIGNAL(pageCountChanged(int))); + + delete doc; + + QCOMPARE(statusChangedSpy.count(), 2); + QCOMPARE(statusChangedSpy[0][0].value<QPdfDocument::Status>(), QPdfDocument::Unloading); + QCOMPARE(statusChangedSpy[1][0].value<QPdfDocument::Status>(), QPdfDocument::Null); + QCOMPARE(pageCountChangedSpy.count(), 1); + QCOMPARE(pageCountChangedSpy[0][0].toInt(), 0); + } + + // deleting a closed document should not emit any signal + { + QPdfDocument *doc = new QPdfDocument; + doc->load(&tempPdf); + doc->close(); + + QSignalSpy statusChangedSpy(doc, SIGNAL(statusChanged(QPdfDocument::Status))); + QSignalSpy pageCountChangedSpy(doc, SIGNAL(pageCountChanged(int))); + + delete doc; + + QCOMPARE(statusChangedSpy.count(), 0); + QCOMPARE(pageCountChangedSpy.count(), 0); + } +} + +void tst_QPdfDocument::status() +{ + TemporaryPdf tempPdf; + + QPdfDocument doc; + QCOMPARE(doc.status(), QPdfDocument::Null); + + QSignalSpy statusChangedSpy(&doc, SIGNAL(statusChanged(QPdfDocument::Status))); + + // open existing document + doc.load(&tempPdf); + QCOMPARE(statusChangedSpy.count(), 2); + QCOMPARE(statusChangedSpy[0][0].value<QPdfDocument::Status>(), QPdfDocument::Loading); + QCOMPARE(statusChangedSpy[1][0].value<QPdfDocument::Status>(), QPdfDocument::Ready); + statusChangedSpy.clear(); + + QCOMPARE(doc.status(), QPdfDocument::Ready); + + // close document + doc.close(); + + QCOMPARE(statusChangedSpy.count(), 2); + QCOMPARE(statusChangedSpy[0][0].value<QPdfDocument::Status>(), QPdfDocument::Unloading); + QCOMPARE(statusChangedSpy[1][0].value<QPdfDocument::Status>(), QPdfDocument::Null); + statusChangedSpy.clear(); + + QCOMPARE(doc.status(), QPdfDocument::Null); + + // try to open non-existing document + doc.load(QFINDTESTDATA("does-not-exist.pdf")); + QCOMPARE(statusChangedSpy.count(), 2); + QCOMPARE(statusChangedSpy[0][0].value<QPdfDocument::Status>(), QPdfDocument::Loading); + QCOMPARE(statusChangedSpy[1][0].value<QPdfDocument::Status>(), QPdfDocument::Error); + QCOMPARE(doc.status(), QPdfDocument::Error); + statusChangedSpy.clear(); + + // try to open non-existing document asynchronously + QNetworkAccessManager accessManager; + + const QUrl url("http://doesnotexist.qt.io"); + QScopedPointer<QNetworkReply> reply(accessManager.get(QNetworkRequest(url))); + + doc.load(reply.data()); + + QElapsedTimer stopWatch; + stopWatch.start(); + forever { + QCoreApplication::instance()->processEvents(); + if (statusChangedSpy.count() == 2) + break; + if (stopWatch.elapsed() >= 30000) + break; + } + + QCOMPARE(statusChangedSpy.count(), 2); + QCOMPARE(statusChangedSpy[0][0].value<QPdfDocument::Status>(), QPdfDocument::Loading); + QCOMPARE(statusChangedSpy[1][0].value<QPdfDocument::Status>(), QPdfDocument::Error); + statusChangedSpy.clear(); +} + +void tst_QPdfDocument::passwordClearedOnClose() +{ + TemporaryPdf tempPdf; + QPdfDocument doc; + + QSignalSpy passwordChangedSpy(&doc, SIGNAL(passwordChanged())); + + doc.setPassword(QStringLiteral("Qt")); + QCOMPARE(passwordChangedSpy.count(), 1); + QCOMPARE(doc.load(QFINDTESTDATA("pdf-sample.protected.pdf")), QPdfDocument::NoError); + passwordChangedSpy.clear(); + + doc.close(); // password is cleared on close + QCOMPARE(passwordChangedSpy.count(), 1); + passwordChangedSpy.clear(); + + doc.load(&tempPdf); + doc.close(); // signal is not emitted if password didn't change + QCOMPARE(passwordChangedSpy.count(), 0); +} + +void tst_QPdfDocument::metaData() +{ + QPdfDocument doc; + + // a closed document does not return any meta data + QCOMPARE(doc.metaData(QPdfDocument::Title).toString(), QString()); + QCOMPARE(doc.metaData(QPdfDocument::Subject).toString(), QString()); + QCOMPARE(doc.metaData(QPdfDocument::Author).toString(), QString()); + QCOMPARE(doc.metaData(QPdfDocument::Keywords).toString(), QString()); + QCOMPARE(doc.metaData(QPdfDocument::Producer).toString(), QString()); + QCOMPARE(doc.metaData(QPdfDocument::Creator).toString(), QString()); + QCOMPARE(doc.metaData(QPdfDocument::CreationDate).toDateTime(), QDateTime()); + QCOMPARE(doc.metaData(QPdfDocument::ModificationDate).toDateTime(), QDateTime()); + + QCOMPARE(doc.load(QFINDTESTDATA("pdf-sample.metadata.pdf")), QPdfDocument::NoError); + + // check for proper meta data from sample document + QCOMPARE(doc.metaData(QPdfDocument::Title).toString(), QString::fromLatin1("Qt PDF Unit Test Document")); + QCOMPARE(doc.metaData(QPdfDocument::Subject).toString(), QString::fromLatin1("A test for meta data access")); + QCOMPARE(doc.metaData(QPdfDocument::Author).toString(), QString::fromLatin1("John Doe")); + QCOMPARE(doc.metaData(QPdfDocument::Keywords).toString(), QString::fromLatin1("meta data keywords")); + QCOMPARE(doc.metaData(QPdfDocument::Producer).toString(), QString::fromLatin1("LibreOffice 5.1")); + QCOMPARE(doc.metaData(QPdfDocument::Creator).toString(), QString::fromLatin1("Writer")); + QCOMPARE(doc.metaData(QPdfDocument::CreationDate).toDateTime(), QDateTime(QDate(2016, 8, 7), QTime(7, 3, 6), Qt::UTC)); + QCOMPARE(doc.metaData(QPdfDocument::ModificationDate).toDateTime(), QDateTime(QDate(2016, 8, 8), QTime(8, 3, 6), Qt::UTC)); +} + +QTEST_MAIN(tst_QPdfDocument) + +#include "tst_qpdfdocument.moc" + diff --git a/tests/auto/pdf/qpdfpagenavigation/pdf-sample.pagenavigation.pdf b/tests/auto/pdf/qpdfpagenavigation/pdf-sample.pagenavigation.pdf Binary files differnew file mode 100644 index 000000000..c4e1aa36e --- /dev/null +++ b/tests/auto/pdf/qpdfpagenavigation/pdf-sample.pagenavigation.pdf diff --git a/tests/auto/pdf/qpdfpagenavigation/qpdfpagenavigation.pro b/tests/auto/pdf/qpdfpagenavigation/qpdfpagenavigation.pro new file mode 100644 index 000000000..8de99543f --- /dev/null +++ b/tests/auto/pdf/qpdfpagenavigation/qpdfpagenavigation.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +TARGET = tst_qpdfpagenavigation +QT += pdf testlib network +macos:CONFIG -= app_bundle +SOURCES += tst_qpdfpagenavigation.cpp diff --git a/tests/auto/pdf/qpdfpagenavigation/tst_qpdfpagenavigation.cpp b/tests/auto/pdf/qpdfpagenavigation/tst_qpdfpagenavigation.cpp new file mode 100644 index 000000000..ff6a02750 --- /dev/null +++ b/tests/auto/pdf/qpdfpagenavigation/tst_qpdfpagenavigation.cpp @@ -0,0 +1,200 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <QPdfDocument> +#include <QPdfPageNavigation> + +class tst_QPdfPageNavigation: public QObject +{ + Q_OBJECT + +private slots: + void defaultValues(); + void setEmptyDocument(); + void setEmptyDocumentAndLoad(); + void setLoadedDocument(); + void unloadDocument(); + void navigate(); +}; + +void tst_QPdfPageNavigation::defaultValues() +{ + QPdfPageNavigation pageNavigation; + + QCOMPARE(pageNavigation.document(), nullptr); + QCOMPARE(pageNavigation.currentPage(), 0); + QCOMPARE(pageNavigation.pageCount(), 0); + QCOMPARE(pageNavigation.canGoToPreviousPage(), false); + QCOMPARE(pageNavigation.canGoToNextPage(), false); +} + +void tst_QPdfPageNavigation::setEmptyDocument() +{ + QPdfDocument document; + QPdfPageNavigation pageNavigation; + + pageNavigation.setDocument(&document); + + QCOMPARE(pageNavigation.document(), &document); + QCOMPARE(pageNavigation.currentPage(), 0); + QCOMPARE(pageNavigation.pageCount(), 0); + QCOMPARE(pageNavigation.canGoToPreviousPage(), false); + QCOMPARE(pageNavigation.canGoToNextPage(), false); +} + +void tst_QPdfPageNavigation::setEmptyDocumentAndLoad() +{ + QPdfDocument document; + QPdfPageNavigation pageNavigation; + + pageNavigation.setDocument(&document); + + QSignalSpy currentPageChangedSpy(&pageNavigation, &QPdfPageNavigation::currentPageChanged); + QSignalSpy pageCountChangedSpy(&pageNavigation, &QPdfPageNavigation::pageCountChanged); + QSignalSpy canGoToPreviousPageChangedSpy(&pageNavigation, &QPdfPageNavigation::canGoToPreviousPageChanged); + QSignalSpy canGoToNextPageChangedSpy(&pageNavigation, &QPdfPageNavigation::canGoToNextPageChanged); + + QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.pagenavigation.pdf")), QPdfDocument::NoError); + + QCOMPARE(currentPageChangedSpy.count(), 0); // current page stays '0' + QCOMPARE(pageCountChangedSpy.count(), 1); + QCOMPARE(pageCountChangedSpy[0][0].toInt(), 3); + QCOMPARE(canGoToPreviousPageChangedSpy.count(), 0); // still no previous page available + QCOMPARE(canGoToNextPageChangedSpy.count(), 1); + QCOMPARE(canGoToNextPageChangedSpy[0][0].toBool(), true); +} + +void tst_QPdfPageNavigation::setLoadedDocument() +{ + QPdfDocument document; + QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.pagenavigation.pdf")), QPdfDocument::NoError); + + QPdfPageNavigation pageNavigation; + + QSignalSpy currentPageChangedSpy(&pageNavigation, &QPdfPageNavigation::currentPageChanged); + QSignalSpy pageCountChangedSpy(&pageNavigation, &QPdfPageNavigation::pageCountChanged); + QSignalSpy canGoToPreviousPageChangedSpy(&pageNavigation, &QPdfPageNavigation::canGoToPreviousPageChanged); + QSignalSpy canGoToNextPageChangedSpy(&pageNavigation, &QPdfPageNavigation::canGoToNextPageChanged); + + pageNavigation.setDocument(&document); + + QCOMPARE(currentPageChangedSpy.count(), 0); // current page stays '0' + QCOMPARE(pageCountChangedSpy.count(), 1); + QCOMPARE(pageCountChangedSpy[0][0].toInt(), 3); + QCOMPARE(canGoToPreviousPageChangedSpy.count(), 0); // still no previous page available + QCOMPARE(canGoToNextPageChangedSpy.count(), 1); + QCOMPARE(canGoToNextPageChangedSpy[0][0].toBool(), true); +} + +void tst_QPdfPageNavigation::unloadDocument() +{ + QPdfDocument document; + QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.pagenavigation.pdf")), QPdfDocument::NoError); + + QPdfPageNavigation pageNavigation; + pageNavigation.setDocument(&document); + + QSignalSpy currentPageChangedSpy(&pageNavigation, &QPdfPageNavigation::currentPageChanged); + QSignalSpy pageCountChangedSpy(&pageNavigation, &QPdfPageNavigation::pageCountChanged); + QSignalSpy canGoToPreviousPageChangedSpy(&pageNavigation, &QPdfPageNavigation::canGoToPreviousPageChanged); + QSignalSpy canGoToNextPageChangedSpy(&pageNavigation, &QPdfPageNavigation::canGoToNextPageChanged); + + document.close(); + + QCOMPARE(currentPageChangedSpy.count(), 0); // current page stays '0' + QCOMPARE(pageCountChangedSpy.count(), 1); + QCOMPARE(pageCountChangedSpy[0][0].toInt(), 0); + QCOMPARE(canGoToPreviousPageChangedSpy.count(), 0); // still no previous page available + QCOMPARE(canGoToNextPageChangedSpy.count(), 1); + QCOMPARE(canGoToNextPageChangedSpy[0][0].toBool(), false); +} + +void tst_QPdfPageNavigation::navigate() +{ + QPdfDocument document; + QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.pagenavigation.pdf")), QPdfDocument::NoError); + + QPdfPageNavigation pageNavigation; + pageNavigation.setDocument(&document); + + QSignalSpy currentPageChangedSpy(&pageNavigation, &QPdfPageNavigation::currentPageChanged); + QSignalSpy canGoToPreviousPageChangedSpy(&pageNavigation, &QPdfPageNavigation::canGoToPreviousPageChanged); + QSignalSpy canGoToNextPageChangedSpy(&pageNavigation, &QPdfPageNavigation::canGoToNextPageChanged); + + QCOMPARE(pageNavigation.currentPage(), 0); + + // try to go to previous page while there is none + QCOMPARE(pageNavigation.canGoToPreviousPage(), false); + pageNavigation.goToPreviousPage(); + QCOMPARE(canGoToPreviousPageChangedSpy.count(), 0); + QCOMPARE(pageNavigation.currentPage(), 0); + QCOMPARE(pageNavigation.canGoToPreviousPage(), false); + + // try to go to next page + QCOMPARE(pageNavigation.canGoToNextPage(), true); + pageNavigation.goToNextPage(); + QCOMPARE(canGoToPreviousPageChangedSpy.count(), 1); + QCOMPARE(canGoToNextPageChangedSpy.count(), 0); + QCOMPARE(currentPageChangedSpy.count(), 1); + QCOMPARE(pageNavigation.currentPage(), 1); + QCOMPARE(pageNavigation.canGoToPreviousPage(), true); + + currentPageChangedSpy.clear(); + canGoToPreviousPageChangedSpy.clear(); + canGoToNextPageChangedSpy.clear(); + + // try to go to last page + pageNavigation.setCurrentPage(2); + QCOMPARE(canGoToPreviousPageChangedSpy.count(), 0); + QCOMPARE(canGoToNextPageChangedSpy.count(), 1); + QCOMPARE(currentPageChangedSpy.count(), 1); + QCOMPARE(pageNavigation.currentPage(), 2); + QCOMPARE(pageNavigation.canGoToNextPage(), false); + + // check that invalid requests are ignored + pageNavigation.setCurrentPage(-1); + QCOMPARE(pageNavigation.currentPage(), 2); + + pageNavigation.setCurrentPage(3); + QCOMPARE(pageNavigation.currentPage(), 2); +} + +QTEST_MAIN(tst_QPdfPageNavigation) + +#include "tst_qpdfpagenavigation.moc" diff --git a/tests/auto/pdf/qpdfpagerenderer/pdf-sample.pagerenderer.pdf b/tests/auto/pdf/qpdfpagerenderer/pdf-sample.pagerenderer.pdf Binary files differnew file mode 100644 index 000000000..c4e1aa36e --- /dev/null +++ b/tests/auto/pdf/qpdfpagerenderer/pdf-sample.pagerenderer.pdf diff --git a/tests/auto/pdf/qpdfpagerenderer/qpdfpagerenderer.pro b/tests/auto/pdf/qpdfpagerenderer/qpdfpagerenderer.pro new file mode 100644 index 000000000..9ccb4e82c --- /dev/null +++ b/tests/auto/pdf/qpdfpagerenderer/qpdfpagerenderer.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +TARGET = tst_qpdfpagerenderer +QT += pdf testlib network +macos:CONFIG -= app_bundle +SOURCES += tst_qpdfpagerenderer.cpp diff --git a/tests/auto/pdf/qpdfpagerenderer/tst_qpdfpagerenderer.cpp b/tests/auto/pdf/qpdfpagerenderer/tst_qpdfpagerenderer.cpp new file mode 100644 index 000000000..8eaef7c6e --- /dev/null +++ b/tests/auto/pdf/qpdfpagerenderer/tst_qpdfpagerenderer.cpp @@ -0,0 +1,184 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Tobias König <tobias.koenig@kdab.com> +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtPDF module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QPdfDocument> +#include <QPdfPageRenderer> + +#include <QtTest/QtTest> + +class tst_QPdfPageRenderer: public QObject +{ + Q_OBJECT + +private slots: + void defaultValues(); + void withNoDocument(); + void withEmptyDocument(); + void withLoadedDocumentSingleThreaded(); + void withLoadedDocumentMultiThreaded(); + void switchingRenderMode(); +}; + +void tst_QPdfPageRenderer::defaultValues() +{ + QPdfPageRenderer pageRenderer; + + QCOMPARE(pageRenderer.document(), nullptr); + QCOMPARE(pageRenderer.renderMode(), QPdfPageRenderer::SingleThreadedRenderMode); +} + +void tst_QPdfPageRenderer::withNoDocument() +{ + QPdfPageRenderer pageRenderer; + + const QSize imageSize(100, 100); + const quint64 requestId = pageRenderer.requestPage(0, imageSize); + + QCOMPARE(requestId, quint64(0)); +} + +void tst_QPdfPageRenderer::withEmptyDocument() +{ + QPdfDocument document; + QPdfPageRenderer pageRenderer; + + pageRenderer.setDocument(&document); + + const QSize imageSize(100, 100); + const quint64 requestId = pageRenderer.requestPage(0, imageSize); + + QCOMPARE(requestId, quint64(0)); +} + +void tst_QPdfPageRenderer::withLoadedDocumentSingleThreaded() +{ + QPdfDocument document; + QPdfPageRenderer pageRenderer; + pageRenderer.setDocument(&document); + + QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.pagerenderer.pdf")), QPdfDocument::NoError); + + QSignalSpy pageRenderedSpy(&pageRenderer, &QPdfPageRenderer::pageRendered); + + const QSize imageSize(100, 100); + const quint64 requestId = pageRenderer.requestPage(0, imageSize); + + QCOMPARE(requestId, quint64(1)); + QTRY_COMPARE(pageRenderedSpy.count(), 1); + QCOMPARE(pageRenderedSpy[0][0].toInt(), 0); + QCOMPARE(pageRenderedSpy[0][1].toSize(), imageSize); + QCOMPARE(pageRenderedSpy[0][2].value<QImage>().size(), imageSize); + QCOMPARE(pageRenderedSpy[0][4].toULongLong(), requestId); +} + +void tst_QPdfPageRenderer::withLoadedDocumentMultiThreaded() +{ + QPdfDocument document; + + QPdfPageRenderer pageRenderer; + pageRenderer.setDocument(&document); + pageRenderer.setRenderMode(QPdfPageRenderer::MultiThreadedRenderMode); + + QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.pagerenderer.pdf")), QPdfDocument::NoError); + + QSignalSpy pageRenderedSpy(&pageRenderer, &QPdfPageRenderer::pageRendered); + + const QSize imageSize(100, 100); + const quint64 requestId = pageRenderer.requestPage(0, imageSize); + + QCOMPARE(requestId, quint64(1)); + QTRY_COMPARE(pageRenderedSpy.count(), 1); + QCOMPARE(pageRenderedSpy[0][0].toInt(), 0); + QCOMPARE(pageRenderedSpy[0][1].toSize(), imageSize); + QCOMPARE(pageRenderedSpy[0][2].value<QImage>().size(), imageSize); + QCOMPARE(pageRenderedSpy[0][4].toULongLong(), requestId); +} + +void tst_QPdfPageRenderer::switchingRenderMode() +{ + QPdfDocument document; + QPdfPageRenderer pageRenderer; + pageRenderer.setDocument(&document); + + QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.pagerenderer.pdf")), QPdfDocument::NoError); + + QSignalSpy pageRenderedSpy(&pageRenderer, &QPdfPageRenderer::pageRendered); + + // render single threaded + const QSize imageSize(100, 100); + const quint64 firstRequestId = pageRenderer.requestPage(0, imageSize); + + QTRY_COMPARE(pageRenderedSpy.count(), 1); + QCOMPARE(pageRenderedSpy[0][0].toInt(), 0); + QCOMPARE(pageRenderedSpy[0][1].toSize(), imageSize); + QCOMPARE(pageRenderedSpy[0][2].value<QImage>().size(), imageSize); + QCOMPARE(pageRenderedSpy[0][4].toULongLong(), firstRequestId); + + const QImage image = pageRenderedSpy[0][2].value<QImage>(); + + pageRenderedSpy.clear(); + + // switch to multi threaded + pageRenderer.setRenderMode(QPdfPageRenderer::MultiThreadedRenderMode); + + const quint64 secondRequestId = pageRenderer.requestPage(0, imageSize); + + QVERIFY(firstRequestId != secondRequestId); + QTRY_COMPARE(pageRenderedSpy.count(), 1); + QCOMPARE(pageRenderedSpy[0][0].toInt(), 0); + QCOMPARE(pageRenderedSpy[0][1].toSize(), imageSize); + QCOMPARE(pageRenderedSpy[0][2].value<QImage>(), image); + QCOMPARE(pageRenderedSpy[0][2].value<QImage>().size(), imageSize); + QCOMPARE(pageRenderedSpy[0][4].toULongLong(), secondRequestId); + + pageRenderedSpy.clear(); + + // switch back to single threaded + pageRenderer.setRenderMode(QPdfPageRenderer::SingleThreadedRenderMode); + + const quint64 thirdRequestId = pageRenderer.requestPage(0, imageSize); + + QTRY_COMPARE(pageRenderedSpy.count(), 1); + QCOMPARE(pageRenderedSpy[0][0].toInt(), 0); + QCOMPARE(pageRenderedSpy[0][1].toSize(), imageSize); + QCOMPARE(pageRenderedSpy[0][2].value<QImage>(), image); + QCOMPARE(pageRenderedSpy[0][2].value<QImage>().size(), imageSize); + QCOMPARE(pageRenderedSpy[0][4].toULongLong(), thirdRequestId); +} + +QTEST_MAIN(tst_QPdfPageRenderer) + +#include "tst_qpdfpagerenderer.moc" diff --git a/tests/auto/pdf/qpdfsearchmodel/qpdfsearchmodel.pro b/tests/auto/pdf/qpdfsearchmodel/qpdfsearchmodel.pro new file mode 100644 index 000000000..205fef175 --- /dev/null +++ b/tests/auto/pdf/qpdfsearchmodel/qpdfsearchmodel.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +TARGET = tst_qpdfsearchmodel +QT += pdf testlib network +macos:CONFIG -= app_bundle +SOURCES += tst_qpdfsearchmodel.cpp diff --git a/tests/auto/pdf/qpdfsearchmodel/test.pdf b/tests/auto/pdf/qpdfsearchmodel/test.pdf Binary files differnew file mode 100644 index 000000000..a9dc1bc29 --- /dev/null +++ b/tests/auto/pdf/qpdfsearchmodel/test.pdf diff --git a/tests/auto/pdf/qpdfsearchmodel/tst_qpdfsearchmodel.cpp b/tests/auto/pdf/qpdfsearchmodel/tst_qpdfsearchmodel.cpp new file mode 100644 index 000000000..e690bfc11 --- /dev/null +++ b/tests/auto/pdf/qpdfsearchmodel/tst_qpdfsearchmodel.cpp @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <QPdfDocument> +#include <QPdfSearchModel> + +class tst_QPdfSearchModel: public QObject +{ + Q_OBJECT + +public: + tst_QPdfSearchModel() {} + +private slots: + void findText(); +}; + +void tst_QPdfSearchModel::findText() +{ + QPdfDocument document; + QCOMPARE(document.load(QFINDTESTDATA("test.pdf")), QPdfDocument::NoError); + + QPdfSearchModel model; + model.setDocument(&document); + QVector<QRectF> matches = model.matches(1, "ai"); + + qDebug() << matches; + QCOMPARE(matches.count(), 3); +} + +QTEST_MAIN(tst_QPdfSearchModel) + +#include "tst_qpdfsearchmodel.moc" |