2010-06-15 15:24:17 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-06-15 15:24:17 +02: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "test_utils.h"
|
|
|
|
#include "gmock/gmock-matchers.h"
|
|
|
|
#include "gtest/gtest.h"
|
2010-08-31 23:24:57 +02:00
|
|
|
#include "mock_librarybackend.h"
|
2010-06-15 15:24:17 +02:00
|
|
|
|
|
|
|
#include "core/songloader.h"
|
|
|
|
#include "engines/gstengine.h"
|
|
|
|
|
|
|
|
#include <QBuffer>
|
2010-06-15 16:24:17 +02:00
|
|
|
#include <QDir>
|
2010-06-15 15:24:17 +02:00
|
|
|
#include <QEventLoop>
|
|
|
|
#include <QSignalSpy>
|
2010-06-17 00:32:02 +02:00
|
|
|
#include <QtDebug>
|
2010-06-15 15:24:17 +02:00
|
|
|
|
|
|
|
#include <boost/scoped_ptr.hpp>
|
2010-06-15 16:24:17 +02:00
|
|
|
#include <cstdlib>
|
2010-06-15 15:24:17 +02:00
|
|
|
|
2010-08-31 23:24:57 +02:00
|
|
|
using ::testing::_;
|
|
|
|
using ::testing::Return;
|
|
|
|
|
2010-06-15 15:24:17 +02:00
|
|
|
class SongLoaderTest : public ::testing::Test {
|
|
|
|
public:
|
2010-06-15 15:56:41 +02:00
|
|
|
static void SetUpTestCase() {
|
|
|
|
sGstEngine = new GstEngine;
|
|
|
|
ASSERT_TRUE(sGstEngine->Init());
|
2010-08-28 21:22:58 +02:00
|
|
|
sGstEngine->EnsureInitialised();
|
2010-06-15 15:56:41 +02:00
|
|
|
}
|
2010-06-15 15:24:17 +02:00
|
|
|
|
2010-06-15 15:56:41 +02:00
|
|
|
static void TearDownTestCase() {
|
|
|
|
delete sGstEngine;
|
|
|
|
sGstEngine = NULL;
|
|
|
|
}
|
2010-06-15 15:24:17 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void SetUp() {
|
2010-08-31 23:24:57 +02:00
|
|
|
library_.reset(new MockLibraryBackend);
|
|
|
|
loader_.reset(new SongLoader(library_.get()));
|
2010-06-23 13:51:13 +02:00
|
|
|
loader_->set_timeout(20000);
|
2011-01-12 00:09:59 +01:00
|
|
|
|
|
|
|
// the thing we return is not really important
|
|
|
|
EXPECT_CALL(*library_.get(), GetSongByFilename(_, _)).WillRepeatedly(Return(Song()));
|
2010-06-15 15:24:17 +02:00
|
|
|
}
|
|
|
|
|
2010-06-15 16:24:17 +02:00
|
|
|
void LoadLocalDirectory(const QString& dir);
|
|
|
|
|
2010-06-15 15:24:17 +02:00
|
|
|
static const char* kRemoteUrl;
|
|
|
|
static GstEngine* sGstEngine;
|
|
|
|
|
|
|
|
boost::scoped_ptr<SongLoader> loader_;
|
2010-08-31 23:24:57 +02:00
|
|
|
boost::scoped_ptr<MockLibraryBackend> library_;
|
2010-06-15 15:24:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const char* SongLoaderTest::kRemoteUrl = "http://remotetestdata.clementine-player.org";
|
|
|
|
GstEngine* SongLoaderTest::sGstEngine = NULL;
|
|
|
|
|
|
|
|
TEST_F(SongLoaderTest, LoadLocalMp3) {
|
|
|
|
TemporaryResource file(":/testdata/beep.mp3");
|
|
|
|
SongLoader::Result ret = loader_->Load(QUrl::fromLocalFile(file.fileName()));
|
|
|
|
|
|
|
|
ASSERT_EQ(SongLoader::Success, ret);
|
|
|
|
ASSERT_EQ(1, loader_->songs().count());
|
|
|
|
EXPECT_TRUE(loader_->songs()[0].is_valid());
|
2010-08-31 23:24:57 +02:00
|
|
|
EXPECT_EQ("Beep mp3", loader_->songs()[0].title());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SongLoaderTest, LoadLocalFileQueryExecutesButEmpty) {
|
|
|
|
EXPECT_CALL(*library_.get(), ExecQuery(_)).WillOnce(Return(true));
|
|
|
|
TemporaryResource file(":/testdata/beep.mp3");
|
|
|
|
SongLoader::Result ret = loader_->Load(QUrl::fromLocalFile(file.fileName()));
|
|
|
|
|
|
|
|
ASSERT_EQ(SongLoader::Success, ret);
|
|
|
|
ASSERT_EQ(1, loader_->songs().count());
|
|
|
|
EXPECT_TRUE(loader_->songs()[0].is_valid());
|
2010-06-15 15:24:17 +02:00
|
|
|
EXPECT_EQ("Beep mp3", loader_->songs()[0].title());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SongLoaderTest, LoadLocalPls) {
|
|
|
|
TemporaryResource file(":/testdata/pls_one.pls");
|
|
|
|
SongLoader::Result ret = loader_->Load(QUrl::fromLocalFile(file.fileName()));
|
|
|
|
|
2010-08-03 18:00:51 +02:00
|
|
|
ASSERT_EQ(SongLoader::WillLoadAsync, ret);
|
|
|
|
QSignalSpy spy(loader_.get(), SIGNAL(LoadFinished(bool)));
|
|
|
|
|
|
|
|
// Start an event loop to wait for gstreamer to do its thing
|
|
|
|
QEventLoop loop;
|
|
|
|
QObject::connect(loader_.get(), SIGNAL(LoadFinished(bool)),
|
|
|
|
&loop, SLOT(quit()));
|
|
|
|
loop.exec(QEventLoop::ExcludeUserInputEvents);
|
|
|
|
|
|
|
|
// Check the signal was emitted with Success
|
|
|
|
ASSERT_EQ(1, spy.count());
|
|
|
|
EXPECT_EQ(true, spy[0][0].toBool());
|
|
|
|
|
|
|
|
// Check the song got loaded
|
2010-06-15 15:24:17 +02:00
|
|
|
ASSERT_EQ(1, loader_->songs().count());
|
|
|
|
EXPECT_EQ("Title", loader_->songs()[0].title());
|
2011-02-14 20:34:37 +01:00
|
|
|
EXPECT_EQ(123 * kNsecPerSec, loader_->songs()[0].length_nanosec());
|
2010-06-15 15:24:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SongLoaderTest, LoadLocalM3U) {
|
|
|
|
TemporaryResource file(":/testdata/test.m3u");
|
|
|
|
SongLoader::Result ret = loader_->Load(QUrl::fromLocalFile(file.fileName()));
|
|
|
|
|
2010-08-03 18:00:51 +02:00
|
|
|
QSignalSpy spy(loader_.get(), SIGNAL(LoadFinished(bool)));
|
|
|
|
|
|
|
|
QEventLoop loop;
|
|
|
|
QObject::connect(loader_.get(), SIGNAL(LoadFinished(bool)),
|
|
|
|
&loop, SLOT(quit()));
|
|
|
|
loop.exec(QEventLoop::ExcludeUserInputEvents);
|
|
|
|
|
|
|
|
// Check the signal was emitted with Success
|
|
|
|
ASSERT_EQ(1, spy.count());
|
|
|
|
EXPECT_EQ(true, spy[0][0].toBool());
|
|
|
|
|
|
|
|
ASSERT_EQ(SongLoader::WillLoadAsync, ret);
|
2010-06-15 15:24:17 +02:00
|
|
|
ASSERT_EQ(239, loader_->songs().count());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SongLoaderTest, LoadLocalXSPF) {
|
|
|
|
TemporaryResource file(":/testdata/test.xspf");
|
|
|
|
SongLoader::Result ret = loader_->Load(QUrl::fromLocalFile(file.fileName()));
|
|
|
|
|
2010-08-03 18:00:51 +02:00
|
|
|
QSignalSpy spy(loader_.get(), SIGNAL(LoadFinished(bool)));
|
|
|
|
|
|
|
|
QEventLoop loop;
|
|
|
|
QObject::connect(loader_.get(), SIGNAL(LoadFinished(bool)),
|
|
|
|
&loop, SLOT(quit()));
|
|
|
|
loop.exec(QEventLoop::ExcludeUserInputEvents);
|
|
|
|
|
|
|
|
// Check the signal was emitted with Success
|
|
|
|
ASSERT_EQ(1, spy.count());
|
|
|
|
EXPECT_EQ(true, spy[0][0].toBool());
|
|
|
|
|
|
|
|
ASSERT_EQ(SongLoader::WillLoadAsync, ret);
|
2010-06-15 15:24:17 +02:00
|
|
|
ASSERT_EQ(1, loader_->songs().count());
|
|
|
|
EXPECT_EQ("Foo", loader_->songs()[0].title());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SongLoaderTest, LoadLocalASX) {
|
|
|
|
TemporaryResource file(":/testdata/test.asx");
|
|
|
|
SongLoader::Result ret = loader_->Load(QUrl::fromLocalFile(file.fileName()));
|
|
|
|
|
2010-08-03 18:00:51 +02:00
|
|
|
QSignalSpy spy(loader_.get(), SIGNAL(LoadFinished(bool)));
|
|
|
|
|
|
|
|
QEventLoop loop;
|
|
|
|
QObject::connect(loader_.get(), SIGNAL(LoadFinished(bool)),
|
|
|
|
&loop, SLOT(quit()));
|
|
|
|
loop.exec(QEventLoop::ExcludeUserInputEvents);
|
|
|
|
|
|
|
|
// Check the signal was emitted with Success
|
|
|
|
ASSERT_EQ(1, spy.count());
|
|
|
|
EXPECT_EQ(true, spy[0][0].toBool());
|
|
|
|
|
|
|
|
ASSERT_EQ(SongLoader::WillLoadAsync, ret);
|
2010-06-15 15:24:17 +02:00
|
|
|
ASSERT_EQ(1, loader_->songs().count());
|
|
|
|
EXPECT_EQ("Foo", loader_->songs()[0].title());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SongLoaderTest, LoadRemoteMp3) {
|
|
|
|
SongLoader::Result ret = loader_->Load(QString(kRemoteUrl) + "/beep.mp3");
|
|
|
|
ASSERT_EQ(SongLoader::WillLoadAsync, ret);
|
|
|
|
|
|
|
|
QSignalSpy spy(loader_.get(), SIGNAL(LoadFinished(bool)));
|
|
|
|
|
|
|
|
// Start an event loop to wait for gstreamer to do its thing
|
|
|
|
QEventLoop loop;
|
|
|
|
QObject::connect(loader_.get(), SIGNAL(LoadFinished(bool)),
|
|
|
|
&loop, SLOT(quit()));
|
2010-06-15 15:56:41 +02:00
|
|
|
loop.exec(QEventLoop::ExcludeUserInputEvents);
|
2010-06-15 15:24:17 +02:00
|
|
|
|
|
|
|
// Check the signal was emitted with Success
|
|
|
|
ASSERT_EQ(1, spy.count());
|
|
|
|
EXPECT_EQ(true, spy[0][0].toBool());
|
|
|
|
|
|
|
|
// Check the song got loaded
|
|
|
|
ASSERT_EQ(1, loader_->songs().count());
|
2010-06-20 14:50:08 +02:00
|
|
|
EXPECT_EQ(QString(kRemoteUrl) + "/beep.mp3", loader_->songs()[0].filename());
|
2010-06-15 15:24:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SongLoaderTest, LoadRemote404) {
|
|
|
|
SongLoader::Result ret = loader_->Load(QString(kRemoteUrl) + "/404.mp3");
|
|
|
|
ASSERT_EQ(SongLoader::WillLoadAsync, ret);
|
|
|
|
|
|
|
|
QSignalSpy spy(loader_.get(), SIGNAL(LoadFinished(bool)));
|
|
|
|
|
|
|
|
// Start an event loop to wait for gstreamer to do its thing
|
|
|
|
QEventLoop loop;
|
|
|
|
QObject::connect(loader_.get(), SIGNAL(LoadFinished(bool)),
|
|
|
|
&loop, SLOT(quit()));
|
2010-06-15 15:56:41 +02:00
|
|
|
loop.exec(QEventLoop::ExcludeUserInputEvents);
|
2010-06-15 15:24:17 +02:00
|
|
|
|
|
|
|
// Check the signal was emitted with Error
|
|
|
|
ASSERT_EQ(1, spy.count());
|
|
|
|
EXPECT_EQ(false, spy[0][0].toBool());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SongLoaderTest, LoadRemotePls) {
|
|
|
|
SongLoader::Result ret = loader_->Load(QString(kRemoteUrl) + "/pls_somafm.pls");
|
|
|
|
ASSERT_EQ(SongLoader::WillLoadAsync, ret);
|
|
|
|
|
|
|
|
QSignalSpy spy(loader_.get(), SIGNAL(LoadFinished(bool)));
|
|
|
|
|
|
|
|
// Start an event loop to wait for gstreamer to do its thing
|
|
|
|
QEventLoop loop;
|
|
|
|
QObject::connect(loader_.get(), SIGNAL(LoadFinished(bool)),
|
|
|
|
&loop, SLOT(quit()));
|
2010-06-15 15:56:41 +02:00
|
|
|
loop.exec(QEventLoop::ExcludeUserInputEvents);
|
2010-06-15 15:24:17 +02:00
|
|
|
|
|
|
|
// Check the signal was emitted with Success
|
|
|
|
ASSERT_EQ(1, spy.count());
|
|
|
|
EXPECT_EQ(true, spy[0][0].toBool());
|
|
|
|
|
|
|
|
// Check some metadata
|
|
|
|
ASSERT_EQ(4, loader_->songs().count());
|
|
|
|
EXPECT_EQ("SomaFM: Groove Salad (#3 128k mp3): A nicely chilled plate of ambient beats and grooves.",
|
|
|
|
loader_->songs()[2].title());
|
|
|
|
EXPECT_EQ("http://ice.somafm.com/groovesalad", loader_->songs()[3].filename());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SongLoaderTest, LoadRemotePlainText) {
|
|
|
|
SongLoader::Result ret = loader_->Load(QString(kRemoteUrl) + "/notaplaylist.txt");
|
|
|
|
ASSERT_EQ(SongLoader::WillLoadAsync, ret);
|
|
|
|
|
|
|
|
QSignalSpy spy(loader_.get(), SIGNAL(LoadFinished(bool)));
|
|
|
|
|
|
|
|
// Start an event loop to wait for gstreamer to do its thing
|
|
|
|
QEventLoop loop;
|
|
|
|
QObject::connect(loader_.get(), SIGNAL(LoadFinished(bool)),
|
|
|
|
&loop, SLOT(quit()));
|
2010-06-15 15:56:41 +02:00
|
|
|
loop.exec(QEventLoop::ExcludeUserInputEvents);
|
2010-06-15 15:24:17 +02:00
|
|
|
|
|
|
|
// Check the signal was emitted with Error
|
|
|
|
ASSERT_EQ(1, spy.count());
|
|
|
|
EXPECT_EQ(false, spy[0][0].toBool());
|
|
|
|
}
|
2010-06-15 16:24:17 +02:00
|
|
|
|
2010-06-15 16:52:42 +02:00
|
|
|
TEST_F(SongLoaderTest, LoadRemotePlainM3U) {
|
|
|
|
SongLoader::Result ret = loader_->Load(QString(kRemoteUrl) + "/plainm3u.m3u");
|
|
|
|
ASSERT_EQ(SongLoader::WillLoadAsync, ret);
|
|
|
|
|
|
|
|
QSignalSpy spy(loader_.get(), SIGNAL(LoadFinished(bool)));
|
|
|
|
|
|
|
|
// Start an event loop to wait for gstreamer to do its thing
|
|
|
|
QEventLoop loop;
|
|
|
|
QObject::connect(loader_.get(), SIGNAL(LoadFinished(bool)),
|
|
|
|
&loop, SLOT(quit()));
|
|
|
|
loop.exec(QEventLoop::ExcludeUserInputEvents);
|
|
|
|
|
|
|
|
// Check the signal was emitted with Success
|
|
|
|
ASSERT_EQ(1, spy.count());
|
|
|
|
EXPECT_EQ(true, spy[0][0].toBool());
|
|
|
|
|
|
|
|
ASSERT_EQ(2, loader_->songs().count());
|
|
|
|
EXPECT_EQ("http://www.example.com/one.mp3", loader_->songs()[0].filename());
|
|
|
|
EXPECT_EQ("http://www.example.com/two.mp3", loader_->songs()[1].filename());
|
|
|
|
}
|
|
|
|
|
2010-06-15 16:24:17 +02:00
|
|
|
TEST_F(SongLoaderTest, LoadLocalDirectory) {
|
|
|
|
// Make a directory and shove some files in it
|
2010-06-17 00:32:02 +02:00
|
|
|
QString dir;
|
|
|
|
{
|
|
|
|
QTemporaryFile ffffuuuuuuuu;
|
|
|
|
ffffuuuuuuuu.open();
|
|
|
|
dir = ffffuuuuuuuu.fileName();
|
|
|
|
}
|
2010-06-16 15:13:34 +02:00
|
|
|
|
|
|
|
QDir d;
|
2010-06-17 00:32:02 +02:00
|
|
|
ASSERT_TRUE(d.mkdir(dir));
|
2010-06-15 16:24:17 +02:00
|
|
|
|
|
|
|
QFile resource(":/testdata/beep.mp3");
|
|
|
|
resource.open(QIODevice::ReadOnly);
|
|
|
|
QByteArray data(resource.readAll());
|
|
|
|
|
|
|
|
// Write 3 MP3 files
|
|
|
|
for (int i=0 ; i<3 ; ++i) {
|
2010-06-15 20:34:01 +02:00
|
|
|
QFile mp3(QString("%1/%2.mp3").arg(dir).arg(i));
|
2010-06-15 16:24:17 +02:00
|
|
|
mp3.open(QIODevice::WriteOnly);
|
|
|
|
mp3.write(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// And one file that isn't an MP3
|
2010-06-15 20:34:01 +02:00
|
|
|
QFile somethingelse(QString(dir) + "/somethingelse.foo");
|
2010-06-15 16:24:17 +02:00
|
|
|
somethingelse.open(QIODevice::WriteOnly);
|
|
|
|
somethingelse.write("I'm not an MP3!");
|
|
|
|
somethingelse.close();
|
|
|
|
|
|
|
|
// The actual test happens in another function so we can always clean up if
|
|
|
|
// it asserts
|
2010-06-15 20:34:01 +02:00
|
|
|
LoadLocalDirectory(dir);
|
2010-06-15 16:24:17 +02:00
|
|
|
|
|
|
|
QFile::remove(QString(dir) + "/0.mp3");
|
|
|
|
QFile::remove(QString(dir) + "/1.mp3");
|
|
|
|
QFile::remove(QString(dir) + "/2.mp3");
|
|
|
|
QFile::remove(QString(dir) + "/somethingelse.foo");
|
2010-06-17 00:32:02 +02:00
|
|
|
|
|
|
|
d.rmdir(dir);
|
2010-06-15 16:24:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SongLoaderTest::LoadLocalDirectory(const QString &filename) {
|
|
|
|
// Load the directory
|
|
|
|
SongLoader::Result ret = loader_->Load(QUrl::fromLocalFile(filename));
|
|
|
|
ASSERT_EQ(SongLoader::WillLoadAsync, ret);
|
|
|
|
|
|
|
|
QSignalSpy spy(loader_.get(), SIGNAL(LoadFinished(bool)));
|
|
|
|
|
|
|
|
// Start an event loop to wait for it to read the directory
|
|
|
|
QEventLoop loop;
|
|
|
|
QObject::connect(loader_.get(), SIGNAL(LoadFinished(bool)),
|
|
|
|
&loop, SLOT(quit()));
|
|
|
|
loop.exec(QEventLoop::ExcludeUserInputEvents);
|
|
|
|
|
|
|
|
// Check the signal was emitted with Success
|
|
|
|
ASSERT_EQ(1, spy.count());
|
|
|
|
EXPECT_EQ(true, spy[0][0].toBool());
|
|
|
|
|
|
|
|
// Check it loaded three files
|
|
|
|
ASSERT_EQ(3, loader_->songs().count());
|
|
|
|
EXPECT_EQ("Beep mp3", loader_->songs()[2].title());
|
|
|
|
}
|