Skip to content
This repository has been archived by the owner on May 3, 2019. It is now read-only.

Commit

Permalink
#69 Dependency injection of IUserScriptFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinDuquesnoy committed Sep 24, 2017
1 parent d085981 commit daf2385
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ using namespace MellowPlayer::Application;
using namespace MellowPlayer::Application;
using namespace MellowPlayer::Presentation;

StreamingServiceViewModel::StreamingServiceViewModel(StreamingService& streamingService, ISettingsProvider& settings, Players& players,
QObject* parent)
: QObject(parent), streamingService_(streamingService), settingsProvider_(settings), player_(players.get(streamingService.name()))
{
StreamingServiceViewModel::StreamingServiceViewModel(StreamingService& streamingService,
ISettingsProvider& settings,
IUserScriptFactory& factory,
Players& players,
QObject* parent) :
QObject(parent),
streamingService_(streamingService),
settingsProvider_(settings),
player_(players.get(streamingService.name())),
userScriptsViewModel_(streamingService.name(), factory, settings, this)
{

}

QString StreamingServiceViewModel::logo() const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

#include <QtCore/QObject>
#include <memory>
#include <MellowPlayer/Presentation/ViewModels/UserScripts/UserScriptsViewModel.hpp>


namespace MellowPlayer::Application
{
class StreamingService;
class ISettingsProvider;
class IUserScriptFactory;
class StreamingService;
class Player;
class Players;
}
Expand All @@ -27,7 +30,9 @@ namespace MellowPlayer::Presentation
Q_PROPERTY(int sortOrder READ sortOrder WRITE setSortOrder NOTIFY sortOrderChanged)
Q_PROPERTY(bool isRunning READ isRunning NOTIFY isRunningChanged)
public:
StreamingServiceViewModel(Application::StreamingService& streamingService, Application::ISettingsProvider& settings,
StreamingServiceViewModel(Application::StreamingService& streamingService,
Application::ISettingsProvider& settings,
Application::IUserScriptFactory& userScriptFactory,
Application::Players& players, QObject* parent = nullptr);

QString logo() const;
Expand Down Expand Up @@ -67,5 +72,6 @@ namespace MellowPlayer::Presentation
Application::StreamingService& streamingService_;
Application::ISettingsProvider& settingsProvider_;
std::shared_ptr<Application::Player> player_;
UserScriptsViewModel userScriptsViewModel_;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,31 @@
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QtWebEngine/QtWebEngine>
#include <sys/user.h>

using namespace MellowPlayer::Application;
using namespace MellowPlayer::Application;
using namespace MellowPlayer::Presentation;
using namespace MellowPlayer::Infrastructure;

StreamingServicesControllerViewModel::StreamingServicesControllerViewModel(StreamingServicesController& streamingServices, Players& players,
Settings& settings, IWorkDispatcher& workDispatcher,
StreamingServicesControllerViewModel::StreamingServicesControllerViewModel(StreamingServicesController& streamingServices,
Players& players,
Settings& settings,
IWorkDispatcher& workDispatcher,
IStreamingServiceCreator& streamingServiceCreator,
ICommandLineParser& commandLineParser)
: QObject(),
streamingServices_(streamingServices),
players_(players),
settings_(settings),
currentServiceSetting_(settings.get(SettingKey::PRIVATE_CURRENT_SERVICE)),
workDispatcher_(workDispatcher),
streamingServiceCreator_(streamingServiceCreator),
commandLineParser_(commandLineParser),
allServices_(new StreamingServiceListModel(this, QByteArray(), "name")),
enabledServices_(allServices_)
ICommandLineParser& commandLineParser,
IUserScriptFactory& userScriptFactory) :
QObject(),
streamingServices_(streamingServices),
players_(players),
settings_(settings),
currentServiceSetting_(settings.get(SettingKey::PRIVATE_CURRENT_SERVICE)),
workDispatcher_(workDispatcher),
streamingServiceCreator_(streamingServiceCreator),
commandLineParser_(commandLineParser),
userScriptFactory_(userScriptFactory),
allServices_(new StreamingServiceListModel(this, QByteArray(), "name")),
enabledServices_(allServices_)
{

connect(&streamingServices, &StreamingServicesController::added, this, &StreamingServicesControllerViewModel::onServiceAdded);
Expand Down Expand Up @@ -102,7 +107,11 @@ void StreamingServicesControllerViewModel::reload()

void StreamingServicesControllerViewModel::onServiceAdded(StreamingService* streamingService)
{
auto* sv = new StreamingServiceViewModel(*streamingService, settings_.settingsProvider(), players_, this);
auto* sv = new StreamingServiceViewModel(*streamingService,
settings_.settingsProvider(),
userScriptFactory_,
players_,
this);
Player* player = sv->player();
connect(player, &Player::isRunningChanged, this, &StreamingServicesControllerViewModel::onPlayerRunningChanged);
connect(sv, &StreamingServiceViewModel::isEnabledChanged, this, &StreamingServicesControllerViewModel::onServiceEnabledChanged);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace MellowPlayer::Application
class IWorkDispatcher;
class IStreamingServiceCreator;
class ICommandLineParser;
class IUserScriptFactory;
}

namespace MellowPlayer::Presentation
Expand All @@ -29,10 +30,13 @@ namespace MellowPlayer::Presentation
Q_PROPERTY(int currentIndex READ currentIndex NOTIFY currentIndexChanged)
Q_PROPERTY(bool isCurrentServiceRunning READ isCurrentServiceRunning NOTIFY isCurrentServiceRunningChanged)
public:
StreamingServicesControllerViewModel(Application::StreamingServicesController& streamingServices, Application::Players& players,
Application::Settings& settings, Application::IWorkDispatcher& workDispatcher,
StreamingServicesControllerViewModel(Application::StreamingServicesController& streamingServices,
Application::Players& players,
Application::Settings& settings,
Application::IWorkDispatcher& workDispatcher,
Application::IStreamingServiceCreator& streamingServiceCreator,
Application::ICommandLineParser& commandLineParser);
Application::ICommandLineParser& commandLineParser,
Application::IUserScriptFactory& userScriptFactory);
void initialize();

Q_INVOKABLE void reload();
Expand Down Expand Up @@ -77,6 +81,7 @@ namespace MellowPlayer::Presentation
Application::IWorkDispatcher& workDispatcher_;
Application::IStreamingServiceCreator& streamingServiceCreator_;
Application::ICommandLineParser& commandLineParser_;
Application::IUserScriptFactory& userScriptFactory_;
StreamingServiceListModel* allServices_;
StreamingServiceProxyListModel enabledServices_;
StreamingServiceViewModel* currentService_ = nullptr;
Expand Down
6 changes: 5 additions & 1 deletion src/DI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <MellowPlayer/Application/Updater/IHttpClient.hpp>
#include <MellowPlayer/Application/Updater/ILatestReleaseQuerier.hpp>
#include <MellowPlayer/Application/Updater/Updater.hpp>
#include <MellowPlayer/Application/UserScripts/IUserScriptFactory.hpp>
#include <MellowPlayer/Application/UserScripts/IUserScript.hpp>
#include <MellowPlayer/Infrastructure/AlbumArt/AlbumArtDownloader.hpp>
#include <MellowPlayer/Infrastructure/Applications/CoreApplication.hpp>
#include <MellowPlayer/Infrastructure/Applications/IApplication.hpp>
Expand All @@ -38,6 +40,7 @@
#include <MellowPlayer/Infrastructure/StreamingServices/StreamingServiceWatcher.hpp>
#include <MellowPlayer/Infrastructure/Theme/ThemeLoader.hpp>
#include <MellowPlayer/Infrastructure/Updater/HttpClient.hpp>
#include <MellowPlayer/Infrastructure/UserScripts/UserScriptFactory.hpp>
#include <MellowPlayer/Presentation/Notifications/Notifier.hpp>
#include <MellowPlayer/Presentation/Notifications/Presenters/SystemTrayIconPresenter.hpp>
#include <MellowPlayer/Presentation/Notifications/SystemTrayIcon.hpp>
Expand Down Expand Up @@ -139,7 +142,8 @@ auto defaultInjector = [](ScopedScope &scope) {
di::bind<IThemeLoader>().to<ThemeLoader>().in(scope),
di::bind<ILatestReleaseQuerier>().to<LatestGithubReleaseQuerier>().in(scope),
di::bind<IHttpClient>().to<HttpClient>().in(scope),
di::bind<IFileDownloader>().to<FileDownloader>().in(scope)
di::bind<IFileDownloader>().to<FileDownloader>().in(scope),
di::bind<IUserScriptFactory>().to<UserScriptFactory>().in(scope)
);
};

Expand Down
19 changes: 18 additions & 1 deletion tests/Lib/Utils/DependencyPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "MellowPlayer/Application/Settings/ISettingsProvider.hpp"
#include "MellowPlayer/Application/StreamingServices/IStreamingServiceCreator.hpp"
#include "MellowPlayer/Application/Updater/AbstractPlatformUpdater.hpp"
#include "MellowPlayer/Application/UserScripts/IUserScriptFactory.hpp"

#include <MellowPlayer/Infrastructure/Services/LocalAlbumArt.hpp>
#include <MellowPlayer/Infrastructure/Settings/SettingsSchemaLoader.hpp>
Expand All @@ -38,10 +39,12 @@
#include <Mocks/StreamingServiceLoaderMock.hpp>
#include <Mocks/StreamingServiceWatcherMock.hpp>
#include <Mocks/ThemeLoaderMock.hpp>
#include <UnitTests/Application/UserScripts/FakeUserScript.hpp>

using namespace std;
using namespace fakeit;
using namespace MellowPlayer::Application;
using namespace MellowPlayer::Application::Tests;
using namespace MellowPlayer::Presentation;
using namespace MellowPlayer::Infrastructure;
using namespace MellowPlayer::Tests;
Expand All @@ -54,6 +57,9 @@ DependencyPool::DependencyPool()
mINotificationPresenter(NotificationPresenterMock::get()),
dataProvider(make_unique<InMemoryListeningHistoryDataProvider>())
{
When(Method(mUserScriptsFactoryMock, create)).AlwaysDo([]() -> IUserScript* {
return new FakeUserScript;
});
}

DependencyPool::~DependencyPool() = default;
Expand All @@ -73,7 +79,13 @@ StreamingServicesControllerViewModel& DependencyPool::getStreamingServicesContro
{
if (pStreamingServicesControllerViewModel == nullptr)
pStreamingServicesControllerViewModel = make_unique<StreamingServicesControllerViewModel>(
getStreamingServicesController(), getPlayers(), getSettings(), getWorkDispatcher(), getStreamingServicesCreator(), getCommandLineParser());
getStreamingServicesController(),
getPlayers(),
getSettings(),
getWorkDispatcher(),
getStreamingServicesCreator(),
getCommandLineParser(),
getUserScriptFactory());
return *pStreamingServicesControllerViewModel;
}

Expand Down Expand Up @@ -214,3 +226,8 @@ AbstractPlatformUpdater& DependencyPool::getPlatformUpdater()
pPlatformUpdater = make_unique<FakePlatformUpdater>(fakeFileDownloader);
return *pPlatformUpdater;
}

IUserScriptFactory& DependencyPool::getUserScriptFactory()
{
return mUserScriptsFactoryMock.get();
}
3 changes: 3 additions & 0 deletions tests/Lib/Utils/DependencyPool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <MellowPlayer/Application/Notifications/INotificationPresenter.hpp>
#include <MellowPlayer/Application/Settings/ISettingsProvider.hpp>
#include <MellowPlayer/Application/StreamingServices/IStreamingServiceCreator.hpp>
#include <MellowPlayer/Application/UserScripts/IUserScriptFactory.hpp>

class InMemoryListeningHistoryDataProvider;

Expand Down Expand Up @@ -65,6 +66,7 @@ namespace MellowPlayer::Tests
Application::IQtApplication& getQtApplication();
Application::INotificationPresenter& getNotificationPresenter();
Application::AbstractPlatformUpdater& getPlatformUpdater();
Application::IUserScriptFactory& getUserScriptFactory();

// Infrastructure layer
Infrastructure::LocalAlbumArt& getLocalAlbumArt();
Expand All @@ -84,6 +86,7 @@ namespace MellowPlayer::Tests
fakeit::Mock<Application::ISettingsProvider> mISettingsProvider;
fakeit::Mock<Application::IStreamingServiceCreator> mIStreamingServiceCreator;
fakeit::Mock<Application::INotificationPresenter> mINotificationPresenter;
fakeit::Mock<Application::IUserScriptFactory> mUserScriptsFactoryMock;

// app
std::unique_ptr<Application::CurrentPlayer> pCurrentPlayer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ TEST_CASE("StreamingServiceModelTests", "[UnitTest]")
StreamingService& service1 = *streamingServices.toList()[0];
StreamingService& service2 = *streamingServices.toList()[1];

StreamingServiceViewModel viewModel(service1, settingsProvider, players);
StreamingServiceViewModel sameModel(service1, settingsProvider, players);
StreamingServiceViewModel model2(service2, settingsProvider, players);
StreamingServiceViewModel viewModel(service1, settingsProvider, pool.getUserScriptFactory(), players);
StreamingServiceViewModel sameModel(service1, settingsProvider, pool.getUserScriptFactory(), players);
StreamingServiceViewModel model2(service2, settingsProvider, pool.getUserScriptFactory(), players);

SECTION("basic properties")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ TEST_CASE("StreamingServicesControllerViewModel", "[UnitTest]")
auto creatorMock = StreamingServiceCreatorMock::get();
auto commandLineParserMock = CommandLineParserMock::get();
StreamingServicesControllerViewModel viewModel(streamingServices, players, settings, workDispatcher, creatorMock.get(),
commandLineParserMock.get());
commandLineParserMock.get(), pool.getUserScriptFactory());
viewModel.initialize();
viewModel.reload();

Expand Down Expand Up @@ -113,7 +113,7 @@ TEST_CASE("StreamingServicesControllerViewModel", "[UnitTest]")
settings.get(SettingKey::PRIVATE_CURRENT_SERVICE).setValue("");
When(Method(commandLineParserMock, service)).AlwaysReturn("Deezer");
StreamingServicesControllerViewModel viewModelWithCmdLine(streamingServices, players, settings, workDispatcher, creatorMock.get(),
commandLineParserMock.get());
commandLineParserMock.get(), pool.getUserScriptFactory());
REQUIRE(viewModelWithCmdLine.currentIndex() == -1);
viewModelWithCmdLine.initialize();
viewModelWithCmdLine.reload();
Expand Down

0 comments on commit daf2385

Please sign in to comment.