From ea83789f1747b512a6ab6e7cd7a5867b899b8433 Mon Sep 17 00:00:00 2001 From: David Sansome Date: Tue, 23 Mar 2010 21:52:42 +0000 Subject: [PATCH] Some more library model tests --- src/librarybackend.h | 6 +++ tests/CMakeLists.txt | 1 + tests/library_test.cpp | 85 +++++++++++++++++++++++++++++------ tests/mock_librarybackend.cpp | 14 ++++++ tests/mock_librarybackend.h | 8 ++++ 5 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 tests/mock_librarybackend.cpp diff --git a/src/librarybackend.h b/src/librarybackend.h index ab9312302..978526cb7 100644 --- a/src/librarybackend.h +++ b/src/librarybackend.h @@ -22,6 +22,12 @@ class LibraryBackendInterface : public QObject { LibraryBackendInterface(QObject* parent = 0); 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 album_name; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9ced4ca02..ace7a3c1e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -32,6 +32,7 @@ set(TESTUTILS-SOURCES test_utils.cpp mock_networkaccessmanager.cpp mock_taglib.cpp + mock_librarybackend.cpp ) set(TESTUTILS-MOC-HEADERS diff --git a/tests/library_test.cpp b/tests/library_test.cpp index 86b7198db..6a6522c4e 100644 --- a/tests/library_test.cpp +++ b/tests/library_test.cpp @@ -7,19 +7,26 @@ #include "mock_backgroundthread.h" #include "mock_librarybackend.h" -#include - #include #include #include +#include using ::testing::_; using ::testing::Return; +using ::testing::StrictMock; + +void PrintTo(const ::QString& str, std::ostream& os) { + os << str.toStdString(); +} + +namespace { class LibraryTest : public ::testing::Test { protected: - virtual void SetUp() { - library_.reset(new Library(NULL, NULL)); + void SetUp() { + library_ = new StrictMock( + static_cast(NULL), static_cast(NULL)); library_->set_backend_factory( new FakeBackgroundThreadFactory); library_->set_watcher_factory( @@ -28,19 +35,71 @@ class LibraryTest : public ::testing::Test { library_->Init(); backend_ = static_cast(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_; + void TearDown() { + EXPECT_CALL(*backend_, Die()); + delete library_; + delete library_sorted_; + } + + Library* library_; MockLibraryBackend* backend_; + QSortFilterProxyModel* library_sorted_; }; -TEST_F(LibraryTest, TestInitialisation) { - EXPECT_CALL(*backend_, LoadDirectoriesAsync()); - EXPECT_CALL(*backend_, UpdateTotalSongCountAsync()); - EXPECT_CALL(*backend_, HasCompilations(_)) - .WillOnce(Return(false)); - EXPECT_CALL(*backend_, GetAllArtists(_)) - .WillOnce(Return(QStringList())); - +TEST_F(LibraryTest, Initialisation) { + backend_->ExpectSetup(); 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 diff --git a/tests/mock_librarybackend.cpp b/tests/mock_librarybackend.cpp new file mode 100644 index 000000000..effa3f3f7 --- /dev/null +++ b/tests/mock_librarybackend.cpp @@ -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)); +} diff --git a/tests/mock_librarybackend.h b/tests/mock_librarybackend.h index 41bfa8207..587f5de16 100644 --- a/tests/mock_librarybackend.h +++ b/tests/mock_librarybackend.h @@ -3,8 +3,13 @@ #include "librarybackend.h" +#include + class MockLibraryBackend : public LibraryBackendInterface { public: + ~MockLibraryBackend() { Die(); } + MOCK_METHOD0(Die, void()); + MOCK_METHOD0(LoadDirectoriesAsync, void()); MOCK_METHOD0(UpdateTotalSongCountAsync, @@ -53,6 +58,9 @@ class MockLibraryBackend : public LibraryBackendInterface { void(const QString& artist, const QString& album, const QString& art)); MOCK_METHOD3(ForceCompilation, void(const QString& artist, const QString& album, bool on)); + + void ExpectSetup(bool has_compilations = false, + const QStringList& artists = QStringList()); }; #endif