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

Commit

Permalink
Don't use boost::di in tests any more.
Browse files Browse the repository at this point in the history
There is now a helper class to help create the most needed dependencies.
It helps writing cleaner tests and speed up compilation a bit because
it uses forward declarations for most types (all except mocked objects).
  • Loading branch information
ColinDuquesnoy committed Jul 18, 2017
1 parent 727d10d commit e40531e
Show file tree
Hide file tree
Showing 29 changed files with 449 additions and 517 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <QString>
#include <QVariant>
#include <MellowPlayer/Application/Notifications/Notifications.hpp>

namespace MellowPlayer::Application {

Expand Down
9 changes: 5 additions & 4 deletions lib/MellowPlayer/Presentation/Notifications/Notifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ namespace MellowPlayer::Presentation {
class Notifier: public QObject, public Application::INotifier {
Q_OBJECT
public:
Notifier(Application::IPlayer& player, Application::ILocalAlbumArt& localAlbumArtService,
Application::INotificationPresenter& presenter, Application::StreamingServicesController& streamingServices,
Application::Settings& settings);
Notifier(Application::IPlayer& player,
Application::ILocalAlbumArt& localAlbumArtService,
Application::INotificationPresenter& presenter,
Application::StreamingServicesController& streamingServices,
Application::Settings& settings);

void initialize() override;
bool display(const Application::Notification& notification) override;
Expand All @@ -48,5 +50,4 @@ namespace MellowPlayer::Presentation {
NotificationFactory notificationFactory;
QString previousSongId;
};

}
2 changes: 1 addition & 1 deletion scripts/travis/commit-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fi

if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
# build
export CMAKE_PREFIX_PATH=$PWD/qt;
export CMAKE_PREFIX_PATH=$PWD/../qt;
cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTS=ON -DCMAKE_INSTALL_PREFIX=/usr ..;
make;

Expand Down
2 changes: 1 addition & 1 deletion scripts/travis/full-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fi

if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
# build
export CMAKE_PREFIX_PATH=$PWD/qt;
export CMAKE_PREFIX_PATH=$PWD/../qt;
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=ON -DBUILD_INTEGRATION_TESTS=ON ..;
make;

Expand Down
154 changes: 0 additions & 154 deletions tests/DI.hpp

This file was deleted.

7 changes: 3 additions & 4 deletions tests/IntegrationTests/MainWindowViewModelTests.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#include <catch.hpp>
#include <QtTest/QSignalSpy>
#include <MellowPlayer/Application/Settings/Settings.hpp>
#include <MellowPlayer/Presentation/ViewModels/MainWindowViewModel.hpp>
#include "Utils/DependencyFactory.hpp";
#include "Utils/DependencyPool.hpp"

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

TEST_CASE("MainWindowViewModel") {
Tests::DependencyFactory dependencyFactory;
MainWindowViewModel& mainWindow = dependencyFactory.createMainWindowViewModel();
Tests::DependencyPool pool;
MainWindowViewModel& mainWindow = pool.getMainWindowViewModel();
QSignalSpy visibleChangedSpy(&mainWindow, SIGNAL(visibleChanged()));

REQUIRE(mainWindow.load());
Expand Down
1 change: 0 additions & 1 deletion tests/Mocks/InMemoryListeningHistoryDataProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <MellowPlayer/Application/ListeningHistory/IListeningHistoryDataProvider.hpp>
#include <MellowPlayer/Application/ListeningHistory/ListeningHistoryEntry.hpp>


class InMemoryListeningHistoryDataProvider: public MellowPlayer::Application::IListeningHistoryDataProvider {
public:
bool initialized = false;
Expand Down
4 changes: 3 additions & 1 deletion tests/Mocks/SettingsProviderMock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ QMap<QString, QVariant> SettingsProviderMock::values;
Mock<ISettingsProvider> SettingsProviderMock::get() {
Mock<ISettingsProvider> mock;

When(Method(mock, clear)).AlwaysReturn();
When(Method(mock, clear)).AlwaysDo([](){ values.clear(); });

When(Method(mock, getValue)).AlwaysDo([](const QString& key, const QVariant& defaultValue) -> QVariant {
if (values.contains(key))
Expand All @@ -16,5 +16,7 @@ Mock<ISettingsProvider> SettingsProviderMock::get() {
values[key] = value;
});

values.clear();

return mock;
}
Original file line number Diff line number Diff line change
@@ -1,43 +1,31 @@
#include <QVariant>
#include <catch.hpp>
#include <MellowPlayer/Application/ListeningHistory/ListeningHistory.hpp>
#include <MellowPlayer/Application/Player/CurrentPlayer.hpp>
#include <Mocks/PlayerMock.hpp>
#include <Mocks/FakeWorkDispatcher.hpp>
#include <Mocks/InMemoryListeningHistoryDataProvider.hpp>
#include <Mocks/StreamingServiceLoaderMock.hpp>
#include <MellowPlayer/Application/StreamingServices/StreamingServicesController.hpp>
#include <MellowPlayer/Application/Player/Players.hpp>
#include <Utils/Helpers.hpp>
#include <DI.hpp>
#include <Utils/DependencyPool.hpp>
#include <MellowPlayer/Application/Settings/Setting.hpp>
#include <MellowPlayer/Application/Settings/Settings.hpp>
#include <MellowPlayer/Application/StreamingServices/StreamingService.hpp>

using namespace MellowPlayer;
using namespace MellowPlayer::Application;
using namespace MellowPlayer::Application;


TEST_CASE("ListeningHistoryTests") {
auto mock = StreamingServiceLoaderMock::get();
auto watcherMock = StreamingServiceWatcherMock::get();
StreamingServicesController streamingServices(mock.get(), watcherMock.get());
Tests::DependencyPool pool;
StreamingServicesController& streamingServices = pool.getStreamingServicesController();
streamingServices.load();
Players players(streamingServices);
CurrentPlayer player(players, streamingServices);
FakeWorkDispatcher workDispatcher;
InMemoryListeningHistoryDataProvider dataProvider;
ScopedScope scope;
auto injector = getTestInjector(scope);
Settings& settings = injector.create<Settings&>();
ListeningHistory listeningHistoryService(dataProvider, player, workDispatcher, settings);
Players& players = pool.getPlayers();
Settings& settings = pool.getSettings();
ListeningHistory& listeningHistoryService = pool.getListeningHistory();
Player& currentPlayer = *players.get(streamingServices.getAll()[0]->getName());
streamingServices.setCurrent(streamingServices.getAll()[0].get());
Setting& isEnabledSetting = settings.get(SettingKey::PRIVACY_ENABLE_LISTENING_HISTORY);
isEnabledSetting.setValue(true);

SECTION("Initialize") {
REQUIRE(!dataProvider.initialized);
listeningHistoryService.initialize();
REQUIRE(dataProvider.initialized);
}

SECTION("New song will be added to history") {
REQUIRE(listeningHistoryService.count() == 0);
currentPlayer.setUpdateResults(getSongVariantMap("Song1", "Id1"));
Expand Down
25 changes: 13 additions & 12 deletions tests/UnitTests/Application/Updater/UpdaterTests.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#include "catch.hpp"
#include <QtTest/QSignalSpy>
#include <MellowPlayer/Application/Updater/Updater.hpp>
#include <Mocks/FakeHttpClient.hpp>
#include <MellowPlayer/Application/Updater/Github/GithubReleaseQuerier.hpp>
#include <QtTest/QSignalSpy>
#include "DI.hpp"
#include <MellowPlayer/Application/Settings/Setting.hpp>
#include <MellowPlayer/Application/Settings/Settings.hpp>
#include <MellowPlayer/Application/Settings/SettingKey.hpp>
#include <Mocks/FakeHttpClient.hpp>
#include "Utils/DependencyPool.hpp"

using namespace MellowPlayer;
using namespace MellowPlayer::Application;

SCENARIO("check for stable updates") {
FakeHttpClient fakeHttpClient;
GithubReleaseQuerier querier(fakeHttpClient);
ScopedScope scope;
auto injector = getTestInjector(scope);
Settings& settings = injector.create<Settings&>();
Tests::DependencyPool pool;
Settings& settings = pool.getSettings();
settings.get(SettingKey::MAIN_UPDATE_CHANNEL).setValue(UpdateChannelStringer::toString(UpdateChannel::Stable));

GIVEN("current version is 2.2.4 from April 2017") {
Expand Down Expand Up @@ -71,9 +74,8 @@ SCENARIO("check for stable updates") {
SCENARIO("check for beta updates") {
FakeHttpClient fakeHttpClient;
GithubReleaseQuerier querier(fakeHttpClient);
ScopedScope scope;
auto injector = getTestInjector(scope);
Settings& settings = injector.create<Settings&>();
Tests::DependencyPool pool;
Settings& settings = pool.getSettings();
settings.get(SettingKey::MAIN_UPDATE_CHANNEL).setValue(UpdateChannelStringer::toString(UpdateChannel::Beta));

GIVEN("current version is 2.2.4 from April 2017") {
Expand Down Expand Up @@ -115,9 +117,8 @@ SCENARIO("check for beta updates") {
SCENARIO("check for Continuous updates") {
FakeHttpClient fakeHttpClient;
GithubReleaseQuerier querier(fakeHttpClient);
ScopedScope scope;
auto injector = getTestInjector(scope);
Settings& settings = injector.create<Settings&>();
Tests::DependencyPool pool;
Settings& settings = pool.getSettings();
settings.get(SettingKey::MAIN_UPDATE_CHANNEL).setValue(UpdateChannelStringer::toString(UpdateChannel::Continuous));

GIVEN("current version is 2.2.4 from April 2017") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
#include <MellowPlayer/Infrastructure/Controllers/HotkeysController.hpp>
#include <Mocks/PlayerMock.hpp>
#include <Mocks/MainWindowMock.hpp>
#include "DI.hpp"
#include <Utils/DependencyPool.hpp>

using namespace MellowPlayer;
using namespace MellowPlayer::Application;
using namespace MellowPlayer::Infrastructure;
using namespace fakeit;

TEST_CASE("HotkeysServiceTests", "[UnitTest]") {
auto playerMock = PlayerMock::get();
auto mainWindowMock = MainWindowMock::get();
ScopedScope scope;
auto injector = getTestInjector(scope);
Settings& settings = injector.create<Settings&>();

Tests::DependencyPool pool;
Settings& settings = pool.getSettings();
HotkeysController hotkeys(playerMock.get(), settings, mainWindowMock.get());
hotkeys.start();

Expand Down
Loading

0 comments on commit e40531e

Please sign in to comment.