From 5844dcad2adaf740f832f3b769e950278c6a1aa9 Mon Sep 17 00:00:00 2001 From: Gemma Guest <gemma.guest@stfc.ac.uk> Date: Mon, 1 Oct 2018 12:47:21 +0100 Subject: [PATCH] Add more unit tests for experiment presenter Re #22263 --- .../GUI/Experiment/ExperimentPresenter.cpp | 9 +- .../GUI/Experiment/ExperimentView.cpp | 28 ++++ .../GUI/Experiment/ExperimentView.h | 2 + .../GUI/Experiment/IExperimentView.h | 2 + .../Reduction/Experiment.cpp | 6 +- .../ISISReflectometry/Reduction/Experiment.h | 3 +- .../Reduction/PolarizationCorrections.cpp | 11 ++ .../Reduction/PolarizationCorrections.h | 7 + .../Experiment/ExperimentPresenterTest.h | 133 +++++++++++++++++- .../Experiment/MockExperimentView.h | 2 + 10 files changed, 192 insertions(+), 11 deletions(-) diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/Experiment/ExperimentPresenter.cpp b/qt/scientific_interfaces/ISISReflectometry/GUI/Experiment/ExperimentPresenter.cpp index 38fc3987f46..13fadc8e8cc 100644 --- a/qt/scientific_interfaces/ISISReflectometry/GUI/Experiment/ExperimentPresenter.cpp +++ b/qt/scientific_interfaces/ISISReflectometry/GUI/Experiment/ExperimentPresenter.cpp @@ -63,8 +63,13 @@ PolarizationCorrections ExperimentPresenter::polarizationCorrectionsFromView() { } RangeInLambda ExperimentPresenter::transmissionRunRangeFromView() { - return RangeInLambda(m_view->getTransmissionStartOverlap(), - m_view->getTransmissionEndOverlap()); + auto const range = RangeInLambda(m_view->getTransmissionStartOverlap(), + m_view->getTransmissionEndOverlap()); + if (range.isValid()) + m_view->showTransmissionRangeValid(); + else + m_view->showTransmissionRangeInvalid(); + return range; } ExperimentValidationResult ExperimentPresenter::validateExperimentFromView() { diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/Experiment/ExperimentView.cpp b/qt/scientific_interfaces/ISISReflectometry/GUI/Experiment/ExperimentView.cpp index a18f545e799..a5a2de8dd84 100644 --- a/qt/scientific_interfaces/ISISReflectometry/GUI/Experiment/ExperimentView.cpp +++ b/qt/scientific_interfaces/ISISReflectometry/GUI/Experiment/ExperimentView.cpp @@ -7,6 +7,24 @@ namespace MantidQt { namespace CustomInterfaces { +namespace { +// TODO: these don't seem to work for spin boxes - is there another way to set +// the backround colour?gg +void showAsInvalid(QDoubleSpinBox &spinBox) { + auto palette = spinBox.palette(); + palette.setColor(spinBox.backgroundRole(), QColor("#ffb8ad")); + spinBox.setAutoFillBackground(true); + spinBox.setPalette(palette); +} + +void showAsValid(QDoubleSpinBox &spinBox) { + auto palette = spinBox.palette(); + palette.setColor(spinBox.backgroundRole(), Qt::transparent); + spinBox.setAutoFillBackground(false); + spinBox.setPalette(palette); +} +} // namespace + /** Constructor * @param algorithmForTooltips :: [input] An algorithm that will be * used to find tooltips for the input properties @@ -492,6 +510,16 @@ double ExperimentView::getTransmissionEndOverlap() const { return m_ui.endOverlapEdit->value(); } +void ExperimentView::showTransmissionRangeInvalid() { + showAsInvalid(*m_ui.startOverlapEdit); + showAsInvalid(*m_ui.endOverlapEdit); +} + +void ExperimentView::showTransmissionRangeValid() { + showAsValid(*m_ui.startOverlapEdit); + showAsValid(*m_ui.endOverlapEdit); +} + void ExperimentView::setTransmissionEndOverlap(double end) { m_ui.endOverlapEdit->setValue(end); } diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/Experiment/ExperimentView.h b/qt/scientific_interfaces/ISISReflectometry/GUI/Experiment/ExperimentView.h index a58bb44a29e..2ac78c3264a 100644 --- a/qt/scientific_interfaces/ISISReflectometry/GUI/Experiment/ExperimentView.h +++ b/qt/scientific_interfaces/ISISReflectometry/GUI/Experiment/ExperimentView.h @@ -67,6 +67,8 @@ public: void setTransmissionStartOverlap(double start) override; double getTransmissionEndOverlap() const override; void setTransmissionEndOverlap(double end) override; + void showTransmissionRangeInvalid() override; + void showTransmissionRangeValid() override; std::string getPolarisationCorrectionType() const override; void setPolarisationCorrectionType(std::string const &type) override; diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/Experiment/IExperimentView.h b/qt/scientific_interfaces/ISISReflectometry/GUI/Experiment/IExperimentView.h index fb1bdc51980..02b55bc7020 100644 --- a/qt/scientific_interfaces/ISISReflectometry/GUI/Experiment/IExperimentView.h +++ b/qt/scientific_interfaces/ISISReflectometry/GUI/Experiment/IExperimentView.h @@ -84,6 +84,8 @@ public: virtual void setTransmissionStartOverlap(double start) = 0; virtual double getTransmissionEndOverlap() const = 0; virtual void setTransmissionEndOverlap(double end) = 0; + virtual void showTransmissionRangeInvalid() = 0; + virtual void showTransmissionRangeValid() = 0; virtual std::string getPolarisationCorrectionType() const = 0; virtual void setPolarisationCorrectionType(std::string const &type) = 0; diff --git a/qt/scientific_interfaces/ISISReflectometry/Reduction/Experiment.cpp b/qt/scientific_interfaces/ISISReflectometry/Reduction/Experiment.cpp index 7fb25e24345..8617f5cb3df 100644 --- a/qt/scientific_interfaces/ISISReflectometry/Reduction/Experiment.cpp +++ b/qt/scientific_interfaces/ISISReflectometry/Reduction/Experiment.cpp @@ -25,7 +25,11 @@ PolarizationCorrections const &Experiment::polarizationCorrections() const { return m_polarizationCorrections; } -RangeInLambda const &Experiment::transissionRunRange() const { +bool Experiment::isValid() const { + return m_transmissionRunRange.isValid(); +} + +RangeInLambda const &Experiment::transmissionRunRange() const { return m_transmissionRunRange; } diff --git a/qt/scientific_interfaces/ISISReflectometry/Reduction/Experiment.h b/qt/scientific_interfaces/ISISReflectometry/Reduction/Experiment.h index 2cf2d9661bf..16b969ff2b9 100644 --- a/qt/scientific_interfaces/ISISReflectometry/Reduction/Experiment.h +++ b/qt/scientific_interfaces/ISISReflectometry/Reduction/Experiment.h @@ -45,11 +45,12 @@ public: std::map<std::string, std::string> stitchParameters, std::vector<PerThetaDefaults> perThetaDefaults); + bool isValid() const; AnalysisMode analysisMode() const; ReductionType reductionType() const; SummationType summationType() const; PolarizationCorrections const &polarizationCorrections() const; - RangeInLambda const &transissionRunRange() const; + RangeInLambda const &transmissionRunRange() const; std::map<std::string, std::string> stitchParameters() const; std::vector<PerThetaDefaults> const &perThetaDefaults() const; diff --git a/qt/scientific_interfaces/ISISReflectometry/Reduction/PolarizationCorrections.cpp b/qt/scientific_interfaces/ISISReflectometry/Reduction/PolarizationCorrections.cpp index 85bc1022223..b2022c72a86 100644 --- a/qt/scientific_interfaces/ISISReflectometry/Reduction/PolarizationCorrections.cpp +++ b/qt/scientific_interfaces/ISISReflectometry/Reduction/PolarizationCorrections.cpp @@ -13,5 +13,16 @@ double PolarizationCorrections::cAlpha() const { return CAlpha; } double PolarizationCorrections::cAp() const { return CAp; } double PolarizationCorrections::cPp() const { return CPp; } + +bool operator!=(PolarizationCorrections const &lhs, + PolarizationCorrections const &rhs) { + return !(lhs == rhs); +} + +bool operator==(PolarizationCorrections const &lhs, + PolarizationCorrections const &rhs) { + return lhs.cRho() == rhs.cRho() && lhs.cAlpha() == rhs.cAlpha() && + lhs.cAp() == rhs.cAp() && lhs.cPp() == rhs.cPp(); +} } // namespace CustomInterfaces } // namespace MantidQt diff --git a/qt/scientific_interfaces/ISISReflectometry/Reduction/PolarizationCorrections.h b/qt/scientific_interfaces/ISISReflectometry/Reduction/PolarizationCorrections.h index 63249b94bbf..25fe794e048 100644 --- a/qt/scientific_interfaces/ISISReflectometry/Reduction/PolarizationCorrections.h +++ b/qt/scientific_interfaces/ISISReflectometry/Reduction/PolarizationCorrections.h @@ -19,6 +19,13 @@ private: double CAp; double CPp; }; + +MANTIDQT_ISISREFLECTOMETRY_DLL bool +operator==(PolarizationCorrections const &lhs, + PolarizationCorrections const &rhs); +MANTIDQT_ISISREFLECTOMETRY_DLL bool +operator!=(PolarizationCorrections const &lhs, + PolarizationCorrections const &rhs); } // namespace CustomInterfaces } // namespace MantidQt #endif // MANTID_CUSTOMINTERFACES_POLARIZATIONCORRECTIONS_H_ diff --git a/qt/scientific_interfaces/test/ISISReflectometry/Experiment/ExperimentPresenterTest.h b/qt/scientific_interfaces/test/ISISReflectometry/Experiment/ExperimentPresenterTest.h index 2e0a4f75f0e..94387c36570 100644 --- a/qt/scientific_interfaces/test/ISISReflectometry/Experiment/ExperimentPresenterTest.h +++ b/qt/scientific_interfaces/test/ISISReflectometry/Experiment/ExperimentPresenterTest.h @@ -46,10 +46,24 @@ public: verifyAndClear(); } + void testModelUpdatedWhenAnalysisModeChanged() { + auto presenter = makePresenter(); + + expectViewReturnsSumInLambdaDefaults(); + EXPECT_CALL(m_view, getAnalysisMode()) + .WillOnce(Return(std::string("MultiDetectorAnalysis"))); + presenter.notifySettingsChanged(); + + TS_ASSERT_EQUALS(presenter.experiment().analysisMode(), + AnalysisMode::MultiDetector); + verifyAndClear(); + } + void testModelUpdatedWhenSummationTypeChanged() { auto presenter = makePresenter(); - expectViewReturnsDefaultValuesForSumInQ(); + expectViewReturnsDefaultAnalysisMode(); + expectViewReturnsSumInQDefaults(); presenter.notifySummationTypeChanged(); TS_ASSERT_EQUALS(presenter.experiment().summationType(), @@ -60,7 +74,8 @@ public: void testReductionTypeDisabledWhenChangeToSumInLambda() { auto presenter = makePresenter(); - expectViewReturnsDefaultValues(); + expectViewReturnsDefaultAnalysisMode(); + expectViewReturnsSumInLambdaDefaults(); EXPECT_CALL(m_view, disableReductionType()).Times(1); presenter.notifySummationTypeChanged(); @@ -70,13 +85,74 @@ public: void testReductionTypeEnbledWhenChangeToSumInQ() { auto presenter = makePresenter(); - expectViewReturnsDefaultValuesForSumInQ(); + expectViewReturnsDefaultAnalysisMode(); + expectViewReturnsSumInQDefaults(); EXPECT_CALL(m_view, enableReductionType()).Times(1); presenter.notifySummationTypeChanged(); verifyAndClear(); } + void testSetPolarizationCorrections() { + auto presenter = makePresenter(); + PolarizationCorrections polCorr(1.2, 1.3, 2.4, 2.5); + + expectViewReturnsDefaultValues(); + EXPECT_CALL(m_view, getCRho()).WillOnce(Return(polCorr.cRho())); + EXPECT_CALL(m_view, getCAlpha()).WillOnce(Return(polCorr.cAlpha())); + EXPECT_CALL(m_view, getCAp()).WillOnce(Return(polCorr.cAp())); + EXPECT_CALL(m_view, getCPp()).WillOnce(Return(polCorr.cPp())); + presenter.notifySettingsChanged(); + + TS_ASSERT_EQUALS(presenter.experiment().polarizationCorrections(), polCorr); + verifyAndClear(); + } + + void testSetTransmissionRunRange() { + auto presenter = makePresenter(); + RangeInLambda range(7.2, 10); + + expectViewReturnsDefaultValues(); + EXPECT_CALL(m_view, getTransmissionStartOverlap()).WillOnce(Return(range.min())); + EXPECT_CALL(m_view, getTransmissionEndOverlap()).WillOnce(Return(range.max())); + EXPECT_CALL(m_view, showTransmissionRangeValid()).Times(1); + presenter.notifySettingsChanged(); + + TS_ASSERT_EQUALS(presenter.experiment().transmissionRunRange(), range); + verifyAndClear(); + } + + void testSetTransmissionRunRangeInvalid() { + auto presenter = makePresenter(); + RangeInLambda range(10.2, 7.1); + + expectViewReturnsDefaultValues(); + EXPECT_CALL(m_view, getTransmissionStartOverlap()).WillOnce(Return(range.min())); + EXPECT_CALL(m_view, getTransmissionEndOverlap()).WillOnce(Return(range.max())); + EXPECT_CALL(m_view, showTransmissionRangeInvalid()).Times(1); + presenter.notifySettingsChanged(); + + TS_ASSERT_EQUALS(presenter.experiment().transmissionRunRange(), range); + verifyAndClear(); + } + + void testSetStitchOptions() { + auto presenter = makePresenter(); + auto const optionsString = "Params=0.02"; + std::map<std::string, std::string> optionsMap = {{"Params", "0.02"}}; + + expectViewReturnsDefaultValues(); + EXPECT_CALL(m_view, getStitchOptions()).WillOnce(Return(optionsString)); + presenter.notifySettingsChanged(); + + TS_ASSERT_EQUALS(presenter.experiment().stitchParameters(), optionsMap); + verifyAndClear(); + } + + void testSetStitchOptionsInvalid() { + // TODO + } + void testNewPerAngleDefaultsRequested() { auto presenter = makePresenter(); @@ -123,6 +199,43 @@ public: verifyAndClear(); } + // TODO + void testSetMultipleUniqueAngles() {} + + void testSetMultipoleNonUniqueAngles() {} + + void testSetAngleAndWildcardRow() {} + + void testSetSingleWildcardRow() {} + + void testSetMultipleWildcardRows() {} + + void testSetFirstTransmissionRun() {} + + void testSetSecondTransmissionRun() {} + + void testSetBothTransmissionRuns() {} + + void testSetQMin() {} + + void testSetQMinInvalid() {} + + void testSetQMax() {} + + void testSetQMaxInvalid() {} + + void testSetQStep() {} + + void testSetQStepInvalid() {} + + void testSetScale() {} + + void testSetScaleInvalid() {} + + void testSetProcessingInstructions() {} + + void testSetProcessingInstructionsInvalid() {} + private: NiceMock<MockExperimentView> m_view; @@ -139,24 +252,30 @@ private: TS_ASSERT(Mock::VerifyAndClearExpectations(&m_view)); } - void expectViewReturnsDefaultValues() { + void expectViewReturnsDefaultAnalysisMode() { EXPECT_CALL(m_view, getAnalysisMode()) .WillOnce(Return(std::string("PointDetectorAnalysis"))); + } + + void expectViewReturnsSumInLambdaDefaults() { EXPECT_CALL(m_view, getSummationType()) .WillOnce(Return(std::string("SumInLambda"))); EXPECT_CALL(m_view, getReductionType()) .WillOnce(Return(std::string("Normal"))); } - void expectViewReturnsDefaultValuesForSumInQ() { - EXPECT_CALL(m_view, getAnalysisMode()) - .WillOnce(Return(std::string("PointDetectorAnalysis"))); + void expectViewReturnsSumInQDefaults() { EXPECT_CALL(m_view, getSummationType()) .WillOnce(Return(std::string("SumInQ"))); EXPECT_CALL(m_view, getReductionType()) .WillOnce(Return(std::string("DivergentBeam"))); } + void expectViewReturnsDefaultValues() { + expectViewReturnsDefaultAnalysisMode(); + expectViewReturnsSumInLambdaDefaults(); + } + // These functions create various rows in the per-theta defaults tables, // either as an input array of strings or an output model OptionsRow optionsWithAngleAndOneTrans() { return {"0.5", "13463"}; } diff --git a/qt/scientific_interfaces/test/ISISReflectometry/Experiment/MockExperimentView.h b/qt/scientific_interfaces/test/ISISReflectometry/Experiment/MockExperimentView.h index 61b5bb0cdb8..f000bc0350e 100644 --- a/qt/scientific_interfaces/test/ISISReflectometry/Experiment/MockExperimentView.h +++ b/qt/scientific_interfaces/test/ISISReflectometry/Experiment/MockExperimentView.h @@ -33,6 +33,8 @@ public: MOCK_METHOD1(setTransmissionStartOverlap, void(double)); MOCK_CONST_METHOD0(getTransmissionEndOverlap, double()); MOCK_METHOD1(setTransmissionEndOverlap, void(double)); + MOCK_METHOD0(showTransmissionRangeValid, void(void)); + MOCK_METHOD0(showTransmissionRangeInvalid, void(void)); MOCK_CONST_METHOD0(getPolarisationCorrectionType, std::string()); MOCK_METHOD1(setPolarisationCorrectionType, void(std::string const &)); MOCK_CONST_METHOD0(getCRho, double()); -- GitLab