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