Clementine-audio-player-Mac.../tests/m3uparser_test.cpp

115 lines
3.7 KiB
C++
Raw Normal View History

/* This file is part of Clementine.
Clementine is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clementine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gtest/gtest.h"
#include "test_utils.h"
2010-03-07 15:04:17 +01:00
#include "mock_taglib.h"
#include "m3uparser.h"
2010-03-05 12:57:06 +01:00
#include <QBuffer>
#include <QTemporaryFile>
2010-03-05 12:57:06 +01:00
class M3UParserTest : public ::testing::Test {
protected:
2010-03-07 15:04:17 +01:00
static void SetUpTestCase() {
testing::DefaultValue<TagLib::String>::Set("foobarbaz");
}
M3UParserTest()
: parser_(NULL) {
}
M3UParser parser_;
2010-03-07 15:04:17 +01:00
MockFileRefFactory taglib_;
};
TEST_F(M3UParserTest, ParsesMetadata) {
QString line("#EXTINF:123,Foo artist - Foo track");
M3UParser::Metadata metadata;
ASSERT_TRUE(parser_.ParseMetadata(line, &metadata));
EXPECT_EQ("Foo artist", metadata.artist.toStdString());
EXPECT_EQ("Foo track", metadata.title.toStdString());
EXPECT_EQ(123, metadata.length);
}
TEST_F(M3UParserTest, ParsesTrackLocation) {
QTemporaryFile temp;
temp.open();
taglib_.ExpectCall(temp.fileName(), "foo", "bar", "baz");
2010-03-07 15:04:17 +01:00
Song song(&taglib_);
QString line(temp.fileName());
2010-03-07 15:04:17 +01:00
ASSERT_TRUE(parser_.ParseTrackLocation(line, &song));
EXPECT_EQ(temp.fileName(), song.filename());
2010-03-07 15:04:17 +01:00
EXPECT_EQ("foo", song.title());
EXPECT_EQ("bar", song.artist());
EXPECT_EQ("baz", song.album());
}
TEST_F(M3UParserTest, ParsesTrackLocationRelative) {
QTemporaryFile temp;
temp.open();
QFileInfo info(temp);
taglib_.ExpectCall(temp.fileName(), "foo", "bar", "baz");
M3UParser parser(NULL, info.dir());
QString line(info.fileName());
2010-03-07 15:04:17 +01:00
Song song(&taglib_);
ASSERT_TRUE(parser.ParseTrackLocation(line, &song));
EXPECT_EQ(temp.fileName(), song.filename());
2010-03-07 15:04:17 +01:00
EXPECT_EQ("foo", song.title());
}
TEST_F(M3UParserTest, ParsesTrackLocationHttp) {
QString line("http://example.com/foo/bar.mp3");
2010-03-07 15:04:17 +01:00
Song song;
ASSERT_TRUE(parser_.ParseTrackLocation(line, &song));
EXPECT_EQ("http://example.com/foo/bar.mp3", song.filename());
}
2010-03-05 12:57:06 +01:00
TEST_F(M3UParserTest, ParsesSongsFromDevice) {
QByteArray data = "#EXTM3U\n"
"#EXTINF:123,Some Artist - Some Title\n"
2010-03-07 15:04:17 +01:00
"http://foo.com/bar/somefile.mp3\n";
2010-03-05 12:57:06 +01:00
QBuffer buffer(&data);
buffer.open(QIODevice::ReadOnly);
2010-03-07 15:04:17 +01:00
M3UParser parser(&buffer);
2010-03-05 12:57:06 +01:00
const QList<Song>& songs = parser.Parse();
ASSERT_EQ(1, songs.size());
Song s = songs[0];
EXPECT_EQ("Some Artist", s.artist());
EXPECT_EQ("Some Title", s.title());
EXPECT_EQ(123, s.length());
EXPECT_PRED_FORMAT2(::testing::IsSubstring,
2010-03-07 15:04:17 +01:00
"http://foo.com/bar/somefile.mp3", s.filename().toStdString());
2010-03-05 12:57:06 +01:00
}
TEST_F(M3UParserTest, ParsesNonExtendedM3U) {
2010-03-07 15:04:17 +01:00
QByteArray data = "http://foo.com/bar/somefile.mp3\n"
"http://baz.com/thing.mp3\n";
2010-03-05 12:57:06 +01:00
QBuffer buffer(&data);
buffer.open(QIODevice::ReadOnly);
M3UParser parser(&buffer, QDir("somedir"));
const QList<Song>& songs = parser.Parse();
ASSERT_EQ(2, songs.size());
EXPECT_PRED_FORMAT2(::testing::IsSubstring,
2010-03-07 15:04:17 +01:00
"http://foo.com/bar/somefile.mp3", songs[0].filename().toStdString());
2010-03-05 12:57:06 +01:00
EXPECT_PRED_FORMAT2(::testing::IsSubstring,
2010-03-07 15:04:17 +01:00
"http://baz.com/thing.mp3", songs[1].filename().toStdString());
2010-03-05 12:57:06 +01:00
EXPECT_EQ(-1, songs[0].length());
EXPECT_EQ(-1, songs[1].length());
EXPECT_TRUE(songs[0].artist().isEmpty());
}