File indexing completed on 2024-05-05 04:55:40

0001 /**
0002  * \file testserverimporterbase.cpp
0003  * Base class for server importer tests.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 07 Oct 2012
0008  *
0009  * Copyright (C) 2012-2018  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "testserverimporterbase.h"
0028 #include <QTest>
0029 #include <QNetworkAccessManager>
0030 #include <QTimer>
0031 #include "dummysettings.h"
0032 #include "kid3application.h"
0033 #include "iserverimporterfactory.h"
0034 #include "serverimporter.h"
0035 #include "trackdatamodel.h"
0036 #include "configstore.h"
0037 
0038 TestServerImporterBase::TestServerImporterBase(QObject* parent)
0039   : QObject(parent),
0040     m_netMgr(new QNetworkAccessManager(this)),
0041     m_trackDataModel(new TrackDataModel(nullptr, this)),
0042     m_importer(nullptr), m_settings(nullptr), m_configStore(nullptr)
0043 {
0044   if (!ConfigStore::instance()) {
0045     m_settings = new DummySettings;
0046     m_configStore = new ConfigStore(m_settings);
0047   }
0048 }
0049 
0050 TestServerImporterBase::~TestServerImporterBase()
0051 {
0052   delete m_configStore;
0053   delete m_settings;
0054 }
0055 
0056 void TestServerImporterBase::onFindFinished(const QByteArray& searchStr)
0057 {
0058   if (m_importer)
0059     m_importer->parseFindResults(searchStr);
0060   emit albumsUpdated();
0061 }
0062 
0063 void TestServerImporterBase::onAlbumFinished(const QByteArray& albumStr)
0064 {
0065   if (m_importer) {
0066     m_importer->setStandardTags(true);
0067     m_importer->setAdditionalTags(true);
0068     m_importer->parseAlbumResults(albumStr);
0069   }
0070   emit trackDataUpdated();
0071 }
0072 
0073 void TestServerImporterBase::setServerImporter(ServerImporter* importer)
0074 {
0075   if (importer != m_importer) {
0076     m_importer = importer;
0077     if (m_importer) {
0078       connect(m_importer, &ImportClient::findFinished,
0079               this, &TestServerImporterBase::onFindFinished);
0080       connect(m_importer, &ImportClient::albumFinished,
0081               this, &TestServerImporterBase::onAlbumFinished);
0082     }
0083   }
0084 }
0085 
0086 void TestServerImporterBase::setServerImporter(const QString& key)
0087 {
0088   ServerImporter* serverImporter = nullptr;
0089   QObjectList plugins = Kid3Application::loadPlugins();
0090   foreach (QObject* plugin, plugins) {
0091     if (IServerImporterFactory* importerFactory =
0092         qobject_cast<IServerImporterFactory*>(plugin)) {
0093       if (importerFactory->serverImporterKeys().contains(key)) {
0094         serverImporter = importerFactory->createServerImporter(
0095               key, m_netMgr, m_trackDataModel);
0096         break;
0097       }
0098     }
0099   }
0100   QVERIFY(serverImporter != nullptr);
0101   setServerImporter(serverImporter);
0102 }
0103 
0104 void TestServerImporterBase::queryAlbums(const QString& artist, const QString& album)
0105 {
0106   QEventLoop eventLoop;
0107   QTimer timer;
0108   timer.setSingleShot(true);
0109   connect(&timer, &QTimer::timeout, &eventLoop, &QEventLoop::quit);
0110   connect(this, &TestServerImporterBase::albumsUpdated,
0111           &eventLoop, &QEventLoop::quit);
0112   m_importer->find(m_importer->config(), artist, album);
0113   timer.start(5000);
0114   eventLoop.exec();
0115   QVERIFY(timer.isActive());
0116 }
0117 
0118 void TestServerImporterBase::queryTracks(const QString& cat, const QString& id)
0119 {
0120   QEventLoop eventLoop;
0121   QTimer timer;
0122   timer.setSingleShot(true);
0123   connect(&timer, &QTimer::timeout, &eventLoop, &QEventLoop::quit);
0124   connect(this, &TestServerImporterBase::trackDataUpdated,
0125           &eventLoop, &QEventLoop::quit);
0126   m_importer->getTrackList(m_importer->config(), cat, id);
0127   timer.start(5000);
0128   eventLoop.exec();
0129   QVERIFY(timer.isActive());
0130 }