File indexing completed on 2024-05-12 05:10:04

0001 /***************************************************************************
0002     Copyright (C) 2010-2011 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 "musicbrainzfetchertest.h"
0028 
0029 #include "../fetch/musicbrainzfetcher.h"
0030 #include "../collections/musiccollection.h"
0031 #include "../collectionfactory.h"
0032 #include "../entry.h"
0033 #include "../images/imagefactory.h"
0034 #include "../utils/datafileregistry.h"
0035 
0036 #include <KSharedConfig>
0037 #include <KLocalizedString>
0038 
0039 #include <QTest>
0040 
0041 QTEST_GUILESS_MAIN( MusicBrainzFetcherTest )
0042 
0043 MusicBrainzFetcherTest::MusicBrainzFetcherTest() : AbstractFetcherTest() {
0044 }
0045 
0046 void MusicBrainzFetcherTest::initTestCase() {
0047   Tellico::RegisterCollection<Tellico::Data::MusicCollection> registerMusic(Tellico::Data::Collection::Album, "album");
0048   // since we use the importer
0049   Tellico::DataFileRegistry::self()->addDataLocation(QFINDTESTDATA("../../xslt/musicbrainz2tellico.xsl"));
0050   Tellico::ImageFactory::init();
0051 
0052   m_fieldValues.insert(QStringLiteral("title"), QStringLiteral("carried along"));
0053   m_fieldValues.insert(QStringLiteral("artist"), QStringLiteral("Andrew Peterson"));
0054   m_fieldValues.insert(QStringLiteral("label"), QStringLiteral("essential records"));
0055   m_fieldValues.insert(QStringLiteral("year"), QStringLiteral("2000"));
0056   m_fieldValues.insert(QStringLiteral("medium"), QStringLiteral("compact disc"));
0057 }
0058 
0059 void MusicBrainzFetcherTest::testTitle() {
0060   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::Title,
0061                                        m_fieldValues.value(QStringLiteral("title")));
0062   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::MusicBrainzFetcher(this));
0063   QVERIFY(fetcher->canSearch(request.key()));
0064 
0065   static_cast<Tellico::Fetch::MusicBrainzFetcher*>(fetcher.data())->setLimit(1);
0066   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0067 
0068   QCOMPARE(results.size(), 1);
0069 
0070   Tellico::Data::EntryPtr entry = results.at(0);
0071   QHashIterator<QString, QString> i(m_fieldValues);
0072   while(i.hasNext()) {
0073     i.next();
0074     QString result = entry->field(i.key()).toLower();
0075     QCOMPARE(result, i.value().toLower());
0076   }
0077   QVERIFY(!entry->field(QStringLiteral("track")).isEmpty());
0078   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0079   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0080 }
0081 
0082 void MusicBrainzFetcherTest::testKeyword() {
0083   // the total test case ends up exceeding the throttle limit so pause for a second
0084   QTest::qWait(1000);
0085 
0086   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::Keyword,
0087                                        m_fieldValues.value(QStringLiteral("title")));
0088   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::MusicBrainzFetcher(this));
0089 
0090   static_cast<Tellico::Fetch::MusicBrainzFetcher*>(fetcher.data())->setLimit(1);
0091   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0092 
0093   QCOMPARE(results.size(), 1);
0094 
0095   Tellico::Data::EntryPtr entry = results.at(0);
0096   QHashIterator<QString, QString> i(m_fieldValues);
0097   while(i.hasNext()) {
0098     i.next();
0099     QString result = entry->field(i.key()).toLower();
0100     QCOMPARE(result, i.value().toLower());
0101   }
0102   QVERIFY(!entry->field(QStringLiteral("track")).isEmpty());
0103   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0104   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0105 }
0106 
0107 void MusicBrainzFetcherTest::testBug426560() {
0108   // the total test case ends up exceeding the throttle limit so pause for a second
0109   QTest::qWait(1000);
0110 
0111   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::Keyword,
0112                                        QStringLiteral("lily allen - no shame"));
0113   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::MusicBrainzFetcher(this));
0114   QVERIFY(fetcher->canSearch(request.key()));
0115 
0116   static_cast<Tellico::Fetch::MusicBrainzFetcher*>(fetcher.data())->setLimit(1);
0117   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0118 
0119   QCOMPARE(results.size(), 1);
0120 
0121   Tellico::Data::EntryPtr entry = results.at(0);
0122   QVERIFY(entry);
0123   QCOMPARE(entry->title(), QStringLiteral("No Shame"));
0124   QCOMPARE(entry->field(QStringLiteral("artist")), QStringLiteral("Lily Allen"));
0125 }
0126 
0127 void MusicBrainzFetcherTest::testPerson() {
0128   const QString artist(QStringLiteral("artist"));
0129   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::Person,
0130                                        m_fieldValues.value(artist));
0131   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::MusicBrainzFetcher(this));
0132 
0133   // TODO: Fetcher::setLimit should be virtual in Fetcher class
0134   static_cast<Tellico::Fetch::MusicBrainzFetcher*>(fetcher.data())->setLimit(1);
0135   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0136 
0137   QVERIFY(results.size() > 0);
0138 
0139   Tellico::Data::EntryPtr entry = results.at(0);
0140   QVERIFY(entry);
0141   QCOMPARE(entry->field(artist), m_fieldValues.value(artist));
0142 }
0143 
0144 void MusicBrainzFetcherTest::testACDC() {
0145   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::Person,
0146                                        QStringLiteral("AC/DC"));
0147   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::MusicBrainzFetcher(this));
0148   QVERIFY(fetcher->canSearch(request.key()));
0149 
0150   // TODO: Fetcher::setLimit should be virtual in Fetcher class
0151   static_cast<Tellico::Fetch::MusicBrainzFetcher*>(fetcher.data())->setLimit(1);
0152   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0153 
0154   QVERIFY(results.size() > 0);
0155 
0156   Tellico::Data::EntryPtr entry = results.at(0);
0157   QVERIFY(entry);
0158   QCOMPARE(entry->field(QStringLiteral("artist")), QStringLiteral("AC/DC"));
0159 }
0160 
0161 // test grabbing cover art from coverartarchive.org
0162 void MusicBrainzFetcherTest::testCoverArt() {
0163   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::Title,
0164                                        QStringLiteral("Laulut ja tarinat"));
0165   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::MusicBrainzFetcher(this));
0166 
0167   static_cast<Tellico::Fetch::MusicBrainzFetcher*>(fetcher.data())->setLimit(1);
0168   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0169 
0170   QVERIFY(!results.isEmpty());
0171 
0172   Tellico::Data::EntryPtr entry = results.at(0);
0173   QCOMPARE(entry->title(), QStringLiteral("Laulut Ja Tarinat"));
0174   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0175   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0176 }
0177 
0178 void MusicBrainzFetcherTest::testSoundtrack() {
0179   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::Title,
0180                                        QStringLiteral("legend of bagger vance"));
0181   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::MusicBrainzFetcher(this));
0182   QVERIFY(fetcher->canSearch(request.key()));
0183 
0184   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0185 
0186   QVERIFY(!results.isEmpty());
0187 
0188   Tellico::Data::EntryPtr entry = results.at(0);
0189   QCOMPARE(entry->title(), QStringLiteral("Legend of Bagger Vance, The"));
0190   // sound tracks are the only genre tag that is read
0191   QCOMPARE(entry->field(QStringLiteral("genre")), QStringLiteral("Soundtrack"));
0192 }
0193 
0194 void MusicBrainzFetcherTest::testBarcode() {
0195   KConfigGroup cg = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig)->group(QStringLiteral("musicbrainz"));
0196   cg.writeEntry("Custom Fields", QStringLiteral("barcode"));
0197 
0198   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::UPC,
0199                                        QStringLiteral("8024391054123"));
0200   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::MusicBrainzFetcher(this));
0201   fetcher->readConfig(cg);
0202   QVERIFY(fetcher->canSearch(request.key()));
0203 
0204   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0205 
0206   QVERIFY(!results.isEmpty());
0207 
0208   Tellico::Data::EntryPtr entry = results.at(0);
0209   QCOMPARE(entry->title(), QStringLiteral("Old Man and the Spirit, The"));
0210   QCOMPARE(entry->field(QStringLiteral("barcode")), QStringLiteral("8024391054123"));
0211 }
0212 
0213 // bug 479503, https://bugs.kde.org/show_bug.cgi?id=479503
0214 void MusicBrainzFetcherTest::testMultiDisc() {
0215   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::UPC,
0216                                        QStringLiteral("4988031446843"));
0217   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::MusicBrainzFetcher(this));
0218 
0219   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0220 
0221   QVERIFY(!results.isEmpty());
0222 
0223   Tellico::Data::EntryPtr entry = results.at(0);
0224   QCOMPARE(entry->title(), QStringLiteral("Silver Lining Suite"));
0225   auto trackField = entry->collection()->fieldByName(QStringLiteral("track"));
0226   QVERIFY(trackField);
0227   // verify the title was updated to include the disc number
0228   QVERIFY(trackField->title() != i18n("Tracks"));
0229   QStringList tracks1 = Tellico::FieldFormat::splitTable(entry->field(QStringLiteral("track")));
0230   QCOMPARE(tracks1.count(), 9);
0231   // new field with Disc 2 tracks
0232   QStringList tracks2 = Tellico::FieldFormat::splitTable(entry->field(QStringLiteral("track2")));
0233   QCOMPARE(tracks2.count(), 8);
0234   QCOMPARE(tracks2.first(), QStringLiteral("Somewhere::上原ひろみ::7:31"));
0235 }