File indexing completed on 2024-05-12 04:49:42

0001 /****************************************************************************************
0002  * Copyright (c) 2012 Tatjana Gornak <t.gornak@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 "TestPlaylistObserver.h"
0018 
0019 #include "EngineController.h"
0020 #include "config-amarok-test.h"
0021 #include "core/support/Components.h"
0022 #include "core-impl/collections/support/CollectionManager.h"
0023 #include "core-impl/playlists/types/file/xspf/XSPFPlaylist.h"
0024 
0025 #include <QDir>
0026 #include <QEventLoop>
0027 #include <QFile>
0028 #include <QSignalSpy>
0029 #include <QTemporaryFile>
0030 #include <QTest>
0031 #include <QTimer>
0032 
0033 #include <ThreadWeaver/Queue>
0034 
0035 QTEST_GUILESS_MAIN( TestPlaylistObserver )
0036 
0037 TestPlaylistObserver::TestPlaylistObserver()
0038     : m_observer( nullptr )
0039 {
0040 }
0041 
0042 QString
0043 TestPlaylistObserver::dataPath( const QString &relPath )
0044 {
0045     return QDir::toNativeSeparators( QString( AMAROK_TEST_DIR ) + "/data/playlists/" + relPath );
0046 }
0047 
0048 void
0049 TestPlaylistObserver::initTestCase()
0050 {
0051     EngineController *controller = new EngineController();
0052     Amarok::Components::setEngineController( controller );
0053     CollectionManager::instance();
0054 
0055     qRegisterMetaType<Meta::TrackPtr>( "Meta::TrackPtr" );
0056 }
0057 
0058 void
0059 TestPlaylistObserver::cleanupTestCase()
0060 {
0061     // Wait for other jobs, like MetaProxys fetching meta data, to finish
0062     ThreadWeaver::Queue::instance()->finish();
0063 
0064     delete Amarok::Components::setEngineController( nullptr );
0065 }
0066 
0067 void
0068 TestPlaylistObserver::init()
0069 {
0070     const QString testXspf = "test.xspf";
0071     const QUrl url = QUrl::fromLocalFile(dataPath( testXspf ));
0072 
0073     m_testPlaylist = new Playlists::XSPFPlaylist( url );
0074 
0075     // test that behaviour before loading is correct
0076     QCOMPARE( m_testPlaylist->name(), QString( "test.xspf" ) );
0077     QCOMPARE( m_testPlaylist->trackCount(), -1 );
0078 
0079     m_observer = new Observer();
0080     m_observer->subscribeTo( m_testPlaylist );
0081 }
0082 
0083 void
0084 TestPlaylistObserver::cleanup()
0085 {
0086     delete m_observer;
0087     m_observer = nullptr;
0088     m_testPlaylist = nullptr;
0089 }
0090 
0091 void
0092 TestPlaylistObserver::testMetadataChanged( )
0093 {
0094     QSKIP( "Functionality this test tests has not yet been implemented", SkipAll );
0095     QSignalSpy spy( m_observer, &Observer::metadataChangedSignal );
0096     m_testPlaylist->triggerTrackLoad();
0097     QSignalSpy spyTracksLoaded(m_observer, &Observer::tracksLoadedSignal);
0098     QVERIFY( spyTracksLoaded.wait( 10000 ) );
0099 
0100     QVERIFY( spy.count() > 0 );
0101     // changed methadata means that we should get new name
0102     QCOMPARE( m_testPlaylist->name(), QString( "my playlist" ) );
0103 }
0104 
0105 void
0106 TestPlaylistObserver::testTracksLoaded()
0107 {
0108     m_testPlaylist->triggerTrackLoad();
0109     QSignalSpy spyTracksLoaded(m_observer, &Observer::tracksLoadedSignal);
0110     QVERIFY( spyTracksLoaded.wait( 10000 ) );
0111 
0112     QCOMPARE( m_testPlaylist->trackCount(), 23 );
0113 }
0114 
0115 void
0116 TestPlaylistObserver::testTrackAdded( )
0117 {
0118     QSignalSpy spy( m_observer, &Observer::trackAddedSignal );
0119     m_testPlaylist->triggerTrackLoad();
0120     QSignalSpy spyTracksLoaded(m_observer, &Observer::tracksLoadedSignal);
0121     QVERIFY( spyTracksLoaded.wait( 10000 ) );
0122     QCOMPARE( spy.count(), 23 );
0123 }
0124 
0125 void
0126 TestPlaylistObserver::testTrackRemoved()
0127 {
0128     m_testPlaylist->triggerTrackLoad();
0129     QSignalSpy spyTracksLoaded( m_observer, &Observer::tracksLoadedSignal );
0130     QVERIFY( spyTracksLoaded.wait( 10000 ) );
0131 
0132     QString newName = "test playlist written to.xspf";
0133     m_testPlaylist->setName( newName ); // don't overwrite original playlist
0134     QSignalSpy spyTrackRemoved( m_observer, &Observer::trackRemovedSignal );
0135     QCOMPARE( m_testPlaylist->trackCount(), 23 );
0136     m_testPlaylist->removeTrack( -1 ); // no effect
0137     m_testPlaylist->removeTrack( 0 ); // has effect
0138     m_testPlaylist->removeTrack( 22 ); // no effect, too far
0139     m_testPlaylist->removeTrack( 21 ); // has effect
0140     QCOMPARE( m_testPlaylist->trackCount(), 21 );
0141     QCOMPARE( spyTrackRemoved.count(), 2 );
0142 
0143     qDebug() << dataPath( newName );
0144     QVERIFY( QFile::remove( dataPath( newName ) ) );
0145 }