summaryrefslogtreecommitdiffstats
path: root/tests/auto/pdf
diff options
context:
space:
mode:
authorTobias Koenig <tobias.koenig@kdab.com>2016-08-22 14:28:40 +0200
committerMichal Klocek <michal.klocek@qt.io>2019-11-25 12:01:39 +0100
commit398880564621b06a79a4ba245baf350022ad438a (patch)
tree3beef2d144aa09ffba4c51fa9230c86e0d378a8f /tests/auto/pdf
parentdf9eb7e46608cce59cb7910cbf2025a53d4de969 (diff)
Add QPdfBookmarkModel class
Change-Id: I000a398d2347870916bd93b290a1ddf5023fb0ce Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'tests/auto/pdf')
-rw-r--r--tests/auto/pdf/pdf.pro4
-rw-r--r--tests/auto/pdf/qpdfbookmarkmodel/pdf-sample.bookmarks.pdfbin0 -> 8925 bytes
-rw-r--r--tests/auto/pdf/qpdfbookmarkmodel/qpdfbookmarkmodel.pro5
-rw-r--r--tests/auto/pdf/qpdfbookmarkmodel/tst_qpdfbookmarkmodel.cpp246
4 files changed, 254 insertions, 1 deletions
diff --git a/tests/auto/pdf/pdf.pro b/tests/auto/pdf/pdf.pro
index bb4d02075..04e36b3dc 100644
--- a/tests/auto/pdf/pdf.pro
+++ b/tests/auto/pdf/pdf.pro
@@ -1,2 +1,4 @@
TEMPLATE = subdirs
-qtHaveModule(printsupport): SUBDIRS = qpdfdocument
+
+SUBDIRS = qpdfbookmarkmodel
+qtHaveModule(printsupport): SUBDIRS += qpdfdocument
diff --git a/tests/auto/pdf/qpdfbookmarkmodel/pdf-sample.bookmarks.pdf b/tests/auto/pdf/qpdfbookmarkmodel/pdf-sample.bookmarks.pdf
new file mode 100644
index 000000000..bd27c18b6
--- /dev/null
+++ b/tests/auto/pdf/qpdfbookmarkmodel/pdf-sample.bookmarks.pdf
Binary files differ
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..afaaf4d6f
--- /dev/null
+++ b/tests/auto/pdf/qpdfbookmarkmodel/tst_qpdfbookmarkmodel.cpp
@@ -0,0 +1,246 @@
+/******************************************************************************
+**
+** Copyright (C) 2016 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing/
+**
+** This file is part of the Qt PDF Module.
+**
+** $QT_BEGIN_LICENSE:COMM$
+**
+** 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.
+**
+** $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 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());
+}
+QTEST_MAIN(tst_QPdfBookmarkModel)
+
+#include "tst_qpdfbookmarkmodel.moc"