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 "TestM3UPlaylist.h"
21 #include "config-amarok-test.h"
22 #include "core-impl/collections/support/CollectionManager.h"
23 #include "core-impl/playlists/types/file/m3u/M3UPlaylist.h"
24 
25 #include <QDir>
26 #include <QFile>
27 #include <QStandardPaths>
28 #include <QTemporaryDir>
29 #include <QTest>
30 
31 #include <ThreadWeaver/ThreadWeaver>
32 
33 
QTEST_GUILESS_MAIN(TestM3UPlaylist)34 QTEST_GUILESS_MAIN( TestM3UPlaylist )
35 
36 TestM3UPlaylist::TestM3UPlaylist()
37 {
38 }
39 
40 QString
dataPath(const QString & relPath)41 TestM3UPlaylist::dataPath( const QString &relPath )
42 {
43     return QDir::toNativeSeparators( QString( AMAROK_TEST_DIR ) + '/' + relPath );
44 }
45 
initTestCase()46 void TestM3UPlaylist::initTestCase()
47 {
48     m_tempDir = new QTemporaryDir;
49     QVERIFY( m_tempDir->isValid() );
50 
51     qRegisterMetaType<Meta::TrackPtr>( "Meta::TrackPtr" );
52 
53     /* Collection manager needs to be instantiated in the main thread, but
54      * MetaProxy::Tracks used by playlist may trigger its creation in a different thread.
55      * Pre-create it explicitly */
56     CollectionManager::instance();
57 
58     const QUrl url = QUrl::fromLocalFile(dataPath( "data/playlists/test.m3u" ));
59     QFile playlistFile1( url.toLocalFile() );
60     QTextStream playlistStream;
61 
62     QString tempPath = m_tempDir->path() + "/test.m3u";
63     QVERIFY( QFile::copy( url.toLocalFile(), tempPath ) );
64     QVERIFY( QFile::exists( tempPath ) );
65 
66     QVERIFY( playlistFile1.open( QFile::ReadOnly ) );
67     playlistStream.setDevice( &playlistFile1 );
68     QVERIFY( playlistStream.device() );
69 
70     m_testPlaylist = new Playlists::M3UPlaylist( QUrl::fromLocalFile(tempPath) );
71     QVERIFY( m_testPlaylist );
72     QVERIFY( m_testPlaylist->load( playlistStream ) );
73     QCOMPARE( m_testPlaylist->tracks().size(), 10 );
74     playlistFile1.close();
75 }
76 
cleanupTestCase()77 void TestM3UPlaylist::cleanupTestCase()
78 {
79     // Wait for other jobs, like MetaProxys fetching meta data, to finish
80     ThreadWeaver::Queue::instance()->finish();
81 
82     delete m_testPlaylist;
83     delete m_tempDir;
84 }
85 
testSetAndGetName()86 void TestM3UPlaylist::testSetAndGetName()
87 {
88     QCOMPARE( m_testPlaylist->prettyName(), QString( "test.m3u" ) );
89 
90     QCOMPARE( m_testPlaylist->name(), QString( "test.m3u" ) );
91 
92     m_testPlaylist->setName( "set name test" );
93     QCOMPARE( m_testPlaylist->name(), QString( "set name test.m3u" ) );
94 
95     m_testPlaylist->setName( "set name test aäoöuüß.m3u" );
96     QCOMPARE( m_testPlaylist->name(), QString( "set name test aäoöuüß.m3u" ) );
97 
98     m_testPlaylist->setName( "test" );
99     m_testPlaylist->setName( "" );
100     QCOMPARE( m_testPlaylist->name(), QString( "test.m3u" ) );
101 }
102 
testTracks()103 void TestM3UPlaylist::testTracks()
104 {
105     Meta::TrackList tracklist = m_testPlaylist->tracks();
106 
107     QCOMPARE( tracklist.size(), 10 );
108     QCOMPARE( tracklist.at( 0 )->name(), QString( "Platz 01" ) );
109     QCOMPARE( tracklist.at( 1 )->name(), QString( "Platz 02" ) );
110     QCOMPARE( tracklist.at( 2 )->name(), QString( "Platz 03" ) );
111     QCOMPARE( tracklist.at( 9 )->name(), QString( "Platz 10" ) );
112 }
113 
testUidUrl()114 void TestM3UPlaylist::testUidUrl()
115 {
116     QString tempPath = m_tempDir->path() + "/test.m3u";
117     //we have changed the name around so much, better reset it
118     m_testPlaylist->setName( "test" );
119     QCOMPARE( m_testPlaylist->uidUrl().toLocalFile(), tempPath );
120 }
121 
testSetAndGetGroups()122 void TestM3UPlaylist::testSetAndGetGroups()
123 {
124     QStringList grouplist;
125     QStringList newGrouplist;
126 
127     grouplist = m_testPlaylist->groups();
128     QCOMPARE( grouplist.size(), 0 );
129 
130     newGrouplist.append( "test" );
131     m_testPlaylist->setGroups( newGrouplist );
132     grouplist = m_testPlaylist->groups();
133     QCOMPARE( grouplist.size(), 1 );
134     QCOMPARE( grouplist.at(0), QString( "test" ) );
135 }
136 
testIsWritable()137 void TestM3UPlaylist::testIsWritable()
138 {
139     QVERIFY( m_testPlaylist->isWritable() );
140 }
141 
testSave()142 void TestM3UPlaylist::testSave()
143 {
144     QVERIFY( m_testPlaylist->save( false ) );
145 }
146