Warning, file /graphics/okular/autotests/addremoveannotationtest.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 Jon Mease <jon.mease@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QMimeDatabase>
0008 #include <QTest>
0009 
0010 #include "../core/annotations.h"
0011 #include "../core/document.h"
0012 #include "../core/page.h"
0013 #include "../settings_core.h"
0014 #include "testingutils.h"
0015 
0016 class AddRemoveAnnotationTest : public QObject
0017 {
0018     Q_OBJECT
0019 
0020 private Q_SLOTS:
0021     void initTestCase();
0022     void init();
0023     void cleanup();
0024     void testAddAnnotations();
0025     void testAddAnnotationUndoWithRotate_Bug318091();
0026     void testRemoveAnnotations();
0027 
0028 private:
0029     Okular::Document *m_document;
0030 };
0031 
0032 void AddRemoveAnnotationTest::initTestCase()
0033 {
0034     Okular::SettingsCore::instance(QStringLiteral("addannotationtest"));
0035     m_document = new Okular::Document(nullptr);
0036 }
0037 
0038 void AddRemoveAnnotationTest::init()
0039 {
0040     const QString testFile = QStringLiteral(KDESRCDIR "data/file1.pdf");
0041     QMimeDatabase db;
0042     const QMimeType mime = db.mimeTypeForFile(testFile);
0043     QCOMPARE(m_document->openDocument(testFile, QUrl(), mime), Okular::Document::OpenSuccess);
0044 }
0045 
0046 void AddRemoveAnnotationTest::cleanup()
0047 {
0048     m_document->closeDocument();
0049 }
0050 
0051 void AddRemoveAnnotationTest::testAddAnnotations()
0052 {
0053     // Undo and Redo should be unavailable when docuemnt is first opened.
0054     QVERIFY(!m_document->canUndo());
0055     QVERIFY(!m_document->canRedo());
0056 
0057     // Create two distinct text annotations
0058     Okular::Annotation *annot1 = new Okular::TextAnnotation();
0059     annot1->setBoundingRectangle(Okular::NormalizedRect(0.1, 0.1, 0.15, 0.15));
0060     annot1->setContents(QStringLiteral("annot contents"));
0061 
0062     Okular::Annotation *annot2 = new Okular::TextAnnotation();
0063     annot2->setBoundingRectangle(Okular::NormalizedRect(0.2, 0.2, 0.3, 0.4));
0064     annot2->setContents(QStringLiteral("annot contents"));
0065 
0066     // The two annotations shold have different properties XML strings
0067     QVERIFY(TestingUtils::getAnnotationXml(annot1) != TestingUtils::getAnnotationXml(annot2));
0068 
0069     // We start with no annotations in the docuemnt
0070     QVERIFY(m_document->page(0)->annotations().size() == 0);
0071 
0072     // After adding annot1 we should have one annotation in the page and it should be annot1.
0073     m_document->addPageAnnotation(0, annot1);
0074     QVERIFY(m_document->page(0)->annotations().size() == 1);
0075     QCOMPARE(annot1, m_document->page(0)->annotations().first());
0076 
0077     // Record the properties and name of annot1 just after insertion for later comparisons
0078     QString origLine1Xml = TestingUtils::getAnnotationXml(annot1);
0079     QString annot1Name = annot1->uniqueName();
0080     QVERIFY(!annot1Name.isEmpty());
0081 
0082     // Now undo the addition of annot1 and verify that annot1's properties haven't changed
0083     m_document->undo();
0084     QVERIFY(m_document->page(0)->annotations().empty());
0085     QVERIFY(!m_document->canUndo());
0086     QVERIFY(m_document->canRedo());
0087     QCOMPARE(TestingUtils::getAnnotationXml(annot1), origLine1Xml);
0088 
0089     // redo addition of annot1
0090     m_document->redo();
0091     QVERIFY(m_document->page(0)->annotations().size() == 1);
0092     QVERIFY(annot1 == m_document->page(0)->annotations().first());
0093     QCOMPARE(TestingUtils::getAnnotationXml(annot1), origLine1Xml);
0094 
0095     // undo once more
0096     m_document->undo();
0097     QVERIFY(m_document->page(0)->annotations().empty());
0098     QVERIFY(!m_document->canUndo());
0099     QVERIFY(m_document->canRedo());
0100     QCOMPARE(TestingUtils::getAnnotationXml(annot1), origLine1Xml);
0101 
0102     // Set AnnotationDisposeWatcher dispose function on annot1 so we can detect
0103     // when it is deleted
0104     annot1->setDisposeDataFunction(TestingUtils::AnnotationDisposeWatcher::disposeAnnotation);
0105     TestingUtils::AnnotationDisposeWatcher::resetDisposedAnnotationName();
0106     QCOMPARE(TestingUtils::AnnotationDisposeWatcher::disposedAnnotationName(), QString());
0107 
0108     // now add annot2
0109     m_document->addPageAnnotation(0, annot2);
0110     QString annot2Name = annot2->uniqueName();
0111     QVERIFY(!annot2Name.isEmpty());
0112     QVERIFY(annot1Name != annot2Name);
0113     QVERIFY(m_document->page(0)->annotations().size() == 1);
0114     QCOMPARE(annot2, m_document->page(0)->annotations().first());
0115 
0116     // Check that adding annot2 while annot1 was in the unadded state triggered the deletion of annot1
0117     QVERIFY(TestingUtils::AnnotationDisposeWatcher::disposedAnnotationName() == annot1Name);
0118 }
0119 
0120 void AddRemoveAnnotationTest::testAddAnnotationUndoWithRotate_Bug318091()
0121 {
0122     Okular::Annotation *annot = new Okular::TextAnnotation();
0123     annot->setBoundingRectangle(Okular::NormalizedRect(0.1, 0.1, 0.15, 0.15));
0124     annot->setContents(QStringLiteral("annot contents"));
0125 
0126     m_document->addPageAnnotation(0, annot);
0127     QString origAnnotXml = TestingUtils::getAnnotationXml(annot);
0128 
0129     // Now undo annotation addition, rotate the page, and redo to annotation addition
0130     m_document->undo();
0131     m_document->setRotation(1);
0132     m_document->redo();
0133 
0134     // Verify that annotation's properties remain unchanged
0135     // In Bug318091 the bounding rectangle was being rotated upon each redo
0136     QString newAnnotXml = TestingUtils::getAnnotationXml(annot);
0137     QCOMPARE(origAnnotXml, newAnnotXml);
0138 }
0139 
0140 void AddRemoveAnnotationTest::testRemoveAnnotations()
0141 {
0142     // Undo and Redo should be unavailable when docuemnt is first opened.
0143     QVERIFY(!m_document->canUndo());
0144     QVERIFY(!m_document->canRedo());
0145 
0146     // Create two distinct text annotations
0147     Okular::Annotation *annot1 = new Okular::TextAnnotation();
0148     annot1->setBoundingRectangle(Okular::NormalizedRect(0.1, 0.1, 0.15, 0.15));
0149     annot1->setContents(QStringLiteral("annot contents"));
0150 
0151     Okular::Annotation *annot2 = new Okular::TextAnnotation();
0152     annot2->setBoundingRectangle(Okular::NormalizedRect(0.2, 0.2, 0.3, 0.4));
0153     annot2->setContents(QStringLiteral("annot contents"));
0154 
0155     // Add annot1 and annot2 to document
0156     m_document->addPageAnnotation(0, annot1);
0157     m_document->addPageAnnotation(0, annot2);
0158     QVERIFY(m_document->page(0)->annotations().size() == 2);
0159     QVERIFY(m_document->page(0)->annotations().contains(annot1));
0160     QVERIFY(m_document->page(0)->annotations().contains(annot2));
0161 
0162     // Now remove annot1
0163     m_document->removePageAnnotation(0, annot1);
0164     QVERIFY(m_document->page(0)->annotations().size() == 1);
0165     QVERIFY(m_document->page(0)->annotations().contains(annot2));
0166 
0167     // Undo removal of annot1
0168     m_document->undo();
0169     QVERIFY(m_document->page(0)->annotations().size() == 2);
0170     QVERIFY(m_document->page(0)->annotations().contains(annot1));
0171     QVERIFY(m_document->page(0)->annotations().contains(annot2));
0172 
0173     // Redo removal
0174     m_document->redo();
0175     QVERIFY(m_document->page(0)->annotations().size() == 1);
0176     QVERIFY(m_document->page(0)->annotations().contains(annot2));
0177 
0178     // Verify that annot1 is disposed of if document is closed with annot1 in removed state
0179     QString annot1Name = annot1->uniqueName();
0180     annot1->setDisposeDataFunction(TestingUtils::AnnotationDisposeWatcher::disposeAnnotation);
0181     TestingUtils::AnnotationDisposeWatcher::resetDisposedAnnotationName();
0182     QVERIFY(TestingUtils::AnnotationDisposeWatcher::disposedAnnotationName().isEmpty());
0183     m_document->closeDocument();
0184     QVERIFY(TestingUtils::AnnotationDisposeWatcher::disposedAnnotationName() == annot1Name);
0185 }
0186 
0187 QTEST_MAIN(AddRemoveAnnotationTest)
0188 #include "addremoveannotationtest.moc"