2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-24 00:11:46 +01:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2010-03-09 18:17:32 +01:00
|
|
|
#include "test_utils.h"
|
2010-05-23 18:26:32 +02:00
|
|
|
#include "gmock/gmock-matchers.h"
|
2010-03-09 18:17:32 +01:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2010-05-22 22:06:19 +02:00
|
|
|
#include "playlistparsers/xspfparser.h"
|
2010-03-09 18:17:32 +01:00
|
|
|
|
|
|
|
#include <QBuffer>
|
|
|
|
|
2010-05-23 18:26:32 +02:00
|
|
|
using ::testing::HasSubstr;
|
|
|
|
|
2010-03-09 18:17:32 +01:00
|
|
|
class XSPFParserTest : public ::testing::Test {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(XSPFParserTest, ParsesOneTrackFromXML) {
|
|
|
|
QByteArray data =
|
|
|
|
"<playlist><trackList><track>"
|
|
|
|
"<location>http://example.com/foo.mp3</location>"
|
|
|
|
"<title>Foo</title>"
|
|
|
|
"<creator>Bar</creator>"
|
|
|
|
"<album>Baz</album>"
|
|
|
|
"<duration>60000</duration>"
|
|
|
|
"<image>http://example.com/albumcover.jpg</image>"
|
|
|
|
"<info>http://example.com</info>"
|
|
|
|
"</track></trackList></playlist>";
|
|
|
|
QBuffer buffer(&data);
|
|
|
|
buffer.open(QIODevice::ReadOnly);
|
2010-12-11 12:07:00 +01:00
|
|
|
XSPFParser parser(NULL);
|
2010-05-22 22:06:19 +02:00
|
|
|
SongList songs = parser.Load(&buffer);
|
2010-03-09 18:17:32 +01:00
|
|
|
ASSERT_EQ(1, songs.length());
|
|
|
|
const Song& song = songs[0];
|
|
|
|
EXPECT_EQ("Foo", song.title());
|
|
|
|
EXPECT_EQ("Bar", song.artist());
|
|
|
|
EXPECT_EQ("Baz", song.album());
|
2011-04-28 14:27:53 +02:00
|
|
|
EXPECT_EQ(QUrl("http://example.com/foo.mp3"), song.url());
|
2011-02-14 20:34:37 +01:00
|
|
|
EXPECT_EQ(60 * kNsecPerSec, song.length_nanosec());
|
2010-03-09 18:17:32 +01:00
|
|
|
EXPECT_TRUE(song.is_valid());
|
|
|
|
}
|
2010-03-11 16:25:25 +01:00
|
|
|
|
|
|
|
TEST_F(XSPFParserTest, ParsesMoreThanOneTrackFromXML) {
|
|
|
|
QByteArray data =
|
|
|
|
"<playlist><trackList>"
|
|
|
|
"<track>"
|
|
|
|
"<location>http://example.com/foo.mp3</location>"
|
|
|
|
"</track>"
|
|
|
|
"<track>"
|
|
|
|
"<location>http://example.com/bar.mp3</location>"
|
|
|
|
"</track>"
|
|
|
|
"</trackList></playlist>";
|
|
|
|
QBuffer buffer(&data);
|
|
|
|
buffer.open(QIODevice::ReadOnly);
|
2010-12-11 12:07:00 +01:00
|
|
|
XSPFParser parser(NULL);
|
2010-05-22 22:06:19 +02:00
|
|
|
SongList songs = parser.Load(&buffer);
|
2010-03-11 16:25:25 +01:00
|
|
|
ASSERT_EQ(2, songs.length());
|
2011-04-28 14:27:53 +02:00
|
|
|
EXPECT_EQ(QUrl("http://example.com/foo.mp3"), songs[0].url());
|
|
|
|
EXPECT_EQ(QUrl("http://example.com/bar.mp3"), songs[1].url());
|
2011-03-20 13:43:10 +01:00
|
|
|
EXPECT_TRUE(songs[0].is_stream());
|
|
|
|
EXPECT_TRUE(songs[1].is_stream());
|
2010-03-11 16:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(XSPFParserTest, IgnoresInvalidLength) {
|
|
|
|
QByteArray data =
|
|
|
|
"<playlist><trackList><track>"
|
|
|
|
"<location>http://example.com/foo.mp3</location>"
|
|
|
|
"<title>Foo</title>"
|
|
|
|
"<creator>Bar</creator>"
|
|
|
|
"<album>Baz</album>"
|
|
|
|
"<duration>60000qwerty</duration>"
|
|
|
|
"<image>http://example.com/albumcover.jpg</image>"
|
|
|
|
"<info>http://example.com</info>"
|
|
|
|
"</track></trackList></playlist>";
|
|
|
|
QBuffer buffer(&data);
|
|
|
|
buffer.open(QIODevice::ReadOnly);
|
2010-12-11 12:07:00 +01:00
|
|
|
XSPFParser parser(NULL);
|
2010-05-22 22:06:19 +02:00
|
|
|
SongList songs = parser.Load(&buffer);
|
2010-03-11 16:25:25 +01:00
|
|
|
ASSERT_EQ(1, songs.length());
|
2011-02-13 19:35:26 +01:00
|
|
|
EXPECT_EQ(-1, songs[0].length_nanosec());
|
2010-03-11 16:25:25 +01:00
|
|
|
}
|
2010-05-23 18:26:32 +02:00
|
|
|
|
|
|
|
TEST_F(XSPFParserTest, SavesSong) {
|
|
|
|
QByteArray data;
|
|
|
|
QBuffer buffer(&data);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
2010-12-11 12:07:00 +01:00
|
|
|
XSPFParser parser(NULL);
|
2010-05-23 18:26:32 +02:00
|
|
|
Song one;
|
2011-04-28 14:27:53 +02:00
|
|
|
one.set_url(QUrl("http://www.example.com/foo.mp3"));
|
2010-05-23 18:26:32 +02:00
|
|
|
one.set_filetype(Song::Type_Stream);
|
|
|
|
one.set_title("foo");
|
2011-02-14 20:34:37 +01:00
|
|
|
one.set_length_nanosec(123 * kNsecPerSec);
|
2010-05-23 18:26:32 +02:00
|
|
|
one.set_artist("bar");
|
|
|
|
SongList songs;
|
|
|
|
songs << one;
|
|
|
|
|
|
|
|
parser.Save(songs, &buffer);
|
|
|
|
EXPECT_THAT(data.constData(), HasSubstr("<location>http://www.example.com/foo.mp3</location>"));
|
|
|
|
EXPECT_THAT(data.constData(), HasSubstr("<duration>123000</duration>"));
|
|
|
|
EXPECT_THAT(data.constData(), HasSubstr("<title>foo</title>"));
|
|
|
|
EXPECT_THAT(data.constData(), HasSubstr("<creator>bar</creator>"));
|
|
|
|
}
|
2010-05-28 00:43:07 +02:00
|
|
|
|
|
|
|
TEST_F(XSPFParserTest, SavesLocalFile) {
|
|
|
|
QByteArray data;
|
|
|
|
QBuffer buffer(&data);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
2010-12-11 12:07:00 +01:00
|
|
|
XSPFParser parser(NULL);
|
2010-05-28 00:43:07 +02:00
|
|
|
Song one;
|
2011-04-28 14:27:53 +02:00
|
|
|
one.set_url(QUrl("file:///bar/foo.mp3"));
|
2010-05-28 00:43:07 +02:00
|
|
|
one.set_filetype(Song::Type_Mpeg);
|
|
|
|
one.set_title("foo");
|
2011-02-14 20:34:37 +01:00
|
|
|
one.set_length_nanosec(123 * kNsecPerSec);
|
2010-05-28 00:43:07 +02:00
|
|
|
one.set_artist("bar");
|
|
|
|
SongList songs;
|
|
|
|
songs << one;
|
|
|
|
|
|
|
|
parser.Save(songs, &buffer);
|
|
|
|
EXPECT_THAT(data.constData(), HasSubstr("<location>file:///bar/foo.mp3</location>"));
|
|
|
|
EXPECT_THAT(data.constData(), HasSubstr("<duration>123000</duration>"));
|
|
|
|
EXPECT_THAT(data.constData(), HasSubstr("<title>foo</title>"));
|
|
|
|
EXPECT_THAT(data.constData(), HasSubstr("<creator>bar</creator>"));
|
|
|
|
}
|
|
|
|
|