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

Commit

Permalink
#48 Finish binding View and ViewModels + UnitTests
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinDuquesnoy committed Jul 20, 2017
1 parent d32eae4 commit 387900e
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#include "UpdaterStatusConverter.hpp"

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

const QString UpdaterStatusConverter::NONE = "";
const QString UpdaterStatusConverter::CHECKING = QObject::tr("Checking for update");
const QString UpdaterStatusConverter::DOWNLOADING = QObject::tr("Downloading update");
const QString UpdaterStatusConverter::INSTALLING = QObject::tr("Installing update");
const QString UpdaterStatusConverter::CHECKING = "Checking for update";
const QString UpdaterStatusConverter::DOWNLOADING = "Downloading update";
const QString UpdaterStatusConverter::INSTALLING = "Installing update";

QString UpdaterStatusConverter::toString(Updater::Status status) {
static QMap<Updater::Status, QString> map = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <MellowPlayer/Application/Updater/Updater.hpp>

namespace MellowPlayer::Infrastructure {
namespace MellowPlayer::Presentation {

class UpdaterStatusConverter {
public:
Expand Down
73 changes: 52 additions & 21 deletions lib/MellowPlayer/Presentation/ViewModels/UpdaterViewModel.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
#include <MellowPlayer/Presentation/Converters/UpdaterStatusConverter.hpp>
#include "UpdaterViewModel.hpp"

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


UpdaterViewModel::UpdaterViewModel(Updater& updater): updater_(updater) {
UpdaterViewModel::UpdaterViewModel(Updater& updater): updater_(updater)
{
connect(&updater, &Updater::updateAvailable, this, &UpdaterViewModel::onUpdateAvailable);
connect(&updater, &Updater::noUpdateAvailable, this, &UpdaterViewModel::onNoUpdateAvailable);
connect(&updater, &Updater::statusChanged, this, &UpdaterViewModel::statusChanged);
connect(&updater, &Updater::statusChanged, this, &UpdaterViewModel::busyChanged);
emit statusChanged();
emit busyChanged();
}

QString UpdaterViewModel::getMessage() const {
QString UpdaterViewModel::getMessage() const
{
return message_;
}

bool UpdaterViewModel::isVisible() const {
bool UpdaterViewModel::isVisible() const
{
return visible_;
}

bool UpdaterViewModel::canInstall() const {
return canInstall_;
bool UpdaterViewModel::isInstallEnabled() const
{
return installEnabled_;
}

int UpdaterViewModel::getProgress() const {
int UpdaterViewModel::getProgress() const
{
return progress_;
}

Expand All @@ -30,40 +40,47 @@ bool UpdaterViewModel::isProgressVisible() const
return progressVisible_;
}

void UpdaterViewModel::close() {
void UpdaterViewModel::close()
{
setVisible(false);
}

void UpdaterViewModel::check() {
void UpdaterViewModel::check()
{
setProgressVisible(true);
setProgress(-1);
updater_.check();
}

void UpdaterViewModel::install() {
void UpdaterViewModel::install()
{
setMessage("Downloading update...");
setInstallEnabled(false);
setProgressVisible(true);
setProgress(-1);
updater_.install();
}

void UpdaterViewModel::setVisible(bool visible) {
void UpdaterViewModel::setVisible(bool visible)
{
if (visible_ == visible)
return;

visible_ = visible;
emit visibleChanged();
}

void UpdaterViewModel::setCanInstall(bool canInstall) {
if (canInstall_ == canInstall)
void UpdaterViewModel::setInstallEnabled(bool enabled)
{
if (installEnabled_ == enabled)
return;

canInstall_ = canInstall;
emit canInstallChanged();
installEnabled_ = enabled;
emit installEnabledChanged();
}

void UpdaterViewModel::setProgress(int progress) {
void UpdaterViewModel::setProgress(int progress)
{
if (progress_ == progress)
return;

Expand All @@ -80,8 +97,9 @@ void UpdaterViewModel::setProgressVisible(bool progressVisible)
emit progressVisibleChanged();
}

void UpdaterViewModel::onUpdateAvailable() {
setCanInstall(updater_.canInstall());
void UpdaterViewModel::onUpdateAvailable()
{
setInstallEnabled(updater_.canInstall());
setProgressVisible(false);
setProgress(-1);
setVisible(true);
Expand All @@ -90,20 +108,33 @@ void UpdaterViewModel::onUpdateAvailable() {

void UpdaterViewModel::onNoUpdateAvailable()
{
setCanInstall(false);
setInstallEnabled(false);
setProgressVisible(false);
setProgress(-1);
setVisible(false);
setMessage("");
}

void UpdaterViewModel::setMessage(const QString& message) {
void UpdaterViewModel::setMessage(const QString& message)
{
if (message_ == message)
return;
message_ = message;
emit messageChanged();
}

QString UpdaterViewModel::getUrl() const {
return updater_.getLatestRelease()->getUrl();
QString UpdaterViewModel::getUrl() const
{
const Release* r = updater_.getLatestRelease();
if (r != nullptr)
return r->getUrl();
return "";
}

QString UpdaterViewModel::getStatus() const {
return UpdaterStatusConverter::toString(updater_.getStatus());
}

bool UpdaterViewModel::isBusy() const {
return updater_.getStatus() != Updater::Status::None;
}
18 changes: 12 additions & 6 deletions lib/MellowPlayer/Presentation/ViewModels/UpdaterViewModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ namespace MellowPlayer::Presentation {
Q_OBJECT
Q_PROPERTY(QString message READ getMessage NOTIFY messageChanged)
Q_PROPERTY(QString url READ getUrl CONSTANT)
Q_PROPERTY(QString status READ getStatus NOTIFY statusChanged)
Q_PROPERTY(bool busy READ isBusy NOTIFY busyChanged)
Q_PROPERTY(bool visible READ isVisible NOTIFY visibleChanged)
Q_PROPERTY(bool canInstall READ canInstall NOTIFY canInstallChanged)
Q_PROPERTY(bool installEnabled READ isInstallEnabled NOTIFY installEnabledChanged)
Q_PROPERTY(int progress READ getProgress NOTIFY progressChanged)
Q_PROPERTY(bool progressVisible READ isProgressVisible NOTIFY progressVisibleChanged)
public:
UpdaterViewModel(Application::Updater& updater);

QString getMessage() const;
QString getUrl() const;
QString getStatus() const;
bool isBusy() const;
bool isVisible() const;
bool canInstall() const;
bool isInstallEnabled() const;
int getProgress() const;
bool isProgressVisible() const;

Expand All @@ -30,14 +34,16 @@ namespace MellowPlayer::Presentation {
signals:
void messageChanged();
void visibleChanged();
void canInstallChanged();
void installEnabledChanged();
void progressChanged();
void progressVisibleChanged();
void statusChanged();
void busyChanged();

private slots:
void setMessage(const QString& message);
void setVisible(bool visible);
void setCanInstall(bool canInstall);
void setInstallEnabled(bool enabled);
void setProgress(int progress);
void setProgressVisible(bool progressVisible);
void onUpdateAvailable();
Expand All @@ -47,8 +53,8 @@ namespace MellowPlayer::Presentation {
Application::Updater& updater_;
QString message_;
bool visible_ = false;
bool canInstall_ = false;
bool installEnabled_ = false;
int progress_ = -1;
bool progressVisible_ = true;
bool progressVisible_ = false;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ ToolBar {

MenuIconItem {
icon: MaterialIcons.icon_update
text: "Check for updates"
text: _updater.busy ? _updater.status : "Check for update"
enabled: !_updater.busy
onClicked: _updater.check()

ProgressBar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import MellowPlayer 3.0
ToolBar {
id: updateToolBar

enabled: !_updater.busy
height: 0
transform: Scale {
id: scaleTransform
Expand Down Expand Up @@ -61,7 +62,7 @@ ToolBar {
highlighted: true
text: "Install"
onClicked: _updater.install()
// visible: _updater.canInstall
visible: _updater.installEnabled
}

ToolButton {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <MellowPlayer/Presentation/Converters/UpdaterStatusConverter.hpp>

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

SCENARIO("Updater status can be converted to a string") {
GIVEN("UpdaterStatus::None") {
Expand All @@ -11,8 +11,8 @@ SCENARIO("Updater status can be converted to a string") {
WHEN("converting to string") {
QString statusString = UpdaterStatusConverter::toString(status);

THEN("the string is empty") {
REQUIRE(statusString.isEmpty());
THEN("the string is None") {
REQUIRE(statusString == UpdaterStatusConverter::NONE);
}
}
}
Expand Down Expand Up @@ -67,6 +67,18 @@ SCENARIO("Updater status can be converted from a string") {
}
}

GIVEN("a None status string") {
QString statusString = UpdaterStatusConverter::NONE;

WHEN("converting from string") {
Updater::Status status = UpdaterStatusConverter::fromString(statusString);

THEN("Status is None") {
REQUIRE(status == Updater::Status::None);
}
}
}

GIVEN("a valid status string") {
QString statusString = UpdaterStatusConverter::CHECKING;

Expand Down
84 changes: 84 additions & 0 deletions tests/UnitTests/Presentation/ViewModels/UpdaterViewModelTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <catch.hpp>
#include <MellowPlayer/Presentation/ViewModels/UpdaterViewModel.hpp>
#include <Utils/DependencyPool.hpp>

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


SCENARIO("check for updates") {
DependencyPool pool;
Updater& updater = pool.getUpdater();
UpdaterViewModel& viewModel = pool.getUpdaterViewModel();

GIVEN("current version is 2.2.4 from April 2017") {
Release currentRelease("2.2.4", QDate::fromString("2017-04-29", Qt::ISODate));
updater.setCurrentRelease(&currentRelease);

REQUIRE(!viewModel.isBusy());
REQUIRE(!viewModel.isVisible());
REQUIRE(viewModel.getMessage().isEmpty());
REQUIRE(viewModel.getUrl().isEmpty());
REQUIRE(viewModel.getStatus().isEmpty());
REQUIRE(!viewModel.isInstallEnabled());
REQUIRE(!viewModel.isProgressVisible());
REQUIRE(viewModel.getProgress() == -1);

WHEN("checking for updates") {
viewModel.check();
REQUIRE(!viewModel.isBusy());

THEN("an update is available") {
REQUIRE(viewModel.isVisible());
REQUIRE(!viewModel.getMessage().isEmpty());
REQUIRE(!viewModel.getUrl().isEmpty());

AND_WHEN("closing the pane") {
viewModel.close();

THEN("visible changed but all other properties remains") {
REQUIRE(!viewModel.isVisible());

REQUIRE(!viewModel.getMessage().isEmpty());
REQUIRE(!viewModel.getUrl().isEmpty());
}
}

AND_WHEN("clicking on install") {
viewModel.install();

THEN("progress bar appear and updater starts downloading") {
REQUIRE(viewModel.isProgressVisible());
REQUIRE(viewModel.isBusy());
}
}
}
}
}

GIVEN("current version is 3.0 from August 2017") {
Release currentRelease("3.0", QDate::fromString("2017-08-12", Qt::ISODate));
updater.setCurrentRelease(&currentRelease);

REQUIRE(!viewModel.isBusy());
REQUIRE(!viewModel.isVisible());
REQUIRE(viewModel.getMessage().isEmpty());
REQUIRE(viewModel.getUrl().isEmpty());
REQUIRE(viewModel.getStatus().isEmpty());
REQUIRE(!viewModel.isInstallEnabled());
REQUIRE(!viewModel.isProgressVisible());
REQUIRE(viewModel.getProgress() == -1);

WHEN("checking for updates") {
viewModel.check();
REQUIRE(!viewModel.isBusy());

THEN("no update is available") {
REQUIRE(!viewModel.isVisible());
REQUIRE(viewModel.getMessage().isEmpty());
REQUIRE(viewModel.getUrl().isEmpty());
}
}
}
}

0 comments on commit 387900e

Please sign in to comment.