File indexing completed on 2024-05-05 03:54:34

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_libreoffice.pptx",         "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
0055             { "test_libreoffice.xlsx",         "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
0056             { "test.m4a",                      "audio/mp4"},
0057             { "test_missing_content.odt",      "application/vnd.oasis.opendocument.text"},
0058             { "test_missing_meta.odt",         "application/vnd.oasis.opendocument.text"},
0059             { "test.mkv",                      "video/x-matroska"},
0060             { "test.mp3",                      "audio/mpeg"},
0061             { "test.id3v1.mp3",                "audio/mpeg"},
0062             { "test.mpc",                      "audio/x-musepack"},
0063             { "test_no_gps.jpg",               "image/jpeg"},
0064             { "test.odg",                      "application/vnd.oasis.opendocument.graphics"},
0065             { "test.odp",                      "application/vnd.oasis.opendocument.presentation"},
0066             { "test.odt",                      "application/vnd.oasis.opendocument.text"},
0067             { "test.ods",                      "application/vnd.oasis.opendocument.spreadsheet"},
0068             { "test.fodg",                     "application/vnd.oasis.opendocument.graphics-flat-xml"},
0069             { "test.fodp",                     "application/vnd.oasis.opendocument.presentation-flat-xml"},
0070             { "test.fodt",                     "application/vnd.oasis.opendocument.text-flat-xml"},
0071             { "test.ogg",                      "audio/x-vorbis+ogg"},
0072             { "test.flac.ogg",                 "audio/x-flac+ogg"},
0073             { "test.mml",                      "application/mathml+xml"},
0074             { "test_multivalue.ogg",           "audio/x-vorbis+ogg"},
0075             { "test.ogv",                      "video/x-theora+ogg"},
0076             { "test.opus",                     "audio/x-opus+ogg"},
0077             { "test.pdf",                      "application/pdf"},
0078             { "test.pl",                       "application/x-perl"},
0079             { "test.ps",                       "application/postscript"},
0080             { "test_public_key.gpg",           "application/pgp-encrypted"},
0081             { "test_repeated.epub",            "application/epub+zip"},
0082             { "test.spx",                      "audio/x-speex+ogg"},
0083             { "test.ts",                       "video/mp2t"},
0084             // Check both the actual name and its alias for wav
0085             { "test.wav",                      "audio/vnd.wave"},
0086             { "test.wav",                      "audio/x-wav"},
0087             { "test.webm",                     "video/webm"},
0088             { "test.webp",                     "image/webp"},
0089             { "test_dcterms.svg",              "image/svg+xml"},
0090             { "test_with_container.svg",       "image/svg+xml"},
0091             { "test_with_metadata.svg",        "image/svg+xml"},
0092             { "test.wma",                      "audio/x-ms-wma"},
0093             { "test.wv",                       "audio/x-wavpack"},
0094             { "test_zero_gps.jpg",             "image/jpeg"},
0095             { "test.mobi",                     "application/x-mobipocket-ebook"},
0096             { "test.png",                      "image/png"},
0097             { "test.kra",                      "application/x-krita"},
0098         };
0099 
0100         // Collect all test files from the samplefiles directory
0101         QDirIterator it(filePath(), {QStringLiteral("test*")}, QDir::Files);
0102         while (it.hasNext()) {
0103             it.next();
0104             m_testFiles.append(it.fileName());
0105         }
0106     }
0107 
0108     void testMimetype_data()
0109     {
0110         /*
0111          * Check if we get the correct mimetype for
0112          * each available test sample
0113          */
0114         QTest::addColumn<QString>("fileName");
0115         QTest::addColumn<QString>("mimeType");
0116 
0117         auto it = m_knownFiles.cbegin();
0118         while (it != m_knownFiles.cend()) {
0119             QTest::addRow("%s_%s", it.key().toUtf8().constData(), it.value().toUtf8().constData())
0120                 << it.key() << it.value();
0121             ++it;
0122         }
0123     }
0124 
0125     void testMimetype()
0126     {
0127         QFETCH(QString, fileName);
0128         QFETCH(QString, mimeType);
0129         QString url = filePath() + QChar('/') + fileName;
0130 
0131         QMimeDatabase db;
0132         auto fileMime = MimeUtils::strictMimeType(url, db);
0133 
0134         QVERIFY(fileMime.isValid());
0135         if (fileMime.name() == "application/xml" && mimeType == "application/x-apple-systemprofiler+xml") {
0136             // s-m-i < 2.0 didn't have application/x-apple-systemprofiler+xml yet, it's all fine
0137             return;
0138         }
0139         if (!db.mimeTypeForName(mimeType).isValid()) {
0140             /* Examples when test could be skipped:
0141              * image/avif is available since s-m-i 2.0
0142              * image/jxl will be registered in s-m-i 2.2 or when libjxl is installed
0143              */
0144             QSKIP("Expected mimetype is not registered");
0145         }
0146         if (fileMime.name() != mimeType) {
0147             const auto aliases = fileMime.aliases();
0148         if (!aliases.contains(mimeType))
0149         QCOMPARE(fileMime.name(), mimeType);
0150             QVERIFY(aliases.contains(mimeType));
0151         }
0152     }
0153 
0154     void testFileCoverage_data()
0155     {
0156         /*
0157          * Check if we get the correct mimetype for
0158          * each available test sample
0159          */
0160         QTest::addColumn<QString>("fileName");
0161 
0162         auto it = m_testFiles.cbegin();
0163         while (it != m_testFiles.cend()) {
0164             QTest::addRow("%s", it->toUtf8().constData()) << (*it);
0165             ++it;
0166         }
0167     }
0168 
0169     void testFileCoverage()
0170     {
0171         QFETCH(QString, fileName);
0172 
0173         QVERIFY2(m_knownFiles.contains(fileName), "test file omitted from test suite");
0174     }
0175 
0176 };
0177 
0178 }
0179 
0180 QTEST_GUILESS_MAIN(KFileMetaData::ExtractorCoverageTest)
0181 
0182 #include "extractorcoveragetest.moc"