Commit 506a620d authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Share testing MediaLibrary overloads accross tests

parent bd055d78
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
*
* Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "MediaLibraryTester.h"
#include "Album.h"
#include "AlbumTrack.h"
#include "Artist.h"
#include "Device.h"
#include "Playlist.h"
#include "Genre.h"
#include "Media.h"
#include "Folder.h"
#include "mocks/FileSystem.h"
MediaLibraryTester::MediaLibraryTester()
: dummyDirectory( new mock::NoopDirectory )
, dummyFolder( nullptr, "./", 0, 0, false )
{
}
std::shared_ptr<Media> MediaLibraryTester::media( unsigned int id )
{
return Media::fetch( this, id );
}
MediaPtr MediaLibraryTester::media( const std::string& path )
{
auto medias = files();
for ( auto& m : medias )
{
auto files = m->files();
for ( auto& f : files )
{
if ( f->mrl() == path )
return m;
}
}
return nullptr;
}
std::shared_ptr<Folder> MediaLibraryTester::folder( const std::string& path )
{
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name +
" WHERE is_blacklisted = 0 AND is_present = 1";
auto folders = Folder::DatabaseHelpers::fetchAll<Folder>( this, req );
for ( auto &f : folders )
{
if ( f->path() == path )
return f;
}
return nullptr;
}
std::shared_ptr<Media> MediaLibraryTester::addFile( const std::string& path )
{
mock::NoopFile file( path );
return MediaLibrary::addFile( file, dummyFolder, *dummyDirectory );
}
std::shared_ptr<Playlist> MediaLibraryTester::playlist(unsigned int playlistId)
{
return Playlist::fetch( this, playlistId );
}
void MediaLibraryTester::deleteAlbum( unsigned int albumId )
{
Album::destroy( this, albumId );
}
std::shared_ptr<Genre> MediaLibraryTester::createGenre( const std::string& name )
{
return Genre::create( this, name );
}
void MediaLibraryTester::deleteGenre( unsigned int genreId )
{
Genre::destroy( this, genreId );
}
void MediaLibraryTester::deleteArtist( unsigned int artistId )
{
Artist::destroy( this, artistId );
}
std::shared_ptr<Device> MediaLibraryTester::addDevice( const std::string& uuid, bool isRemovable )
{
return Device::create( this, uuid, isRemovable );
}
void MediaLibraryTester::setFsFactory(std::shared_ptr<factory::IFileSystem> fsFactory)
{
m_fsFactory = fsFactory;
}
void MediaLibraryTester::deleteTrack(unsigned int trackId)
{
AlbumTrack::destroy( this, trackId );
}
std::shared_ptr<AlbumTrack> MediaLibraryTester::albumTrack( unsigned int id )
{
return AlbumTrack::fetch( this, id );
}
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
*
* Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#pragma once
#include "MediaLibrary.h"
#include "Folder.h"
class Playlist;
class AlbumTrack;
class MediaLibraryTester : public MediaLibrary
{
public:
MediaLibraryTester();
std::shared_ptr<Media> media( unsigned int id );
MediaPtr media( const std::string& path );
std::shared_ptr<Folder> folder( const std::string& path );
std::shared_ptr<Media> addFile( const std::string& path );
std::shared_ptr<Playlist> playlist( unsigned int playlistId );
void deleteAlbum( unsigned int albumId );
std::shared_ptr<Genre> createGenre( const std::string& name );
void deleteGenre( unsigned int genreId );
void deleteArtist( unsigned int artistId );
std::shared_ptr<Device> addDevice( const std::string& uuid, bool isRemovable );
void setFsFactory( std::shared_ptr<factory::IFileSystem> fsFactory );
void deleteTrack( unsigned int trackId );
std::shared_ptr<AlbumTrack> albumTrack( unsigned int id );
private:
std::unique_ptr<fs::IDirectory> dummyDirectory;
Folder dummyFolder;
};
class MediaLibraryWithoutParser : public MediaLibraryTester
{
virtual void startParser() override {}
};
class MediaLibraryWithoutBackground : public MediaLibraryTester
{
virtual void startDiscoverer() override {}
virtual void startParser() override {}
};
class MediaLibraryWithNotifier : public MediaLibraryTester
{
virtual void startDiscoverer() override {}
virtual void startParser() override {}
};
......@@ -42,7 +42,7 @@ void Tests::SetUp()
{
unlink("test.db");
m_cb.reset( new MockCallback );
m_ml.reset( new MediaLibrary );
m_ml.reset( new MediaLibraryTester );
if ( ExtraVerbose == true )
m_ml->setVerbosity( LogLevel::Debug );
else if ( Verbose == true )
......
......@@ -28,7 +28,7 @@
#include <mutex>
#include <rapidjson/document.h>
#include "MediaLibrary.h"
#include "common/MediaLibraryTester.h"
#include "IAlbum.h"
#include "IArtist.h"
#include "IMedia.h"
......@@ -58,7 +58,7 @@ class Tests : public ::testing::TestWithParam<const char*>
{
protected:
std::unique_ptr<MockCallback> m_cb;
std::unique_ptr<MediaLibrary> m_ml;
std::unique_ptr<MediaLibraryTester> m_ml;
virtual void SetUp() override;
......
list(APPEND SAMPLE_TEST_SRCS
samples/main.cpp
samples/Tester.cpp
common/MediaLibraryTester.cpp
)
find_package(rapidjson REQUIRED)
......
......@@ -22,22 +22,7 @@
#include "Tests.h"
#include <memory>
#include "Album.h"
#include "AlbumTrack.h"
#include "Artist.h"
#include "Device.h"
#include "File.h"
#include "filesystem/IFile.h"
#include "filesystem/IDirectory.h"
#include "utils/Filename.h"
#include "discoverer/FsDiscoverer.h"
#include "mocks/FileSystem.h"
#include "Genre.h"
#include "Media.h"
#include "Folder.h"
#include "Playlist.h"
class TestEnv : public ::testing::Environment
{
......@@ -88,92 +73,3 @@ void Tests::InstantiateMediaLibrary()
::testing::Environment* const env = ::testing::AddGlobalTestEnvironment(new TestEnv);
MediaLibraryTester::MediaLibraryTester()
: dummyDirectory( new mock::NoopDirectory )
, dummyFolder( nullptr, "./", 0, 0, false )
{
}
std::shared_ptr<Media> MediaLibraryTester::media( unsigned int id )
{
return Media::fetch( this, id );
}
MediaPtr MediaLibraryTester::media( const std::string& path )
{
auto medias = files();
for ( auto& m : medias )
{
auto files = m->files();
for ( auto& f : files )
{
if ( f->mrl() == path )
return m;
}
}
return nullptr;
}
std::shared_ptr<Folder> MediaLibraryTester::folder( const std::string& path )
{
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name +
" WHERE is_blacklisted = 0 AND is_present = 1";
auto folders = Folder::DatabaseHelpers::fetchAll<Folder>( this, req );
for ( auto &f : folders )
{
if ( f->path() == path )
return f;
}
return nullptr;
}
std::shared_ptr<Media> MediaLibraryTester::addFile( const std::string& path )
{
mock::NoopFile file( path );
return MediaLibrary::addFile( file, dummyFolder, *dummyDirectory );
}
std::shared_ptr<Playlist> MediaLibraryTester::playlist(unsigned int playlistId)
{
return Playlist::fetch( this, playlistId );
}
void MediaLibraryTester::deleteAlbum( unsigned int albumId )
{
Album::destroy( this, albumId );
}
std::shared_ptr<Genre> MediaLibraryTester::createGenre( const std::string& name )
{
return Genre::create( this, name );
}
void MediaLibraryTester::deleteGenre( unsigned int genreId )
{
Genre::destroy( this, genreId );
}
void MediaLibraryTester::deleteArtist( unsigned int artistId )
{
Artist::destroy( this, artistId );
}
std::shared_ptr<Device> MediaLibraryTester::addDevice( const std::string& uuid, bool isRemovable )
{
return Device::create( this, uuid, isRemovable );
}
void MediaLibraryTester::setFsFactory(std::shared_ptr<factory::IFileSystem> fsFactory)
{
m_fsFactory = fsFactory;
}
void MediaLibraryTester::deleteTrack(unsigned int trackId)
{
AlbumTrack::destroy( this, trackId );
}
std::shared_ptr<AlbumTrack> MediaLibraryTester::albumTrack( unsigned int id )
{
return AlbumTrack::fetch( this, id );
}
......@@ -22,53 +22,9 @@
#include "gtest/gtest.h"
class Playlist;
class AlbumTrack;
#include "factory/IFileSystem.h"
#include "MediaLibrary.h"
#include "Folder.h"
#include "mocks/NoopCallback.h"
class MediaLibraryTester : public MediaLibrary
{
public:
MediaLibraryTester();
std::shared_ptr<Media> media( unsigned int id );
MediaPtr media( const std::string& path );
std::shared_ptr<Folder> folder( const std::string& path );
std::shared_ptr<Media> addFile( const std::string& path );
std::shared_ptr<Playlist> playlist( unsigned int playlistId );
void deleteAlbum( unsigned int albumId );
std::shared_ptr<Genre> createGenre( const std::string& name );
void deleteGenre( unsigned int genreId );
void deleteArtist( unsigned int artistId );
std::shared_ptr<Device> addDevice( const std::string& uuid, bool isRemovable );
void setFsFactory( std::shared_ptr<factory::IFileSystem> fsFactory );
void deleteTrack( unsigned int trackId );
std::shared_ptr<AlbumTrack> albumTrack( unsigned int id );
private:
std::unique_ptr<fs::IDirectory> dummyDirectory;
Folder dummyFolder;
};
class MediaLibraryWithoutParser : public MediaLibraryTester
{
virtual void startParser() override {}
};
class MediaLibraryWithoutBackground : public MediaLibraryTester
{
virtual void startDiscoverer() override {}
virtual void startParser() override {}
};
class MediaLibraryWithNotifier : public MediaLibraryTester
{
virtual void startDiscoverer() override {}
virtual void startParser() override {}
};
#include "common/MediaLibraryTester.h"
class Tests : public testing::Test
{
......
......@@ -25,6 +25,8 @@ list(APPEND TEST_SRCS
mocks/filesystem/MockDevice.cpp
mocks/filesystem/MockFile.cpp
mocks/filesystem/MockDirectory.cpp
common/MediaLibraryTester.cpp
)
add_executable(unittest ${TEST_SRCS})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment