File: modifyannotationpropertiestest.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 (168 lines) | stat: -rw-r--r-- 6,721 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/***************************************************************************
 *   Copyright (C) 2013 by Jon Mease <jon.mease@gmail.com>                 *
 *                                                                         *
 *   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 <kmimetype.h>
#include "../settings_core.h"
#include <core/area.h>
#include <core/annotations.h>
#include "core/document.h"
#include "testingutils.h"

static const QColor RED = QColor(255, 0, 0);
static const QColor GREEN = QColor(0, 255, 0.0);
static const QColor BLUE = QColor(0, 0, 255);

class ModifyAnnotationPropertiesTest : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase();
    void cleanupTestCase();
    void init();
    void cleanup();
    void testModifyAnnotationProperties();
    void testModifyDefaultAnnotationProperties();
    void testModifyAnnotationPropertiesWithRotation_Bug318828();
private:
    Okular::Document *m_document;
    Okular::TextAnnotation *m_annot1;
};

void ModifyAnnotationPropertiesTest::initTestCase()
{
    Okular::SettingsCore::instance( "editannotationcontentstest" );
    m_document = new Okular::Document( 0 );
}

void ModifyAnnotationPropertiesTest::cleanupTestCase()
{
    delete m_document;
}

void ModifyAnnotationPropertiesTest::init()
{
    const QString testFile = KDESRCDIR "data/file1.pdf";
    const KMimeType::Ptr mime = KMimeType::findByPath( testFile );
    m_document->openDocument(testFile, KUrl(), mime);

    // Undo and Redo should be unavailable when docuemnt is first opened.
    QVERIFY( !m_document->canUndo() );
    QVERIFY( !m_document->canRedo() );

    // Create two distinct text annotations
    m_annot1 = new Okular::TextAnnotation();
    m_annot1->setBoundingRectangle( Okular::NormalizedRect( 0.1, 0.1, 0.15, 0.15 ) );
    m_annot1->setContents( QString( "Hello, World" ) );
    m_annot1->setAuthor( "Jon Mease" );
    m_annot1->style().setColor( RED );
    m_annot1->style().setWidth( 4.0 );
    m_document->addPageAnnotation( 0, m_annot1 );
}

void ModifyAnnotationPropertiesTest::cleanup()
{
    m_document->closeDocument();
    // m_annot1 and m_annot2 are deleted when document is closed
}

void ModifyAnnotationPropertiesTest::testModifyAnnotationProperties()
{
    // Add m_annot1 to document and record its properties XML string
    QString origLine1Xml =  TestingUtils::getAnnotationXml( m_annot1 );

    // Tell document we're going to modify m_annot1's properties
    m_document->prepareToModifyAnnotationProperties( m_annot1 );

    // Now modify m_annot1's properties and record properties XML string
    m_annot1->style().setWidth( 8.0 );
    m_annot1->style().setColor( GREEN );
    m_document->modifyPageAnnotationProperties( 0, m_annot1 );
    QString m_annot1XmlA =  TestingUtils::getAnnotationXml( m_annot1 );
    QCOMPARE( 8.0, m_annot1->style().width() );
    QCOMPARE( GREEN, m_annot1->style().color() );

    // undo modification and check that original properties have been restored
    m_document->undo();
    QCOMPARE( 4.0, m_annot1->style().width() );
    QCOMPARE( RED, m_annot1->style().color() );
    QCOMPARE( origLine1Xml, TestingUtils::getAnnotationXml( m_annot1 ) );

    // redo modification and verify that new properties have been restored
    m_document->redo();
    QCOMPARE( 8.0, m_annot1->style().width() );
    QCOMPARE( GREEN, m_annot1->style().color() );
    QCOMPARE( m_annot1XmlA, TestingUtils::getAnnotationXml( m_annot1 ) );

    // Verify that default values are properly restored.  (We haven't explicitly set opacity yet)
    QCOMPARE( 1.0, m_annot1->style().opacity() );
    m_document->prepareToModifyAnnotationProperties( m_annot1 );
    m_annot1->style().setOpacity( 0.5 );
    m_document->modifyPageAnnotationProperties( 0, m_annot1 );
    QCOMPARE( 0.5, m_annot1->style().opacity() );

    m_document->undo();
    QCOMPARE( 1.0, m_annot1->style().opacity() );
    QCOMPARE( m_annot1XmlA, TestingUtils::getAnnotationXml( m_annot1 ) );

    // And finally undo back to original properties
    m_document->undo();
    QCOMPARE( 4.0, m_annot1->style().width() );
    QCOMPARE( RED, m_annot1->style().color() );
    QCOMPARE( origLine1Xml, TestingUtils::getAnnotationXml( m_annot1 ) );
}

void ModifyAnnotationPropertiesTest::testModifyDefaultAnnotationProperties()
{
    QString origLine1Xml =  TestingUtils::getAnnotationXml( m_annot1 );

    // Verify that default values are properly restored.  (We haven't explicitly set opacity yet)
    QCOMPARE( 1.0, m_annot1->style().opacity() );
    m_document->prepareToModifyAnnotationProperties( m_annot1 );
    m_annot1->style().setOpacity( 0.5 );
    m_document->modifyPageAnnotationProperties( 0, m_annot1 );
    QCOMPARE( 0.5, m_annot1->style().opacity() );

    m_document->undo();
    QCOMPARE( 1.0, m_annot1->style().opacity() );
    QCOMPARE( origLine1Xml, TestingUtils::getAnnotationXml( m_annot1 ) );
}

void ModifyAnnotationPropertiesTest::testModifyAnnotationPropertiesWithRotation_Bug318828()
{
    Okular::NormalizedRect boundingRect = Okular::NormalizedRect( 0.1, 0.1, 0.15, 0.15 );
    Okular::NormalizedRect transformedBoundingRect;
    m_annot1->setBoundingRectangle( boundingRect );
    m_document->addPageAnnotation( 0, m_annot1 );

    transformedBoundingRect = m_annot1->transformedBoundingRectangle();

    // Before page rotation boundingRect and transformedBoundingRect should be equal
    QCOMPARE( boundingRect, transformedBoundingRect );
    m_document->setRotation( 1 );

    // After rotation boundingRect should remain unchanged but
    // transformedBoundingRect should no longer equal boundingRect
    QCOMPARE( boundingRect, m_annot1->boundingRectangle() );
    transformedBoundingRect = m_annot1->transformedBoundingRectangle();
    QVERIFY( !( boundingRect == transformedBoundingRect ) );

    // Modifying the properties of m_annot1 while page is rotated shouldn't
    // alter either boundingRect or transformedBoundingRect
    m_document->prepareToModifyAnnotationProperties( m_annot1 );
    m_annot1->style().setOpacity( 0.5 );
    m_document->modifyPageAnnotationProperties( 0, m_annot1 );

    QCOMPARE( boundingRect, m_annot1->boundingRectangle() );
    QCOMPARE( transformedBoundingRect, m_annot1->transformedBoundingRectangle() );
}

QTEST_KDEMAIN( ModifyAnnotationPropertiesTest, GUI )
#include "modifyannotationpropertiestest.moc"