diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 7345b9f..626747f 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -1,175 +1,190 @@ find_package(Qt5 ${REQUIRED_QT_VERSION} CONFIG REQUIRED Test) find_package(PythonInterp) set_package_properties(PythonInterp PROPERTIES DESCRIPTION "Python Interpreter" URL "https://www.python.org" TYPE OPTIONAL PURPOSE "Python interpreter is needed to execute test for external extractors or writers") remove_definitions(-DQT_NO_CAST_FROM_ASCII) configure_file(indexerextractortestsconfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/indexerextractortestsconfig.h @ONLY) set(KfileMetaDataAutotest_SRCS) ecm_qt_declare_logging_category(KfileMetaDataAutotest_SRCS HEADER kfilemetadata_debug.h IDENTIFIER KFILEMETADATA_LOG CATEGORY_NAME kf5.kfilemetadata) set(indexerextractor_SRCS indexerextractortests.cpp ../src/extractors/plaintextextractor.cpp ) ecm_add_test(${indexerextractor_SRCS} TEST_NAME "indexextractortest" LINK_LIBRARIES Qt5::Test KF5::FileMetaData ) # # Office # if(KF5Archive_FOUND) ecm_add_test(odfextractortest.cpp ../src/extractors/odfextractor.cpp TEST_NAME "odfextractortest" LINK_LIBRARIES Qt5::Test Qt5::Xml KF5::FileMetaData KF5::Archive ) endif() if(KF5Archive_FOUND) ecm_add_test(office2007extractortest.cpp ../src/extractors/office2007extractor.cpp TEST_NAME "officeextractortest" LINK_LIBRARIES Qt5::Test Qt5::Xml KF5::FileMetaData KF5::Archive ) endif() # # Poppler # if(Poppler_Qt5_FOUND) ecm_add_test(popplerextractortest.cpp ../src/extractors/popplerextractor.cpp TEST_NAME "popplerextractortest" LINK_LIBRARIES Qt5::Test KF5::FileMetaData Poppler::Qt5 ) endif() # # EPub # if(EPUB_FOUND) include_directories(${EPUB_INCLUDE_DIR}) ecm_add_test(epubextractortest.cpp ../src/extractors/epubextractor.cpp TEST_NAME "epubextractortest" LINK_LIBRARIES Qt5::Test KF5::FileMetaData ${EPUB_LIBRARIES} ) endif() # # Mobi # if (QMOBIPOCKET_FOUND) include_directories(${QMOBIPOCKET_INCLUDE_DIR}) ecm_add_test(mobiextractortest.cpp ../src/extractors/mobiextractor.cpp TEST_NAME "mobiextractortest" LINK_LIBRARIES Qt5::Test KF5::FileMetaData ${QMOBIPOCKET_LIBRARIES} ) endif() # # Property Info # ecm_add_test(propertyinfotest.cpp TEST_NAME "propertyinfotest" LINK_LIBRARIES Qt5::Test KF5::FileMetaData ) # # Exiv2 # if(EXIV2_FOUND) include_directories(${EXIV2_INCLUDE_DIR}) kde_enable_exceptions() ecm_add_test(exiv2extractortest.cpp ../src/extractors/exiv2extractor.cpp TEST_NAME "exiv2extractortest" LINK_LIBRARIES Qt5::Test KF5::FileMetaData ${EXIV2_LIBRARIES} ) endif() # # TagLib # if(TAGLIB_FOUND) kde_enable_exceptions() ecm_add_test(taglibextractortest.cpp ../src/extractors/taglibextractor.cpp TEST_NAME "taglibextractortest" LINK_LIBRARIES Qt5::Test KF5::FileMetaData ${TAGLIB_LIBRARIES} ) target_include_directories(taglibextractortest SYSTEM PRIVATE ${TAGLIB_INCLUDES}) ecm_add_test(embeddedimagedatatest.cpp TEST_NAME "embeddedimagedatatest" LINK_LIBRARIES Qt5::Test KF5::FileMetaData ${TAGLIB_LIBRARIES} ) target_include_directories(embeddedimagedatatest SYSTEM PRIVATE ${TAGLIB_INCLUDES}) endif() if(PYTHONINTERP_FOUND) configure_file(samplefiles/testexternalextractor/main.py samplefiles/testexternalextractor/main.py) configure_file(samplefiles/testexternalextractor/manifest.json samplefiles/testexternalextractor/manifest.json COPYONLY) ecm_add_test(externalextractortest.cpp ../src/externalextractor.cpp ${KfileMetaDataAutotest_SRCS} TEST_NAME "externalextractortest" LINK_LIBRARIES Qt5::Test KF5::FileMetaData KF5::I18n ) endif() # # Collection # set(extractorcollection_SRCS extractorcollectiontest.cpp ) ecm_add_test(${extractorcollection_SRCS} TEST_NAME "extractorcollectiontest" LINK_LIBRARIES Qt5::Test KF5::FileMetaData ) +# +# XML +# +set(xmlextractor_SRCS + xmlextractortest.cpp + ../src/extractors/xmlextractor.cpp + ../src/extractors/dublincoreextractor.cpp + ../src/kfilemetadata_debug.cpp +) + +ecm_add_test(${xmlextractor_SRCS} + TEST_NAME "xmlextractortest" + LINK_LIBRARIES Qt5::Test Qt5::Xml KF5::FileMetaData +) + ################ # Writer tests # ################ # # UserMetaData # if(CMAKE_SYSTEM_NAME MATCHES "Linux") kde_enable_exceptions() ecm_add_test(usermetadatawritertest.cpp ../src/usermetadata.cpp TEST_NAME "usermetadatawritertest" LINK_LIBRARIES Qt5::Test KF5::FileMetaData ) endif() # # TagLib # if(TAGLIB_FOUND) kde_enable_exceptions() ecm_add_test(taglibwritertest.cpp ../src/writers/taglibwriter.cpp TEST_NAME "taglibwritertest" LINK_LIBRARIES Qt5::Test KF5::FileMetaData ${TAGLIB_LIBRARIES} ) target_include_directories(taglibwritertest SYSTEM PRIVATE ${TAGLIB_INCLUDES}) endif() if(PYTHONINTERP_FOUND) configure_file(samplefiles/testexternalwriter/main.py samplefiles/testexternalwriter/main.py) configure_file(samplefiles/testexternalwriter/manifest.json samplefiles/testexternalwriter/manifest.json COPYONLY) ecm_add_test(externalwritertest.cpp ../src/externalwriter.cpp ${KfileMetaDataAutotest_SRCS} TEST_NAME "externalwritertest" LINK_LIBRARIES Qt5::Test KF5::FileMetaData KF5::I18n ) endif() diff --git a/autotests/samplefiles/test_with_metadata.svg b/autotests/samplefiles/test_with_metadata.svg new file mode 100644 index 0000000..f80f947 --- /dev/null +++ b/autotests/samplefiles/test_with_metadata.svg @@ -0,0 +1,22 @@ + + + + + + image/svg+xml + + Document Title + + + + + Some text + + + diff --git a/autotests/xmlextractortest.cpp b/autotests/xmlextractortest.cpp new file mode 100644 index 0000000..37cec3c --- /dev/null +++ b/autotests/xmlextractortest.cpp @@ -0,0 +1,117 @@ +/* + Copyright (C) 2018 Stefan Brüns + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +*/ + +#include "xmlextractortest.h" + +#include + +#include "simpleextractionresult.h" +#include "indexerextractortestsconfig.h" +#include "extractors/xmlextractor.h" + +using namespace KFileMetaData; + +XmlExtractorTests::XmlExtractorTests(QObject* parent) : + QObject(parent) +{ +} + +QString XmlExtractorTests::testFilePath(const QString& fileName) const +{ + return QLatin1String(INDEXER_TESTS_SAMPLE_FILES_PATH) + QLatin1Char('/') + fileName; +} + +void XmlExtractorTests::benchMarkXmlExtractor() +{ + XmlExtractor plugin(this); + + // generate a test file with varying number of words per line + QTemporaryFile file(QStringLiteral("XXXXXX.xml")); + QVERIFY(file.open()); + + int count = 0; + file.write("\n"); + QByteArray chunk("foo bar "); + for (int line = 0; line < 10000; ++line) { + // staircase pattern, 0, 1, 2, ... 98, 0, 0, 1 ... chunks per line + for (int i = 0; i < line % 100; ++i) { + count++; + file.write(chunk); + } + file.write("\n"); + } + file.write("\n"); + file.close(); + + SimpleExtractionResult result(file.fileName(), QStringLiteral("application/xml")); + + plugin.extract(&result); + + QString content = QStringLiteral("foo bar\n"); + content.replace(QLatin1Char('\n'), QLatin1Char(' ')); + QCOMPARE(result.text().leftRef(8), content.leftRef(8)); + QCOMPARE(result.text().size(), 1 + 8 * count); + + QBENCHMARK { + plugin.extract(&result); + } +} + +void XmlExtractorTests::testXmlExtractor() +{ + XmlExtractor plugin{this}; + + SimpleExtractionResult result(testFilePath(QStringLiteral("test_with_metadata.svg")), + QStringLiteral("image/svg"), + ExtractionResult::ExtractEverything); + plugin.extract(&result); + + QString content = QStringLiteral("Some text\n"); + + QCOMPARE(result.types().size(), 1); + QCOMPARE(result.types().at(0), Type::Image); + + QCOMPARE(result.properties().size(), 1); + QCOMPARE(result.properties().value(Property::Title), QStringLiteral("Document Title")); + + content.replace(QLatin1Char('\n'), QLatin1Char(' ')); + QCOMPARE(result.text(), content); +} + +void XmlExtractorTests::testXmlExtractorNoContent() +{ + XmlExtractor plugin{this}; + + SimpleExtractionResult result(testFilePath(QStringLiteral("test_with_metadata.svg")), + QStringLiteral("image/svg"), + ExtractionResult::ExtractMetaData); + plugin.extract(&result); + + QCOMPARE(result.types().size(), 1); + QCOMPARE(result.types().at(0), Type::Image); + + QCOMPARE(result.properties().size(), 1); + QCOMPARE(result.properties().value(Property::Title), QStringLiteral("Document Title")); + + QVERIFY(result.text().isEmpty()); +} + +QTEST_GUILESS_MAIN(XmlExtractorTests) + diff --git a/autotests/xmlextractortest.h b/autotests/xmlextractortest.h new file mode 100644 index 0000000..4065410 --- /dev/null +++ b/autotests/xmlextractortest.h @@ -0,0 +1,42 @@ +/* + Copyright (C) 2018 Stefan Brüns + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +*/ + +#ifndef XMLEXTRACTORTESTS_H +#define XMLEXTRACTORTESTS_H + +#include +#include + +class XmlExtractorTests : public QObject +{ + Q_OBJECT +public: + explicit XmlExtractorTests(QObject* parent = nullptr); + +private: + QString testFilePath(const QString& fileName) const; + +private Q_SLOTS: + void benchMarkXmlExtractor(); + void testXmlExtractor(); + void testXmlExtractorNoContent(); +}; + +#endif // XMLEXTRACTORTESTS_H