File indexing completed on 2024-12-22 04:33:38
0001 /*************************************************************************** 0002 * Copyright (c) 2009 Sven Krohlas <sven@asbest-online.de> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify * 0005 * it under the terms of the GNU General Public License as published by * 0006 * the Free Software Foundation; either version 2 of the License, or * 0007 * (at your option) any later version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, * 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0012 * GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License * 0015 * along with this program; if not, write to the * 0016 * Free Software Foundation, Inc., * 0017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * 0018 ***************************************************************************/ 0019 0020 #include "TestPlaylistFileProvider.h" 0021 0022 #include "config-amarok-test.h" 0023 #include "core/support/Amarok.h" 0024 #include "core-impl/collections/support/CollectionManager.h" 0025 #include "playlistmanager/file/PlaylistFileProvider.h" 0026 0027 #include <QTest> 0028 #include <QDir> 0029 #include <QFile> 0030 0031 #include <KConfigGroup> 0032 0033 QTEST_MAIN( TestPlaylistFileProvider ) 0034 0035 TestPlaylistFileProvider::TestPlaylistFileProvider() 0036 {} 0037 0038 void TestPlaylistFileProvider::initTestCase() 0039 { 0040 m_testPlaylistFileProvider = new Playlists::PlaylistFileProvider(); 0041 QVERIFY( m_testPlaylistFileProvider ); 0042 0043 removeConfigPlaylistEntries(); 0044 removeTestPlaylist(); 0045 m_testPlaylistFileProvider->deletePlaylists( m_testPlaylistFileProvider->playlists() ); 0046 } 0047 0048 void TestPlaylistFileProvider::cleanupTestCase() 0049 { 0050 removeTestPlaylist(); 0051 removeConfigPlaylistEntries(); 0052 delete m_testPlaylistFileProvider; 0053 } 0054 0055 void TestPlaylistFileProvider::testPlaylists() 0056 { 0057 Playlists::PlaylistList tempList = m_testPlaylistFileProvider->playlists(); 0058 QCOMPARE( tempList.size(), 0 ); 0059 } 0060 0061 void TestPlaylistFileProvider::testSave_data() 0062 { 0063 QTest::addColumn<QString>("name"); 0064 QTest::addColumn<QString>("result"); 0065 0066 QTest::newRow("no extension, should default to xspf") << "Amarok Test Playlist" << "Amarok Test Playlist.xspf"; 0067 QTest::newRow("directory separators '\\' and '/' in file name are being replaced by '-'") << "amarok/playlist" << "amarok-playlist.xspf"; 0068 QTest::newRow("directory separators '\\' and '/' in file name are being replaced by '-'") << "amarok\\playlist" << "amarok-playlist.xspf"; 0069 QTest::newRow("xspf") << "Amarok Test Playlist.xspf" << "Amarok Test Playlist.xspf"; 0070 QTest::newRow("m3u") << "Amarok Test Playlist.m3u" << "Amarok Test Playlist.m3u"; 0071 QTest::newRow("pls") << "Amarok Test Playlist.pls" << "Amarok Test Playlist.pls"; 0072 QTest::newRow("playlist name with dot in it (BR 290318)") << "Amarok Test Playlist. With dots." << "Amarok Test Playlist. With dots..xspf"; 0073 } 0074 0075 void TestPlaylistFileProvider::testSave() 0076 { 0077 Meta::TrackList tempTrackList; 0078 const QUrl trackUrl = QUrl::fromLocalFile(dataPath( "data/audio/Platz 01.mp3" )); 0079 tempTrackList.append( CollectionManager::instance()->trackForUrl( trackUrl ) ); 0080 QCOMPARE( tempTrackList.size(), 1 ); 0081 0082 QFETCH(QString, name); 0083 QFETCH(QString, result); 0084 0085 Playlists::PlaylistPtr testPlaylist = m_testPlaylistFileProvider->save( tempTrackList, name ); 0086 QVERIFY( testPlaylist ); 0087 0088 QVERIFY( QFile::exists( Amarok::saveLocation( "playlists" ) + result ) ); 0089 QCOMPARE( testPlaylist->name(), QString( result ) ); 0090 QCOMPARE( testPlaylist->tracks().size(), 1 ); 0091 QFile::remove( Amarok::saveLocation( "playlists" ) + result ); 0092 } 0093 0094 void TestPlaylistFileProvider::testImportAndDeletePlaylists() 0095 { 0096 m_testPlaylistFileProvider->deletePlaylists( m_testPlaylistFileProvider->playlists() ); 0097 Playlists::PlaylistList tempList = m_testPlaylistFileProvider->playlists(); 0098 QCOMPARE( tempList.size(), 0 ); 0099 0100 QVERIFY( QFile::exists( dataPath( "data/playlists/test.m3u" ) ) ); 0101 QFile::copy( dataPath( "data/playlists/test.m3u" ), QDir::tempPath() + "/test.m3u" ); 0102 QVERIFY( QFile::exists( QDir::tempPath() + "/test.m3u" ) ); 0103 0104 QVERIFY( m_testPlaylistFileProvider->import( QUrl::fromLocalFile( QDir::tempPath() + "/test.m3u") ) ); 0105 tempList = m_testPlaylistFileProvider->playlists(); 0106 QCOMPARE( tempList.size(), 1 ); 0107 0108 m_testPlaylistFileProvider->deletePlaylists( tempList ); 0109 tempList = m_testPlaylistFileProvider->playlists(); 0110 QCOMPARE( tempList.size(), 0 ); 0111 } 0112 0113 void TestPlaylistFileProvider::testRename() 0114 { 0115 m_testPlaylistFileProvider->deletePlaylists( m_testPlaylistFileProvider->playlists() ); 0116 Playlists::PlaylistList tempList = m_testPlaylistFileProvider->playlists(); 0117 QCOMPARE( tempList.size(), 0 ); 0118 0119 QVERIFY( QFile::exists( dataPath( "data/playlists/test.m3u" ) ) ); 0120 QFile::copy( dataPath( "data/playlists/test.m3u" ), QDir::tempPath() + "/test.m3u" ); 0121 QVERIFY( QFile::exists( QDir::tempPath() + "/test.m3u" ) ); 0122 0123 QVERIFY( m_testPlaylistFileProvider->import( QUrl::fromLocalFile( QDir::tempPath() + "/test.m3u") ) ); 0124 tempList = m_testPlaylistFileProvider->playlists(); 0125 QCOMPARE( tempList.size(), 1 ); 0126 0127 m_testPlaylistFileProvider->renamePlaylist( tempList.at( 0 ), "New Test Name" ); 0128 tempList = m_testPlaylistFileProvider->playlists(); 0129 QCOMPARE( tempList.at( 0 )->name(), QString( "New Test Name.m3u" ) ); 0130 0131 m_testPlaylistFileProvider->deletePlaylists( tempList ); 0132 tempList = m_testPlaylistFileProvider->playlists(); 0133 QCOMPARE( tempList.size(), 0 ); 0134 } 0135 0136 QString TestPlaylistFileProvider::dataPath( const QString &relPath ) 0137 { 0138 return QDir::toNativeSeparators( QString( AMAROK_TEST_DIR ) + '/' + relPath ); 0139 } 0140 0141 void TestPlaylistFileProvider::removeTestPlaylist() 0142 { 0143 const QString m3u = Amarok::saveLocation( "playlists" ) + "Amarok Test Playlist.m3u"; 0144 if( QFile::exists( m3u ) ) 0145 QFile::remove( m3u ); 0146 } 0147 0148 void TestPlaylistFileProvider::removeConfigPlaylistEntries() 0149 { 0150 KConfigGroup config = Amarok::config( "Loaded Playlist Files" ); 0151 config.deleteGroup(); 0152 }