File: annotationstest.cpp

package info (click to toggle)
okular 4%3A4.14.2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,584 kB
  • ctags: 7,898
  • sloc: cpp: 73,406; ansic: 3,964; xml: 57; sh: 42; makefile: 7
file content (144 lines) | stat: -rw-r--r-- 6,270 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
/***************************************************************************
 *   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.                                   *
 ***************************************************************************/

#include <qtest_kde.h>

#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();
    void cleanupTestCase();

private:
    Okular::Document *m_document;
};

void AnnotationTest::initTestCase()
{
    Okular::SettingsCore::instance( "annotationtest" );
    m_document = new Okular::Document( 0 );
    const QString testFile = KDESRCDIR "data/file1.pdf";
    const KMimeType::Ptr mime = KMimeType::findByPath( testFile );
    m_document->openDocument(testFile, KUrl(), mime);
}

void AnnotationTest::cleanupTestCase()
{
    foreach ( Okular::Annotation* annotation, m_document->page( 0 )->annotations() )
        m_document->removePageAnnotation( 0, annotation );
}



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");

    double documentX = m_document->page(0)->width();
    double documentY = m_document->page(0)->height();

    // 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 ) );
}


QTEST_KDEMAIN( AnnotationTest, GUI )

#include "annotationstest.moc"