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

0001 /*
0002     This file is part of the KDE KFileMetaData project
0003     SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in>
0004     SPDX-FileCopyrightText: 2017 Igor Poboiko <igor.poboiko@gmail.com>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-or-later
0007 */
0008 
0009 #include <QObject>
0010 #include <QTest>
0011 #include <QDirIterator>
0012 #include <QMimeDatabase>
0013 #include <QMultiMap>
0014 
0015 #include "mimeutils.h"
0016 
0017 #include "indexerextractortestsconfig.h"
0018 
0019 namespace KFileMetaData {
0020 
0021 class ExtractorCoverageTest : public QObject
0022 {
0023     Q_OBJECT
0024 
0025 private:
0026     static QString filePath() {
0027         return QLatin1String(INDEXER_TESTS_SAMPLE_FILES_PATH);
0028     }
0029 
0030     QStringList m_testFiles;
0031     QMultiMap<QString, QString> m_knownFiles;
0032 
0033 private Q_SLOTS:
0034 
0035     void initTestCase() {
0036         // Expected mimetypes
0037         m_knownFiles = {
0038             { "test.avi",                      "video/vnd.avi"},
0039             { "test.aif",                      "audio/x-aifc"},
0040             { "test.ape",                      "audio/x-ape"},
0041             { "test.avif",                     "image/avif"},
0042             { "test.AppImage",                 "application/vnd.appimage"},
0043             { "test_apple_systemprofiler.spx", "application/x-apple-systemprofiler+xml"},  // s-m-i < 2.0 would give "application/xml"
0044             { "test.dot",                      "text/vnd.graphviz"},
0045             { "test.eps",                      "image/x-eps"},
0046             { "test.epub",                     "application/epub+zip"},
0047             { "test.fb2",                      "application/x-fictionbook+xml"},
0048             { "test.fb2.zip",                  "application/x-zip-compressed-fb2"},
0049             { "test.flac",                     "audio/flac"},
0050             { "test.heif",                     "image/heif"}, // alias for image/heic
0051             { "test.jpg",                      "image/jpeg"},
0052             { "test.jxl",                      "image/jxl"},
0053             { "test_libreoffice.docx",         "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
0054             { "test.m4a",                      "audio/mp4"},
0055             { "test_missing_content.odt",      "application/vnd.oasis.opendocument.text"},
0056             { "test_missing_meta.odt",         "application/vnd.oasis.opendocument.text"},
0057             { "test.mkv",                      "video/x-matroska"},
0058             { "test.mp3",                      "audio/mpeg"},
0059             { "test.mpc",                      "audio/x-musepack"},
0060             { "test_no_gps.jpg",               "image/jpeg"},
0061             { "test.odg",                      "application/vnd.oasis.opendocument.graphics"},
0062             { "test.odp",                      "application/vnd.oasis.opendocument.presentation"},
0063             { "test.odt",                      "application/vnd.oasis.opendocument.text"},
0064             { "test.fodg",                     "application/vnd.oasis.opendocument.graphics-flat-xml"},
0065             { "test.fodp",                     "application/vnd.oasis.opendocument.presentation-flat-xml"},
0066             { "test.fodt",                     "application/vnd.oasis.opendocument.text-flat-xml"},
0067             { "test.ogg",                      "audio/x-vorbis+ogg"},
0068             { "test.flac.ogg",                 "audio/x-flac+ogg"},
0069             { "test.mml",                      "application/mathml+xml"},
0070             { "test_multivalue.ogg",           "audio/x-vorbis+ogg"},
0071             { "test.ogv",                      "video/x-theora+ogg"},
0072             { "test.opus",                     "audio/x-opus+ogg"},
0073             { "test.pdf",                      "application/pdf"},
0074             { "test.pl",                       "application/x-perl"},
0075             { "test.ps",                       "application/postscript"},
0076             { "test_public_key.gpg",           "application/pgp-encrypted"},
0077             { "test_repeated.epub",            "application/epub+zip"},
0078             { "test.spx",                      "audio/x-speex+ogg"},
0079             { "test.ts",                       "video/mp2t"},
0080             // Check both the actual name and its alias for wav
0081             { "test.wav",                      "audio/vnd.wave"},
0082             { "test.wav",                      "audio/x-wav"},
0083             { "test.webm",                     "video/webm"},
0084             { "test_dcterms.svg",              "image/svg+xml"},
0085             { "test_with_container.svg",       "image/svg+xml"},
0086             { "test_with_metadata.svg",        "image/svg+xml"},
0087             { "test.wma",                      "audio/x-ms-wma"},
0088             { "test.wv",                       "audio/x-wavpack"},
0089             { "test_zero_gps.jpg",             "image/jpeg"},
0090             { "test.mobi",                     "application/x-mobipocket-ebook"},
0091             { "test.png",                      "image/png"},
0092         };
0093 
0094         // Collect all test files from the samplefiles directory
0095         QDirIterator it(filePath(), {QStringLiteral("test*")}, QDir::Files);
0096         while (it.hasNext()) {
0097             it.next();
0098             m_testFiles.append(it.fileName());
0099         }
0100     }
0101 
0102     void testMimetype_data()
0103     {
0104         /*
0105          * Check if we get the correct mimetype for
0106          * each available test sample
0107          */
0108         QTest::addColumn<QString>("fileName");
0109         QTest::addColumn<QString>("mimeType");
0110 
0111         auto it = m_knownFiles.cbegin();
0112         while (it != m_knownFiles.cend()) {
0113             QTest::addRow("%s_%s", it.key().toUtf8().constData(), it.value().toUtf8().constData())
0114                 << it.key() << it.value();
0115             ++it;
0116         }
0117     }
0118 
0119     void testMimetype()
0120     {
0121         QFETCH(QString, fileName);
0122         QFETCH(QString, mimeType);
0123         QString url = filePath() + QChar('/') + fileName;
0124 
0125         QMimeDatabase db;
0126         auto fileMime = MimeUtils::strictMimeType(url, db);
0127 
0128         QVERIFY(fileMime.isValid());
0129         if (fileMime.name() == "application/xml" && mimeType == "application/x-apple-systemprofiler+xml") {
0130             // s-m-i < 2.0 didn't have application/x-apple-systemprofiler+xml yet, it's all fine
0131             return;
0132         }
0133         if (!db.mimeTypeForName(mimeType).isValid()) {
0134             /* Examples when test could be skipped:
0135              * image/avif is available since s-m-i 2.0
0136              * image/jxl will be registered in s-m-i 2.2 or when libjxl is installed
0137              */
0138             QSKIP("Expected mimetype is not registered");
0139         }
0140         if (fileMime.name() != mimeType) {
0141             const auto aliases = fileMime.aliases();
0142         if (!aliases.contains(mimeType))
0143         QCOMPARE(fileMime.name(), mimeType);
0144             QVERIFY(aliases.contains(mimeType));
0145         }
0146     }
0147 
0148     void testFileCoverage_data()
0149     {
0150         /*
0151          * Check if we get the correct mimetype for
0152          * each available test sample
0153          */
0154         QTest::addColumn<QString>("fileName");
0155 
0156         auto it = m_testFiles.cbegin();
0157         while (it != m_testFiles.cend()) {
0158             QTest::addRow("%s", it->toUtf8().constData()) << (*it);
0159             ++it;
0160         }
0161     }
0162 
0163     void testFileCoverage()
0164     {
0165         QFETCH(QString, fileName);
0166 
0167         QVERIFY2(m_knownFiles.contains(fileName), "test file omitted from test suite");
0168     }
0169 
0170 };
0171 
0172 }
0173 
0174 QTEST_GUILESS_MAIN(KFileMetaData::ExtractorCoverageTest)
0175 
0176 #include "extractorcoveragetest.moc"