// Copyright (C) Stichting Deltares 2017. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Core.Common.Util.Reflection;
using NUnit.Extensions.Forms;
using NUnit.Framework;
using Ringtoets.AssemblyTool.Data;
using Ringtoets.AssemblyTool.KernelWrapper.Calculators;
using Ringtoets.AssemblyTool.KernelWrapper.TestUtil.Calculators;
using Ringtoets.AssemblyTool.KernelWrapper.TestUtil.Calculators.Assembly;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Data.TestUtil;
using Ringtoets.Common.Forms.Controls;
using Ringtoets.Common.Forms.Views;
namespace Ringtoets.Common.Forms.TestUtil
{
///
/// Class for testing data and styling in a view with a .
///
/// The type of the view to test.
/// The type of the failure mechanism the view belongs to.
/// The type of the section results shown in the view.
/// The type of the presentation objects used in the view.
/// The type of calculations to get the input from.
/// The type of the input of a calculation.
public abstract class FailureMechanismAssemblyControlTester
where TView : FailureMechanismResultView
where TFailureMechanism : IFailureMechanism, IHasSectionResults, ICalculatableFailureMechanism, new()
where TSectionResult : FailureMechanismSectionResult
where TResultRow : FailureMechanismSectionResultRow
where TCalculation : ICalculation
where TCalculationInput : ICalculationInput
{
private Form testForm;
[SetUp]
public void Setup()
{
testForm = new Form();
}
[TearDown]
public void TearDown()
{
testForm.Dispose();
}
[Test]
public void FailureMechanismResultsView_Always_FailureMechanismResultControlCorrectlyInitialized()
{
// Setup
var failureMechanism = new TFailureMechanism();
failureMechanism.AddSection(FailureMechanismSectionTestFactory.CreateFailureMechanismSection());
// Call
using (new AssemblyToolCalculatorFactoryConfig())
using (ShowFailureMechanismResultsView(failureMechanism))
{
// Assert
var assemblyResultPanel = (TableLayoutPanel) new ControlTester("TableLayoutPanel").TheObject;
var assemblyResultControl = (FailureMechanismAssemblyControl) assemblyResultPanel.GetControlFromPosition(1, 0);
Assert.IsInstanceOf(assemblyResultControl);
Assert.AreEqual(DockStyle.Left, assemblyResultControl.Dock);
}
}
[Test]
public void GivenFailureMechanismResultsView_WhenNoExceptionThrownByCalculator_ErrorSetToControl()
{
// Given
var failureMechanism = new TFailureMechanism();
using (new AssemblyToolCalculatorFactoryConfig())
using (ShowFailureMechanismResultsView(failureMechanism))
{
// Precondition
FailureMechanismAssemblyControl assemblyControl = GetFailureMechanismAssemblyControl();
ErrorProvider errorProvider = GetErrorProvider(assemblyControl);
Assert.IsEmpty(errorProvider.GetError(assemblyControl));
// When
var calculatorfactory = (TestAssemblyToolCalculatorFactory) AssemblyToolCalculatorFactory.Instance;
FailureMechanismAssemblyCalculatorStub calculator = calculatorfactory.LastCreatedFailureMechanismAssemblyCalculator;
calculator.ThrowExceptionOnCalculate = true;
failureMechanism.NotifyObservers();
// Then
Assert.AreEqual("Message", errorProvider.GetError(assemblyControl));
}
}
[Test]
public void GivenFailureMechanismResultsView_WhenNoExceptionThrownByCalculator_ErrorCleared()
{
// Given
var failureMechanism = new TFailureMechanism();
using (new AssemblyToolCalculatorFactoryConfig())
using (ShowFailureMechanismResultsView(failureMechanism))
{
var calculatorfactory = (TestAssemblyToolCalculatorFactory) AssemblyToolCalculatorFactory.Instance;
FailureMechanismAssemblyCalculatorStub calculator = calculatorfactory.LastCreatedFailureMechanismAssemblyCalculator;
calculator.ThrowExceptionOnCalculate = true;
failureMechanism.NotifyObservers();
// Precondition
FailureMechanismAssemblyControl assemblyControl = GetFailureMechanismAssemblyControl();
ErrorProvider errorProvider = GetErrorProvider(assemblyControl);
Assert.AreEqual("Message", errorProvider.GetError(assemblyControl));
// When
calculator.ThrowExceptionOnCalculate = false;
failureMechanism.NotifyObservers();
// Then
Assert.IsEmpty(errorProvider.GetError(assemblyControl));
}
}
[Test]
public void GivenFailureMechanismResultsViewWithAssemblyResult_WhenFailureMechanismAssemblyResultChangedAndSectionResultNotified_FailureMechanismAssemblyResultUpdated()
{
// Given
var failureMechanism = new TFailureMechanism();
failureMechanism.AddSection(FailureMechanismSectionTestFactory.CreateFailureMechanismSection());
using (new AssemblyToolCalculatorFactoryConfig())
using (ShowFailureMechanismResultsView(failureMechanism))
{
// Precondition
BorderedLabel assemblyGroupLabel = GetGroupLabel();
BorderedLabel assemblyProbabilityLabel = GetProbabilityLabelControl();
Assert.AreEqual("1/1", assemblyProbabilityLabel.Text);
Assert.AreEqual("IIIt", assemblyGroupLabel.Text);
// When
var calculatorfactory = (TestAssemblyToolCalculatorFactory) AssemblyToolCalculatorFactory.Instance;
FailureMechanismAssemblyCalculatorStub calculator = calculatorfactory.LastCreatedFailureMechanismAssemblyCalculator;
calculator.FailureMechanismAssemblyOutput = new FailureMechanismAssembly(0.5, FailureMechanismAssemblyCategoryGroup.VIt);
failureMechanism.SectionResults.Single().NotifyObservers();
// Then
Assert.AreEqual("1/2", assemblyProbabilityLabel.Text);
Assert.AreEqual("VIt", assemblyGroupLabel.Text);
}
}
[Test]
public void GivenFailureMechanismResultsViewWithAssemblyResult_WhenFailureMechanismAssemblyResultChangedAndCalculationNotified_FailureMechanismAssemblyResultUpdated()
{
// Given
var failureMechanism = new TFailureMechanism();
TCalculation calculation = CreateCalculation();
failureMechanism.CalculationsGroup.Children.Add(calculation);
using (new AssemblyToolCalculatorFactoryConfig())
using (ShowFailureMechanismResultsView(failureMechanism))
{
// Precondition
BorderedLabel assemblyGroupLabel = GetGroupLabel();
BorderedLabel assemblyProbabilityLabel = GetProbabilityLabelControl();
Assert.AreEqual("1/1", assemblyProbabilityLabel.Text);
Assert.AreEqual("IIIt", assemblyGroupLabel.Text);
// When
var calculatorfactory = (TestAssemblyToolCalculatorFactory) AssemblyToolCalculatorFactory.Instance;
FailureMechanismAssemblyCalculatorStub calculator = calculatorfactory.LastCreatedFailureMechanismAssemblyCalculator;
calculator.FailureMechanismAssemblyOutput = new FailureMechanismAssembly(0.5, FailureMechanismAssemblyCategoryGroup.VIt);
calculation.NotifyObservers();
// Then
Assert.AreEqual("1/2", assemblyProbabilityLabel.Text);
Assert.AreEqual("VIt", assemblyGroupLabel.Text);
}
}
[Test]
public void GivenFailureMechanismResultsViewWithAssemblyResult_WhenFailureMechanismAssemblyResultChangedAndCalculationInputNotified_FailureMechanismAssemblyResultUpdated()
{
// Given
var failureMechanism = new TFailureMechanism();
TCalculation calculation = CreateCalculation();
failureMechanism.CalculationsGroup.Children.Add(calculation);
using (new AssemblyToolCalculatorFactoryConfig())
using (ShowFailureMechanismResultsView(failureMechanism))
{
// Precondition
BorderedLabel assemblyGroupLabel = GetGroupLabel();
BorderedLabel assemblyProbabilityLabel = GetProbabilityLabelControl();
Assert.AreEqual("1/1", assemblyProbabilityLabel.Text);
Assert.AreEqual("IIIt", assemblyGroupLabel.Text);
// When
var calculatorfactory = (TestAssemblyToolCalculatorFactory) AssemblyToolCalculatorFactory.Instance;
FailureMechanismAssemblyCalculatorStub calculator = calculatorfactory.LastCreatedFailureMechanismAssemblyCalculator;
calculator.FailureMechanismAssemblyOutput = new FailureMechanismAssembly(0.5, FailureMechanismAssemblyCategoryGroup.VIt);
GetInput(calculation).NotifyObservers();
// Then
Assert.AreEqual("1/2", assemblyProbabilityLabel.Text);
Assert.AreEqual("VIt", assemblyGroupLabel.Text);
}
}
///
/// Method for creating an instance of .
///
/// The failure mechanism to create the view for.
/// A new .
protected abstract TView CreateResultView(TFailureMechanism failureMechanism);
///
/// Method for creating an instance of .
///
/// A new .
protected abstract TCalculation CreateCalculation();
///
/// Method to get the from a
/// .
///
/// The calculation to get the input from.
/// A new .
protected abstract TCalculationInput GetInput(TCalculation calculation);
private static BorderedLabel GetGroupLabel()
{
return (BorderedLabel) new ControlTester("GroupLabel").TheObject;
}
private static BorderedLabel GetProbabilityLabelControl()
{
return (BorderedLabel) new ControlTester("ProbabilityLabel").TheObject;
}
private static ErrorProvider GetErrorProvider(FailureMechanismAssemblyControl control)
{
return TypeUtils.GetField(control, "ErrorProvider");
}
private static FailureMechanismAssemblyControl GetFailureMechanismAssemblyControl()
{
return (FailureMechanismAssemblyControl) ((TableLayoutPanel) new ControlTester("TableLayoutPanel").TheObject).GetControlFromPosition(1, 0);
}
private TView ShowFailureMechanismResultsView(TFailureMechanism failureMechanism)
{
TView failureMechanismResultView = CreateResultView(failureMechanism);
testForm.Controls.Add(failureMechanismResultView);
testForm.Show();
return failureMechanismResultView;
}
}
}