forked from KDE/okular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keystroketest.cpp
104 lines (82 loc) · 3.26 KB
/
keystroketest.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
/*
SPDX-FileCopyrightText: 2019 João Netto <[email protected]>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <QTest>
#include "../settings_core.h"
#include <QLocale>
#include <QMap>
#include <QMimeDatabase>
#include <QMimeType>
#include <core/document.h>
#include <core/form.h>
#include <core/page.h>
#include <qtestcase.h>
class KeystrokeTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void testCommit();
void testKeystroke();
private:
Okular::Document *m_document;
QMap<QString, Okular::FormField *> m_fields;
};
void KeystrokeTest::initTestCase()
{
Okular::SettingsCore::instance(QStringLiteral("keystroketest"));
m_document = new Okular::Document(nullptr);
// Force consistent locale
QLocale locale(QStringLiteral("en_US"));
QLocale::setDefault(locale);
const QString testFile = QStringLiteral(KDESRCDIR "data/keystroketest.pdf");
QMimeDatabase db;
const QMimeType mime = db.mimeTypeForFile(testFile);
QCOMPARE(m_document->openDocument(testFile, QUrl(), mime), Okular::Document::OpenSuccess);
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 KeystrokeTest::testCommit()
{
Okular::FormFieldText *fft = reinterpret_cast<Okular::FormFieldText *>(m_fields[QStringLiteral("field2")]);
// text that will be accepted
fft->setText(QStringLiteral("Lorem ipsum"));
m_document->processKeystrokeCommitAction(fft->additionalAction(Okular::FormField::FieldModified), fft);
QCOMPARE(fft->text(), QStringLiteral("Lorem ipsum"));
// text that will be rejected
fft->setText(QStringLiteral("foo"));
m_document->processKeystrokeCommitAction(fft->additionalAction(Okular::FormField::FieldModified), fft);
QEXPECT_FAIL("", "reset to commited value not implemented", Continue);
QCOMPARE(fft->text(), QStringLiteral("Lorem ipsum"));
}
void KeystrokeTest::testKeystroke()
{
Okular::FormFieldText *fft = reinterpret_cast<Okular::FormFieldText *>(m_fields[QStringLiteral("field3")]);
// accept
m_document->processKeystrokeAction(fft->additionalAction(Okular::FormField::FieldModified), fft, QStringLiteral("hello"));
QCOMPARE(fft->text(), QStringLiteral("hello"));
// accept
m_document->processKeystrokeAction(fft->additionalAction(Okular::FormField::FieldModified), fft, QStringLiteral("e"));
QCOMPARE(fft->text(), QStringLiteral("e"));
// accept
m_document->processKeystrokeAction(fft->additionalAction(Okular::FormField::FieldModified), fft, QStringLiteral("ee"));
QCOMPARE(fft->text(), QStringLiteral("ee"));
// accept
m_document->processKeystrokeAction(fft->additionalAction(Okular::FormField::FieldModified), fft, QStringLiteral("eee"));
QCOMPARE(fft->text(), QStringLiteral("eee"));
// reject
m_document->processKeystrokeAction(fft->additionalAction(Okular::FormField::FieldModified), fft, QStringLiteral("eeef"));
QCOMPARE(fft->text(), QStringLiteral("eee"));
}
void KeystrokeTest::cleanupTestCase()
{
m_document->closeDocument();
delete m_document;
}
QTEST_MAIN(KeystrokeTest)
#include "keystroketest.moc"