File indexing completed on 2024-04-28 04:48:12

0001 /****************************************************************************************
0002  * Copyright (c) 2013 Konrad Zemek <konrad.zemek@gmail.com>                             *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "TestAmarokImporter.h"
0018 
0019 #include "MetaValues.h"
0020 #include "importers/amarok/AmarokConfigWidget.h"
0021 #include "importers/amarok/AmarokManager.h"
0022 #include "importers/amarok/AmarokProvider.h"
0023 
0024 #include <QProcess>
0025 #include <QTest>
0026 
0027 
0028 Q_DECLARE_METATYPE( QProcess::ProcessError )
0029 QTEST_MAIN( TestAmarokImporter )
0030 
0031 using namespace StatSyncing;
0032 
0033 ProviderPtr
0034 TestAmarokImporter::getProvider()
0035 {
0036     QVariantMap cfg = AmarokConfigWidget( QVariantMap() ).config();
0037     cfg.insert( "name", "Amarok2Test" );
0038     cfg.insert( "embedded", true );
0039     cfg.insert( "dbPath", QCoreApplication::applicationDirPath() +
0040                           "/importers_files/amarok2_mysqle" );
0041 
0042     return ProviderPtr( new AmarokProvider( cfg, nullptr ) );
0043 }
0044 
0045 ProviderPtr
0046 TestAmarokImporter::getWritableProvider()
0047 {
0048     QDir base( QCoreApplication::applicationDirPath() );
0049     QDir files( base.filePath( "importers_files" ) );
0050     QDir tmp( base.filePath( "importers_tmp" ) );
0051 
0052     foreach( const QString &subdir,
0053              QList<QString>() << "amarok2_mysqle" << "amarok2_mysqle/amarok" )
0054     {
0055         tmp.mkpath( subdir );
0056 
0057         QDir src( files.filePath( subdir ) );
0058         QDir dst( tmp.filePath( subdir ) );
0059 
0060         foreach( const QString &filename, src.entryList( QStringList(), QDir::Files ) )
0061         {
0062             QFile( dst.filePath( filename ) ).remove();
0063             QFile( src.filePath( filename ) ).copy( dst.filePath( filename ) );
0064         }
0065     }
0066 
0067     QVariantMap cfg = AmarokConfigWidget( QVariantMap() ).config();
0068     cfg.insert( "name", "Amarok2Test" );
0069     cfg.insert( "embedded", true );
0070     cfg.insert( "dbPath", tmp.filePath( "amarok2_mysqle" ) );
0071 
0072     return ProviderPtr( new AmarokProvider( cfg, nullptr ) );
0073 }
0074 
0075 qint64
0076 TestAmarokImporter::reliableStatistics() const
0077 {
0078     return Meta::valFirstPlayed | Meta::valLastPlayed | Meta::valRating
0079             | Meta::valPlaycount | Meta::valLabel;
0080 }
0081 
0082 void
0083 TestAmarokImporter::initTestCase()
0084 {
0085     qRegisterMetaType<QProcess::ProcessError>();
0086 }
0087 
0088 void
0089 TestAmarokImporter::init()
0090 {
0091     m_cfg = AmarokConfigWidget( QVariantMap() ).config();
0092     m_cfg.insert( "embedded", true );
0093 }
0094 
0095 void
0096 TestAmarokImporter::configWidgetShouldOnlyShowFieldsRelevantToConnection()
0097 {
0098     AmarokConfigWidget widget( m_cfg );
0099 
0100     const QList<QWidget*> remoteConfigWidgets = QList<QWidget*>()
0101             << widget.m_databaseName << widget.m_hostname << widget.m_port
0102             << widget.m_password << widget.m_username;
0103 
0104     widget.m_connectionType->setCurrentIndex( AmarokConfigWidget::Embedded );
0105     QVERIFY( !widget.m_databaseLocation->isHidden() );
0106     foreach( QWidget *w, remoteConfigWidgets )
0107         QVERIFY( w->isHidden() );
0108 
0109     widget.m_connectionType->setCurrentIndex( AmarokConfigWidget::External );
0110     QVERIFY( widget.m_databaseLocation->isHidden() );
0111     foreach( QWidget *w, remoteConfigWidgets )
0112         QVERIFY( !w->isHidden() );
0113 }
0114 
0115 void
0116 TestAmarokImporter::configWidgetShouldNotSetDriver()
0117 {
0118     AmarokConfigWidget widget( m_cfg );
0119 
0120     widget.m_connectionType->setCurrentIndex( AmarokConfigWidget::Embedded );
0121     QVERIFY( !widget.config().contains( "dbDriver" ) );
0122 
0123     widget.m_connectionType->setCurrentIndex( AmarokConfigWidget::External );
0124     QVERIFY( !widget.config().contains( "dbDriver" ) );
0125 }
0126 
0127 void
0128 TestAmarokImporter::configWidgetShouldShowExternalAsDefault()
0129 {
0130     QVariantMap cfg;
0131     AmarokConfigWidget widget( cfg );
0132     QCOMPARE( widget.m_connectionType->currentIndex(),
0133               static_cast<int>( AmarokConfigWidget::External ) );
0134 }
0135 
0136 void
0137 TestAmarokImporter::configWidgetShouldNotBreakOnNonsenseInitialValues()
0138 {
0139     m_cfg.insert( "dbName", QColor( Qt::red ) );
0140     m_cfg.insert( "dbPort", "nonsensePort" );
0141     m_cfg.insert( "dbPath", reinterpret_cast<qptrdiff>( this ) );
0142 
0143     AmarokConfigWidget widget( m_cfg );
0144 
0145     QVERIFY( !widget.m_databaseName->text().isEmpty() );
0146     QVERIFY( !widget.m_databaseLocation->text().isEmpty() );
0147 }
0148 
0149 void
0150 TestAmarokImporter::configWidgetShouldReadSavedConfig()
0151 {
0152     m_cfg.insert( "embedded", true );
0153     m_cfg.insert( "dbName", "MyName" );
0154     m_cfg.insert( "dbPort", 19 );
0155     m_cfg.insert( "name", "theName" );
0156     AmarokConfigWidget widget( m_cfg );
0157 
0158     QCOMPARE( widget.m_connectionType->currentIndex(),
0159               static_cast<int>( AmarokConfigWidget::Embedded ) );
0160 
0161     QCOMPARE( widget.m_databaseName->text(), QString( "MyName" ) );
0162     QCOMPARE( widget.m_port->value(), 19 );
0163     QCOMPARE( widget.m_targetName->text(), QString( "theName" ) );
0164 
0165     m_cfg.insert( "embedded", false );
0166     AmarokConfigWidget widgetExternal( m_cfg );
0167 
0168     QCOMPARE( widgetExternal.m_connectionType->currentIndex(),
0169               static_cast<int>( AmarokConfigWidget::External ) );
0170 }
0171 
0172 void
0173 TestAmarokImporter::providerShouldIgnoreConfigsDbDriver()
0174 {
0175     if( !QFileInfo( "/usr/bin/mysqld" ).isExecutable() )
0176         QSKIP( "/usr/bin/mysqld is not executable", SkipAll );
0177 
0178     m_cfg.insert( "dbDriver", "QPSQL" );
0179     m_cfg.insert( "dbPath", QCoreApplication::applicationDirPath() +
0180                             "/importers_files/amarok2_mysqle" );
0181 
0182     AmarokProvider provider( m_cfg, nullptr );
0183 
0184     // The database isn't accessible by QPSQL driver, but it still should work
0185     QVERIFY( !provider.artists().empty() );
0186 }
0187 
0188 void
0189 TestAmarokImporter::providerShouldHandleNonexistentDbDir()
0190 {
0191     m_cfg.insert( "dbPath", "/Im/sure/this/wont/exist" );
0192 
0193     AmarokProvider provider( m_cfg, nullptr );
0194     QVERIFY( provider.artists().isEmpty() );
0195 }
0196 
0197 void
0198 TestAmarokImporter::providerShouldHandleInvalidDbDir()
0199 {
0200     m_cfg.insert( "dbPath", QApplication::applicationDirPath() );
0201 
0202     AmarokProvider provider( m_cfg, nullptr );
0203     QVERIFY( provider.artists().isEmpty() );
0204 }
0205 
0206 void
0207 TestAmarokImporter::providerShouldHandleExternalConnectionError()
0208 {
0209     m_cfg.insert( "dbHost", "I hope this isn't a valid hostname" );
0210 
0211     AmarokProvider provider( m_cfg, nullptr );
0212     QVERIFY( provider.artists().isEmpty() );
0213 }
0214 
0215 void
0216 TestAmarokImporter::providerShouldHandleErroneousConfigValues()
0217 {
0218     m_cfg.insert( "dbDriver", 19 );
0219     m_cfg.insert( "dbName", QColor( Qt::red ) );
0220     m_cfg.insert( "dbPort", "nonsensePort" );
0221 
0222     AmarokProvider provider( m_cfg, nullptr );
0223     QVERIFY( provider.artists().isEmpty() );
0224 }
0225