File indexing completed on 2024-05-12 05:09:58

0001 /***************************************************************************
0002     Copyright (C) 2015 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #undef QT_NO_CAST_FROM_ASCII
0026 
0027 #include <config.h>
0028 #include "filelistingtest.h"
0029 
0030 #include "../translators/filelistingimporter.h"
0031 #include "../translators/xmphandler.h"
0032 #include "../images/imagefactory.h"
0033 #include "../core/netaccess.h"
0034 
0035 #include <KLocalizedString>
0036 
0037 #include <QTest>
0038 #include <QStandardPaths>
0039 
0040 // KIO::listDir in FileListingImporter seems to require a GUI Application
0041 QTEST_MAIN( FileListingTest )
0042 
0043 void FileListingTest::initTestCase() {
0044   QStandardPaths::setTestModeEnabled(true);
0045   KLocalizedString::setApplicationDomain("tellico");
0046   Tellico::ImageFactory::init();
0047 }
0048 
0049 void FileListingTest::testCpp() {
0050   QUrl url = QUrl::fromLocalFile(QFINDTESTDATA("filelistingtest.cpp"));
0051   Tellico::Import::FileListingImporter importer(url.adjusted(QUrl::RemoveFilename));
0052   QVERIFY(importer.canImport(Tellico::Data::Collection::File));
0053   // can't import images for local test
0054 //  importer.setOptions(importer.options() & ~Tellico::Import::ImportShowImageErrors);
0055   importer.setUseFilePreview(true);
0056   Tellico::Data::CollPtr coll = importer.collection();
0057 
0058   QVERIFY(coll);
0059   QCOMPARE(coll->type(), Tellico::Data::Collection::File);
0060   QVERIFY(coll->entryCount() > 0);
0061 
0062   Tellico::Data::EntryPtr entry;
0063   foreach(Tellico::Data::EntryPtr tmpEntry, coll->entries()) {
0064     if(tmpEntry->field(QStringLiteral("title")) == QStringLiteral("filelistingtest.cpp")) {
0065       entry = tmpEntry;
0066     }
0067   }
0068   QVERIFY(entry);
0069   QCOMPARE(entry->field("title"), QStringLiteral("filelistingtest.cpp"));
0070   QCOMPARE(entry->field("url"), url.url());
0071   QVERIFY(entry->field("description").contains("C++"));
0072   QCOMPARE(entry->field("folder"), QString()); // empty relative folder location
0073   QCOMPARE(entry->field("mimetype"), QStringLiteral("text/x-c++src"));
0074   QVERIFY(!entry->field("size").isEmpty());
0075   QVERIFY(!entry->field("permissions").isEmpty());
0076   QVERIFY(!entry->field("owner").isEmpty());
0077   QVERIFY(!entry->field("group").isEmpty());
0078   // for some reason, the Creation time isn't populated for this test
0079 //  QVERIFY(!entry->field("created").isEmpty());
0080   QVERIFY(!entry->field("modified").isEmpty());
0081   QCOMPARE(entry->field("metainfo"), QString());
0082   QVERIFY(!entry->field("icon").isEmpty());
0083 }
0084 
0085 void FileListingTest::testXMPData() {
0086   {
0087     Tellico::XMPHandler xmp;
0088 #ifdef HAVE_EXEMPI
0089     QVERIFY(xmp.isXMPEnabled());
0090     QVERIFY(!xmp.extractXMP(QFINDTESTDATA("data/BlueSquare.jpg")).isEmpty());
0091 #endif
0092   }
0093   // initializing exempi can cause a crash in Exiv for files with XMP data
0094   // see https://bugs.kde.org/show_bug.cgi?id=390744
0095   QUrl url = QUrl::fromLocalFile(QFINDTESTDATA("data/BlueSquare.jpg"));
0096   Tellico::Import::FileListingImporter importer(url.adjusted(QUrl::RemoveFilename));
0097   Tellico::Data::CollPtr coll = importer.collection();
0098 
0099   QVERIFY(coll);
0100   QCOMPARE(coll->type(), Tellico::Data::Collection::File);
0101   QVERIFY(coll->entryCount() > 0);
0102 
0103   Tellico::Data::EntryPtr entry, oggEntry;
0104   foreach(Tellico::Data::EntryPtr tmpEntry, coll->entries()) {
0105     if(tmpEntry->field(QStringLiteral("title")) == QStringLiteral("BlueSquare.jpg")) {
0106       entry = tmpEntry;
0107     } else if(tmpEntry->field(QStringLiteral("title")) == QStringLiteral("test.ogg")) {
0108       oggEntry = tmpEntry;
0109     }
0110   }
0111   QVERIFY(entry);
0112   QCOMPARE(entry->field("title"), QStringLiteral("BlueSquare.jpg"));
0113   QCOMPARE(entry->field("mimetype"), QStringLiteral("image/jpeg"));
0114   QCOMPARE(entry->field("size"), QStringLiteral("23.6 KiB"));
0115 #ifdef HAVE_KFILEMETADATA
0116 #ifdef HAVE_EXEMPI
0117   QEXPECT_FAIL("", "Because of a crash related to exempi and kfilemetadata linking, no metadata is read", Continue);
0118 #endif
0119   QVERIFY(!entry->field("metainfo").isEmpty());
0120 #endif
0121 
0122   QVERIFY(oggEntry);
0123   QCOMPARE(oggEntry->field("title"), QStringLiteral("test.ogg"));
0124   QCOMPARE(oggEntry->field("mimetype"), QStringLiteral("audio/x-vorbis+ogg"));
0125   QStringList meta = Tellico::FieldFormat::splitTable(oggEntry->field(QStringLiteral("metainfo")));
0126   QVERIFY(!meta.isEmpty());
0127   QVERIFY(meta.contains(QStringLiteral("Bitrate::159000")));
0128 }
0129 
0130 void FileListingTest::testStat() {
0131   QUrl local = QUrl::fromLocalFile(QFINDTESTDATA("filelistingtest.cpp"));
0132   QVERIFY(Tellico::NetAccess::exists(local, false, nullptr));
0133   QUrl remote(QStringLiteral("https://tellico-project.org"));
0134   // http doesn't support existence without downloading
0135   QVERIFY(!Tellico::NetAccess::exists(remote, false, nullptr));
0136 }