forked from KDE/okular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visibilitytest.cpp
163 lines (124 loc) · 4.74 KB
/
visibilitytest.cpp
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
/*
SPDX-FileCopyrightText: 2018 Intevation GmbH <[email protected]>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <QTest>
#include "../settings_core.h"
#include "core/document.h"
#include <QMap>
#include <QMimeDatabase>
#include <QMimeType>
#include <QTemporaryFile>
#include <core/form.h>
#include <core/page.h>
class VisibilityTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void testJavaScriptVisibility();
void testSaveLoad();
void testActionVisibility();
private:
void verifyTargetStates(bool visible);
Okular::Document *m_document;
QMap<QString, Okular::FormField *> m_fields;
};
void VisibilityTest::initTestCase()
{
Okular::SettingsCore::instance(QStringLiteral("visibilitytest"));
m_document = new Okular::Document(nullptr);
const QString testFile = QStringLiteral(KDESRCDIR "data/visibilitytest.pdf");
QMimeDatabase db;
const QMimeType mime = db.mimeTypeForFile(testFile);
QCOMPARE(m_document->openDocument(testFile, QUrl(), mime), Okular::Document::OpenSuccess);
// The test document has four buttons:
// HideScriptButton -> Hides targets with JavaScript
// ShowScriptButton -> Shows targets with JavaScript
// HideActionButton -> Hides targets with HideAction
// ShowActionButton -> Shows targets with HideAction
//
// The target fields are:
// TargetButton TargetText TargetCheck TargetDropDown TargetRadio
//
// With two radio buttons named TargetRadio.
const Okular::Page *page = m_document->page(0);
const QList<Okular::FormField *> pageFormFields = page->formFields();
for (Okular::FormField *ff : pageFormFields) {
m_fields.insert(ff->name(), ff);
}
}
void VisibilityTest::cleanupTestCase()
{
m_document->closeDocument();
delete m_document;
}
void VisibilityTest::verifyTargetStates(bool visible)
{
QCOMPARE(m_fields[QStringLiteral("TargetButton")]->isVisible(), visible);
QCOMPARE(m_fields[QStringLiteral("TargetText")]->isVisible(), visible);
QCOMPARE(m_fields[QStringLiteral("TargetCheck")]->isVisible(), visible);
QCOMPARE(m_fields[QStringLiteral("TargetDropDown")]->isVisible(), visible);
// Radios do not properly inherit a name from the parent group so
// this does not work yet (And would probably need some list handling).
// QCOMPARE( m_fields[QStringLiteral( "TargetRadio" )].isVisible(), visible );
}
void VisibilityTest::testJavaScriptVisibility()
{
auto hideBtn = m_fields[QStringLiteral("HideScriptButton")];
auto showBtn = m_fields[QStringLiteral("ShowScriptButton")];
// We start with all fields visible
verifyTargetStates(true);
m_document->processAction(hideBtn->activationAction());
// Now all should be hidden
verifyTargetStates(false);
// And show again
m_document->processAction(showBtn->activationAction());
verifyTargetStates(true);
}
void VisibilityTest::testSaveLoad()
{
auto hideBtn = m_fields[QStringLiteral("HideScriptButton")];
auto showBtn = m_fields[QStringLiteral("ShowScriptButton")];
// We start with all fields visible
verifyTargetStates(true);
m_document->processAction(hideBtn->activationAction());
// Now all should be hidden
verifyTargetStates(false);
// Save the changed states
QTemporaryFile saveFile(QStringLiteral("%1/okrXXXXXX.pdf").arg(QDir::tempPath()));
QVERIFY(saveFile.open());
saveFile.close();
QVERIFY(m_document->saveChanges(saveFile.fileName()));
auto newDoc = new Okular::Document(nullptr);
QMimeDatabase db;
const QMimeType mime = db.mimeTypeForFile(saveFile.fileName());
QCOMPARE(newDoc->openDocument(saveFile.fileName(), QUrl(), mime), Okular::Document::OpenSuccess);
const Okular::Page *page = newDoc->page(0);
bool anyChecked = false; // Saveguard against accidental test passing here ;-)
const QList<Okular::FormField *> pageFormFields = page->formFields();
for (Okular::FormField *ff : pageFormFields) {
if (ff->name().startsWith(QStringLiteral("Target"))) {
QVERIFY(!ff->isVisible());
anyChecked = true;
}
}
QVERIFY(anyChecked);
newDoc->closeDocument();
delete newDoc;
// Restore the state of the member document
m_document->processAction(showBtn->activationAction());
}
void VisibilityTest::testActionVisibility()
{
auto hideBtn = m_fields[QStringLiteral("HideActionButton")];
auto showBtn = m_fields[QStringLiteral("ShowActionButton")];
verifyTargetStates(true);
m_document->processAction(hideBtn->activationAction());
verifyTargetStates(false);
m_document->processAction(showBtn->activationAction());
verifyTargetStates(true);
}
QTEST_MAIN(VisibilityTest)
#include "visibilitytest.moc"