Clementine-audio-player-Mac.../tests/librarybackend_test.cpp

305 lines
8.8 KiB
C++
Raw Normal View History

/* This file is part of Clementine.
Copyright 2010, David Sansome <me@davidsansome.com>
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 "gtest/gtest.h"
#include "library/librarybackend.h"
#include "library/library.h"
#include "core/song.h"
#include "core/database.h"
#include <boost/scoped_ptr.hpp>
2011-03-13 20:22:41 +01:00
#include <QFileInfo>
2010-03-03 01:33:31 +01:00
#include <QSignalSpy>
2011-03-13 20:22:41 +01:00
#include <QThread>
#include <QtDebug>
namespace {
class LibraryBackendTest : public ::testing::Test {
protected:
virtual void SetUp() {
database_.reset(new MemoryDatabase);
2010-06-02 18:58:34 +02:00
backend_.reset(new LibraryBackend);
backend_->Init(database_, Library::kSongsTable,
Library::kDirsTable, Library::kSubdirsTable,
Library::kFtsTable);
}
Song MakeDummySong(int directory_id) {
// Returns a valid song with all the required fields set
Song ret;
ret.set_directory_id(directory_id);
ret.set_url(QUrl::fromLocalFile("foo.mp3"));
2010-06-17 18:54:23 +02:00
ret.set_mtime(1);
ret.set_ctime(1);
ret.set_filesize(1);
return ret;
}
2010-06-02 18:58:34 +02:00
boost::shared_ptr<Database> database_;
boost::scoped_ptr<LibraryBackend> backend_;
};
TEST_F(LibraryBackendTest, EmptyDatabase) {
// Check the database is empty to start with
QStringList artists = backend_->GetAllArtists();
EXPECT_TRUE(artists.isEmpty());
LibraryBackend::AlbumList albums = backend_->GetAllAlbums();
EXPECT_TRUE(albums.isEmpty());
}
2010-03-03 01:33:31 +01:00
TEST_F(LibraryBackendTest, AddDirectory) {
2010-04-04 15:17:07 +02:00
QSignalSpy spy(backend_.get(), SIGNAL(DirectoryDiscovered(Directory, SubdirectoryList)));
2010-03-03 01:33:31 +01:00
2011-03-13 19:50:32 +01:00
backend_->AddDirectory("/tmp");
2010-03-03 01:33:31 +01:00
// Check the signal was emitted correctly
ASSERT_EQ(1, spy.count());
2010-04-04 15:17:07 +02:00
Directory dir = spy[0][0].value<Directory>();
2011-03-13 20:22:41 +01:00
EXPECT_EQ(QFileInfo("/tmp").canonicalFilePath(), dir.path);
2010-04-04 15:17:07 +02:00
EXPECT_EQ(1, dir.id);
EXPECT_EQ(0, spy[0][1].value<SubdirectoryList>().size());
2010-03-03 01:33:31 +01:00
}
TEST_F(LibraryBackendTest, RemoveDirectory) {
// Add a directory
Directory dir;
dir.id = 1;
2011-03-13 19:50:32 +01:00
dir.path = "/tmp";
2010-03-03 01:33:31 +01:00
backend_->AddDirectory(dir.path);
2010-04-04 15:17:07 +02:00
QSignalSpy spy(backend_.get(), SIGNAL(DirectoryDeleted(Directory)));
2010-03-03 01:33:31 +01:00
// Remove the directory again
backend_->RemoveDirectory(dir);
// Check the signal was emitted correctly
ASSERT_EQ(1, spy.count());
2010-04-04 15:17:07 +02:00
dir = spy[0][0].value<Directory>();
2011-03-13 19:50:32 +01:00
EXPECT_EQ("/tmp", dir.path);
2010-04-04 15:17:07 +02:00
EXPECT_EQ(1, dir.id);
2010-03-03 01:33:31 +01:00
}
TEST_F(LibraryBackendTest, AddInvalidSong) {
// Adding a song without certain fields set should fail
2011-03-13 19:50:32 +01:00
backend_->AddDirectory("/tmp");
Song s;
s.set_directory_id(1);
QSignalSpy spy(database_.get(), SIGNAL(Error(QString)));
backend_->AddOrUpdateSongs(SongList() << s);
ASSERT_EQ(1, spy.count()); spy.takeFirst();
s.set_url(QUrl::fromLocalFile("foo"));
backend_->AddOrUpdateSongs(SongList() << s);
ASSERT_EQ(1, spy.count()); spy.takeFirst();
s.set_filesize(100);
backend_->AddOrUpdateSongs(SongList() << s);
ASSERT_EQ(1, spy.count()); spy.takeFirst();
s.set_mtime(100);
backend_->AddOrUpdateSongs(SongList() << s);
ASSERT_EQ(1, spy.count()); spy.takeFirst();
s.set_ctime(100);
backend_->AddOrUpdateSongs(SongList() << s);
ASSERT_EQ(0, spy.count());
}
TEST_F(LibraryBackendTest, GetAlbumArtNonExistent) {
}
2010-03-03 01:33:31 +01:00
// Test adding a single song to the database, then getting various information
// back about it.
class SingleSong : public LibraryBackendTest {
protected:
virtual void SetUp() {
LibraryBackendTest::SetUp();
// Add a directory - this will get ID 1
2011-03-13 19:50:32 +01:00
backend_->AddDirectory("/tmp");
2010-03-03 01:33:31 +01:00
// Make a song in that directory
song_ = MakeDummySong(1);
song_.set_title("Title");
song_.set_artist("Artist");
song_.set_album("Album");
2010-03-03 01:49:44 +01:00
}
2010-03-03 01:33:31 +01:00
2010-03-03 01:49:44 +01:00
void AddDummySong() {
2010-03-03 01:33:31 +01:00
QSignalSpy added_spy(backend_.get(), SIGNAL(SongsDiscovered(SongList)));
QSignalSpy deleted_spy(backend_.get(), SIGNAL(SongsDeleted(SongList)));
// Add the song
backend_->AddOrUpdateSongs(SongList() << song_);
// Check the correct signals were emitted
EXPECT_EQ(0, deleted_spy.count());
ASSERT_EQ(1, added_spy.count());
SongList list = added_spy[0][0].value<SongList>();
ASSERT_EQ(1, list.count());
EXPECT_EQ(song_.title(), list[0].title());
EXPECT_EQ(song_.artist(), list[0].artist());
EXPECT_EQ(song_.album(), list[0].album());
EXPECT_EQ(1, list[0].id());
EXPECT_EQ(1, list[0].directory_id());
}
Song song_;
};
TEST_F(SingleSong, GetSongWithNoAlbum) {
song_.set_album("");
AddDummySong(); if (HasFatalFailure()) return;
EXPECT_EQ(1, backend_->GetAllArtists().size());
LibraryBackend::AlbumList albums = backend_->GetAllAlbums();
EXPECT_EQ(1, albums.size());
EXPECT_EQ("Artist", albums[0].artist);
EXPECT_EQ("", albums[0].album_name);
}
2010-03-03 01:33:31 +01:00
TEST_F(SingleSong, GetAllArtists) {
2010-03-03 19:38:20 +01:00
AddDummySong(); if (HasFatalFailure()) return;
2010-03-03 01:49:44 +01:00
QStringList artists = backend_->GetAllArtists();
ASSERT_EQ(1, artists.size());
2010-03-03 01:33:31 +01:00
EXPECT_EQ(song_.artist(), artists[0]);
}
2010-03-03 01:33:31 +01:00
TEST_F(SingleSong, GetAllAlbums) {
2010-03-03 19:38:20 +01:00
AddDummySong(); if (HasFatalFailure()) return;
2010-03-03 01:49:44 +01:00
LibraryBackend::AlbumList albums = backend_->GetAllAlbums();
ASSERT_EQ(1, albums.size());
2010-03-03 01:33:31 +01:00
EXPECT_EQ(song_.album(), albums[0].album_name);
EXPECT_EQ(song_.artist(), albums[0].artist);
}
2010-03-03 01:33:31 +01:00
TEST_F(SingleSong, GetAlbumsByArtist) {
2010-03-03 19:38:20 +01:00
AddDummySong(); if (HasFatalFailure()) return;
2010-03-03 01:49:44 +01:00
2010-03-03 01:33:31 +01:00
LibraryBackend::AlbumList albums = backend_->GetAlbumsByArtist("Artist");
ASSERT_EQ(1, albums.size());
2010-03-03 01:33:31 +01:00
EXPECT_EQ(song_.album(), albums[0].album_name);
EXPECT_EQ(song_.artist(), albums[0].artist);
}
2010-03-03 01:33:31 +01:00
TEST_F(SingleSong, GetAlbumArt) {
2010-03-03 19:38:20 +01:00
AddDummySong(); if (HasFatalFailure()) return;
2010-03-03 01:49:44 +01:00
2010-03-03 01:33:31 +01:00
LibraryBackend::Album album = backend_->GetAlbumArt("Artist", "Album");
EXPECT_EQ(song_.album(), album.album_name);
EXPECT_EQ(song_.artist(), album.artist);
}
2010-03-03 01:33:31 +01:00
TEST_F(SingleSong, GetSongs) {
2010-03-03 19:38:20 +01:00
AddDummySong(); if (HasFatalFailure()) return;
2010-03-03 01:49:44 +01:00
2010-03-03 01:33:31 +01:00
SongList songs = backend_->GetSongs("Artist", "Album");
ASSERT_EQ(1, songs.size());
2010-03-03 01:33:31 +01:00
EXPECT_EQ(song_.album(), songs[0].album());
EXPECT_EQ(song_.artist(), songs[0].artist());
EXPECT_EQ(song_.title(), songs[0].title());
EXPECT_EQ(1, songs[0].id());
2010-03-03 01:33:31 +01:00
}
TEST_F(SingleSong, GetSongById) {
2010-03-03 19:38:20 +01:00
AddDummySong(); if (HasFatalFailure()) return;
2010-03-03 01:49:44 +01:00
2010-03-03 01:33:31 +01:00
Song song = backend_->GetSongById(1);
EXPECT_EQ(song_.album(), song.album());
EXPECT_EQ(song_.artist(), song.artist());
EXPECT_EQ(song_.title(), song.title());
EXPECT_EQ(1, song.id());
}
2010-03-03 01:33:31 +01:00
TEST_F(SingleSong, FindSongsInDirectory) {
2010-03-03 19:38:20 +01:00
AddDummySong(); if (HasFatalFailure()) return;
2010-03-03 01:49:44 +01:00
2010-03-03 01:33:31 +01:00
SongList songs = backend_->FindSongsInDirectory(1);
ASSERT_EQ(1, songs.size());
EXPECT_EQ(song_.album(), songs[0].album());
EXPECT_EQ(song_.artist(), songs[0].artist());
EXPECT_EQ(song_.title(), songs[0].title());
EXPECT_EQ(1, songs[0].id());
}
2010-03-03 19:38:20 +01:00
TEST_F(SingleSong, UpdateSong) {
AddDummySong(); if (HasFatalFailure()) return;
Song new_song(song_);
new_song.set_id(1);
new_song.set_title("A different title");
QSignalSpy deleted_spy(backend_.get(), SIGNAL(SongsDeleted(SongList)));
QSignalSpy added_spy(backend_.get(), SIGNAL(SongsDiscovered(SongList)));
backend_->AddOrUpdateSongs(SongList() << new_song);
ASSERT_EQ(1, added_spy.size());
ASSERT_EQ(1, deleted_spy.size());
SongList songs_added = added_spy[0][0].value<SongList>();
SongList songs_deleted = deleted_spy[0][0].value<SongList>();
ASSERT_EQ(1, songs_added.size());
ASSERT_EQ(1, songs_deleted.size());
EXPECT_EQ("Title", songs_deleted[0].title());
EXPECT_EQ("A different title", songs_added[0].title());
EXPECT_EQ(1, songs_deleted[0].id());
EXPECT_EQ(1, songs_added[0].id());
}
TEST_F(SingleSong, DeleteSongs) {
AddDummySong(); if (HasFatalFailure()) return;
Song new_song(song_);
new_song.set_id(1);
QSignalSpy deleted_spy(backend_.get(), SIGNAL(SongsDeleted(SongList)));
backend_->DeleteSongs(SongList() << new_song);
ASSERT_EQ(1, deleted_spy.size());
SongList songs_deleted = deleted_spy[0][0].value<SongList>();
ASSERT_EQ(1, songs_deleted.size());
EXPECT_EQ("Title", songs_deleted[0].title());
EXPECT_EQ(1, songs_deleted[0].id());
2011-05-18 17:05:19 +02:00
// Check the song is marked as deleted.
2010-03-03 19:38:20 +01:00
Song song = backend_->GetSongById(1);
2011-05-18 17:05:19 +02:00
EXPECT_TRUE(song.is_valid());
EXPECT_TRUE(song.is_unavailable());
2010-03-03 19:38:20 +01:00
// And the artist or album shouldn't show up either
QStringList artists = backend_->GetAllArtists();
EXPECT_EQ(0, artists.size());
LibraryBackend::AlbumList albums = backend_->GetAllAlbums();
EXPECT_EQ(0, albums.size());
}
} // namespace