// 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;
using System.Globalization;
using System.Windows.Forms;
using Core.Common.Base;
using NUnit.Extensions.Forms;
using NUnit.Framework;
using Rhino.Mocks;
using Ringtoets.AssemblyTool.Data;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.TestUtil;
using Ringtoets.Common.Forms.Helpers;
using Ringtoets.Common.Forms.Views;
using Ringtoets.Common.Primitives;
using Ringtoets.GrassCoverErosionInwards.Data;
using Ringtoets.GrassCoverErosionInwards.Data.TestUtil;
using Ringtoets.GrassCoverErosionInwards.Forms.Views;
namespace Ringtoets.GrassCoverErosionInwards.Forms.Test.Views
{
[TestFixture]
public class GrassCoverErosionInwardsFailureMechanismResultViewTest
{
private const int nameColumnIndex = 0;
private const int simpleAssessmentResultIndex = 1;
private const int detailedAssessmentResultIndex = 2;
private const int detailedAssessmentProbabilityIndex = 3;
private const int tailorMadeAssessmentResultIndex = 4;
private const int tailorMadeAssessmentProbabilityIndex = 5;
private const int simpleAssemblyCategoryGroupIndex = 6;
private const int detailedAssemblyCategoryGroupIndex = 7;
private const int tailorMadeAssemblyCategoryGroupIndex = 8;
private const int combinedAssemblyCategoryGroupIndex = 9;
private const int combinedAssemblyProbabilityIndex = 10;
private const int useManualAssemblyProbabilityIndex = 11;
private const int manualAssemblyProbabilityIndex = 12;
private const int columnCount = 13;
private Form testForm;
[SetUp]
public void Setup()
{
testForm = new Form();
}
[TearDown]
public void TearDown()
{
testForm.Dispose();
}
[Test]
public void Constructor_ExpectedValues()
{
// Setup
var mocks = new MockRepository();
var assessmentSection = mocks.Stub();
mocks.ReplayAll();
var failureMechanism = new GrassCoverErosionInwardsFailureMechanism();
// Call
using (var view = new GrassCoverErosionInwardsFailureMechanismResultView(failureMechanism.SectionResults, failureMechanism, assessmentSection))
{
// Assert
Assert.IsInstanceOf>(view);
Assert.IsNull(view.Data);
Assert.AreSame(failureMechanism, view.FailureMechanism);
}
mocks.VerifyAll();
}
[Test]
public void Constructor_AssessmentSectionNull_ThrowsArgumentNullException()
{
// Setup
var failureMechanism = new GrassCoverErosionInwardsFailureMechanism();
// Call
TestDelegate call = () => new GrassCoverErosionInwardsFailureMechanismResultView(failureMechanism.SectionResults, failureMechanism, null);
// Assert
var exception = Assert.Throws(call);
Assert.AreEqual("assessmentSection", exception.ParamName);
}
[Test]
public void GivenFormWithGrassCoverErosionInwardsFailureMechanismResultView_ThenExpectedColumnsAreVisible()
{
// Given
using (ShowFailureMechanismResultsView(new ObservableList()))
{
// Then
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
Assert.AreEqual(13, dataGridView.ColumnCount);
Assert.IsTrue(dataGridView.Columns[detailedAssessmentProbabilityIndex].ReadOnly);
Assert.IsInstanceOf(dataGridView.Columns[nameColumnIndex]);
Assert.IsInstanceOf(dataGridView.Columns[simpleAssessmentResultIndex]);
Assert.IsInstanceOf(dataGridView.Columns[detailedAssessmentResultIndex]);
Assert.IsInstanceOf(dataGridView.Columns[detailedAssessmentProbabilityIndex]);
Assert.IsInstanceOf(dataGridView.Columns[tailorMadeAssessmentResultIndex]);
Assert.IsInstanceOf(dataGridView.Columns[tailorMadeAssessmentProbabilityIndex]);
Assert.IsInstanceOf(dataGridView.Columns[simpleAssemblyCategoryGroupIndex]);
Assert.IsInstanceOf(dataGridView.Columns[detailedAssemblyCategoryGroupIndex]);
Assert.IsInstanceOf(dataGridView.Columns[tailorMadeAssemblyCategoryGroupIndex]);
Assert.IsInstanceOf(dataGridView.Columns[combinedAssemblyCategoryGroupIndex]);
Assert.IsInstanceOf(dataGridView.Columns[combinedAssemblyProbabilityIndex]);
Assert.IsInstanceOf(dataGridView.Columns[useManualAssemblyProbabilityIndex]);
Assert.IsInstanceOf(dataGridView.Columns[manualAssemblyProbabilityIndex]);
Assert.AreEqual("Vak", dataGridView.Columns[nameColumnIndex].HeaderText);
Assert.AreEqual("Eenvoudige toets", dataGridView.Columns[simpleAssessmentResultIndex].HeaderText);
Assert.AreEqual("Gedetailleerde toets per vak", dataGridView.Columns[detailedAssessmentResultIndex].HeaderText);
Assert.AreEqual("Gedetailleerde toets per vak\r\nfaalkans", dataGridView.Columns[detailedAssessmentProbabilityIndex].HeaderText);
Assert.AreEqual("Toets op maat", dataGridView.Columns[tailorMadeAssessmentResultIndex].HeaderText);
Assert.AreEqual("Toets op maat\r\nfaalkans", dataGridView.Columns[tailorMadeAssessmentProbabilityIndex].HeaderText);
Assert.AreEqual("Assemblageresultaat\r\neenvoudige toets", dataGridView.Columns[simpleAssemblyCategoryGroupIndex].HeaderText);
Assert.AreEqual("Assemblageresultaat\r\ngedetailleerde toets per vak", dataGridView.Columns[detailedAssemblyCategoryGroupIndex].HeaderText);
Assert.AreEqual("Assemblageresultaat\r\ntoets op maat", dataGridView.Columns[tailorMadeAssemblyCategoryGroupIndex].HeaderText);
Assert.AreEqual("Assemblageresultaat\r\ngecombineerd", dataGridView.Columns[combinedAssemblyCategoryGroupIndex].HeaderText);
Assert.AreEqual("Assemblageresultaat\r\ngecombineerde\r\nfaalkansschatting", dataGridView.Columns[combinedAssemblyProbabilityIndex].HeaderText);
Assert.AreEqual("Overschrijf\r\nassemblageresultaat", dataGridView.Columns[useManualAssemblyProbabilityIndex].HeaderText);
Assert.AreEqual("Assemblageresultaat\r\nhandmatig", dataGridView.Columns[manualAssemblyProbabilityIndex].HeaderText);
Assert.AreEqual(DataGridViewAutoSizeColumnsMode.AllCells, dataGridView.AutoSizeColumnsMode);
Assert.AreEqual(DataGridViewContentAlignment.MiddleCenter, dataGridView.ColumnHeadersDefaultCellStyle.Alignment);
}
}
[Test]
public void FailureMechanismResultsView_AllDataSet_DataGridViewCorrectlyInitialized()
{
// Setup & Call
using (CreateConfiguredFailureMechanismResultsView())
{
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
// Assert
DataGridViewRowCollection rows = dataGridView.Rows;
Assert.AreEqual(1, rows.Count);
DataGridViewCellCollection cells = rows[0].Cells;
Assert.AreEqual(columnCount, cells.Count);
Assert.AreEqual("Section 1", cells[nameColumnIndex].FormattedValue);
Assert.AreEqual(SimpleAssessmentResultValidityOnlyType.None, cells[simpleAssessmentResultIndex].Value);
Assert.AreEqual(DetailedAssessmentResultType.Probability, cells[detailedAssessmentResultIndex].Value);
Assert.AreEqual("-", cells[detailedAssessmentProbabilityIndex].FormattedValue);
Assert.AreEqual(TailorMadeAssessmentProbabilityCalculationResultType.None, cells[tailorMadeAssessmentResultIndex].Value);
Assert.AreEqual("-", cells[tailorMadeAssessmentProbabilityIndex].FormattedValue);
Assert.AreEqual(FailureMechanismSectionAssemblyCategoryGroup.None, cells[simpleAssemblyCategoryGroupIndex].Value);
Assert.AreEqual(FailureMechanismSectionAssemblyCategoryGroup.None, cells[detailedAssemblyCategoryGroupIndex].Value);
Assert.AreEqual(FailureMechanismSectionAssemblyCategoryGroup.None, cells[tailorMadeAssemblyCategoryGroupIndex].Value);
Assert.AreEqual(FailureMechanismSectionAssemblyCategoryGroup.None, cells[combinedAssemblyCategoryGroupIndex].Value);
Assert.AreEqual("-", cells[combinedAssemblyProbabilityIndex].FormattedValue);
Assert.AreEqual(false, cells[useManualAssemblyProbabilityIndex].Value);
Assert.AreEqual("-", cells[manualAssemblyProbabilityIndex].FormattedValue);
}
}
[Test]
[TestCase("test")]
[TestCase(";/[].,~!@#$%^&*()_-+={}|?")]
public void FailureMechanismResultView_EditValueNotAProbability_ShowsErrorTooltip(string newValue)
{
// Setup
using (CreateConfiguredFailureMechanismResultsView())
{
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
// Call
dataGridView.Rows[0].Cells[tailorMadeAssessmentProbabilityIndex].Value = newValue;
// Assert
Assert.AreEqual("De waarde kon niet geïnterpreteerd worden als een kans.", dataGridView.Rows[0].ErrorText);
}
}
[Test]
[SetCulture("nl-NL")]
[TestCase(1.01)]
[TestCase(-0.01)]
[TestCase(5)]
[TestCase(-10)]
public void FailureMechanismResultView_EditValueTailorMadeAssessmentProbabilityInvalid_ShowErrorToolTip(double newValue)
{
// Setup
using (CreateConfiguredFailureMechanismResultsView())
{
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
// Call
dataGridView.Rows[0].Cells[tailorMadeAssessmentProbabilityIndex].Value = newValue.ToString(CultureInfo.CurrentCulture);
// Assert
Assert.AreEqual("De waarde voor de faalkans moet in het bereik [0,0, 1,0] liggen.", dataGridView.Rows[0].ErrorText);
}
}
[Test]
[SetCulture("nl-NL")]
[TestCase(1)]
[TestCase(0)]
[TestCase(0.5)]
[TestCase(1e-6)]
[TestCase(double.NaN)]
public void FailureMechanismResultView_EditValueTailorMadeAssessmentValid_DoNotShowErrorToolTipAndEditValue(double newValue)
{
// Setup
var result = new GrassCoverErosionInwardsFailureMechanismSectionResult(FailureMechanismSectionTestFactory.CreateFailureMechanismSection());
using (ShowFailureMechanismResultsView(new ObservableList
{
result
}))
{
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
// Call
dataGridView.Rows[0].Cells[tailorMadeAssessmentProbabilityIndex].Value = newValue.ToString(CultureInfo.CurrentCulture);
// Assert
Assert.IsEmpty(dataGridView.Rows[0].ErrorText);
Assert.AreEqual(newValue, result.TailorMadeAssessmentProbability);
}
}
[Test]
[TestCase(SimpleAssessmentResultValidityOnlyType.None)]
[TestCase(SimpleAssessmentResultValidityOnlyType.Applicable)]
public void GivenSectionResultAndSuccessfulCalculation_WhenChangingCalculationToFailed_ThenDetailedAssessmentHasError(
SimpleAssessmentResultValidityOnlyType simpleAssessmentResult)
{
// Given
var sectionResult = new GrassCoverErosionInwardsFailureMechanismSectionResult(FailureMechanismSectionTestFactory.CreateFailureMechanismSection())
{
Calculation = new GrassCoverErosionInwardsCalculation
{
Output = new GrassCoverErosionInwardsOutput(new TestOvertoppingOutput(0.56789),
new TestDikeHeightOutput(0),
new TestOvertoppingRateOutput(0))
},
SimpleAssessmentResult = simpleAssessmentResult
};
using (ShowFailureMechanismResultsView(
new ObservableList
{
sectionResult
}))
{
var gridTester = new ControlTester("dataGridView");
var dataGridView = (DataGridView) gridTester.TheObject;
DataGridViewCell dataGridViewCell = dataGridView.Rows[0].Cells[detailedAssessmentProbabilityIndex];
// Precondition
Assert.AreEqual(ProbabilityFormattingHelper.Format(0.25), dataGridViewCell.FormattedValue);
Assert.IsEmpty(dataGridViewCell.ErrorText);
// When
sectionResult.Calculation = new GrassCoverErosionInwardsCalculation
{
Output = new GrassCoverErosionInwardsOutput(new TestOvertoppingOutput(double.NaN),
new TestDikeHeightOutput(double.NaN),
new TestOvertoppingRateOutput(double.NaN))
};
sectionResult.NotifyObservers();
// Then
Assert.AreEqual("-", dataGridViewCell.FormattedValue);
Assert.AreEqual("De maatgevende berekening voor dit vak moet een geldige uitkomst hebben.", dataGridViewCell.ErrorText);
}
}
private GrassCoverErosionInwardsFailureMechanismResultView CreateConfiguredFailureMechanismResultsView()
{
var results = new ObservableList
{
new GrassCoverErosionInwardsFailureMechanismSectionResult(FailureMechanismSectionTestFactory.CreateFailureMechanismSection("Section 1"))
};
GrassCoverErosionInwardsFailureMechanismResultView failureMechanismResultView = ShowFailureMechanismResultsView(results);
return failureMechanismResultView;
}
private GrassCoverErosionInwardsFailureMechanismResultView ShowFailureMechanismResultsView(
IObservableEnumerable sectionResults)
{
var failureMechanismResultView = new GrassCoverErosionInwardsFailureMechanismResultView(sectionResults,
new GrassCoverErosionInwardsFailureMechanism(),
new ObservableTestAssessmentSectionStub());
testForm.Controls.Add(failureMechanismResultView);
testForm.Show();
return failureMechanismResultView;
}
}
}