File indexing completed on 2024-05-12 16:06:01

0001 /*
0002     SPDX-FileCopyrightText: 2018 Intevation GmbH <intevation@intevation.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QTest>
0008 
0009 #include "../settings_core.h"
0010 #include "core/document.h"
0011 #include <QMap>
0012 #include <QMimeDatabase>
0013 #include <QMimeType>
0014 #include <core/form.h>
0015 #include <core/page.h>
0016 
0017 class CalculateTextTest : public QObject
0018 {
0019     Q_OBJECT
0020 
0021 private Q_SLOTS:
0022     void initTestCase();
0023     void cleanupTestCase();
0024 
0025     void testSimpleCalculate();
0026 
0027 private:
0028     Okular::Document *m_document;
0029 };
0030 
0031 void CalculateTextTest::initTestCase()
0032 {
0033     Okular::SettingsCore::instance(QStringLiteral("calculatetexttest"));
0034     m_document = new Okular::Document(nullptr);
0035 }
0036 
0037 void CalculateTextTest::cleanupTestCase()
0038 {
0039     m_document->closeDocument();
0040     delete m_document;
0041 }
0042 
0043 void CalculateTextTest::testSimpleCalculate()
0044 {
0045     // Force consistent locale
0046     QLocale locale(QStringLiteral("en_US.UTF-8"));
0047     if (locale == QLocale::c()) {
0048         locale = QLocale(QLocale::English, QLocale::UnitedStates);
0049     }
0050 
0051     QLocale::setDefault(locale);
0052     const QString testFile = QStringLiteral(KDESRCDIR "data/simpleCalculate.pdf");
0053     QMimeDatabase db;
0054     const QMimeType mime = db.mimeTypeForFile(testFile);
0055     QCOMPARE(m_document->openDocument(testFile, QUrl(), mime), Okular::Document::OpenSuccess);
0056 
0057     const Okular::Page *page = m_document->page(0);
0058 
0059     QMap<QString, Okular::FormFieldText *> fields;
0060 
0061     // Field names in test document are:
0062     // field1, field2, field3, Sum, AVG, Prod, Min, Max
0063 
0064     const QList<Okular::FormField *> pageFormFields = page->formFields();
0065     for (Okular::FormField *ff : pageFormFields) {
0066         fields.insert(ff->name(), static_cast<Okular::FormFieldText *>(ff));
0067     }
0068 
0069     // Set some values and do calculation
0070     Okular::FormFieldText *field1 = fields[QStringLiteral("field1")];
0071     QVERIFY(field1);
0072     m_document->editFormText(0, field1, QStringLiteral("10"), 0, 0, 0);
0073 
0074     Okular::FormFieldText *field2 = fields[QStringLiteral("field2")];
0075     QVERIFY(field2);
0076     m_document->editFormText(0, field2, QStringLiteral("20"), 0, 0, 0);
0077 
0078     Okular::FormFieldText *field3 = fields[QStringLiteral("field3")];
0079     QVERIFY(field3);
0080     m_document->editFormText(0, field3, QStringLiteral("30"), 0, 0, 0);
0081 
0082     // Verify the results
0083     QCOMPARE(fields[QStringLiteral("Sum")]->text(), QStringLiteral("60"));
0084     QCOMPARE(fields[QStringLiteral("AVG")]->text(), QStringLiteral("20"));
0085     QCOMPARE(fields[QStringLiteral("Prod")]->text(), QStringLiteral("6,000"));
0086     QCOMPARE(fields[QStringLiteral("Min")]->text(), QStringLiteral("10"));
0087     QCOMPARE(fields[QStringLiteral("Max")]->text(), QStringLiteral("30"));
0088 
0089     // Verify that Sum was not recalculated after set without edit
0090     QCOMPARE(fields[QStringLiteral("Sum")]->text(), QStringLiteral("60"));
0091 
0092     // Test that multiplication with zero works
0093     m_document->editFormText(0, field2, QStringLiteral("0"), 0, 0, 0);
0094     QCOMPARE(fields[QStringLiteral("Prod")]->text(), QStringLiteral("0"));
0095 
0096     // Test that updating the field also worked with sum
0097     QCOMPARE(fields[QStringLiteral("Sum")]->text(), QStringLiteral("40"));
0098 
0099     // Test that undo / redo works
0100     QVERIFY(m_document->canUndo());
0101     m_document->undo();
0102     QCOMPARE(fields[QStringLiteral("Sum")]->text(), QStringLiteral("60"));
0103 
0104     QVERIFY(m_document->canRedo());
0105     m_document->redo();
0106     QCOMPARE(fields[QStringLiteral("Sum")]->text(), QStringLiteral("40"));
0107 }
0108 
0109 QTEST_MAIN(CalculateTextTest)
0110 #include "calculatetexttest.moc"