File indexing completed on 2024-04-28 15:22:15

0001 /*
0002     SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in>
0003     SPDX-FileCopyrightText: 2016 Christoph Cullmann <cullmann@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "odfextractortest.h"
0009 
0010 #include <QTest>
0011 
0012 #include "simpleextractionresult.h"
0013 #include "indexerextractortestsconfig.h"
0014 #include "extractors/odfextractor.h"
0015 #include "mimeutils.h"
0016 
0017 using namespace KFileMetaData;
0018 
0019 Q_DECLARE_METATYPE(Type::Type)
0020 
0021 QString OdfExtractorTest::testFilePath(const QString& fileName) const
0022 {
0023     return QLatin1String(INDEXER_TESTS_SAMPLE_FILES_PATH) + QLatin1Char('/') + fileName;
0024 }
0025 
0026 void OdfExtractorTest::testNoExtraction_data()
0027 {
0028     QTest::addColumn<QString>("fileName");
0029     QTest::addColumn<QVector<Type::Type>>("types");
0030 
0031     QTest::newRow("regular text") << QStringLiteral("test.odt") << QVector<Type::Type>{Type::Document};
0032     QTest::newRow("flat xml text") << QStringLiteral("test.fodt") << QVector<Type::Type>{Type::Document};
0033 
0034     QTest::newRow("regular presentation") << QStringLiteral("test.odp") << QVector<Type::Type>{Type::Document, Type::Presentation};
0035     QTest::newRow("flat xml presentation") << QStringLiteral("test.fodp") << QVector<Type::Type>{Type::Document, Type::Presentation};
0036 
0037     QTest::newRow("regular graphic") << QStringLiteral("test.odg") << QVector<Type::Type>{Type::Document, Type::Image};
0038     QTest::newRow("flat xml graphic") << QStringLiteral("test.fodg") << QVector<Type::Type>{Type::Document, Type::Image};
0039 }
0040 
0041 void OdfExtractorTest::testNoExtraction()
0042 {
0043     QFETCH(QString, fileName);
0044     QFETCH(QVector<Type::Type>, types);
0045 
0046     OdfExtractor plugin{this};
0047 
0048     const QString path = testFilePath(fileName);
0049     const QString mimeType = MimeUtils::strictMimeType(path, mimeDb).name();
0050     QVERIFY(plugin.mimetypes().contains(mimeType));
0051 
0052     SimpleExtractionResult result(path, mimeType, ExtractionResult::ExtractNothing);
0053     plugin.extract(&result);
0054 
0055     QCOMPARE(result.types().size(), types.size());
0056     QCOMPARE(result.types(), types);
0057     QCOMPARE(result.properties().size(),0);
0058 }
0059 
0060 void OdfExtractorTest::testText_data()
0061 {
0062     QTest::addColumn<QString>("fileName");
0063 
0064     QTest::newRow("regular") << QStringLiteral("test.odt");
0065     QTest::newRow("flat xml") << QStringLiteral("test.fodt");
0066 }
0067 
0068 void OdfExtractorTest::testText()
0069 {
0070     QFETCH(QString, fileName);
0071 
0072     OdfExtractor plugin{this};
0073 
0074     const QString path = testFilePath(fileName);
0075     const QString mimeType = MimeUtils::strictMimeType(path, mimeDb).name();
0076     QVERIFY(plugin.mimetypes().contains(mimeType));
0077 
0078     SimpleExtractionResult result(path, mimeType);
0079     plugin.extract(&result);
0080 
0081     QCOMPARE(result.types().size(), 1);
0082     QCOMPARE(result.types().at(0), Type::Document);
0083 
0084     QCOMPARE(result.properties().value(Property::Title), QVariant(QStringLiteral("KFileMetaData Title")));
0085     QCOMPARE(result.properties().value(Property::Subject), QVariant(QStringLiteral("KFileMetaData Subject")));
0086     QCOMPARE(result.properties().value(Property::Keywords), QVariant(QStringLiteral("KFileMetaData keyword")));
0087     QCOMPARE(result.properties().value(Property::Description), QVariant(QStringLiteral("KFileMetaData description")));
0088     QVERIFY(result.properties().value(Property::Generator).toString().contains(QStringLiteral("LibreOffice")));
0089 
0090     QDateTime dt(QDate(2014, 07, 01), QTime(17, 37, 40, 690));
0091     QCOMPARE(result.properties().value(Property::CreationDate), QVariant(dt));
0092 
0093     QCOMPARE(result.properties().value(Property::WordCount), QVariant(4));
0094     QCOMPARE(result.properties().value(Property::PageCount), QVariant(1));
0095     QCOMPARE(result.text(), QStringLiteral("Test file for KFileMetaData. "));
0096     QCOMPARE(result.properties().size(), 8);
0097 }
0098 
0099 void OdfExtractorTest::testTextMetaDataOnly()
0100 {
0101     OdfExtractor plugin{this};
0102 
0103     SimpleExtractionResult result(testFilePath(QStringLiteral("test.odt")), QStringLiteral("application/vnd.oasis.opendocument.text"), ExtractionResult::ExtractMetaData);
0104     plugin.extract(&result);
0105 
0106     QCOMPARE(result.types().size(), 1);
0107     QCOMPARE(result.properties().size(), 8);
0108     QVERIFY(result.text().isEmpty());
0109 }
0110 
0111 void OdfExtractorTest::testPresentation_data()
0112 {
0113     QTest::addColumn<QString>("fileName");
0114 
0115     QTest::newRow("regular") << QStringLiteral("test.odp");
0116     QTest::newRow("flat xml") << QStringLiteral("test.fodp");
0117 }
0118 
0119 void OdfExtractorTest::testPresentation()
0120 {
0121     QFETCH(QString, fileName);
0122 
0123     OdfExtractor plugin{this};
0124 
0125     const QString path = testFilePath(fileName);
0126     const QString mimeType = MimeUtils::strictMimeType(path, mimeDb).name();
0127     QVERIFY(plugin.mimetypes().contains(mimeType));
0128 
0129     SimpleExtractionResult result(path, mimeType);
0130     plugin.extract(&result);
0131 
0132     QCOMPARE(result.types().size(), 2);
0133     QCOMPARE(result.types().at(0), Type::Document);
0134     QCOMPARE(result.types().at(1), Type::Presentation);
0135 
0136     QVERIFY(result.properties().value(Property::Generator).toString().contains(QStringLiteral("LibreOffice")));
0137     QDateTime dt(QDate(2014, 07, 02), QTime(10, 59, 23, 434));
0138     QCOMPARE(result.properties().value(Property::CreationDate), QVariant(dt));
0139 
0140     QCOMPARE(result.text(), QStringLiteral("KFileMetaData Pres "));
0141 }
0142 
0143 void OdfExtractorTest::testGraphic_data()
0144 {
0145     QTest::addColumn<QString>("fileName");
0146 
0147     QTest::newRow("regular") << QStringLiteral("test.odg");
0148     QTest::newRow("flat xml") << QStringLiteral("test.fodg");
0149 }
0150 
0151 void OdfExtractorTest::testGraphic()
0152 {
0153     QFETCH(QString, fileName);
0154 
0155     OdfExtractor plugin{this};
0156 
0157     const QString path = testFilePath(fileName);
0158     const QString mimeType = MimeUtils::strictMimeType(path, mimeDb).name();
0159     QVERIFY(plugin.mimetypes().contains(mimeType));
0160 
0161     SimpleExtractionResult result(path, mimeType);
0162     plugin.extract(&result);
0163 
0164     QCOMPARE(result.types().size(), 2);
0165     QCOMPARE(result.types().at(0), Type::Document);
0166     QCOMPARE(result.types().at(1), Type::Image);
0167 
0168     QVERIFY(result.properties().value(Property::Generator).toString().contains(QStringLiteral("LibreOffice")));
0169     QDateTime dt(QDate(2022, 11, 8), QTime(15, 27, 0, 660));
0170     QCOMPARE(result.properties().value(Property::CreationDate), QVariant(dt));
0171 
0172     QCOMPARE(result.text(), QStringLiteral("KFileMetaData Graphic "));
0173 }
0174 
0175 void OdfExtractorTest::testTextMissingMetaNoCrash()
0176 {
0177     OdfExtractor plugin{this};
0178 
0179     SimpleExtractionResult result(testFilePath(QStringLiteral("test_missing_meta.odt")), QStringLiteral("application/vnd.oasis.opendocument.text"));
0180     plugin.extract(&result);
0181 }
0182 
0183 void OdfExtractorTest::testTextMissingContentNoCrash()
0184 {
0185     OdfExtractor plugin{this};
0186 
0187     SimpleExtractionResult result(testFilePath(QStringLiteral("test_missing_content.odt")), QStringLiteral("application/vnd.oasis.opendocument.text"));
0188     plugin.extract(&result);
0189 }
0190 
0191 QTEST_GUILESS_MAIN(OdfExtractorTest)
0192 
0193 #include "moc_odfextractortest.cpp"