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 "TestXSPFPlaylist.h"
21 
22 #include "config-amarok-test.h"
23 #include "EngineController.h"
24 #include "core/support/Components.h"
25 #include "core-impl/collections/support/CollectionManager.h"
26 #include "core-impl/playlists/types/file/xspf/XSPFPlaylist.h"
27 
28 #include <ThreadWeaver/Queue>
29 
30 #include <QDebug>
31 #include <QDir>
32 #include <QFile>
33 #include <QStandardPaths>
34 #include <QTest>
35 
36 
QTEST_GUILESS_MAIN(TestXSPFPlaylist)37 QTEST_GUILESS_MAIN( TestXSPFPlaylist )
38 
39 TestXSPFPlaylist::TestXSPFPlaylist()
40 {}
41 
42 QString
dataPath(const QString & relPath)43 TestXSPFPlaylist::dataPath( const QString &relPath )
44 {
45     return QDir::toNativeSeparators( QString( AMAROK_TEST_DIR ) + '/' + relPath );
46 }
47 
48 
initTestCase()49 void TestXSPFPlaylist::initTestCase()
50 {
51     // EngineController is used in a connection in MetaProxy::Track; avoid null sender
52     // warning
53     EngineController *controller = new EngineController();
54     Amarok::Components::setEngineController( controller );
55 
56     qRegisterMetaType<Meta::TrackPtr>( "Meta::TrackPtr" );
57 
58     /* Collection manager needs to be instantiated in the main thread, but
59      * MetaProxy::Tracks used by playlist may trigger its creation in a different thread.
60      * Pre-create it explicitly */
61     CollectionManager::instance();
62 
63     const QString testXspf = "data/playlists/test.xspf";
64     const QUrl url = QUrl::fromLocalFile(dataPath( testXspf ));
65     QFile playlistFile1( url.toLocalFile() );
66     QTextStream playlistStream1;
67 
68     QVERIFY( playlistFile1.open( QFile::ReadOnly ) );
69     playlistStream1.setDevice( &playlistFile1 );
70     QVERIFY( playlistStream1.device() );
71 
72     qDebug() << "got playlist path: " << url.url();
73 
74     //we need to copy this playlist file to a temp dir as some of the tests we do will delete/overwrite it
75     m_tempDir = new QTemporaryDir;;
76     QVERIFY( m_tempDir->isValid() );
77     QString tempPath = m_tempDir->path() + "/test.xspf";
78     qDebug() << "got temp path: " << tempPath;
79     QVERIFY( QFile::copy( url.toLocalFile(), tempPath ) );
80     QVERIFY( QFile::exists( tempPath ) );
81 
82     m_testPlaylist1 = new Playlists::XSPFPlaylist( QUrl::fromLocalFile(tempPath) );
83     QVERIFY( m_testPlaylist1 );
84     QVERIFY( m_testPlaylist1->load( playlistStream1 ) );
85 }
86 
cleanupTestCase()87 void TestXSPFPlaylist::cleanupTestCase()
88 {
89     QFile::remove( QStandardPaths::locate( QStandardPaths::TempLocation, "test.xspf" ) );
90     // Wait for other jobs, like MetaProxys fetching meta data, to finish
91     ThreadWeaver::Queue::instance()->finish();
92 
93     delete m_testPlaylist1;
94     delete m_tempDir;
95     delete Amarok::Components::setEngineController( 0 );
96 }
97 
testSetAndGetName()98 void TestXSPFPlaylist::testSetAndGetName()
99 {
100     QCOMPARE( m_testPlaylist1->name(), QString( "my playlist" ) );
101 
102     m_testPlaylist1->setName( "test" );
103     QCOMPARE( m_testPlaylist1->name(), QString( "test" ) );
104 
105     m_testPlaylist1->setName( "test aäoöuüß" );
106     QCOMPARE( m_testPlaylist1->name(), QString( "test aäoöuüß" ) );
107 
108     m_testPlaylist1->setName( "" );
109     QCOMPARE( m_testPlaylist1->name(), QString( "" ) );
110 }
111 
prettyName()112 void TestXSPFPlaylist::prettyName()
113 {
114     QCOMPARE( m_testPlaylist1->prettyName(), QString( "" ) );
115 }
116 
testSetAndGetTracks()117 void TestXSPFPlaylist::testSetAndGetTracks()
118 {
119     Meta::TrackList tracklist = m_testPlaylist1->tracks();
120 
121     QCOMPARE( tracklist.size(), 23 );
122     QCOMPARE( tracklist.at( 0 )->name(), QString( "Sunset" ) );
123     QCOMPARE( tracklist.at( 1 )->name(), QString( "Heaven" ) );
124     QCOMPARE( tracklist.at( 2 )->name(), QString( "Liquid Sun" ) );
125     QCOMPARE( tracklist.at( 3 )->name(), QString( "Restrained Mind" ) );
126     QCOMPARE( tracklist.at( 22 )->name(), QString( "Trash Bag" ) );
127 }
128 
testSetAndGetTitle()129 void TestXSPFPlaylist::testSetAndGetTitle()
130 {
131     QCOMPARE( m_testPlaylist1->title(), QString( "" ) );
132 
133     m_testPlaylist1->setTitle( "test" );
134     QCOMPARE( m_testPlaylist1->title(), QString( "test" ) );
135 
136     m_testPlaylist1->setTitle( "test aäoöuüß" );
137     QCOMPARE( m_testPlaylist1->title(), QString( "test aäoöuüß" ) );
138 
139     m_testPlaylist1->setTitle( "" );
140     QCOMPARE( m_testPlaylist1->title(), QString( "" ) );
141 }
142 
testSetAndGetCreator()143 void TestXSPFPlaylist::testSetAndGetCreator()
144 {
145     QCOMPARE( m_testPlaylist1->creator(), QString( "" ) );
146 
147     m_testPlaylist1->setCreator( "test" );
148     QCOMPARE( m_testPlaylist1->creator(), QString( "test" ) );
149 
150     m_testPlaylist1->setCreator( "test aäoöuüß" );
151     QCOMPARE( m_testPlaylist1->creator(), QString( "test aäoöuüß" ) );
152 
153     m_testPlaylist1->setCreator( "" );
154     QCOMPARE( m_testPlaylist1->creator(), QString( "" ) );
155 }
156 
testSetAndGetAnnotation()157 void TestXSPFPlaylist::testSetAndGetAnnotation()
158 {
159     QCOMPARE( m_testPlaylist1->annotation(), QString( "" ) );
160 
161     m_testPlaylist1->setAnnotation( "test" );
162     QCOMPARE( m_testPlaylist1->annotation(), QString( "test" ) );
163 
164     m_testPlaylist1->setAnnotation( "test aäoöuüß" );
165     QCOMPARE( m_testPlaylist1->annotation(), QString( "test aäoöuüß" ) );
166 
167     m_testPlaylist1->setAnnotation( "" );
168     QCOMPARE( m_testPlaylist1->annotation(), QString( "" ) );
169 }
170 
testSetAndGetInfo()171 void TestXSPFPlaylist::testSetAndGetInfo()
172 {
173     QUrl testUrl;
174 
175     QCOMPARE( m_testPlaylist1->info(), QUrl( "" ) );
176 
177     testUrl = QUrl::fromUserInput("http://amarok.kde.org");
178     m_testPlaylist1->setInfo( testUrl );
179     QCOMPARE( m_testPlaylist1->info(), QUrl( testUrl ) );
180 
181     testUrl = QUrl::fromUserInput("http://öko.de");
182     m_testPlaylist1->setInfo( testUrl );
183     QCOMPARE( m_testPlaylist1->info(), QUrl( testUrl ) );
184 
185     testUrl = QUrl::fromUserInput("");
186     m_testPlaylist1->setInfo( testUrl );
187     QCOMPARE( m_testPlaylist1->info(), QUrl( "" ) );
188 }
189 
testSetAndGetLocation()190 void TestXSPFPlaylist::testSetAndGetLocation()
191 {
192     QUrl testUrl;
193 
194     QCOMPARE( m_testPlaylist1->location(), QUrl( "" ) );
195 
196     testUrl = QUrl::fromUserInput("http://amarok.kde.org");
197     m_testPlaylist1->setLocation( testUrl );
198     QCOMPARE( m_testPlaylist1->location(), QUrl( testUrl ) );
199 
200     testUrl = QUrl::fromUserInput("http://öko.de");
201     m_testPlaylist1->setLocation( testUrl );
202     QCOMPARE( m_testPlaylist1->location(), QUrl( testUrl ) );
203 
204     testUrl = QUrl::fromUserInput("");
205     m_testPlaylist1->setLocation( testUrl );
206     QCOMPARE( m_testPlaylist1->location(), QUrl( "" ) );
207 }
208 
testSetAndGetIdentifier()209 void TestXSPFPlaylist::testSetAndGetIdentifier()
210 {
211     QCOMPARE( m_testPlaylist1->identifier(), QString( "" ) );
212 
213     m_testPlaylist1->setIdentifier( "test" );
214     QCOMPARE( m_testPlaylist1->identifier(), QString( "test" ) );
215 
216     m_testPlaylist1->setIdentifier( "test aäoöuüß" );
217     QCOMPARE( m_testPlaylist1->identifier(), QString( "test aäoöuüß" ) );
218 
219     m_testPlaylist1->setIdentifier( "" );
220     QCOMPARE( m_testPlaylist1->identifier(), QString( "" ) );
221 }
222 
testSetAndGetImage()223 void TestXSPFPlaylist::testSetAndGetImage()
224 {
225     QUrl testUrl;
226 
227     QCOMPARE( m_testPlaylist1->image(), QUrl( "" ) );
228 
229     testUrl = QUrl::fromUserInput("http://amarok.kde.org");
230     m_testPlaylist1->setImage( testUrl );
231     QCOMPARE( m_testPlaylist1->image(), QUrl( testUrl ) );
232 
233     testUrl = QUrl::fromUserInput("http://öko.de");
234     m_testPlaylist1->setImage( testUrl );
235     QCOMPARE( m_testPlaylist1->image(), QUrl( testUrl ) );
236 
237     testUrl = QUrl::fromUserInput("");
238     m_testPlaylist1->setImage( testUrl );
239     QCOMPARE( m_testPlaylist1->image(), QUrl( "" ) );
240 }
241 
testSetAndGetDate()242 void TestXSPFPlaylist::testSetAndGetDate()
243 {
244     QDateTime testDateTime;
245     QCOMPARE( m_testPlaylist1->date().toString(), QString( "" ) );
246 
247     testDateTime = QDateTime::fromString( "2009/08/13 13:57:18", "yyyy/MM/dd hh:mm:ss" );
248     m_testPlaylist1->setDate( testDateTime );
249     QCOMPARE( m_testPlaylist1->date(), testDateTime );
250 
251     testDateTime = QDateTime::fromString( "", "" );
252     m_testPlaylist1->setDate( testDateTime );
253     QCOMPARE( m_testPlaylist1->date(), testDateTime );
254 }
255 
testSetAndGetLicense()256 void TestXSPFPlaylist::testSetAndGetLicense()
257 {
258     QUrl testUrl;
259 
260     QCOMPARE( m_testPlaylist1->license(), QUrl( "" ) );
261 
262     testUrl = QUrl::fromUserInput("http://amarok.kde.org");
263     m_testPlaylist1->setLicense( testUrl );
264     QCOMPARE( m_testPlaylist1->license(), QUrl( testUrl ) );
265 
266     testUrl = QUrl::fromUserInput("http://öko.de");
267     m_testPlaylist1->setLicense( testUrl );
268     QCOMPARE( m_testPlaylist1->license(), QUrl( testUrl ) );
269 
270     testUrl = QUrl::fromUserInput("");
271     m_testPlaylist1->setLicense( testUrl );
272     QCOMPARE( m_testPlaylist1->license(), QUrl( "" ) );
273 }
274 
testSetAndGetAttribution()275 void TestXSPFPlaylist::testSetAndGetAttribution()
276 {
277     QUrl testUrl;
278 
279     QCOMPARE( m_testPlaylist1->attribution().size(), 0 );
280 
281     testUrl = QUrl::fromUserInput("http://amarok.kde.org");
282     m_testPlaylist1->setAttribution( testUrl );
283     QCOMPARE( m_testPlaylist1->attribution().size(), 1 );
284     QCOMPARE( m_testPlaylist1->attribution().at( 0 ), QUrl( testUrl ) );
285 
286     testUrl = QUrl::fromUserInput("http://öko.de");
287     m_testPlaylist1->setAttribution( testUrl );
288     QCOMPARE( m_testPlaylist1->attribution().size(), 2 );
289     QCOMPARE( m_testPlaylist1->attribution().at( 0 ), QUrl( testUrl ) );
290     QCOMPARE( m_testPlaylist1->attribution().at( 1 ), QUrl("http://amarok.kde.org") );
291 
292     testUrl = QUrl::fromUserInput("http://test.com");
293     m_testPlaylist1->setAttribution( testUrl, false );
294     QCOMPARE( m_testPlaylist1->attribution().size(), 1 );
295     QCOMPARE( m_testPlaylist1->attribution().at( 0 ), QUrl( testUrl ) );
296 
297     testUrl = QUrl::fromUserInput("");
298     m_testPlaylist1->setAttribution( testUrl );
299     QCOMPARE( m_testPlaylist1->attribution().size(), 1 ); // empty url won't be added, but size is 1 from last addition
300 }
301 
testSetAndGetLink()302 void TestXSPFPlaylist::testSetAndGetLink()
303 {
304     QUrl testUrl;
305 
306     QCOMPARE( m_testPlaylist1->link(), QUrl( "" ) );
307 
308     testUrl = QUrl::fromUserInput("http://amarok.kde.org");
309     m_testPlaylist1->setLink( testUrl );
310     QCOMPARE( m_testPlaylist1->link(), QUrl( testUrl ) );
311 
312     testUrl = QUrl::fromUserInput("http://öko.de");
313     m_testPlaylist1->setLink( testUrl );
314     QCOMPARE( m_testPlaylist1->link(), QUrl( testUrl ) );
315 
316     testUrl = QUrl::fromUserInput("");
317     m_testPlaylist1->setLink( testUrl );
318     QCOMPARE( m_testPlaylist1->link(), QUrl( "" ) );
319 }
320 
testUidUrl()321 void TestXSPFPlaylist::testUidUrl()
322 {
323     QString tempPath = m_tempDir->path() + "/test.xspf";
324 
325     //we have changed the name around so much, better reset it
326     m_testPlaylist1->setName( "test" );
327     QCOMPARE( m_testPlaylist1->uidUrl().toLocalFile(), tempPath );
328 }
329 
testIsWritable()330 void TestXSPFPlaylist::testIsWritable()
331 {
332     QVERIFY( m_testPlaylist1->isWritable() );
333 }
334 
testSave()335 void TestXSPFPlaylist::testSave()
336 {
337     QVERIFY( m_testPlaylist1->save( false ) );
338 }
339