File indexing completed on 2024-05-19 16:19:06

0001 /***************************************************************************
0002     Copyright (C) 2020 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 "audiofiletest.h"
0028 
0029 #include "../translators/audiofileimporter.h"
0030 #include "../collections/musiccollection.h"
0031 #include "../fieldformat.h"
0032 #include "../images/imagefactory.h"
0033 #include "../images/image.h"
0034 
0035 #include <QTest>
0036 #include <QStandardPaths>
0037 
0038 QTEST_GUILESS_MAIN( AudioFileTest )
0039 
0040 void AudioFileTest::initTestCase() {
0041   QStandardPaths::setTestModeEnabled(true);
0042   Tellico::ImageFactory::init();
0043 }
0044 
0045 void AudioFileTest::testDirectory() {
0046   QUrl url = QUrl::fromLocalFile(QFINDTESTDATA("data/test.ogg"));
0047   url = url.adjusted(QUrl::RemoveFilename);
0048   // url from chooser can be passed to the importer without the trailing slash, see bug 429803
0049   url = url.adjusted(QUrl::StripTrailingSlash);
0050   QVERIFY(!url.isEmpty());
0051   Tellico::Import::AudioFileImporter importer(url);
0052   importer.setOptions(importer.options() ^ Tellico::Import::ImportProgress);
0053   importer.setRecursive(true);
0054   importer.setAddFilePath(true);
0055   importer.setAddBitrate(true);
0056 
0057   QVERIFY(importer.canImport(Tellico::Data::Collection::Album));
0058   QVERIFY(!importer.canImport(Tellico::Data::Collection::Book));
0059 
0060   Tellico::Data::CollPtr coll = importer.collection();
0061   QVERIFY(coll);
0062   QCOMPARE(coll->type(), Tellico::Data::Collection::Album);
0063   QCOMPARE(coll->entryCount(), 2);
0064   QCOMPARE(coll->title(), QStringLiteral("My Music"));
0065 
0066   Tellico::Data::EntryPtr entry = coll->entryById(1);
0067   QVERIFY(entry);
0068   QCOMPARE(entry->field("title"), QStringLiteral("The Album"));
0069   QVERIFY(entry->field("file").contains(QStringLiteral("data/test.ogg")));
0070 
0071   entry = coll->entryById(2);
0072   QVERIFY(entry);
0073   QCOMPARE(entry->field("title"), QStringLiteral("mp3 album"));
0074   QVERIFY(entry->field("file").contains(QStringLiteral("data/audio/test.mp3")));
0075   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0076   const Tellico::Data::Image& img = Tellico::ImageFactory::imageById(entry->field(QStringLiteral("cover")));
0077   QVERIFY(!img.isNull());
0078 }
0079 
0080 void AudioFileTest::testOgg() {
0081   QUrl url = QUrl::fromLocalFile(QFINDTESTDATA("data/test.ogg"));
0082   QVERIFY(!url.isEmpty());
0083   Tellico::Import::AudioFileImporter importer(url);
0084   importer.setOptions(importer.options() ^ Tellico::Import::ImportProgress);
0085   importer.setAddFilePath(true);
0086   importer.setAddBitrate(true);
0087   Tellico::Data::CollPtr coll = importer.collection();
0088 
0089   QVERIFY(coll);
0090   QCOMPARE(coll->type(), Tellico::Data::Collection::Album);
0091   QCOMPARE(coll->entryCount(), 1);
0092 
0093   Tellico::Data::EntryPtr entry = coll->entryById(1);
0094   QVERIFY(entry);
0095   QCOMPARE(entry->field("title"), QStringLiteral("The Album"));
0096   QCOMPARE(entry->field("artist"), QStringLiteral("Album Artist"));
0097   QCOMPARE(entry->field("track"), QStringLiteral("Test OGG::The Artist::0:03"));
0098   QCOMPARE(entry->field("year"), QStringLiteral("2020"));
0099   QCOMPARE(entry->field("genre"), QStringLiteral("The Genre"));
0100   QCOMPARE(entry->field("label"), QStringLiteral("Label"));
0101   QVERIFY(entry->field("file").contains(QStringLiteral("::160"))); // bitrate
0102 }
0103 
0104 void AudioFileTest::testMp3() {
0105   QUrl url = QUrl::fromLocalFile(QFINDTESTDATA("data/audio/test.mp3"));
0106   QVERIFY(!url.isEmpty());
0107   Tellico::Import::AudioFileImporter importer(url);
0108   importer.setOptions(importer.options() ^ Tellico::Import::ImportProgress);
0109   Tellico::Data::CollPtr coll = importer.collection();
0110 
0111   QVERIFY(coll);
0112   QCOMPARE(coll->type(), Tellico::Data::Collection::Album);
0113   QCOMPARE(coll->entryCount(), 1);
0114 
0115   Tellico::Data::EntryPtr entry = coll->entryById(1);
0116   QVERIFY(entry);
0117   QCOMPARE(entry->field("title"), QStringLiteral("mp3 album"));
0118   QCOMPARE(entry->field("artist"), QStringLiteral("mp3 artist"));
0119   QCOMPARE(entry->field("track"), QStringLiteral("mp3 title::mp3 artist::0:02"));
0120   QCOMPARE(entry->field("year"), QStringLiteral("2020"));
0121   QCOMPARE(entry->field("genre"), QStringLiteral("mp3 genre"));
0122 }
0123 
0124 void AudioFileTest::testNonRecursive() {
0125   // we want the source directory, not the build directory, so look for a source file first
0126   QFileInfo fi(QFINDTESTDATA("data/test.ogg"));
0127   QUrl url = QUrl::fromLocalFile(fi.dir().absolutePath());
0128   QVERIFY(!url.isEmpty());
0129   Tellico::Import::AudioFileImporter importer(url);
0130   importer.setOptions(importer.options() ^ Tellico::Import::ImportProgress);
0131   importer.setRecursive(false);
0132   importer.setRecursive(true); // check that the bit flipping is correct
0133   importer.setRecursive(false);
0134   Tellico::Data::CollPtr coll = importer.collection();
0135 
0136   QVERIFY(coll);
0137   QCOMPARE(coll->type(), Tellico::Data::Collection::Album);
0138   QCOMPARE(coll->entryCount(), 1);
0139 }