File indexing completed on 2024-11-10 04:22:09

0001 /****************************************************************************************
0002  * Copyright (c) 2012 Jasneet Singh Bhatti <jazneetbhatti@gmail.com>                    *
0003  * This program is free software; you can redistribute it and/or modify it under        *
0004  * the terms of the GNU General Public License as published by the Free Software        *
0005  * Foundation; either version 2 of the License, or (at your option) any later           *
0006  * version.                                                                             *
0007  *                                                                                      *
0008  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0009  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0010  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0011  *                                                                                      *
0012  * You should have received a copy of the GNU General Public License along with         *
0013  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0014  ****************************************************************************************/
0015 
0016 #include "TestTrackForUrlWorker.h"
0017 
0018 #include "amarokconfig.h"
0019 #include "config-amarok-test.h"
0020 #include "core-impl/collections/support/CollectionManager.h"
0021 #include "mocks/MockTrackForUrlWorker.h"
0022 
0023 #include <QMetaType>
0024 #include <QSignalSpy>
0025 #include <QUrl>
0026 
0027 #include <ThreadWeaver/Job>
0028 #include <ThreadWeaver/Queue>
0029 
0030 QTEST_GUILESS_MAIN( TestTrackForUrlWorker )
0031 
0032 void
0033 TestTrackForUrlWorker::initTestCase()
0034 {
0035     // To make queued signals/slots work with custom payload
0036     qRegisterMetaType<Meta::TrackPtr>( "Meta::TrackPtr" );
0037     qRegisterMetaType<ThreadWeaver::Job*>( "ThreadWeaver::Job*" );
0038     AmarokConfig::instance("amarokrc");
0039 }
0040 
0041 QString
0042 TestTrackForUrlWorker::dataPath( const QString &relPath )
0043 {
0044     return QDir::toNativeSeparators( QString( AMAROK_TEST_DIR ) + '/' + relPath );
0045 }
0046 
0047 void
0048 TestTrackForUrlWorker::testCompleteJobQUrl_data()
0049 {
0050     testCompleteJobInternal_data();
0051 }
0052 
0053 void
0054 TestTrackForUrlWorker::testCompleteJobQUrl()
0055 {
0056     QUrl url;
0057 
0058     MockTrackForUrlWorker *trackForUrlWorker = new MockTrackForUrlWorker( url );
0059     QVERIFY( trackForUrlWorker );
0060 
0061     testCompleteJobInternal( trackForUrlWorker );
0062 }
0063 
0064 void TestTrackForUrlWorker::testCompleteJobQString_data()
0065 {
0066     testCompleteJobInternal_data();
0067 }
0068 
0069 void
0070 TestTrackForUrlWorker::testCompleteJobQString()
0071 {
0072     QString url;
0073 
0074     MockTrackForUrlWorker *trackForUrlWorker = new MockTrackForUrlWorker( url );
0075     QVERIFY( trackForUrlWorker );
0076 
0077     testCompleteJobInternal( trackForUrlWorker );
0078 }
0079 
0080 void
0081 TestTrackForUrlWorker::testCompleteJobInternal_data()
0082 {
0083     QTest::addColumn<Meta::TrackPtr>( "track" );
0084 
0085     QTest::newRow( "track 1" ) << CollectionManager::instance()->trackForUrl( QUrl::fromLocalFile(dataPath( "data/audio/album/Track01.ogg" )) );
0086     QTest::newRow( "track 2" ) << CollectionManager::instance()->trackForUrl( QUrl::fromLocalFile(dataPath( "data/audio/album/Track02.ogg" )) );
0087     QTest::newRow( "track 3" ) << CollectionManager::instance()->trackForUrl( QUrl::fromLocalFile(dataPath( "data/audio/album/Track03.ogg" )) );
0088 }
0089 
0090 void
0091 TestTrackForUrlWorker::testCompleteJobInternal( MockTrackForUrlWorker *trackForUrlWorker )
0092 {
0093     // Connect finishedLookup with setEmittedTrack() that will store the emitted track
0094     connect( trackForUrlWorker, &MockTrackForUrlWorker::finishedLookup,
0095              this, &TestTrackForUrlWorker::setEmittedTrack );
0096 
0097     QSignalSpy spyFinishedLookup( trackForUrlWorker, &MockTrackForUrlWorker::finishedLookup );
0098     QSignalSpy spyDone( trackForUrlWorker, &MockTrackForUrlWorker::done );
0099 
0100     // Enqueue the job for execution and verify that it emits done when finished, which triggers completeJob
0101     ThreadWeaver::Queue::instance()->enqueue( QSharedPointer<ThreadWeaver::Job>( trackForUrlWorker ) );
0102     bool receivedDone = spyDone.wait( 1000 );
0103     QVERIFY( receivedDone );
0104 
0105     // Verify that finishedLookup was emitted
0106     QCOMPARE( spyFinishedLookup.count(), 1 );
0107 
0108     // Verify that the track emitted with finishedLookup is indeed the track set by run()
0109     QFETCH( Meta::TrackPtr, track );
0110     QCOMPARE( m_emittedTrack, track );
0111 }
0112 
0113 void
0114 TestTrackForUrlWorker::setEmittedTrack( Meta::TrackPtr track )
0115 {
0116     m_emittedTrack = track;
0117 }
0118