Warning, file /graphics/okular/autotests/documenttest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 SPDX-FileCopyrightText: 2013 Fabio D 'Urso <fabiodurso@hotmail.it> 0003 0004 Work sponsored by the LiMux project of the city of Munich: 0005 SPDX-FileCopyrightText: 2017 Klarälvdalens Datakonsult AB a KDAB Group company <info@kdab.com> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 0010 #include <QMimeDatabase> 0011 #include <QTemporaryFile> 0012 #include <QTest> 0013 0014 #include <threadweaver/queue.h> 0015 0016 #include "../core/annotations.h" 0017 #include "../core/document.h" 0018 #include "../core/document_p.h" 0019 #include "../core/generator.h" 0020 #include "../core/observer.h" 0021 #include "../core/page.h" 0022 #include "../core/rotationjob_p.h" 0023 #include "../settings_core.h" 0024 0025 class DocumentTest : public QObject 0026 { 0027 Q_OBJECT 0028 0029 private Q_SLOTS: 0030 void testCloseDuringRotationJob(); 0031 void testDocdataMigration(); 0032 void testDiff_data(); 0033 void testDiff(); 0034 }; 0035 0036 // Test that we don't crash if the document is closed while a RotationJob 0037 // is enqueued/running 0038 void DocumentTest::testCloseDuringRotationJob() 0039 { 0040 Okular::SettingsCore::instance(QStringLiteral("documenttest")); 0041 Okular::Document *m_document = new Okular::Document(nullptr); 0042 const QString testFile = QStringLiteral(KDESRCDIR "data/file1.pdf"); 0043 QMimeDatabase db; 0044 const QMimeType mime = db.mimeTypeForFile(testFile); 0045 0046 Okular::DocumentObserver *dummyDocumentObserver = new Okular::DocumentObserver(); 0047 m_document->addObserver(dummyDocumentObserver); 0048 0049 m_document->openDocument(testFile, QUrl(), mime); 0050 m_document->setRotation(1); 0051 0052 // Tell ThreadWeaver not to start any new job 0053 ThreadWeaver::Queue::instance()->suspend(); 0054 0055 // Request a pixmap. A RotationJob will be enqueued but not started 0056 Okular::PixmapRequest *pixmapReq = new Okular::PixmapRequest(dummyDocumentObserver, 0, 100, 100, qApp->devicePixelRatio(), 1, Okular::PixmapRequest::NoFeature); 0057 m_document->requestPixmaps({pixmapReq}); 0058 0059 // Delete the document 0060 delete m_document; 0061 0062 // Resume job processing and wait for the RotationJob to finish 0063 ThreadWeaver::Queue::instance()->resume(); 0064 ThreadWeaver::Queue::instance()->finish(); 0065 qApp->processEvents(); 0066 0067 delete dummyDocumentObserver; 0068 } 0069 0070 // Test that, if there's a XML file in docdata referring to a document, we 0071 // detect that it must be migrated, that it doesn't get wiped out if you close 0072 // the document without migrating and that it does get wiped out after migrating 0073 void DocumentTest::testDocdataMigration() 0074 { 0075 Okular::SettingsCore::instance(QStringLiteral("documenttest")); 0076 0077 const QUrl testFileUrl = QUrl::fromLocalFile(QStringLiteral(KDESRCDIR "data/file1.pdf")); 0078 const QString testFilePath = testFileUrl.toLocalFile(); 0079 const qint64 testFileSize = QFileInfo(testFilePath).size(); 0080 0081 // Copy XML file to the docdata/ directory 0082 const QString docDataPath = Okular::DocumentPrivate::docDataFileName(testFileUrl, testFileSize); 0083 QFile::remove(docDataPath); 0084 QVERIFY(QFile::copy(QStringLiteral(KDESRCDIR "data/file1-docdata.xml"), docDataPath)); 0085 0086 // Open our document 0087 Okular::Document *m_document = new Okular::Document(nullptr); 0088 QMimeDatabase db; 0089 const QMimeType mime = db.mimeTypeForFile(testFilePath); 0090 QCOMPARE(m_document->openDocument(testFilePath, testFileUrl, mime), Okular::Document::OpenSuccess); 0091 0092 // Check that the annotation from file1-docdata.xml was loaded 0093 QCOMPARE(m_document->page(0)->annotations().size(), 1); 0094 QCOMPARE(m_document->page(0)->annotations().first()->uniqueName(), QStringLiteral("testannot")); 0095 0096 // Check that we detect that it must be migrated 0097 QVERIFY(m_document->isDocdataMigrationNeeded()); 0098 m_document->closeDocument(); 0099 0100 // Reopen the document and check that the annotation is still present 0101 // (because we have not migrated) 0102 QCOMPARE(m_document->openDocument(testFilePath, testFileUrl, mime), Okular::Document::OpenSuccess); 0103 QCOMPARE(m_document->page(0)->annotations().size(), 1); 0104 QCOMPARE(m_document->page(0)->annotations().first()->uniqueName(), QStringLiteral("testannot")); 0105 QVERIFY(m_document->isDocdataMigrationNeeded()); 0106 0107 // Do the migration 0108 QTemporaryFile migratedSaveFile(QStringLiteral("%1/okrXXXXXX.pdf").arg(QDir::tempPath())); 0109 QVERIFY(migratedSaveFile.open()); 0110 migratedSaveFile.close(); 0111 QVERIFY(m_document->saveChanges(migratedSaveFile.fileName())); 0112 m_document->docdataMigrationDone(); 0113 QVERIFY(!m_document->isDocdataMigrationNeeded()); 0114 m_document->closeDocument(); 0115 0116 // Now the docdata file should have no annotations, let's check 0117 QCOMPARE(m_document->openDocument(testFilePath, testFileUrl, mime), Okular::Document::OpenSuccess); 0118 QCOMPARE(m_document->page(0)->annotations().size(), 0); 0119 QVERIFY(!m_document->isDocdataMigrationNeeded()); 0120 m_document->closeDocument(); 0121 0122 // And the new file should have 1 annotation, let's check 0123 QCOMPARE(m_document->openDocument(migratedSaveFile.fileName(), QUrl::fromLocalFile(migratedSaveFile.fileName()), mime), Okular::Document::OpenSuccess); 0124 QCOMPARE(m_document->page(0)->annotations().size(), 1); 0125 QVERIFY(!m_document->isDocdataMigrationNeeded()); 0126 m_document->closeDocument(); 0127 0128 delete m_document; 0129 } 0130 0131 void DocumentTest::testDiff_data() 0132 { 0133 QTest::addColumn<QString>("oldVal"); 0134 QTest::addColumn<QString>("newVal"); 0135 QTest::addColumn<QString>("expectedDiff"); 0136 0137 QTest::addRow("empty") << "" 0138 << "" 0139 << ""; 0140 QTest::addRow("a") << "" 0141 << "a" 0142 << "a"; 0143 QTest::addRow("ab") << "a" 0144 << "b" 0145 << "b"; 0146 QTest::addRow("ab2") << "a" 0147 << "ab" 0148 << "b"; 0149 QTest::addRow("kaesekuchen") << "Käse" 0150 << "Käsekuchen" 0151 << "kuchen"; 0152 QTest::addRow("replace") << "kuchen" 0153 << "wurst" 0154 << "wurst"; 0155 QTest::addRow("okular") << "Oku" 0156 << "Okular" 0157 << "lar"; 0158 QTest::addRow("removal1") << "a" 0159 << "" 0160 << ""; 0161 QTest::addRow("removal2") << "ab" 0162 << "a" 0163 << ""; 0164 QTest::addRow("unicode") << "☮🤌" 0165 << "☮🤌❤️" 0166 << "❤️"; 0167 QTest::addRow("unicode2") << "☮" 0168 << "☮🤌❤️" 0169 << "🤌❤️"; 0170 QTest::addRow("unicode3") << "🤍" 0171 << "🤌" 0172 << "🤌"; 0173 } 0174 0175 void DocumentTest::testDiff() 0176 { 0177 QFETCH(QString, oldVal); 0178 QFETCH(QString, newVal); 0179 QFETCH(QString, expectedDiff); 0180 0181 QCOMPARE(Okular::DocumentPrivate::diff(oldVal, newVal), expectedDiff); 0182 } 0183 0184 QTEST_MAIN(DocumentTest) 0185 #include "documenttest.moc"