File indexing completed on 2024-05-12 04:33:26
0001 /* 0002 SPDX-FileCopyrightText: 2013 Peter Grasch <me@bedahr.org> 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 0015 Q_DECLARE_METATYPE(Okular::Annotation *) 0016 Q_DECLARE_METATYPE(Okular::LineAnnotation *) 0017 0018 class AnnotationTest : public QObject 0019 { 0020 Q_OBJECT 0021 0022 private Q_SLOTS: 0023 void initTestCase(); 0024 void testDistance(); 0025 void testDistance_data(); 0026 // void testLine(); 0027 // void testPoly(); 0028 // void testInk(); 0029 // void testHighlight(); 0030 // void testGeom(); 0031 void testTypewriter(); 0032 void cleanupTestCase(); 0033 0034 private: 0035 Okular::Document *m_document; 0036 }; 0037 0038 void AnnotationTest::initTestCase() 0039 { 0040 Okular::SettingsCore::instance(QStringLiteral("annotationtest")); 0041 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 QCOMPARE(m_document->openDocument(testFile, QUrl(), mime), Okular::Document::OpenSuccess); 0046 } 0047 0048 void AnnotationTest::cleanupTestCase() 0049 { 0050 if (m_document->isOpened()) { 0051 const QList<Okular::Annotation *> annotations = m_document->page(0)->annotations(); 0052 for (Okular::Annotation *annotation : annotations) { 0053 m_document->removePageAnnotation(0, annotation); 0054 } 0055 } 0056 } 0057 0058 void AnnotationTest::testDistance() 0059 { 0060 QFETCH(Okular::Annotation *, annotation); 0061 QFETCH(double, x); 0062 QFETCH(double, y); 0063 QFETCH(int, distance); 0064 Okular::AnnotationObjectRect *rect = new Okular::AnnotationObjectRect(annotation); 0065 QCOMPARE(qRound(rect->distanceSqr(x, y, m_document->page(0)->width(), m_document->page(0)->height())), distance); 0066 delete rect; 0067 } 0068 0069 void AnnotationTest::testDistance_data() 0070 { 0071 QTest::addColumn<Okular::Annotation *>("annotation"); 0072 QTest::addColumn<double>("x"); 0073 QTest::addColumn<double>("y"); 0074 QTest::addColumn<int>("distance"); 0075 0076 double documentX = m_document->page(0)->width(); 0077 double documentY = m_document->page(0)->height(); 0078 0079 // lines 0080 Okular::LineAnnotation *line = new Okular::LineAnnotation; 0081 line->setLinePoints({Okular::NormalizedPoint(0.1, 0.1), Okular::NormalizedPoint(0.9, 0.1)}); 0082 m_document->addPageAnnotation(0, line); 0083 QTest::newRow("Line: Base point 1") << (Okular::Annotation *)line << 0.1 << 0.1 << 0; 0084 QTest::newRow("Line: Base point 2") << (Okular::Annotation *)line << 0.5 << 0.1 << 0; 0085 QTest::newRow("Line: Off by a lot") << (Okular::Annotation *)line << 0.5 << 0.5 << qRound(pow(0.4 * documentY, 2)); 0086 QTest::newRow("Line: Off by a little") << (Okular::Annotation *)line << 0.1 << 0.1 + (5.0 /* px */ / documentY) << 25; 0087 0088 // squares (non-filled squares also act as tests for the ink annotation as they share the path-distance code) 0089 for (int i = 0; i < 2; ++i) { 0090 Okular::GeomAnnotation *square = new Okular::GeomAnnotation; 0091 square->setGeometricalType(Okular::GeomAnnotation::InscribedSquare); 0092 square->setBoundingRectangle(Okular::NormalizedRect(0.1, 0.1, 0.9, 0.9)); 0093 if (i != 0) { 0094 square->setGeometricalInnerColor(QColor(0, 0, 0)); 0095 } 0096 m_document->addPageAnnotation(0, square); 0097 QTest::addRow("Square: Base point 1 %d", i) << (Okular::Annotation *)square << 0.1 << 0.1 << 0; 0098 QTest::addRow("Square: On edge 1 %d", i) << (Okular::Annotation *)square << 0.1 << 0.2 << 0; 0099 QTest::addRow("Square: On edge 2 %d", i) << (Okular::Annotation *)square << 0.2 << 0.1 << 0; 0100 QTest::addRow("Square: Inside %d", i) << (Okular::Annotation *)square << 0.2 << 0.2 << ((i == 0) ? qRound(pow(0.1 * documentX, 2) - 1 /* stroke width */) : 0); 0101 QTest::addRow("Square: Outside %d", i) << (Okular::Annotation *)square << 0.0 << 0.0 << qRound(pow(0.1 * documentX, 2) + pow(0.1 * documentY, 2)); 0102 } 0103 0104 // ellipsis 0105 for (int i = 0; i < 2; ++i) { 0106 Okular::GeomAnnotation *ellipse = new Okular::GeomAnnotation; 0107 ellipse->setGeometricalType(Okular::GeomAnnotation::InscribedCircle); 0108 ellipse->setBoundingRectangle(Okular::NormalizedRect(0.1, 0.1, 0.9, 0.5)); 0109 if (i != 0) { 0110 ellipse->setGeometricalInnerColor(QColor(0, 0, 0)); 0111 } 0112 m_document->addPageAnnotation(0, ellipse); 0113 QTest::addRow("Ellipse: Base point 1 %d", i) << (Okular::Annotation *)ellipse << 0.1 << 0.3 << 0; 0114 QTest::addRow("Ellipse: Inside %d", i) << (Okular::Annotation *)ellipse << 0.2 << 0.3 << qRound((i == 0) ? pow(documentX * 0.1, 2) - 1 /* pen */ : 0); 0115 QTest::addRow("Ellipse: Outside %d", i) << (Okular::Annotation *)ellipse << 0.0 << 0.3 << qRound(pow(documentX * 0.1, 2)); 0116 } 0117 0118 // highlight 0119 Okular::HighlightAnnotation *highlight = new Okular::HighlightAnnotation; 0120 Okular::HighlightAnnotation::Quad q; 0121 q.setPoint(Okular::NormalizedPoint(0.1, 0.1), 0); 0122 q.setPoint(Okular::NormalizedPoint(0.2, 0.1), 1); 0123 q.setPoint(Okular::NormalizedPoint(0.8, 0.9), 2); 0124 q.setPoint(Okular::NormalizedPoint(0.9, 0.9), 3); 0125 highlight->highlightQuads() << q; 0126 m_document->addPageAnnotation(0, highlight); 0127 QTest::newRow("Highlight: Point 1") << (Okular::Annotation *)highlight << 0.1 << 0.1 << 0; 0128 QTest::newRow("Highlight: Point 2") << (Okular::Annotation *)highlight << 0.2 << 0.1 << 0; 0129 QTest::newRow("Highlight: Point 3") << (Okular::Annotation *)highlight << 0.8 << 0.9 << 0; 0130 QTest::newRow("Highlight: Point 4") << (Okular::Annotation *)highlight << 0.9 << 0.9 << 0; 0131 QTest::newRow("Highlight: Inside") << (Okular::Annotation *)highlight << 0.5 << 0.5 << 0; 0132 QTest::newRow("Highlight: Outside") << (Okular::Annotation *)highlight << 1.0 << 0.9 << qRound(pow(documentX * 0.1, 2)); 0133 } 0134 0135 void AnnotationTest::testTypewriter() 0136 { 0137 Okular::Annotation *annot = nullptr; 0138 Okular::TextAnnotation *ta = new Okular::TextAnnotation(); 0139 annot = ta; 0140 ta->setFlags(ta->flags() | Okular::Annotation::FixedRotation); 0141 ta->setTextType(Okular::TextAnnotation::InPlace); 0142 ta->setInplaceIntent(Okular::TextAnnotation::TypeWriter); 0143 ta->style().setWidth(0.0); 0144 ta->style().setColor(QColor(255, 255, 255, 0)); 0145 0146 annot->setBoundingRectangle(Okular::NormalizedRect(0.8, 0.1, 0.85, 0.15)); 0147 annot->setContents(QStringLiteral("annot contents")); 0148 0149 m_document->addPageAnnotation(0, annot); 0150 0151 QDomNode annotNode = annot->getAnnotationPropertiesDomNode(); 0152 QDomNodeList annotNodeList = annotNode.toElement().elementsByTagName(QStringLiteral("base")); 0153 QDomElement annotEl = annotNodeList.item(0).toElement(); 0154 QCOMPARE(annotEl.attribute(QStringLiteral("color")), QStringLiteral("#00ffffff")); 0155 QCOMPARE(annotEl.attribute(QStringLiteral("flags")), QStringLiteral("4")); 0156 QCOMPARE(annotEl.attribute(QStringLiteral("contents")), QStringLiteral("annot contents")); 0157 } 0158 0159 QTEST_MAIN(AnnotationTest) 0160 #include "annotationstest.moc"