1 /***************************************************************************
2  *   Copyright (c) 2009 Sven Krohlas <sven@asbest-online.de>               *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18  ***************************************************************************/
19 
20 #include "TestPLSPlaylist.h"
21 
22 #include "core/support/Components.h"
23 #include "config-amarok-test.h"
24 #include "core-impl/collections/support/CollectionManager.h"
25 #include "core-impl/playlists/types/file/pls/PLSPlaylist.h"
26 #include "EngineController.h"
27 
28 #include <QFile>
29 #include <QDir>
30 #include <QStandardPaths>
31 #include <QTemporaryDir>
32 #include <QTest>
33 
34 #include <ThreadWeaver/Queue>
35 
QTEST_GUILESS_MAIN(TestPLSPlaylist)36 QTEST_GUILESS_MAIN( TestPLSPlaylist )
37 
38 TestPLSPlaylist::TestPLSPlaylist()
39 {}
40 
41 QString
dataPath(const QString & relPath)42 TestPLSPlaylist::dataPath( const QString &relPath )
43 {
44     return QDir::toNativeSeparators( QString( AMAROK_TEST_DIR ) + '/' + relPath );
45 }
46 
initTestCase()47 void TestPLSPlaylist::initTestCase()
48 {
49     m_tempDir = new QTemporaryDir;
50     QVERIFY( m_tempDir->isValid() );
51 
52     // EngineController is used in a connection in MetaProxy::Track; avoid null sender
53     // warning
54     EngineController *controller = new EngineController();
55     Amarok::Components::setEngineController( controller );
56 
57     qRegisterMetaType<Meta::TrackPtr>( "Meta::TrackPtr" );
58 
59     /* Collection manager needs to be instantiated in the main thread, but
60      * MetaProxy::Tracks used by playlist may trigger its creation in a different thread.
61      * Pre-create it explicitly */
62     CollectionManager::instance();
63 
64     const QString testPls = "data/playlists/test.pls";
65     const QUrl url = QUrl::fromLocalFile( dataPath(testPls) );
66     QFile playlistFile1( url.toLocalFile() );
67     QTextStream playlistStream;
68 
69     QString tempPath = m_tempDir->path() + "/test.pls";
70     QVERIFY( QFile::copy( url.toLocalFile(), tempPath ) );
71     QVERIFY( QFile::exists( tempPath ) );
72 
73     QVERIFY( playlistFile1.open( QFile::ReadOnly ) );
74     playlistStream.setDevice( &playlistFile1 );
75     QVERIFY( playlistStream.device() );
76 
77     m_testPlaylist1 = new Playlists::PLSPlaylist( QUrl::fromLocalFile(tempPath) );
78     QVERIFY( m_testPlaylist1 );
79     QVERIFY( m_testPlaylist1->load( playlistStream ) );
80     QCOMPARE( m_testPlaylist1->tracks().size(), 4 );
81     playlistFile1.close();
82 }
83 
cleanupTestCase()84 void TestPLSPlaylist::cleanupTestCase()
85 {
86     // Wait for other jobs, like MetaProxys fetching meta data, to finish
87     ThreadWeaver::Queue::instance()->finish();
88 
89     delete m_testPlaylist1;
90     delete m_tempDir;
91     delete Amarok::Components::setEngineController( 0 );
92 }
93 
testSetAndGetName()94 void TestPLSPlaylist::testSetAndGetName()
95 {
96     QCOMPARE( m_testPlaylist1->name(), QString( "test.pls" ) );
97 
98     m_testPlaylist1->setName( "set name test" );
99     QCOMPARE( m_testPlaylist1->name(), QString( "set name test.pls" ) );
100 
101     m_testPlaylist1->setName( "set name test aäoöuüß" );
102     QCOMPARE( m_testPlaylist1->name(), QString( "set name test aäoöuüß.pls" ) );
103 
104     m_testPlaylist1->setName( "test" );
105     m_testPlaylist1->setName( "" );
106     QCOMPARE( m_testPlaylist1->name(), QString( "test.pls" ) );
107 }
108 
testPrettyName()109 void TestPLSPlaylist::testPrettyName()
110 {
111     QCOMPARE( m_testPlaylist1->prettyName(), QString( "test.pls" ) );
112 }
113 
testTracks()114 void TestPLSPlaylist::testTracks()
115 {
116     Meta::TrackList tracklist = m_testPlaylist1->tracks();
117 
118     QCOMPARE( tracklist.at( 0 )->name(), QString( "::darkerradio:: - DIE Alternative im Netz ::www.darkerradio.de:: Tune In, Turn On, Burn Out!" ) );
119     QCOMPARE( tracklist.at( 1 )->name(), QString( "::darkerradio:: - DIE Alternative im Netz ::www.darkerradio.de:: Tune In, Turn On, Burn Out!" ) );
120     QCOMPARE( tracklist.at( 2 )->name(), QString( "::darkerradio:: - DIE Alternative im Netz ::www.darkerradio.de:: Tune In, Turn On, Burn Out!" ) );
121     QCOMPARE( tracklist.at( 3 )->name(), QString( "::darkerradio:: - DIE Alternative im Netz ::www.darkerradio.de:: Tune In, Turn On, Burn Out!" ) );
122 }
123 
testUidUrl()124 void TestPLSPlaylist::testUidUrl()
125 {
126     QString tempPath = m_tempDir->path() + "/test.pls";
127     m_testPlaylist1->setName( "test" );
128     QCOMPARE( m_testPlaylist1->uidUrl().toLocalFile(), tempPath );
129 }
testIsWritable()130 void TestPLSPlaylist::testIsWritable()
131 {
132     QVERIFY( m_testPlaylist1->isWritable() );
133 }
134 
testSave()135 void TestPLSPlaylist::testSave()
136 {
137     QVERIFY( m_testPlaylist1->save( false ) );
138 }
139