Changeset View
Changeset View
Standalone View
Standalone View
autotests/formattest.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 by João Netto <joaonetto901@gmail.com> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or modify * | ||||
5 | * it under the terms of the GNU General Public License as published by * | ||||
6 | * the Free Software Foundation; either version 2 of the License, or * | ||||
7 | * (at your option) any later version. * | ||||
8 | ***************************************************************************/ | ||||
9 | | ||||
10 | #include <QtTest> | ||||
11 | | ||||
12 | #include <QMap> | ||||
13 | #include <QMimeType> | ||||
14 | #include <QMimeDatabase> | ||||
15 | #include "../settings_core.h" | ||||
16 | #include <core/document.h> | ||||
17 | #include <core/page.h> | ||||
18 | #include <core/form.h> | ||||
19 | #include <QLocale> | ||||
20 | | ||||
21 | #include "../generators/poppler/config-okular-poppler.h" | ||||
22 | | ||||
23 | class FormatTest: public QObject | ||||
24 | { | ||||
25 | Q_OBJECT | ||||
26 | | ||||
27 | private slots: | ||||
28 | void initTestCase(); | ||||
29 | void cleanupTestCase(); | ||||
30 | void testTimeFormat(); | ||||
31 | void testTimeFormat_data(); | ||||
32 | void testSpecialFormat(); | ||||
33 | void testSpecialFormat_data(); | ||||
34 | void testFocusAction(); | ||||
35 | void testFocusAction_data(); | ||||
36 | void testValidateAction(); | ||||
37 | void testValidateAction_data(); | ||||
38 | private: | ||||
39 | | ||||
40 | Okular::Document *m_document; | ||||
41 | QMap<QString, Okular::FormField*> m_fields; | ||||
42 | QString m_formattedText; | ||||
43 | }; | ||||
44 | | ||||
45 | void FormatTest::initTestCase() | ||||
46 | { | ||||
47 | Okular::SettingsCore::instance( QStringLiteral( "formattest" ) ); | ||||
48 | m_document = new Okular::Document( nullptr ); | ||||
49 | | ||||
50 | // Force consistent locale | ||||
51 | QLocale locale( QStringLiteral( "en_US" ) ); | ||||
52 | QLocale::setDefault( locale ); | ||||
53 | | ||||
54 | const QString testFile = QStringLiteral( KDESRCDIR "data/formattest.pdf" ); | ||||
55 | QMimeDatabase db; | ||||
56 | const QMimeType mime = db.mimeTypeForFile( testFile ); | ||||
57 | QCOMPARE( m_document->openDocument( testFile, QUrl(), mime), Okular::Document::OpenSuccess ); | ||||
58 | | ||||
59 | connect( m_document, &Okular::Document::refreshFormWidget, [=]( Okular::FormField * form ) | ||||
60 | { | ||||
61 | Okular::FormFieldText *fft = reinterpret_cast< Okular::FormFieldText * >( form ); | ||||
62 | if( fft ) | ||||
63 | m_formattedText = fft->text(); | ||||
64 | }); | ||||
65 | | ||||
66 | const Okular::Page* page = m_document->page( 0 ); | ||||
67 | for ( Okular::FormField *ff: page->formFields() ) | ||||
68 | { | ||||
69 | m_fields.insert( ff->name(), ff ); | ||||
70 | } | ||||
71 | } | ||||
72 | | ||||
73 | void FormatTest::testTimeFormat() | ||||
74 | { | ||||
75 | QFETCH( QString, fieldName ); | ||||
76 | QFETCH( QString, text ); | ||||
77 | QFETCH( QString, result ); | ||||
78 | | ||||
79 | Okular::FormFieldText *fft = reinterpret_cast< Okular::FormFieldText * >( m_fields[ fieldName ] ); | ||||
80 | fft->setText( text ); | ||||
81 | m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft ); | ||||
82 | | ||||
83 | QCOMPARE( m_formattedText, result ); | ||||
84 | } | ||||
85 | | ||||
86 | void FormatTest::testTimeFormat_data() | ||||
87 | { | ||||
88 | QTest::addColumn< QString >( "fieldName" ); | ||||
89 | QTest::addColumn< QString >( "text" ); | ||||
90 | QTest::addColumn< QString > ( "result" ); | ||||
91 | | ||||
92 | QTest::newRow( "field hh:mm" ) << QStringLiteral( "time1" ) << QStringLiteral( "1:20" ) << QStringLiteral( "01:20" ); | ||||
93 | QTest::newRow( "field hh:mm with pm" ) << QStringLiteral( "time1" ) << QStringLiteral( "1:20 pm" ) << QStringLiteral( "13:20" ); | ||||
94 | QTest::newRow( "field hh:mm invalid one number" ) << QStringLiteral( "time1" ) << QStringLiteral( "1" ) << QStringLiteral( "" ); | ||||
95 | QTest::newRow( "field hh:mm invalid time" ) << QStringLiteral( "time1" ) << QStringLiteral( "25:12" ) << QStringLiteral( "" ); | ||||
96 | QTest::newRow( "field hh:mm invalid only letters" ) << QStringLiteral( "time1" ) << QStringLiteral( "abcd" ) << QStringLiteral( "" ); | ||||
97 | QTest::newRow( "field hh:mm ap" ) << QStringLiteral( "time2" ) << QStringLiteral( "1:20" ) << QStringLiteral( "1:20 am" ); | ||||
98 | QTest::newRow( "field hh:mm ap remove zero" ) << QStringLiteral( "time2" ) << QStringLiteral( "01:20 pm" ) << QStringLiteral( "1:20 pm" ); | ||||
99 | QTest::newRow( "field hh:mm ap change to AM/PM" ) << QStringLiteral( "time2" ) << QStringLiteral( "13:20" ) << QStringLiteral( "1:20 pm" ); | ||||
100 | QTest::newRow( "field hh:mm:ss without seconds" ) << QStringLiteral( "time3" ) << QStringLiteral( "1:20" ) << QStringLiteral( "01:20:00" ); | ||||
101 | QTest::newRow( "field hh:mm:ss with pm" ) << QStringLiteral( "time3" ) << QStringLiteral( "1:20:00 pm" ) << QStringLiteral( "13:20:00" ); | ||||
102 | QTest::newRow( "field hh:mm:ss ap without am" ) << QStringLiteral( "time4" ) << QStringLiteral( "1:20:00" ) << QStringLiteral( "1:20:00 am" ); | ||||
103 | QTest::newRow( "field hh:mm:ss ap remove 0" ) << QStringLiteral( "time4" ) << QStringLiteral( "01:20:00 pm" ) << QStringLiteral( "1:20:00 pm" ); | ||||
104 | QTest::newRow( "field hh:mm:ss ap change to AM/PM" ) << QStringLiteral( "time4" ) << QStringLiteral( "13:20:00" ) << QStringLiteral( "1:20:00 pm" ); | ||||
105 | } | ||||
106 | | ||||
107 | void FormatTest::testSpecialFormat() | ||||
108 | { | ||||
109 | m_formattedText = QStringLiteral( "" ); | ||||
110 | QFETCH( QString, fieldName ); | ||||
111 | QFETCH( QString, text ); | ||||
112 | QFETCH( bool, edited ); | ||||
113 | QFETCH( QString, result ); | ||||
114 | | ||||
115 | Okular::FormFieldText *fft = reinterpret_cast< Okular::FormFieldText * >( m_fields[ fieldName ] ); | ||||
116 | fft->setText( text ); | ||||
117 | bool ok = false; | ||||
118 | m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft ); | ||||
119 | m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok ); | ||||
120 | | ||||
121 | QCOMPARE( m_formattedText, result ); | ||||
122 | QCOMPARE( ok, edited ); | ||||
123 | } | ||||
124 | | ||||
125 | void FormatTest::testSpecialFormat_data() | ||||
126 | { | ||||
127 | QTest::addColumn< QString >( "fieldName" ); | ||||
128 | QTest::addColumn< QString >( "text" ); | ||||
129 | QTest::addColumn< bool >( "edited" ); | ||||
130 | QTest::addColumn< QString > ( "result" ); | ||||
131 | | ||||
132 | // The tests which have invalid edited, keep the same value as when it was formatted before. | ||||
133 | QTest::newRow( "field validated but not changed" ) << QStringLiteral( "CEP" ) << QStringLiteral( "12345" ) << true << QStringLiteral( "" ); | ||||
134 | QTest::newRow( "field invalid but not changed" ) << QStringLiteral( "CEP" ) << QStringLiteral( "123456" ) << false << QStringLiteral( "" ); | ||||
135 | QTest::newRow( "field formatted and changed" ) << QStringLiteral( "8Digits" ) << QStringLiteral( "123456789" ) << true << QStringLiteral( "12345-6789" ); | ||||
136 | QTest::newRow( "field invalid 10 digits" ) << QStringLiteral( "8Digits" ) << QStringLiteral( "1234567890" ) << false << QStringLiteral( "12345-6789" ); | ||||
137 | QTest::newRow( "field formatted telephone" ) << QStringLiteral( "telefone" ) << QStringLiteral( "1234567890" ) << true << QStringLiteral( "(123) 456-7890" ); | ||||
138 | QTest::newRow( "field invalid telephone" ) << QStringLiteral( "telefone" ) << QStringLiteral( "12345678900" ) << false << QStringLiteral( "(123) 456-7890" ); | ||||
139 | QTest::newRow( "field formmated SSN" ) << QStringLiteral( "CPF" ) << QStringLiteral( "123456789" ) << true << QStringLiteral( "123-45-6789" ); | ||||
140 | QTest::newRow( "field invalid SSN" ) << QStringLiteral( "CPF" ) << QStringLiteral( "1234567890" ) << false << QStringLiteral( "123-45-6789" ); | ||||
141 | } | ||||
142 | | ||||
143 | void FormatTest::testFocusAction() | ||||
144 | { | ||||
145 | QFETCH( QString, result ); | ||||
146 | Okular::FormFieldText *fft = reinterpret_cast< Okular::FormFieldText * >( m_fields[ "Validate/Focus" ] ); | ||||
147 | | ||||
148 | m_document->processFocusAction( fft->additionalAction( Okular::Annotation::FocusIn ), fft ); | ||||
149 | QCOMPARE( fft->text(), result ); | ||||
150 | } | ||||
151 | | ||||
152 | void FormatTest::testFocusAction_data() | ||||
153 | { | ||||
154 | QTest::addColumn< QString >( "result" ); | ||||
155 | | ||||
156 | QTest::newRow( "when focuses" ) << QStringLiteral( "No" ); | ||||
157 | } | ||||
158 | | ||||
159 | void FormatTest::testValidateAction() | ||||
160 | { | ||||
161 | QFETCH( QString, text ); | ||||
162 | QFETCH( QString, result ); | ||||
163 | Okular::FormFieldText *fft = reinterpret_cast< Okular::FormFieldText * >( m_fields[ "Validate/Focus" ] ); | ||||
164 | | ||||
165 | fft->setText( text ); | ||||
166 | bool ok = false; | ||||
167 | m_document->processValidateAction( fft->additionalAction( Okular::Annotation::FocusOut ), fft, ok ); | ||||
168 | QCOMPARE( fft->text(), result ); | ||||
169 | QVERIFY( ok ); | ||||
170 | } | ||||
171 | | ||||
172 | void FormatTest::testValidateAction_data() | ||||
173 | { | ||||
174 | QTest::addColumn< QString >( "text" ); | ||||
175 | QTest::addColumn< QString >( "result" ); | ||||
176 | | ||||
177 | QTest::newRow( "valid text was set" ) << QStringLiteral( "123" ) << QStringLiteral( "valid" ); | ||||
178 | QTest::newRow( "invalid text was set" ) << QStringLiteral( "abc" ) << QStringLiteral( "invalid" ); | ||||
179 | } | ||||
180 | | ||||
181 | void FormatTest::cleanupTestCase() | ||||
182 | { | ||||
183 | m_document->closeDocument(); | ||||
184 | delete m_document; | ||||
185 | } | ||||
186 | | ||||
187 | | ||||
188 | QTEST_MAIN( FormatTest ) | ||||
189 | #include "formattest.moc" |