Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for the presenter of elwin interface #37210

Merged
merged 7 commits into from Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 39 additions & 15 deletions qt/scientific_interfaces/Inelastic/Manipulation/ElwinModel.h
Expand Up @@ -19,30 +19,54 @@ using namespace MantidQt::MantidWidgets;
namespace MantidQt {
namespace CustomInterfaces {

class MANTIDQT_INELASTIC_DLL ElwinModel {
class MANTIDQT_INELASTIC_DLL IElwinModel {
public:
virtual ~IElwinModel() = default;
virtual void setupLoadAlgorithm(MantidQt::API::BatchAlgorithmRunner *batchAlgoRunner, std::string const &filepath,
std::string const &outputName) = 0;
virtual std::string createGroupedWorkspaces(MatrixWorkspace_sptr workspace, FunctionModelSpectra const &spectra) = 0;
virtual void setupGroupAlgorithm(MantidQt::API::BatchAlgorithmRunner *batchAlgoRunner,
std::string const &inputWorkspacesString, std::string const &inputGroupWsName) = 0;
virtual void setupElasticWindowMultiple(MantidQt::API::BatchAlgorithmRunner *batchAlgoRunner,
std::string const &workspaceBaseName, std::string const &inputGroupWsName,
std::string const &sampleEnvironmentLogName,
std::string const &sampleEnvironmentLogValue) = 0;
virtual void ungroupAlgorithm(std::string const &inputWorkspace) const = 0;
virtual void groupAlgorithm(std::string const &inputWorkspaces, std::string const &outputWorkspace) const = 0;
virtual void setIntegrationStart(double integrationStart) = 0;
virtual void setIntegrationEnd(double integrationEnd) = 0;
virtual void setBackgroundStart(double backgroundStart) = 0;
virtual void setBackgroundEnd(double backgroundEnd) = 0;
virtual void setBackgroundSubtraction(bool backgroundSubtraction) = 0;
virtual void setNormalise(bool normalise) = 0;
virtual void setOutputWorkspaceNames(std::string const &workspaceBaseName) = 0;
virtual std::string getOutputWorkspaceNames() const = 0;
};

class MANTIDQT_INELASTIC_DLL ElwinModel : public IElwinModel {

public:
ElwinModel();
~ElwinModel() = default;
void setupLoadAlgorithm(MantidQt::API::BatchAlgorithmRunner *batchAlgoRunner, std::string const &filepath,
std::string const &outputName);
std::string createGroupedWorkspaces(MatrixWorkspace_sptr workspace, FunctionModelSpectra const &spectra);
std::string const &outputName) override;
std::string createGroupedWorkspaces(MatrixWorkspace_sptr workspace, FunctionModelSpectra const &spectra) override;
void setupGroupAlgorithm(MantidQt::API::BatchAlgorithmRunner *batchAlgoRunner,
std::string const &inputWorkspacesString, std::string const &inputGroupWsName);
std::string const &inputWorkspacesString, std::string const &inputGroupWsName) override;
void setupElasticWindowMultiple(MantidQt::API::BatchAlgorithmRunner *batchAlgoRunner,
std::string const &workspaceBaseName, std::string const &inputGroupWsName,
std::string const &sampleEnvironmentLogName,
std::string const &sampleEnvironmentLogValue);
void ungroupAlgorithm(std::string const &inputWorkspace) const;
void groupAlgorithm(std::string const &inputWorkspaces, std::string const &outputWorkspace) const;
void setIntegrationStart(double integrationStart);
void setIntegrationEnd(double integrationEnd);
void setBackgroundStart(double backgroundStart);
void setBackgroundEnd(double backgroundEnd);
void setBackgroundSubtraction(bool backgroundSubtraction);
void setNormalise(bool normalise);
void setOutputWorkspaceNames(std::string const &workspaceBaseName);
std::string getOutputWorkspaceNames() const;
std::string const &sampleEnvironmentLogValue) override;
void ungroupAlgorithm(std::string const &inputWorkspace) const override;
void groupAlgorithm(std::string const &inputWorkspaces, std::string const &outputWorkspace) const override;
void setIntegrationStart(double integrationStart) override;
void setIntegrationEnd(double integrationEnd) override;
void setBackgroundStart(double backgroundStart) override;
void setBackgroundEnd(double backgroundEnd) override;
void setBackgroundSubtraction(bool backgroundSubtraction) override;
void setNormalise(bool normalise) override;
void setOutputWorkspaceNames(std::string const &workspaceBaseName) override;
std::string getOutputWorkspaceNames() const override;

private:
double m_integrationStart;
Expand Down
Expand Up @@ -49,6 +49,15 @@ ElwinPresenter::ElwinPresenter(QWidget *parent, IElwinView *view)
std::make_unique<OutputPlotOptionsPresenter>(m_view->getPlotOptions(), PlotWidget::Spectra));
}

ElwinPresenter::ElwinPresenter(QWidget *parent, IElwinView *view, std::unique_ptr<IElwinModel> model,
std::unique_ptr<IFitDataModel> dataModel)
: DataManipulation(parent), m_view(view), m_model(std::move(model)), m_dataModel(std::move(dataModel)),
m_selectedSpectrum(0) {
m_view->subscribePresenter(this);
setOutputPlotOptionsPresenter(
std::make_unique<OutputPlotOptionsPresenter>(m_view->getPlotOptions(), PlotWidget::Spectra));
}

ElwinPresenter::~ElwinPresenter() {}

void ElwinPresenter::setup() {
Expand Down
20 changes: 11 additions & 9 deletions qt/scientific_interfaces/Inelastic/Manipulation/ElwinPresenter.h
Expand Up @@ -43,6 +43,8 @@ class IElwinPresenter {
class MANTIDQT_INELASTIC_DLL ElwinPresenter : public DataManipulation, public IElwinPresenter {
public:
ElwinPresenter(QWidget *parent, IElwinView *view);
ElwinPresenter(QWidget *parent, IElwinView *view, std::unique_ptr<IElwinModel> model,
std::unique_ptr<IFitDataModel> dataModel);
~ElwinPresenter();

// base Manipulation tab methods
Expand All @@ -63,6 +65,13 @@ class MANTIDQT_INELASTIC_DLL ElwinPresenter : public DataManipulation, public IE
void handleRowModeChanged() override;
void updateAvailableSpectra() override;

void setInputWorkspace(MatrixWorkspace_sptr inputWorkspace);
virtual void setSelectedSpectrum(int spectrum);
int getSelectedSpectrum() const;
MatrixWorkspace_sptr getInputWorkspace() const;
MatrixWorkspace_sptr getPreviewPlotWorkspace();
void setPreviewPlotWorkspace(const MatrixWorkspace_sptr &previewPlotWorkspace);

protected:
void runComplete(bool error) override;
void newInputDataFromDialog();
Expand All @@ -72,23 +81,16 @@ class MANTIDQT_INELASTIC_DLL ElwinPresenter : public DataManipulation, public IE
void updateTableFromModel();
void updateIntegrationRange();

int getSelectedSpectrum() const;
virtual void setSelectedSpectrum(int spectrum);

std::vector<std::string> getOutputWorkspaceNames();
std::string getOutputBasename();
MatrixWorkspace_sptr getInputWorkspace() const;
MatrixWorkspace_sptr getPreviewPlotWorkspace();
bool checkForELTWorkspace();
void setInputWorkspace(MatrixWorkspace_sptr inputWorkspace);
void setPreviewPlotWorkspace(const MatrixWorkspace_sptr &previewPlotWorkspace);
void newPreviewFileSelected(const std::string &workspaceName, const std::string &filename);
void newPreviewWorkspaceSelected(const std::string &workspaceName);
size_t findWorkspaceID();

IElwinView *m_view;
std::unique_ptr<ElwinModel> m_model;
std::unique_ptr<FitDataModel> m_dataModel;
std::unique_ptr<IElwinModel> m_model;
std::unique_ptr<IFitDataModel> m_dataModel;
int m_selectedSpectrum;
std::weak_ptr<MatrixWorkspace> m_previewPlotWorkspace;
MatrixWorkspace_sptr m_inputWorkspace;
Expand Down
Expand Up @@ -212,7 +212,7 @@ void ElwinView::addData(MantidWidgets::IAddWorkspaceDialog const *dialog) {
}
}

OutputPlotOptionsView *ElwinView::getPlotOptions() const { return m_uiForm.ipoPlotOptions; }
IOutputPlotOptionsView *ElwinView::getPlotOptions() const { return m_uiForm.ipoPlotOptions; }

void ElwinView::setHorizontalHeaders() {
QStringList headers;
Expand Down
Expand Up @@ -31,7 +31,7 @@ class MANTIDQT_INELASTIC_DLL ElwinView : public QWidget, public IElwinView {
void subscribePresenter(IElwinPresenter *presenter) override;
void setup() override;

OutputPlotOptionsView *getPlotOptions() const override;
IOutputPlotOptionsView *getPlotOptions() const override;

void setAvailableSpectra(WorkspaceIndex minimum, WorkspaceIndex maximum) override;
void setAvailableSpectra(const std::vector<WorkspaceIndex>::const_iterator &from,
Expand Down
4 changes: 2 additions & 2 deletions qt/scientific_interfaces/Inelastic/Manipulation/IElwinView.h
Expand Up @@ -23,15 +23,15 @@
namespace MantidQt {
namespace CustomInterfaces {

class OutputPlotOptionsView;
class IOutputPlotOptionsView;
class IElwinPresenter;

class MANTIDQT_INELASTIC_DLL IElwinView {

public:
virtual void subscribePresenter(IElwinPresenter *presenter) = 0;
virtual void setup() = 0;
virtual OutputPlotOptionsView *getPlotOptions() const = 0;
virtual IOutputPlotOptionsView *getPlotOptions() const = 0;

virtual void setAvailableSpectra(MantidWidgets::WorkspaceIndex minimum, MantidWidgets::WorkspaceIndex maximum) = 0;
virtual void setAvailableSpectra(const std::vector<MantidWidgets::WorkspaceIndex>::const_iterator &from,
Expand Down
@@ -1,6 +1,8 @@
get_filename_component(SUB_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" NAME)

set(TEST_FILES ElwinModelTest.h IqtModelTest.h MomentsModelTest.h SqwModelTest.h SymmetriseModelTest.h)
set(TEST_FILES ElwinModelTest.h ElwinPresenterTest.h IqtModelTest.h MomentsModelTest.h SqwModelTest.h
SymmetriseModelTest.h
)

list(TRANSFORM TEST_FILES PREPEND ${SUB_DIRECTORY}/)

Expand Down
@@ -0,0 +1,141 @@
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright &copy; 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once

#include <cxxtest/TestSuite.h>
#include <gmock/gmock.h>

#include "Manipulation/ElwinModel.h"
#include "Manipulation/ElwinPresenter.h"
#include "Manipulation/ElwinView.h"
#include "MantidQtWidgets/Common/AddWorkspaceDialog.h"

#include "../Common/MockObjects.h"
#include "../QENSFitting/MockObjects.h"

#include "MantidFrameworkTestHelpers/IndirectFitDataCreationHelper.h"
#include "MantidKernel/WarningSuppressions.h"

using namespace Mantid::API;
using namespace Mantid::IndirectFitDataCreationHelper;
using namespace MantidQt::CustomInterfaces;
using namespace MantidQt::CustomInterfaces::Inelastic;
using namespace testing;

class ElwinPresenterTest : public CxxTest::TestSuite {
public:
static ElwinPresenterTest *createSuite() { return new ElwinPresenterTest(); }

static void destroySuite(ElwinPresenterTest *suite) { delete suite; }

void setUp() override {
m_view = std::make_unique<NiceMock<MockElwinView>>();
m_outputPlotView = std::make_unique<NiceMock<MockOutputPlotOptionsView>>();

auto model = std::make_unique<NiceMock<MockElwinModel>>();
auto dataModel = std::make_unique<NiceMock<MockFitDataModel>>();
m_model = model.get();
m_dataModel = dataModel.get();

ON_CALL(*m_view, getPlotOptions()).WillByDefault(Return((m_outputPlotView.get())));
m_presenter = std::make_unique<ElwinPresenter>(nullptr, m_view.get(), std::move(model), std::move(dataModel));

m_workspace = createWorkspace(5);
m_ads = std::make_unique<SetUpADSWithWorkspace>("workspace_test", m_workspace);
}

void tearDown() override {
AnalysisDataService::Instance().clear();

TS_ASSERT(Mock::VerifyAndClearExpectations(m_view.get()));
TS_ASSERT(Mock::VerifyAndClearExpectations(&m_model));
TS_ASSERT(Mock::VerifyAndClearExpectations(&m_dataModel));
TS_ASSERT(Mock::VerifyAndClearExpectations(m_outputPlotView.get()));

m_presenter.reset();
m_view.reset();
m_outputPlotView.reset();
}

///----------------------------------------------------------------------
/// Unit Tests that test the signals, methods and slots of the presenter
///----------------------------------------------------------------------

void test_handleValueChanged_sets_correct_bool_property() {

EXPECT_CALL(*m_model, setNormalise(true)).Times((Exactly(1)));
m_presenter->handleValueChanged("Normalise", true);

EXPECT_CALL(*m_model, setBackgroundSubtraction(true)).Times((Exactly(1)));
m_presenter->handleValueChanged("BackgroundSubtraction", true);
}

void test_handleValueChanged_sets_correct_double_property() {
double value = 0.1;

EXPECT_CALL(*m_model, setIntegrationStart(value)).Times((Exactly(1)));
m_presenter->handleValueChanged("IntegrationStart", value);
EXPECT_CALL(*m_model, setIntegrationEnd(value)).Times((Exactly(1)));
m_presenter->handleValueChanged("IntegrationEnd", value);
EXPECT_CALL(*m_model, setBackgroundStart(value)).Times((Exactly(1)));
m_presenter->handleValueChanged("BackgroundStart", value);
EXPECT_CALL(*m_model, setBackgroundEnd(value)).Times((Exactly(1)));
m_presenter->handleValueChanged("BackgroundEnd", value);
}

void test_handleRunClicked_doesnt_run_with_invalid_ranges() {
EXPECT_CALL(*m_outputPlotView, clearWorkspaces()).Times(1);
EXPECT_CALL(*m_view, isTableEmpty()).Times(1);
m_presenter->handleRunClicked();
}

void test_handlePlotPreviewClicked_calls_warning_when_no_workspace() {
EXPECT_CALL(*m_view, showMessageBox("Workspace not found - data may not be loaded.")).Times(Exactly(1));
m_presenter->handlePlotPreviewClicked();
}

void test_handlePreviewSpectrumChanged_calls_correct_spectrum() {
m_presenter->setInputWorkspace(m_workspace);
auto spectrum = 1;
EXPECT_CALL(*m_view, getPreviewSpec()).Times(Exactly(1)).WillOnce(Return(1));
EXPECT_CALL(*m_view, plotInput(m_workspace, spectrum)).Times(Exactly(1));
m_presenter->handlePreviewSpectrumChanged(spectrum);
}

void test_handleAddData_sets_preview_workspace_and_spectrum() {
auto dialog = new MantidQt::MantidWidgets::AddWorkspaceDialog(nullptr);
m_presenter->setSelectedSpectrum(0);

ON_CALL(*m_view, getPreviewWorkspaceName(0)).WillByDefault(Return("workspace_test"));
EXPECT_CALL(*m_view, clearPreviewFile()).Times(Exactly(1));
EXPECT_CALL(*m_view, newInputDataFromDialog(m_dataModel->getWorkspaceNames())).Times(Exactly(1));
EXPECT_CALL(*m_view, plotInput(m_workspace, 0)).Times(Exactly(1));
m_presenter->handleAddData(dialog);
}

void test_handleRowModeChanged_gets_domains_when_rows_are_not_collapsed() {
EXPECT_CALL(*m_view, isRowCollapsed()).WillOnce(Return(false));
EXPECT_CALL(*m_dataModel, getNumberOfDomains()).Times(Exactly(1));
m_presenter->handleRowModeChanged();
}

void test_handleRowModeChanged_gets_workspaces_when_rows_are_collapsed() {
EXPECT_CALL(*m_view, isRowCollapsed()).WillOnce(Return(true));
EXPECT_CALL(*m_dataModel, getNumberOfWorkspaces()).Times(Exactly(1));
m_presenter->handleRowModeChanged();
}

private:
NiceMock<MockFitDataModel> *m_dataModel;
NiceMock<MockElwinModel> *m_model;
std::unique_ptr<NiceMock<MockOutputPlotOptionsView>> m_outputPlotView;
std::unique_ptr<NiceMock<MockElwinView>> m_view;
std::unique_ptr<ElwinPresenter> m_presenter;

MatrixWorkspace_sptr m_workspace;
std::unique_ptr<SetUpADSWithWorkspace> m_ads;
};