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-05-23 19:00:45 +02:00
|
|
|
#include "gmock/gmock-matchers.h"
|
2010-03-01 16:40:12 +01:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "test_utils.h"
|
|
|
|
|
2011-11-28 14:51:35 +01:00
|
|
|
#include "core/timeconstants.h"
|
2010-05-22 22:06:19 +02:00
|
|
|
#include "playlistparsers/m3uparser.h"
|
2010-03-01 16:40:12 +01:00
|
|
|
|
2010-03-05 12:57:06 +01:00
|
|
|
#include <QBuffer>
|
2010-03-07 16:26:54 +01:00
|
|
|
#include <QTemporaryFile>
|
2010-03-05 12:57:06 +01:00
|
|
|
|
2010-05-23 19:00:45 +02:00
|
|
|
using ::testing::HasSubstr;
|
|
|
|
|
2010-03-01 16:40:12 +01:00
|
|
|
class M3UParserTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
M3UParserTest()
|
2014-02-21 17:24:49 +01:00
|
|
|
: parser_(nullptr) {
|
2010-03-01 16:40:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
M3UParser parser_;
|
|
|
|
};
|
|
|
|
|
|
|
|
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());
|
2011-02-14 20:34:37 +01:00
|
|
|
EXPECT_EQ(123 * kNsecPerSec, metadata.length);
|
2010-03-01 16:40:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(M3UParserTest, ParsesTrackLocation) {
|
2010-03-07 16:26:54 +01:00
|
|
|
QTemporaryFile temp;
|
|
|
|
temp.open();
|
|
|
|
taglib_.ExpectCall(temp.fileName(), "foo", "bar", "baz");
|
2010-03-07 15:04:17 +01:00
|
|
|
Song song(&taglib_);
|
2010-03-07 16:26:54 +01:00
|
|
|
QString line(temp.fileName());
|
2011-04-28 14:27:53 +02:00
|
|
|
parser_.LoadSong(line, 0, QDir(), &song);
|
|
|
|
ASSERT_EQ(QUrl::fromLocalFile(temp.fileName()), song.url());
|
2010-12-25 16:46:20 +01:00
|
|
|
|
2011-04-28 14:27:53 +02:00
|
|
|
song.InitFromFile(song.url().toLocalFile(), -1);
|
2010-12-25 16:46:20 +01:00
|
|
|
|
2010-03-07 15:04:17 +01:00
|
|
|
EXPECT_EQ("foo", song.title());
|
|
|
|
EXPECT_EQ("bar", song.artist());
|
|
|
|
EXPECT_EQ("baz", song.album());
|
2010-03-01 16:40:12 +01:00
|
|
|
}
|
2010-03-01 17:31:19 +01:00
|
|
|
|
|
|
|
TEST_F(M3UParserTest, ParsesTrackLocationRelative) {
|
2010-03-07 16:26:54 +01:00
|
|
|
QTemporaryFile temp;
|
|
|
|
temp.open();
|
|
|
|
QFileInfo info(temp);
|
|
|
|
taglib_.ExpectCall(temp.fileName(), "foo", "bar", "baz");
|
2014-02-21 17:24:49 +01:00
|
|
|
M3UParser parser(nullptr);
|
2010-03-07 16:26:54 +01:00
|
|
|
QString line(info.fileName());
|
2010-03-07 15:04:17 +01:00
|
|
|
Song song(&taglib_);
|
2011-04-28 14:27:53 +02:00
|
|
|
parser.LoadSong(line, 0, info.dir(), &song);
|
|
|
|
ASSERT_EQ(QUrl::fromLocalFile(temp.fileName()), song.url());
|
2010-12-25 16:46:20 +01:00
|
|
|
|
2011-04-28 14:27:53 +02:00
|
|
|
song.InitFromFile(song.url().toLocalFile(), -1);
|
2010-12-25 16:46:20 +01:00
|
|
|
|
2010-03-07 15:04:17 +01:00
|
|
|
EXPECT_EQ("foo", song.title());
|
2010-03-01 17:31:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(M3UParserTest, ParsesTrackLocationHttp) {
|
|
|
|
QString line("http://example.com/foo/bar.mp3");
|
2010-03-07 15:04:17 +01:00
|
|
|
Song song;
|
2011-04-28 14:27:53 +02:00
|
|
|
parser_.LoadSong(line, 0, QDir(), &song);
|
|
|
|
EXPECT_EQ(QUrl("http://example.com/foo/bar.mp3"), song.url());
|
2010-03-01 17:31:19 +01:00
|
|
|
}
|
|
|
|
|
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);
|
2014-02-21 17:24:49 +01:00
|
|
|
M3UParser parser(nullptr);
|
2010-05-22 22:06:19 +02:00
|
|
|
SongList songs = parser.Load(&buffer);
|
2010-03-05 12:57:06 +01:00
|
|
|
ASSERT_EQ(1, songs.size());
|
|
|
|
Song s = songs[0];
|
|
|
|
EXPECT_EQ("Some Artist", s.artist());
|
|
|
|
EXPECT_EQ("Some Title", s.title());
|
2011-02-14 20:34:37 +01:00
|
|
|
EXPECT_EQ(123 * kNsecPerSec, s.length_nanosec());
|
2011-04-28 14:27:53 +02:00
|
|
|
EXPECT_EQ(QUrl("http://foo.com/bar/somefile.mp3"), s.url());
|
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);
|
2014-02-21 17:24:49 +01:00
|
|
|
M3UParser parser(nullptr);
|
2011-01-12 00:09:59 +01:00
|
|
|
SongList songs = parser.Load(&buffer, "", QDir("somedir"));
|
2010-03-05 12:57:06 +01:00
|
|
|
ASSERT_EQ(2, songs.size());
|
2011-04-28 14:27:53 +02:00
|
|
|
EXPECT_EQ(QUrl("http://foo.com/bar/somefile.mp3"), songs[0].url());
|
|
|
|
EXPECT_EQ(QUrl("http://baz.com/thing.mp3"), songs[1].url());
|
2011-02-13 19:35:26 +01:00
|
|
|
EXPECT_EQ(-1, songs[0].length_nanosec());
|
|
|
|
EXPECT_EQ(-1, songs[1].length_nanosec());
|
2010-03-05 12:57:06 +01:00
|
|
|
EXPECT_TRUE(songs[0].artist().isEmpty());
|
|
|
|
}
|
2010-05-23 17:35:28 +02:00
|
|
|
|
|
|
|
TEST_F(M3UParserTest, ParsesActualM3U) {
|
|
|
|
QFile file(":testdata/test.m3u");
|
|
|
|
file.open(QIODevice::ReadOnly);
|
2014-02-21 17:24:49 +01:00
|
|
|
M3UParser parser(nullptr);
|
2011-01-12 00:09:59 +01:00
|
|
|
SongList songs = parser.Load(&file, "", QDir("somedir"));
|
2010-05-23 17:35:28 +02:00
|
|
|
ASSERT_EQ(239, songs.size());
|
|
|
|
EXPECT_EQ("gravity", songs[0].title());
|
2011-02-14 20:34:37 +01:00
|
|
|
EXPECT_EQ(203 * kNsecPerSec, songs[0].length_nanosec());
|
2010-06-02 22:06:47 +02:00
|
|
|
EXPECT_EQ(QString::fromUtf8("ほっぴンちょっぴン"), songs.back().title());
|
2011-02-14 20:34:37 +01:00
|
|
|
EXPECT_EQ(85 * kNsecPerSec, songs.back().length_nanosec());
|
2010-05-23 17:35:28 +02:00
|
|
|
}
|
2010-05-23 19:00:45 +02:00
|
|
|
|
|
|
|
TEST_F(M3UParserTest, SavesSong) {
|
|
|
|
QByteArray data;
|
|
|
|
QBuffer buffer(&data);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
Song one;
|
|
|
|
one.set_filetype(Song::Type_Stream);
|
|
|
|
one.set_title("foo");
|
|
|
|
one.set_artist("bar");
|
2011-02-14 20:34:37 +01:00
|
|
|
one.set_length_nanosec(123 * kNsecPerSec);
|
2011-04-28 14:27:53 +02:00
|
|
|
one.set_url(QUrl("http://www.example.com/foo.mp3"));
|
2010-05-23 19:00:45 +02:00
|
|
|
SongList songs;
|
|
|
|
songs << one;
|
2014-02-21 17:24:49 +01:00
|
|
|
M3UParser parser(nullptr);
|
2010-05-23 19:00:45 +02:00
|
|
|
parser.Save(songs, &buffer);
|
|
|
|
EXPECT_THAT(data.constData(), HasSubstr("#EXTM3U"));
|
|
|
|
EXPECT_THAT(data.constData(), HasSubstr("#EXTINF:123,bar - foo"));
|
|
|
|
EXPECT_THAT(data.constData(), HasSubstr("http://www.example.com/foo.mp3"));
|
|
|
|
}
|
2010-06-02 22:06:47 +02:00
|
|
|
|
|
|
|
TEST_F(M3UParserTest, ParsesUTF8) {
|
|
|
|
QByteArray data = "#EXTM3U\n"
|
|
|
|
"#EXTINF:123,Разные - исполнители\n"
|
|
|
|
"/foo/Разные/исполнители.mp3\n";
|
|
|
|
QBuffer buffer(&data);
|
|
|
|
buffer.open(QIODevice::ReadOnly);
|
2014-02-21 17:24:49 +01:00
|
|
|
M3UParser parser(nullptr);
|
2010-06-02 22:06:47 +02:00
|
|
|
SongList songs = parser.Load(&buffer);
|
|
|
|
ASSERT_EQ(1, songs.length());
|
|
|
|
EXPECT_EQ(6, songs[0].artist().length());
|
|
|
|
EXPECT_EQ(11, songs[0].title().length());
|
|
|
|
EXPECT_EQ(QString::fromUtf8("Разные"), songs[0].artist());
|
|
|
|
EXPECT_EQ(QString::fromUtf8("исполнители"), songs[0].title());
|
2011-04-28 14:27:53 +02:00
|
|
|
EXPECT_EQ(QUrl::fromLocalFile(QString::fromUtf8("/foo/Разные/исполнители.mp3")),
|
|
|
|
songs[0].url());
|
2010-06-02 22:06:47 +02:00
|
|
|
}
|