Some more library model tests

This commit is contained in:
David Sansome 2010-03-23 21:52:42 +00:00
parent dfc972be63
commit ea83789f17
5 changed files with 101 additions and 13 deletions

View File

@ -22,6 +22,12 @@ class LibraryBackendInterface : public QObject {
LibraryBackendInterface(QObject* parent = 0); LibraryBackendInterface(QObject* parent = 0);
struct Album { struct Album {
Album() {}
Album(const QString& _artist, const QString& _album_name,
const QString& _art_automatic, const QString& _art_manual)
: artist(_artist), album_name(_album_name),
art_automatic(_art_automatic), art_manual(_art_manual) {}
QString artist; QString artist;
QString album_name; QString album_name;

View File

@ -32,6 +32,7 @@ set(TESTUTILS-SOURCES
test_utils.cpp test_utils.cpp
mock_networkaccessmanager.cpp mock_networkaccessmanager.cpp
mock_taglib.cpp mock_taglib.cpp
mock_librarybackend.cpp
) )
set(TESTUTILS-MOC-HEADERS set(TESTUTILS-MOC-HEADERS

View File

@ -7,19 +7,26 @@
#include "mock_backgroundthread.h" #include "mock_backgroundthread.h"
#include "mock_librarybackend.h" #include "mock_librarybackend.h"
#include <boost/scoped_ptr.hpp>
#include <QtDebug> #include <QtDebug>
#include <QThread> #include <QThread>
#include <QSignalSpy> #include <QSignalSpy>
#include <QSortFilterProxyModel>
using ::testing::_; using ::testing::_;
using ::testing::Return; using ::testing::Return;
using ::testing::StrictMock;
void PrintTo(const ::QString& str, std::ostream& os) {
os << str.toStdString();
}
namespace {
class LibraryTest : public ::testing::Test { class LibraryTest : public ::testing::Test {
protected: protected:
virtual void SetUp() { void SetUp() {
library_.reset(new Library(NULL, NULL)); library_ = new StrictMock<Library>(
static_cast<EngineBase*>(NULL), static_cast<QObject*>(NULL));
library_->set_backend_factory( library_->set_backend_factory(
new FakeBackgroundThreadFactory<LibraryBackendInterface, MockLibraryBackend>); new FakeBackgroundThreadFactory<LibraryBackendInterface, MockLibraryBackend>);
library_->set_watcher_factory( library_->set_watcher_factory(
@ -28,19 +35,71 @@ class LibraryTest : public ::testing::Test {
library_->Init(); library_->Init();
backend_ = static_cast<MockLibraryBackend*>(library_->GetBackend().get()); backend_ = static_cast<MockLibraryBackend*>(library_->GetBackend().get());
library_sorted_ = new QSortFilterProxyModel;
library_sorted_->setSourceModel(library_);
library_sorted_->setSortRole(Library::Role_SortText);
library_sorted_->setDynamicSortFilter(true);
library_sorted_->sort(0);
} }
boost::scoped_ptr<Library> library_; void TearDown() {
EXPECT_CALL(*backend_, Die());
delete library_;
delete library_sorted_;
}
Library* library_;
MockLibraryBackend* backend_; MockLibraryBackend* backend_;
QSortFilterProxyModel* library_sorted_;
}; };
TEST_F(LibraryTest, TestInitialisation) { TEST_F(LibraryTest, Initialisation) {
EXPECT_CALL(*backend_, LoadDirectoriesAsync()); backend_->ExpectSetup();
EXPECT_CALL(*backend_, UpdateTotalSongCountAsync());
EXPECT_CALL(*backend_, HasCompilations(_))
.WillOnce(Return(false));
EXPECT_CALL(*backend_, GetAllArtists(_))
.WillOnce(Return(QStringList()));
library_->StartThreads(); library_->StartThreads();
EXPECT_EQ(0, library_->rowCount(QModelIndex()));
} }
TEST_F(LibraryTest, WithInitialCompilations) {
backend_->ExpectSetup(true);
library_->StartThreads();
ASSERT_EQ(1, library_->rowCount(QModelIndex()));
QModelIndex va_index = library_->index(0, 0, QModelIndex());
EXPECT_EQ("Various Artists", va_index.data().toString());
EXPECT_TRUE(library_->hasChildren(va_index));
}
TEST_F(LibraryTest, WithInitialArtists) {
backend_->ExpectSetup(false, QStringList() << "Artist 1" << "Artist 2" << "Foo");
library_->StartThreads();
ASSERT_EQ(5, library_sorted_->rowCount(QModelIndex()));
EXPECT_EQ("a", library_sorted_->index(0, 0, QModelIndex()).data().toString());
EXPECT_EQ("Artist 1", library_sorted_->index(1, 0, QModelIndex()).data().toString());
EXPECT_EQ("Artist 2", library_sorted_->index(2, 0, QModelIndex()).data().toString());
EXPECT_EQ("f", library_sorted_->index(3, 0, QModelIndex()).data().toString());
EXPECT_EQ("Foo", library_sorted_->index(4, 0, QModelIndex()).data().toString());
}
TEST_F(LibraryTest, CompilationAlbums) {
backend_->ExpectSetup(true);
library_->StartThreads();
QModelIndex va_index = library_->index(0, 0, QModelIndex());
LibraryBackendInterface::AlbumList albums;
albums << LibraryBackendInterface::Album("Artist", "Album", "", "");
EXPECT_CALL(*backend_, GetCompilationAlbums(_))
.WillOnce(Return(albums));
ASSERT_EQ(library_->rowCount(va_index), 1);
QModelIndex album_index = library_->index(0, 0, va_index);
EXPECT_EQ(library_->data(album_index).toString(), "Album");
EXPECT_TRUE(library_->hasChildren(album_index));
}
} // namespace

View File

@ -0,0 +1,14 @@
#include "mock_librarybackend.h"
using ::testing::_;
using ::testing::Return;
void MockLibraryBackend::ExpectSetup(bool has_compilations,
const QStringList& artists) {
EXPECT_CALL(*this, LoadDirectoriesAsync());
EXPECT_CALL(*this, UpdateTotalSongCountAsync());
EXPECT_CALL(*this, HasCompilations(_))
.WillOnce(Return(has_compilations));
EXPECT_CALL(*this, GetAllArtists(_))
.WillOnce(Return(artists));
}

View File

@ -3,8 +3,13 @@
#include "librarybackend.h" #include "librarybackend.h"
#include <gmock/gmock.h>
class MockLibraryBackend : public LibraryBackendInterface { class MockLibraryBackend : public LibraryBackendInterface {
public: public:
~MockLibraryBackend() { Die(); }
MOCK_METHOD0(Die, void());
MOCK_METHOD0(LoadDirectoriesAsync, MOCK_METHOD0(LoadDirectoriesAsync,
void()); void());
MOCK_METHOD0(UpdateTotalSongCountAsync, MOCK_METHOD0(UpdateTotalSongCountAsync,
@ -53,6 +58,9 @@ class MockLibraryBackend : public LibraryBackendInterface {
void(const QString& artist, const QString& album, const QString& art)); void(const QString& artist, const QString& album, const QString& art));
MOCK_METHOD3(ForceCompilation, MOCK_METHOD3(ForceCompilation,
void(const QString& artist, const QString& album, bool on)); void(const QString& artist, const QString& album, bool on));
void ExpectSetup(bool has_compilations = false,
const QStringList& artists = QStringList());
}; };
#endif #endif