Fix test and fix song loader infinite loop.

This commit is contained in:
John Maguire 2010-08-31 21:24:57 +00:00
parent a05cd6d623
commit 214d77bc0e
10 changed files with 121 additions and 21 deletions

View File

@ -34,7 +34,7 @@
QSet<QString> SongLoader::sRawUriSchemes;
const int SongLoader::kDefaultTimeout = 5000;
SongLoader::SongLoader(LibraryBackend* library, QObject *parent)
SongLoader::SongLoader(LibraryBackendInterface* library, QObject *parent)
: QObject(parent),
timeout_timer_(new QTimer(this)),
playlist_parser_(new PlaylistParser(this)),
@ -173,7 +173,7 @@ void SongLoader::LoadLocalDirectory(const QString& filename) {
while (it.hasNext()) {
// This is in another thread so we can do blocking calls.
LoadLocal(filename, true);
LoadLocal(it.next(), true);
}
qStableSort(songs_.begin(), songs_.end(), CompareSongs);

View File

@ -29,14 +29,14 @@
# include <gst/gst.h>
#endif
class LibraryBackend;
class LibraryBackendInterface;
class ParserBase;
class PlaylistParser;
class SongLoader : public QObject {
Q_OBJECT
public:
SongLoader(LibraryBackend* library, QObject* parent = 0);
SongLoader(LibraryBackendInterface* library, QObject* parent = 0);
~SongLoader();
enum Result {
@ -107,7 +107,7 @@ private:
ParserBase* parser_;
QString mime_type_;
QByteArray buffer_;
LibraryBackend* library_;
LibraryBackendInterface* library_;
#ifdef HAVE_GSTREAMER
boost::shared_ptr<GstElement> pipeline_;

View File

@ -27,7 +27,7 @@
#include <QCoreApplication>
LibraryBackend::LibraryBackend(QObject *parent)
: QObject(parent)
: LibraryBackendInterface(parent)
{
}

View File

@ -28,16 +28,10 @@
class Database;
class LibraryBackend : public QObject {
Q_OBJECT
class LibraryBackendInterface : public QObject {
public:
Q_INVOKABLE LibraryBackend(QObject* parent = 0);
void Init(boost::shared_ptr<Database> db, const QString& songs_table,
const QString& dirs_table, const QString& subdirs_table,
const QString& fts_table);
boost::shared_ptr<Database> db() const { return db_; }
LibraryBackendInterface(QObject* parent = 0) : QObject(parent) {}
virtual ~LibraryBackendInterface() {}
struct Album {
Album() {}
@ -57,6 +51,51 @@ class LibraryBackend : public QObject {
};
typedef QList<Album> AlbumList;
// Get a list of directories in the library. Emits DirectoriesDiscovered.
virtual void LoadDirectoriesAsync() = 0;
// Counts the songs in the library. Emits TotalSongCountUpdated
virtual void UpdateTotalSongCountAsync() = 0;
virtual SongList FindSongsInDirectory(int id) = 0;
virtual SubdirectoryList SubdirsInDirectory(int id) = 0;
virtual DirectoryList GetAllDirectories() = 0;
virtual void ChangeDirPath(int id, const QString& new_path) = 0;
virtual QStringList GetAllArtists(const QueryOptions& opt = QueryOptions()) = 0;
virtual QStringList GetAllArtistsWithAlbums(const QueryOptions& opt = QueryOptions()) = 0;
virtual SongList GetSongs(
const QString& artist, const QString& album, const QueryOptions& opt = QueryOptions()) = 0;
virtual bool HasCompilations(const QueryOptions& opt = QueryOptions()) = 0;
virtual SongList GetCompilationSongs(const QString& album, const QueryOptions& opt = QueryOptions()) = 0;
virtual AlbumList GetAllAlbums(const QueryOptions& opt = QueryOptions()) = 0;
virtual AlbumList GetAlbumsByArtist(const QString& artist, const QueryOptions& opt = QueryOptions()) = 0;
virtual AlbumList GetCompilationAlbums(const QueryOptions& opt = QueryOptions()) = 0;
virtual void UpdateManualAlbumArtAsync(const QString& artist, const QString& album, const QString& art) = 0;
virtual Album GetAlbumArt(const QString& artist, const QString& album) = 0;
virtual Song GetSongById(int id) = 0;
virtual void AddDirectory(const QString& path) = 0;
virtual void RemoveDirectory(const Directory& dir) = 0;
virtual bool ExecQuery(LibraryQuery* q) = 0;
};
class LibraryBackend : public LibraryBackendInterface {
Q_OBJECT
public:
Q_INVOKABLE LibraryBackend(QObject* parent = 0);
void Init(boost::shared_ptr<Database> db, const QString& songs_table,
const QString& dirs_table, const QString& subdirs_table,
const QString& fts_table);
boost::shared_ptr<Database> db() const { return db_; }
QString songs_table() const { return songs_table_; }
QString dirs_table() const { return dirs_table_; }
QString subdirs_table() const { return subdirs_table_; }

View File

@ -20,7 +20,7 @@
#include "core/taskmanager.h"
SongLoaderInserter::SongLoaderInserter(
TaskManager* task_manager, LibraryBackend* library, QObject *parent)
TaskManager* task_manager, LibraryBackendInterface* library, QObject *parent)
: QObject(parent),
task_manager_(task_manager),
destination_(NULL),

View File

@ -23,7 +23,7 @@
#include "core/song.h"
class LibraryBackend;
class LibraryBackendInterface;
class Playlist;
class SongLoader;
class TaskManager;
@ -33,7 +33,7 @@ class QModelIndex;
class SongLoaderInserter : public QObject {
Q_OBJECT
public:
SongLoaderInserter(TaskManager* task_manager, LibraryBackend* library, QObject* parent = 0);
SongLoaderInserter(TaskManager* task_manager, LibraryBackendInterface* library, QObject* parent = 0);
~SongLoaderInserter();
void Load(Playlist* destination, int row, bool play_now, const QList<QUrl>& urls);
@ -60,7 +60,7 @@ private:
QSet<SongLoader*> pending_;
int async_load_id_;
int async_progress_;
LibraryBackend* library_;
LibraryBackendInterface* library_;
};
#endif // SONGLOADERINSERTER_H

View File

@ -30,6 +30,7 @@
#include "devices/devicestatefiltermodel.h"
#include "engines/enginebase.h"
#include "library/groupbydialog.h"
#include "library/librarybackend.h"
#include "library/libraryconfig.h"
#include "library/librarydirectorymodel.h"
#include "library/library.h"

View File

@ -0,0 +1,43 @@
#ifndef MOCKLIBRARYBACKEND_H
#define MOCKLIBRARYBACKEND_H
#include <gmock/gmock.h>
#include "library/librarybackend.h"
class MockLibraryBackend : public LibraryBackendInterface {
public:
// Get a list of directories in the library. Emits DirectoriesDiscovered.
MOCK_METHOD0(LoadDirectoriesAsync, void());
// Counts the songs in the library. Emits TotalSongCountUpdated
MOCK_METHOD0(UpdateTotalSongCountAsync, void());
MOCK_METHOD1(FindSongsInDirectory, SongList(int));
MOCK_METHOD1(SubdirsInDirectory, SubdirectoryList(int));
MOCK_METHOD0(GetAllDirectories, DirectoryList());
MOCK_METHOD2(ChangeDirPath, void(int, const QString&));
MOCK_METHOD1(GetAllArtists, QStringList(const QueryOptions&));
MOCK_METHOD1(GetAllArtistsWithAlbums, QStringList(const QueryOptions&));
MOCK_METHOD3(GetSongs, SongList(const QString&, const QString&, const QueryOptions&));
MOCK_METHOD1(HasCompilations, bool(const QueryOptions&));
MOCK_METHOD2(GetCompilationSongs, SongList(const QString&, const QueryOptions&));
MOCK_METHOD1(GetAllAlbums, AlbumList(const QueryOptions&));
MOCK_METHOD2(GetAlbumsByArtist, AlbumList(const QString&, const QueryOptions&));
MOCK_METHOD1(GetCompilationAlbums, AlbumList(const QueryOptions&));
MOCK_METHOD3(UpdateManualAlbumArtAsync, void(const QString&, const QString&, const QString&));
MOCK_METHOD2(GetAlbumArt, Album(const QString&, const QString&));
MOCK_METHOD1(GetSongById, Song(int));
MOCK_METHOD1(AddDirectory, void(const QString&));
MOCK_METHOD1(RemoveDirectory, void(const Directory&));
MOCK_METHOD1(ExecQuery, bool(LibraryQuery*));
};
#endif

View File

@ -33,7 +33,7 @@ namespace {
class PlaylistTest : public ::testing::Test {
protected:
PlaylistTest()
: playlist_(NULL, NULL, NULL),
: playlist_(NULL, NULL, NULL, NULL),
sequence_(NULL, new DummySettingsProvider)
{
}

View File

@ -17,6 +17,7 @@
#include "test_utils.h"
#include "gmock/gmock-matchers.h"
#include "gtest/gtest.h"
#include "mock_librarybackend.h"
#include "core/songloader.h"
#include "engines/gstengine.h"
@ -30,6 +31,9 @@
#include <boost/scoped_ptr.hpp>
#include <cstdlib>
using ::testing::_;
using ::testing::Return;
class SongLoaderTest : public ::testing::Test {
public:
static void SetUpTestCase() {
@ -45,7 +49,8 @@ public:
protected:
void SetUp() {
loader_.reset(new SongLoader);
library_.reset(new MockLibraryBackend);
loader_.reset(new SongLoader(library_.get()));
loader_->set_timeout(20000);
}
@ -55,6 +60,7 @@ protected:
static GstEngine* sGstEngine;
boost::scoped_ptr<SongLoader> loader_;
boost::scoped_ptr<MockLibraryBackend> library_;
};
const char* SongLoaderTest::kRemoteUrl = "http://remotetestdata.clementine-player.org";
@ -70,6 +76,17 @@ TEST_F(SongLoaderTest, LoadLocalMp3) {
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());
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()));