Warning, file /graphics/okular/autotests/modifyannotationpropertiestest.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 <QTest>
0008 
0009 #include "../settings_core.h"
0010 #include "core/document.h"
0011 #include "testingutils.h"
0012 #include <QMimeDatabase>
0013 #include <QMimeType>
0014 #include <core/annotations.h>
0015 #include <core/area.h>
0016 
0017 class ModifyAnnotationPropertiesTest : public QObject
0018 {
0019     Q_OBJECT
0020 
0021 private Q_SLOTS:
0022     void initTestCase();
0023     void cleanupTestCase();
0024     void init();
0025     void cleanup();
0026     void testModifyAnnotationProperties();
0027     void testModifyDefaultAnnotationProperties();
0028     void testModifyAnnotationPropertiesWithRotation_Bug318828();
0029 
0030 private:
0031     Okular::Document *m_document;
0032     Okular::TextAnnotation *m_annot1;
0033 };
0034 
0035 void ModifyAnnotationPropertiesTest::initTestCase()
0036 {
0037     Okular::SettingsCore::instance(QStringLiteral("editannotationcontentstest"));
0038     m_document = new Okular::Document(nullptr);
0039 }
0040 
0041 void ModifyAnnotationPropertiesTest::cleanupTestCase()
0042 {
0043     delete m_document;
0044 }
0045 
0046 void ModifyAnnotationPropertiesTest::init()
0047 {
0048     const QString testFile = QStringLiteral(KDESRCDIR "data/file1.pdf");
0049     QMimeDatabase db;
0050     const QMimeType mime = db.mimeTypeForFile(testFile);
0051     QCOMPARE(m_document->openDocument(testFile, QUrl(), mime), Okular::Document::OpenSuccess);
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     m_annot1 = new Okular::TextAnnotation();
0059     m_annot1->setBoundingRectangle(Okular::NormalizedRect(0.1, 0.1, 0.15, 0.15));
0060     m_annot1->setContents(QStringLiteral("Hello, World"));
0061     m_annot1->setAuthor(QStringLiteral("Jon Mease"));
0062     m_annot1->style().setColor(Qt::red);
0063     m_annot1->style().setWidth(4.0);
0064     m_document->addPageAnnotation(0, m_annot1);
0065 }
0066 
0067 void ModifyAnnotationPropertiesTest::cleanup()
0068 {
0069     m_document->closeDocument();
0070     // m_annot1 and m_annot2 are deleted when document is closed
0071 }
0072 
0073 void ModifyAnnotationPropertiesTest::testModifyAnnotationProperties()
0074 {
0075     // Add m_annot1 to document and record its properties XML string
0076     QString origLine1Xml = TestingUtils::getAnnotationXml(m_annot1);
0077 
0078     // Tell document we're going to modify m_annot1's properties
0079     m_document->prepareToModifyAnnotationProperties(m_annot1);
0080 
0081     // Now modify m_annot1's properties and record properties XML string
0082     m_annot1->style().setWidth(8.0);
0083     m_annot1->style().setColor(Qt::green);
0084     m_document->modifyPageAnnotationProperties(0, m_annot1);
0085     QString m_annot1XmlA = TestingUtils::getAnnotationXml(m_annot1);
0086     QCOMPARE(8.0, m_annot1->style().width());
0087     QCOMPARE(QColor(Qt::green), m_annot1->style().color());
0088 
0089     // undo modification and check that original properties have been restored
0090     m_document->undo();
0091     QCOMPARE(4.0, m_annot1->style().width());
0092     QCOMPARE(QColor(Qt::red), m_annot1->style().color());
0093     QCOMPARE(origLine1Xml, TestingUtils::getAnnotationXml(m_annot1));
0094 
0095     // redo modification and verify that new properties have been restored
0096     m_document->redo();
0097     QCOMPARE(8.0, m_annot1->style().width());
0098     QCOMPARE(QColor(Qt::green), m_annot1->style().color());
0099     QCOMPARE(m_annot1XmlA, TestingUtils::getAnnotationXml(m_annot1));
0100 
0101     // Verify that default values are properly restored.  (We haven't explicitly set opacity yet)
0102     QCOMPARE(1.0, m_annot1->style().opacity());
0103     m_document->prepareToModifyAnnotationProperties(m_annot1);
0104     m_annot1->style().setOpacity(0.5);
0105     m_document->modifyPageAnnotationProperties(0, m_annot1);
0106     QCOMPARE(0.5, m_annot1->style().opacity());
0107 
0108     m_document->undo();
0109     QCOMPARE(1.0, m_annot1->style().opacity());
0110     QCOMPARE(m_annot1XmlA, TestingUtils::getAnnotationXml(m_annot1));
0111 
0112     // And finally undo back to original properties
0113     m_document->undo();
0114     QCOMPARE(4.0, m_annot1->style().width());
0115     QCOMPARE(QColor(Qt::red), m_annot1->style().color());
0116     QCOMPARE(origLine1Xml, TestingUtils::getAnnotationXml(m_annot1));
0117 }
0118 
0119 void ModifyAnnotationPropertiesTest::testModifyDefaultAnnotationProperties()
0120 {
0121     QString origLine1Xml = TestingUtils::getAnnotationXml(m_annot1);
0122 
0123     // Verify that default values are properly restored.  (We haven't explicitly set opacity yet)
0124     QCOMPARE(1.0, m_annot1->style().opacity());
0125     m_document->prepareToModifyAnnotationProperties(m_annot1);
0126     m_annot1->style().setOpacity(0.5);
0127     m_document->modifyPageAnnotationProperties(0, m_annot1);
0128     QCOMPARE(0.5, m_annot1->style().opacity());
0129 
0130     m_document->undo();
0131     QCOMPARE(1.0, m_annot1->style().opacity());
0132     QCOMPARE(origLine1Xml, TestingUtils::getAnnotationXml(m_annot1));
0133 }
0134 
0135 void ModifyAnnotationPropertiesTest::testModifyAnnotationPropertiesWithRotation_Bug318828()
0136 {
0137     Okular::NormalizedRect boundingRect = Okular::NormalizedRect(0.1, 0.1, 0.15, 0.15);
0138     Okular::NormalizedRect transformedBoundingRect;
0139     m_annot1->setBoundingRectangle(boundingRect);
0140     m_document->addPageAnnotation(0, m_annot1);
0141 
0142     transformedBoundingRect = m_annot1->transformedBoundingRectangle();
0143 
0144     // Before page rotation boundingRect and transformedBoundingRect should be equal
0145     QCOMPARE(boundingRect, transformedBoundingRect);
0146     m_document->setRotation(1);
0147 
0148     // After rotation boundingRect should remain unchanged but
0149     // transformedBoundingRect should no longer equal boundingRect
0150     QCOMPARE(boundingRect, m_annot1->boundingRectangle());
0151     transformedBoundingRect = m_annot1->transformedBoundingRectangle();
0152     QVERIFY(!(boundingRect == transformedBoundingRect));
0153 
0154     // Modifying the properties of m_annot1 while page is rotated shouldn't
0155     // alter either boundingRect or transformedBoundingRect
0156     m_document->prepareToModifyAnnotationProperties(m_annot1);
0157     m_annot1->style().setOpacity(0.5);
0158     m_document->modifyPageAnnotationProperties(0, m_annot1);
0159 
0160     QCOMPARE(boundingRect, m_annot1->boundingRectangle());
0161     QCOMPARE(transformedBoundingRect, m_annot1->transformedBoundingRectangle());
0162 }
0163 
0164 QTEST_MAIN(ModifyAnnotationPropertiesTest)
0165 #include "modifyannotationpropertiestest.moc"