1 /*
2 SPDX-FileCopyrightText: 2013 Fabio D 'Urso <fabiodurso@hotmail.it>
3
4 Work sponsored by the LiMux project of the city of Munich:
5 SPDX-FileCopyrightText: 2017 Klarälvdalens Datakonsult AB a KDAB Group company <info@kdab.com>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
10 #include <QMimeDatabase>
11 #include <QTemporaryFile>
12 #include <QTest>
13
14 #include <threadweaver/queue.h>
15
16 #include "../core/annotations.h"
17 #include "../core/document.h"
18 #include "../core/document_p.h"
19 #include "../core/generator.h"
20 #include "../core/observer.h"
21 #include "../core/page.h"
22 #include "../core/rotationjob_p.h"
23 #include "../settings_core.h"
24
25 class DocumentTest : public QObject
26 {
27 Q_OBJECT
28
29 private slots:
30 void testCloseDuringRotationJob();
31 void testDocdataMigration();
32 };
33
34 // Test that we don't crash if the document is closed while a RotationJob
35 // is enqueued/running
testCloseDuringRotationJob()36 void DocumentTest::testCloseDuringRotationJob()
37 {
38 Okular::SettingsCore::instance(QStringLiteral("documenttest"));
39 Okular::Document *m_document = new Okular::Document(nullptr);
40 const QString testFile = QStringLiteral(KDESRCDIR "data/file1.pdf");
41 QMimeDatabase db;
42 const QMimeType mime = db.mimeTypeForFile(testFile);
43
44 Okular::DocumentObserver *dummyDocumentObserver = new Okular::DocumentObserver();
45 m_document->addObserver(dummyDocumentObserver);
46
47 m_document->openDocument(testFile, QUrl(), mime);
48 m_document->setRotation(1);
49
50 // Tell ThreadWeaver not to start any new job
51 ThreadWeaver::Queue::instance()->suspend();
52
53 // Request a pixmap. A RotationJob will be enqueued but not started
54 Okular::PixmapRequest *pixmapReq = new Okular::PixmapRequest(dummyDocumentObserver, 0, 100, 100, qApp->devicePixelRatio(), 1, Okular::PixmapRequest::NoFeature);
55 m_document->requestPixmaps(QLinkedList<Okular::PixmapRequest *>() << pixmapReq);
56
57 // Delete the document
58 delete m_document;
59
60 // Resume job processing and wait for the RotationJob to finish
61 ThreadWeaver::Queue::instance()->resume();
62 ThreadWeaver::Queue::instance()->finish();
63 qApp->processEvents();
64
65 delete dummyDocumentObserver;
66 }
67
68 // Test that, if there's a XML file in docdata referring to a document, we
69 // detect that it must be migrated, that it doesn't get wiped out if you close
70 // the document without migrating and that it does get wiped out after migrating
testDocdataMigration()71 void DocumentTest::testDocdataMigration()
72 {
73 Okular::SettingsCore::instance(QStringLiteral("documenttest"));
74
75 const QUrl testFileUrl = QUrl::fromLocalFile(KDESRCDIR "data/file1.pdf");
76 const QString testFilePath = testFileUrl.toLocalFile();
77 const qint64 testFileSize = QFileInfo(testFilePath).size();
78
79 // Copy XML file to the docdata/ directory
80 const QString docDataPath = Okular::DocumentPrivate::docDataFileName(testFileUrl, testFileSize);
81 QFile::remove(docDataPath);
82 QVERIFY(QFile::copy(KDESRCDIR "data/file1-docdata.xml", docDataPath));
83
84 // Open our document
85 Okular::Document *m_document = new Okular::Document(nullptr);
86 QMimeDatabase db;
87 const QMimeType mime = db.mimeTypeForFile(testFilePath);
88 QCOMPARE(m_document->openDocument(testFilePath, testFileUrl, mime), Okular::Document::OpenSuccess);
89
90 // Check that the annotation from file1-docdata.xml was loaded
91 QCOMPARE(m_document->page(0)->annotations().size(), 1);
92 QCOMPARE(m_document->page(0)->annotations().first()->uniqueName(), QStringLiteral("testannot"));
93
94 // Check that we detect that it must be migrated
95 QVERIFY(m_document->isDocdataMigrationNeeded());
96 m_document->closeDocument();
97
98 // Reopen the document and check that the annotation is still present
99 // (because we have not migrated)
100 QCOMPARE(m_document->openDocument(testFilePath, testFileUrl, mime), Okular::Document::OpenSuccess);
101 QCOMPARE(m_document->page(0)->annotations().size(), 1);
102 QCOMPARE(m_document->page(0)->annotations().first()->uniqueName(), QStringLiteral("testannot"));
103 QVERIFY(m_document->isDocdataMigrationNeeded());
104
105 // Do the migration
106 QTemporaryFile migratedSaveFile(QStringLiteral("%1/okrXXXXXX.pdf").arg(QDir::tempPath()));
107 QVERIFY(migratedSaveFile.open());
108 migratedSaveFile.close();
109 QVERIFY(m_document->saveChanges(migratedSaveFile.fileName()));
110 m_document->docdataMigrationDone();
111 QVERIFY(!m_document->isDocdataMigrationNeeded());
112 m_document->closeDocument();
113
114 // Now the docdata file should have no annotations, let's check
115 QCOMPARE(m_document->openDocument(testFilePath, testFileUrl, mime), Okular::Document::OpenSuccess);
116 QCOMPARE(m_document->page(0)->annotations().size(), 0);
117 QVERIFY(!m_document->isDocdataMigrationNeeded());
118 m_document->closeDocument();
119
120 // And the new file should have 1 annotation, let's check
121 QCOMPARE(m_document->openDocument(migratedSaveFile.fileName(), QUrl::fromLocalFile(migratedSaveFile.fileName()), mime), Okular::Document::OpenSuccess);
122 QCOMPARE(m_document->page(0)->annotations().size(), 1);
123 QVERIFY(!m_document->isDocdataMigrationNeeded());
124 m_document->closeDocument();
125
126 delete m_document;
127 }
128
129 QTEST_MAIN(DocumentTest)
130 #include "documenttest.moc"
131