File indexing completed on 2024-05-12 04:33:27

0001 /*
0002     SPDX-FileCopyrightText: 2019 João Netto <joaonetto901@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QTest>
0008 
0009 #include "../settings_core.h"
0010 #include <QLocale>
0011 #include <QMap>
0012 #include <QMimeDatabase>
0013 #include <QMimeType>
0014 #include <core/document.h>
0015 #include <core/form.h>
0016 #include <core/page.h>
0017 
0018 class FormatTest : public QObject
0019 {
0020     Q_OBJECT
0021 
0022 private Q_SLOTS:
0023     void initTestCase();
0024     void cleanupTestCase();
0025     void testTimeFormat();
0026     void testTimeFormat_data();
0027     void testSpecialFormat();
0028     void testSpecialFormat_data();
0029     void testFocusAction();
0030     void testFocusAction_data();
0031     void testValidateAction();
0032     void testValidateAction_data();
0033 
0034 private:
0035     Okular::Document *m_document;
0036     QMap<QString, Okular::FormField *> m_fields;
0037     QString m_formattedText;
0038 };
0039 
0040 void FormatTest::initTestCase()
0041 {
0042     Okular::SettingsCore::instance(QStringLiteral("formattest"));
0043     m_document = new Okular::Document(nullptr);
0044 
0045     // Force consistent locale
0046     QLocale locale(QStringLiteral("en_US"));
0047     QLocale::setDefault(locale);
0048 
0049     const QString testFile = QStringLiteral(KDESRCDIR "data/formattest.pdf");
0050     QMimeDatabase db;
0051     const QMimeType mime = db.mimeTypeForFile(testFile);
0052     QCOMPARE(m_document->openDocument(testFile, QUrl(), mime), Okular::Document::OpenSuccess);
0053 
0054     connect(m_document, &Okular::Document::refreshFormWidget, this, [this](Okular::FormField *form) {
0055         Okular::FormFieldText *fft = reinterpret_cast<Okular::FormFieldText *>(form);
0056         if (fft) {
0057             m_formattedText = fft->text();
0058         }
0059     });
0060 
0061     const Okular::Page *page = m_document->page(0);
0062     const QList<Okular::FormField *> pageFormFields = page->formFields();
0063     for (Okular::FormField *ff : pageFormFields) {
0064         m_fields.insert(ff->name(), ff);
0065     }
0066 }
0067 
0068 void FormatTest::testTimeFormat()
0069 {
0070     QFETCH(QString, fieldName);
0071     QFETCH(QString, text);
0072     QFETCH(QString, result);
0073 
0074     Okular::FormFieldText *fft = reinterpret_cast<Okular::FormFieldText *>(m_fields[fieldName]);
0075     fft->setText(text);
0076     m_document->processFormatAction(fft->additionalAction(Okular::FormField::FormatField), fft);
0077 
0078     QCOMPARE(m_formattedText, result);
0079 }
0080 
0081 void FormatTest::testTimeFormat_data()
0082 {
0083     QTest::addColumn<QString>("fieldName");
0084     QTest::addColumn<QString>("text");
0085     QTest::addColumn<QString>("result");
0086 
0087     QTest::newRow("field hh:mm") << QStringLiteral("time1") << QStringLiteral("1:20") << QStringLiteral("01:20");
0088     QTest::newRow("field hh:mm with pm") << QStringLiteral("time1") << QStringLiteral("1:20 pm") << QStringLiteral("13:20");
0089     QTest::newRow("field hh:mm invalid one number") << QStringLiteral("time1") << QStringLiteral("1") << QString(QLatin1String(""));
0090     QTest::newRow("field hh:mm invalid time") << QStringLiteral("time1") << QStringLiteral("25:12") << QString(QLatin1String(""));
0091     QTest::newRow("field hh:mm invalid only letters") << QStringLiteral("time1") << QStringLiteral("abcd") << QString(QLatin1String(""));
0092     QTest::newRow("field hh:mm ap") << QStringLiteral("time2") << QStringLiteral("1:20") << QStringLiteral("1:20 am");
0093     QTest::newRow("field hh:mm ap remove zero") << QStringLiteral("time2") << QStringLiteral("01:20 pm") << QStringLiteral("1:20 pm");
0094     QTest::newRow("field hh:mm ap change to AM/PM") << QStringLiteral("time2") << QStringLiteral("13:20") << QStringLiteral("1:20 pm");
0095     QTest::newRow("field hh:mm:ss without seconds") << QStringLiteral("time3") << QStringLiteral("1:20") << QStringLiteral("01:20:00");
0096     QTest::newRow("field hh:mm:ss with pm") << QStringLiteral("time3") << QStringLiteral("1:20:00 pm") << QStringLiteral("13:20:00");
0097     QTest::newRow("field hh:mm:ss ap without am") << QStringLiteral("time4") << QStringLiteral("1:20:00") << QStringLiteral("1:20:00 am");
0098     QTest::newRow("field hh:mm:ss ap remove 0") << QStringLiteral("time4") << QStringLiteral("01:20:00 pm") << QStringLiteral("1:20:00 pm");
0099     QTest::newRow("field hh:mm:ss ap change to AM/PM") << QStringLiteral("time4") << QStringLiteral("13:20:00") << QStringLiteral("1:20:00 pm");
0100 }
0101 
0102 void FormatTest::testSpecialFormat()
0103 {
0104     m_formattedText = QLatin1String("");
0105     QFETCH(QString, fieldName);
0106     QFETCH(QString, text);
0107     QFETCH(QString, result);
0108 
0109     Okular::FormFieldText *fft = reinterpret_cast<Okular::FormFieldText *>(m_fields[fieldName]);
0110     fft->setText(text);
0111     m_document->processFormatAction(fft->additionalAction(Okular::FormField::FormatField), fft);
0112 
0113     QCOMPARE(m_formattedText, result);
0114 }
0115 
0116 void FormatTest::testSpecialFormat_data()
0117 {
0118     QTest::addColumn<QString>("fieldName");
0119     QTest::addColumn<QString>("text");
0120     QTest::addColumn<QString>("result");
0121 
0122     // The tests which have invalid edited, keep the same value as when it was formatted before.
0123     QTest::newRow("field validated but not changed") << QStringLiteral("CEP") << QStringLiteral("12345") << QString(QLatin1String(""));
0124     QTest::newRow("field invalid but not changed") << QStringLiteral("CEP") << QStringLiteral("123456") << QString(QLatin1String(""));
0125     QTest::newRow("field formatted and changed") << QStringLiteral("8Digits") << QStringLiteral("123456789") << QStringLiteral("12345-6789");
0126     QTest::newRow("field invalid 10 digits") << QStringLiteral("8Digits") << QStringLiteral("1234567890") << QStringLiteral("12345-6789");
0127     QTest::newRow("field formatted telephone") << QStringLiteral("telefone") << QStringLiteral("1234567890") << QStringLiteral("(123) 456-7890");
0128     QTest::newRow("field invalid telephone") << QStringLiteral("telefone") << QStringLiteral("12345678900") << QStringLiteral("(123) 456-7890");
0129     QTest::newRow("field formatted SSN") << QStringLiteral("CPF") << QStringLiteral("123456789") << QStringLiteral("123-45-6789");
0130     QTest::newRow("field invalid SSN") << QStringLiteral("CPF") << QStringLiteral("1234567890") << QStringLiteral("123-45-6789");
0131 }
0132 
0133 void FormatTest::testFocusAction()
0134 {
0135     QFETCH(QString, result);
0136     Okular::FormFieldText *fft = reinterpret_cast<Okular::FormFieldText *>(m_fields[QStringLiteral("Validate/Focus")]);
0137 
0138     m_document->processFocusAction(fft->additionalAction(Okular::Annotation::FocusIn), fft);
0139     QCOMPARE(fft->text(), result);
0140 }
0141 
0142 void FormatTest::testFocusAction_data()
0143 {
0144     QTest::addColumn<QString>("result");
0145 
0146     QTest::newRow("when focuses") << QStringLiteral("No");
0147 }
0148 
0149 void FormatTest::testValidateAction()
0150 {
0151     QFETCH(QString, text);
0152     QFETCH(QString, result);
0153     Okular::FormFieldText *fft = reinterpret_cast<Okular::FormFieldText *>(m_fields[QStringLiteral("Validate/Focus")]);
0154 
0155     fft->setText(text);
0156     bool ok = false;
0157     m_document->processValidateAction(fft->additionalAction(Okular::Annotation::FocusOut), fft, ok);
0158     QCOMPARE(fft->text(), result);
0159     QVERIFY(ok);
0160 }
0161 
0162 void FormatTest::testValidateAction_data()
0163 {
0164     QTest::addColumn<QString>("text");
0165     QTest::addColumn<QString>("result");
0166 
0167     QTest::newRow("valid text was set") << QStringLiteral("123") << QStringLiteral("valid");
0168     QTest::newRow("invalid text was set") << QStringLiteral("abc") << QStringLiteral("invalid");
0169 }
0170 
0171 void FormatTest::cleanupTestCase()
0172 {
0173     m_document->closeDocument();
0174     delete m_document;
0175 }
0176 
0177 QTEST_MAIN(FormatTest)
0178 #include "formattest.moc"