File: addremoveannotationtest.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 (190 lines) | stat: -rw-r--r-- 7,801 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/***************************************************************************
 *   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 "../core/document.h"
#include "../core/page.h"
#include "../core/annotations.h"
#include "../settings_core.h"
#include "testingutils.h"

class AddRemoveAnnotationTest : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase();
    void init();
    void cleanup();
    void testAddAnnotations();
    void testAddAnnotationUndoWithRotate_Bug318091();
    void testRemoveAnnotations();

private:
    Okular::Document *m_document;
};

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

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

void AddRemoveAnnotationTest::cleanup()
{
    m_document->closeDocument();
}

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

    // Create two distinct text annotations
    Okular::Annotation *annot1 = new Okular::TextAnnotation();
    annot1->setBoundingRectangle( Okular::NormalizedRect( 0.1, 0.1, 0.15, 0.15 ) );
    annot1->setContents( "annot contents" );

    Okular::Annotation *annot2 = new Okular::TextAnnotation();
    annot2->setBoundingRectangle( Okular::NormalizedRect( 0.2, 0.2, 0.3, 0.4 ) );
    annot2->setContents( "annot contents" );

    // The two annotations shold have different properties XML strings
    QVERIFY( TestingUtils::getAnnotationXml( annot1 ) != TestingUtils::getAnnotationXml( annot2 ) );

    // We start with no annotations in the docuemnt
    QVERIFY( m_document->page( 0 )->annotations().size() == 0 );

    // After adding annot1 we should have one annotation in the page and it should be annot1.
    m_document->addPageAnnotation( 0, annot1 );
    QVERIFY( m_document->page( 0 )->annotations().size() == 1 );
    QCOMPARE( annot1, m_document->page( 0 )->annotations().first() );

    // Record the properties and name of annot1 just after insertion for later comparisons
    QString origLine1Xml = TestingUtils::getAnnotationXml( annot1 );
    QString annot1Name = annot1->uniqueName();
    QVERIFY( !annot1Name.isEmpty() );

    // Now undo the addition of annot1 and verify that annot1's properties haven't changed
    m_document->undo();
    QVERIFY( m_document->page( 0 )->annotations().empty() );
    QVERIFY( !m_document->canUndo() );
    QVERIFY( m_document->canRedo() );
    QCOMPARE( TestingUtils::getAnnotationXml( annot1 ), origLine1Xml );

    // redo addition of annot1
    m_document->redo();
    QVERIFY( m_document->page( 0 )->annotations().size() == 1 );
    QVERIFY( annot1 == m_document->page( 0 )->annotations().first() );
    QCOMPARE( TestingUtils::getAnnotationXml( annot1 ), origLine1Xml );

    // undo once more
    m_document->undo();
    QVERIFY( m_document->page( 0 )->annotations().empty() );
    QVERIFY( !m_document->canUndo() );
    QVERIFY( m_document->canRedo() );
    QCOMPARE( TestingUtils::getAnnotationXml( annot1 ), origLine1Xml );

    // Set AnnotationDisposeWatcher dispose function on annot1 so we can detect
    // when it is deleted
    annot1->setDisposeDataFunction( TestingUtils::AnnotationDisposeWatcher::disposeAnnotation );
    TestingUtils::AnnotationDisposeWatcher::resetDisposedAnnotationName();
    QCOMPARE( TestingUtils::AnnotationDisposeWatcher::disposedAnnotationName(), QString() );

    // now add annot2
    m_document->addPageAnnotation( 0, annot2 );
    QString annot2Name = annot2->uniqueName();
    QVERIFY( !annot2Name.isEmpty() );
    QVERIFY( annot1Name != annot2Name );
    QVERIFY( m_document->page( 0 )->annotations().size() == 1 );
    QCOMPARE( annot2, m_document->page( 0 )->annotations().first() );

    // Check that adding annot2 while annot1 was in the unadded state triggered the deletion of annot1
    QVERIFY( TestingUtils::AnnotationDisposeWatcher::disposedAnnotationName() == annot1Name );
}

void AddRemoveAnnotationTest::testAddAnnotationUndoWithRotate_Bug318091()
{
    Okular::Annotation *annot = new Okular::TextAnnotation();
    annot->setBoundingRectangle( Okular::NormalizedRect( 0.1, 0.1, 0.15, 0.15 ) );
    annot->setContents( "annot contents" );

    m_document->addPageAnnotation( 0, annot );
    QString origAnnotXml = TestingUtils::getAnnotationXml( annot );

    // Now undo annotation addition, rotate the page, and redo to annotation addition
    m_document->undo();
    m_document->setRotation( 1 );
    m_document->redo();

    // Verify that annotation's properties remain unchanged
    // In Bug318091 the bounding rectangle was being rotated upon each redo
    QString newAnnotXml = TestingUtils::getAnnotationXml( annot );
    QCOMPARE( origAnnotXml, newAnnotXml );
}


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

    // Create two distinct text annotations
    Okular::Annotation *annot1 = new Okular::TextAnnotation();
    annot1->setBoundingRectangle( Okular::NormalizedRect( 0.1, 0.1, 0.15, 0.15 ) );
    annot1->setContents( "annot contents" );

    Okular::Annotation *annot2 = new Okular::TextAnnotation();
    annot2->setBoundingRectangle( Okular::NormalizedRect( 0.2, 0.2, 0.3, 0.4 ) );
    annot2->setContents( "annot contents" );

    // Add annot1 and annot2 to document
    m_document->addPageAnnotation( 0, annot1 );
    m_document->addPageAnnotation( 0, annot2 );
    QVERIFY( m_document->page( 0 )->annotations().size() == 2 );
    QVERIFY( m_document->page( 0 )->annotations().contains(annot1) );
    QVERIFY( m_document->page( 0 )->annotations().contains(annot2) );

    // Now remove annot1
    m_document->removePageAnnotation( 0, annot1 );
    QVERIFY( m_document->page( 0 )->annotations().size() == 1 );
    QVERIFY( m_document->page( 0 )->annotations().contains(annot2) );

    // Undo removal of annot1
    m_document->undo();
    QVERIFY( m_document->page( 0 )->annotations().size() == 2 );
    QVERIFY( m_document->page( 0 )->annotations().contains(annot1) );
    QVERIFY( m_document->page( 0 )->annotations().contains(annot2) );

    // Redo removal
    m_document->redo();
    QVERIFY( m_document->page( 0 )->annotations().size() == 1 );
    QVERIFY( m_document->page( 0 )->annotations().contains(annot2) );

    // Verify that annot1 is disposed of if document is closed with annot1 in removed state
    QString annot1Name = annot1->uniqueName();
    annot1->setDisposeDataFunction( TestingUtils::AnnotationDisposeWatcher::disposeAnnotation );
    TestingUtils::AnnotationDisposeWatcher::resetDisposedAnnotationName();
    QVERIFY( TestingUtils::AnnotationDisposeWatcher::disposedAnnotationName() == QString() );
    m_document->closeDocument();
    QVERIFY( TestingUtils::AnnotationDisposeWatcher::disposedAnnotationName() == annot1Name );
}

QTEST_KDEMAIN( AddRemoveAnnotationTest, GUI )
#include "addremoveannotationtest.moc"