annotationstest.cpp 7.43 KB
Newer Older
Peter Grasch's avatar
Peter Grasch committed
1 2 3 4 5 6 7 8 9
/***************************************************************************
 *   Copyright (C) 2013 by Peter Grasch <me@bedahr.org>                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

10
#include <QtTest>
Peter Grasch's avatar
Peter Grasch committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

#include "../core/document.h"
#include "../core/page.h"
#include "../core/annotations.h"
#include "../settings_core.h"

Q_DECLARE_METATYPE(Okular::Annotation*)
Q_DECLARE_METATYPE(Okular::LineAnnotation*)

class AnnotationTest : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase();
    void testDistance();
    void testDistance_data();
//     void testLine();
//     void testPoly();
//     void testInk();
//     void testHighlight();
//     void testGeom();
Tobias Deiminger's avatar
Tobias Deiminger committed
33
    void testTypewriter();
Peter Grasch's avatar
Peter Grasch committed
34 35 36 37 38 39 40 41
    void cleanupTestCase();

private:
    Okular::Document *m_document;
};

void AnnotationTest::initTestCase()
{
Laurent Montel's avatar
Laurent Montel committed
42
    Okular::SettingsCore::instance( QStringLiteral("annotationtest") );
43
    m_document = new Okular::Document( nullptr );
44
    const QString testFile = QStringLiteral(KDESRCDIR "data/file1.pdf");
Frederik Gladhorn's avatar
Frederik Gladhorn committed
45 46
    QMimeDatabase db;
    const QMimeType mime = db.mimeTypeForFile( testFile );
Lukáš Tinkl's avatar
Lukáš Tinkl committed
47
    QCOMPARE( m_document->openDocument(testFile, QUrl(), mime), Okular::Document::OpenSuccess );
Peter Grasch's avatar
Peter Grasch committed
48 49 50 51
}

void AnnotationTest::cleanupTestCase()
{
52 53 54
    if ( m_document->isOpened() ) {
        const QLinkedList<Okular::Annotation *> annotations = m_document->page( 0 )->annotations();
        for ( Okular::Annotation* annotation : annotations ) {
55
            m_document->removePageAnnotation( 0, annotation );
56 57
        }
    }
Peter Grasch's avatar
Peter Grasch committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
}



void AnnotationTest::testDistance()
{
    QFETCH(Okular::Annotation*, annotation);
    QFETCH(double, x);
    QFETCH(double, y);
    QFETCH(int, distance);
    Okular::AnnotationObjectRect* rect = new Okular::AnnotationObjectRect( annotation );
    QCOMPARE( qRound( rect->distanceSqr( x, y, m_document->page(0)->width(), m_document->page(0)->height() ) ), distance );
    delete rect;
}

void AnnotationTest::testDistance_data()
{
    QTest::addColumn<Okular::Annotation*>("annotation");
    QTest::addColumn<double>("x");
    QTest::addColumn<double>("y");
    QTest::addColumn<int>("distance");

80 81
    double documentX = m_document->page(0)->width();
    double documentY = m_document->page(0)->height();
Peter Grasch's avatar
Peter Grasch committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

    // lines
    Okular::LineAnnotation *line = new Okular::LineAnnotation;
    line->setLinePoints( QLinkedList< Okular::NormalizedPoint >()
        << Okular::NormalizedPoint( 0.1, 0.1 )
        << Okular::NormalizedPoint( 0.9, 0.1 )
    );
    m_document->addPageAnnotation( 0, line );
    QTest::newRow("Line: Base point 1") << (Okular::Annotation*) line << 0.1 << 0.1 << 0;
    QTest::newRow("Line: Base point 2") << (Okular::Annotation*) line << 0.5 << 0.1 << 0;
    QTest::newRow("Line: Off by a lot") << (Okular::Annotation*) line << 0.5 << 0.5 << qRound( pow( 0.4 * documentY, 2 ) );
    QTest::newRow("Line: Off by a little") << (Okular::Annotation*) line << 0.1 << 0.1 + (5.0 /* px */ / documentY) << 25;


    // squares (non-filled squares also act as tests for the ink annotation as they share the path-distance code)
    for ( int i = 0; i < 2; ++i )
    {
        Okular::GeomAnnotation *square = new Okular::GeomAnnotation;
        square->setGeometricalType( Okular::GeomAnnotation::InscribedSquare );
        square->setBoundingRectangle( Okular::NormalizedRect( 0.1, 0.1, 0.9, 0.9 ) );
        if ( i != 0 )
            square->setGeometricalInnerColor( QColor(0, 0, 0) );
        m_document->addPageAnnotation( 0, square );
        QTest::newRow("Square: Base point 1") << (Okular::Annotation*) square << 0.1 << 0.1 << 0;
        QTest::newRow("Square: On edge 1") << (Okular::Annotation*) square << 0.1 << 0.2 << 0;
        QTest::newRow("Square: On edge 2") << (Okular::Annotation*) square << 0.2 << 0.1 << 0;
        QTest::newRow("Square: Inside") << (Okular::Annotation*) square << 0.2 << 0.2 <<
            ((i == 0) ? qRound( pow( 0.1 * documentX, 2 ) - 1 /* stroke width */) : 0 );
        QTest::newRow("Square: Outside") << (Okular::Annotation*) square << 0.0 << 0.0 <<
            qRound( pow( 0.1 * documentX, 2 ) + pow( 0.1 * documentY, 2) ) ;
    }

    // ellipsis
    for ( int i = 0; i < 2; ++i )
    {
        Okular::GeomAnnotation *ellipse = new Okular::GeomAnnotation;
        ellipse->setGeometricalType( Okular::GeomAnnotation::InscribedCircle );
        ellipse->setBoundingRectangle( Okular::NormalizedRect( 0.1, 0.1, 0.9, 0.5 ) );
        if ( i != 0 )
            ellipse->setGeometricalInnerColor( QColor(0, 0, 0) );
        m_document->addPageAnnotation( 0, ellipse );
        QTest::newRow("Ellipse: Base point 1") << (Okular::Annotation*) ellipse << 0.1 << 0.3 << 0;
        QTest::newRow("Ellipse: Inside") << (Okular::Annotation*) ellipse << 0.2 << 0.3 <<
            qRound( (i == 0) ? pow( documentX * 0.1, 2 ) - 1 /* pen */ : 0 );
        QTest::newRow("Ellipse: Outside") << (Okular::Annotation*) ellipse << 0.0 << 0.3 << qRound( pow( documentX * 0.1, 2 ) );
    }


    // highlight
    Okular::HighlightAnnotation *highlight = new Okular::HighlightAnnotation;
    Okular::HighlightAnnotation::Quad q;
    q.setPoint( Okular::NormalizedPoint( 0.1, 0.1 ), 0 );
    q.setPoint( Okular::NormalizedPoint( 0.2, 0.1 ), 1 );
    q.setPoint( Okular::NormalizedPoint( 0.8, 0.9 ), 2 );
    q.setPoint( Okular::NormalizedPoint( 0.9, 0.9 ), 3 );
    highlight->highlightQuads() << q;
    m_document->addPageAnnotation( 0, highlight );
    QTest::newRow("Highlight: Point 1") << (Okular::Annotation*) highlight << 0.1 << 0.1 << 0;
    QTest::newRow("Highlight: Point 2") << (Okular::Annotation*) highlight << 0.2 << 0.1 << 0;
    QTest::newRow("Highlight: Point 3") << (Okular::Annotation*) highlight << 0.8 << 0.9 << 0;
    QTest::newRow("Highlight: Point 4") << (Okular::Annotation*) highlight << 0.9 << 0.9 << 0;
    QTest::newRow("Highlight: Inside") << (Okular::Annotation*) highlight << 0.5 << 0.5 << 0;
    QTest::newRow("Highlight: Outside") << (Okular::Annotation*) highlight << 1.0 << 0.9 << qRound( pow( documentX * 0.1, 2 ) );
}

Tobias Deiminger's avatar
Tobias Deiminger committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
void AnnotationTest::testTypewriter()
{
  Okular::Annotation * annot = nullptr;
  Okular::TextAnnotation * ta = new Okular::TextAnnotation();
  annot = ta;
  ta->setFlags( ta->flags() | Okular::Annotation::FixedRotation );
  ta->setTextType( Okular::TextAnnotation::InPlace );
  ta->setInplaceIntent( Okular::TextAnnotation::TypeWriter );
  ta->style().setWidth( 0.0 );
  ta->style().setColor( QColor(255,255,255,0) );

  annot->setBoundingRectangle( Okular::NormalizedRect( 0.8, 0.1, 0.85, 0.15 ) );
  annot->setContents( QStringLiteral("annot contents") );

  m_document->addPageAnnotation( 0, annot );

  QDomNode annotNode = annot->getAnnotationPropertiesDomNode();
164
  QDomNodeList annotNodeList = annotNode.toElement().elementsByTagName(QStringLiteral("base"));
Tobias Deiminger's avatar
Tobias Deiminger committed
165 166 167 168 169
  QDomElement annotEl = annotNodeList.item(0).toElement();
  QCOMPARE( annotEl.attribute( QStringLiteral("color") ), QStringLiteral("#00ffffff") );
  QCOMPARE( annotEl.attribute( QStringLiteral("flags") ), QStringLiteral("4") );
  QCOMPARE( annotEl.attribute( QStringLiteral("contents") ), QStringLiteral("annot contents") );
}
Peter Grasch's avatar
Peter Grasch committed
170

171
QTEST_MAIN( AnnotationTest )
Peter Grasch's avatar
Peter Grasch committed
172
#include "annotationstest.moc"