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