// Copyright (C) Stichting Deltares 2016. 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.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Core.Common.Base.Data;
using Core.Common.Base.Geometry;
using NUnit.Extensions.Forms;
using NUnit.Framework;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Data.Probability;
using Ringtoets.Common.Forms.Helpers;
using Ringtoets.Common.Forms.TestUtil;
using Ringtoets.Common.Forms.Views;
using Ringtoets.GrassCoverErosionInwards.Data;
using Ringtoets.GrassCoverErosionInwards.Forms.Views;
namespace Ringtoets.GrassCoverErosionInwards.Forms.Test.Views
{
[TestFixture]
public class GrassCoverErosionInwardsFailureMechanismResultViewTest
{
private const int nameColumnIndex = 0;
private const int assessmentLayerOneIndex = 1;
private const int assessmentLayerTwoAIndex = 2;
private const int assessmentLayerThreeIndex = 3;
private Form testForm;
[SetUp]
public void Setup()
{
testForm = new Form();
}
[TearDown]
public void TearDown()
{
testForm.Dispose();
}
[Test]
public void DefaultConstructor_DefaultValues()
{
// Call
using (var view = new GrassCoverErosionInwardsFailureMechanismResultView())
{
// Assert
Assert.IsInstanceOf>(view);
Assert.IsNull(view.Data);
}
}
[Test]
public void GivenFormWithGrassCoverErosionInwardsFailureMechanismResultView_ThenExpectedColumnsAreVisible()
{
// Call
using (ShowFailureMechanismResultsView())
{
// Assert
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
Assert.AreEqual(4, dataGridView.ColumnCount);
Assert.IsTrue(dataGridView.Columns[assessmentLayerTwoAIndex].ReadOnly);
Assert.IsInstanceOf(dataGridView.Columns[assessmentLayerOneIndex]);
Assert.IsInstanceOf(dataGridView.Columns[assessmentLayerTwoAIndex]);
Assert.IsInstanceOf(dataGridView.Columns[assessmentLayerThreeIndex]);
Assert.AreEqual("Toetslaag 1", dataGridView.Columns[assessmentLayerOneIndex].HeaderText);
Assert.AreEqual("Toetslaag 2a", dataGridView.Columns[assessmentLayerTwoAIndex].HeaderText);
Assert.AreEqual("Toetslaag 3", dataGridView.Columns[assessmentLayerThreeIndex].HeaderText);
Assert.AreEqual(DataGridViewAutoSizeColumnsMode.AllCells, dataGridView.AutoSizeColumnsMode);
Assert.AreEqual(DataGridViewContentAlignment.MiddleCenter, dataGridView.ColumnHeadersDefaultCellStyle.Alignment);
}
}
[Test]
public void Data_DataAlreadySetNewDataSet_DataSetAndDataGridViewUpdated()
{
// Setup
using (var view = ShowFullyConfiguredFailureMechanismResultsView())
{
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
FailureMechanismSection section = CreateSimpleFailureMechanismSection();
var sectionResult = new GrassCoverErosionInwardsFailureMechanismSectionResult(section);
var testData = new List
{
sectionResult
};
// Precondition
Assert.AreEqual(2, dataGridView.RowCount);
// Call
view.Data = testData;
// Assert
Assert.AreSame(testData, view.Data);
Assert.AreEqual(testData.Count, dataGridView.RowCount);
Assert.AreEqual(sectionResult.Section.Name, dataGridView.Rows[0].Cells[0].Value);
}
}
[Test]
public void Data_SetOtherThanFailureMechanismSectionResultListData_DataNullAndDataGridViewEmpty()
{
// Setup
var testData = new object();
using (var view = ShowFullyConfiguredFailureMechanismResultsView())
{
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
// Call
view.Data = testData;
// Assert
Assert.IsNull(view.Data);
Assert.AreEqual(0, dataGridView.RowCount);
}
}
[Test]
public void FailureMechanismResultsView_AllDataSet_DataGridViewCorrectlyInitialized()
{
// Setup & Call
using (ShowFullyConfiguredFailureMechanismResultsView())
{
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
// Assert
var rows = dataGridView.Rows;
Assert.AreEqual(2, rows.Count);
var cells = rows[0].Cells;
Assert.AreEqual(4, cells.Count);
Assert.AreEqual("Section 1", cells[nameColumnIndex].FormattedValue);
Assert.AreEqual(AssessmentLayerOneState.NotAssessed, cells[assessmentLayerOneIndex].Value);
Assert.AreEqual("-", cells[assessmentLayerTwoAIndex].FormattedValue);
Assert.AreEqual("-", cells[assessmentLayerThreeIndex].FormattedValue);
cells = rows[1].Cells;
Assert.AreEqual(4, cells.Count);
Assert.AreEqual("Section 2", cells[nameColumnIndex].FormattedValue);
Assert.AreEqual(AssessmentLayerOneState.NotAssessed, cells[assessmentLayerOneIndex].Value);
Assert.AreEqual("-", cells[assessmentLayerTwoAIndex].FormattedValue);
Assert.AreEqual("-", cells[assessmentLayerThreeIndex].FormattedValue);
}
}
[Test]
[SetCulture("nl-NL")]
[TestCase(AssessmentLayerOneState.NotAssessed)]
[TestCase(AssessmentLayerOneState.NeedsDetailedAssessment)]
[TestCase(AssessmentLayerOneState.Sufficient)]
public void FailureMechanismResultsView_ChangeCheckBox_DataGridViewCorrectlySyncedAndStylingSet(
AssessmentLayerOneState assessmentLayerOneState)
{
// Setup
using (ShowFullyConfiguredFailureMechanismResultsView())
{
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
// Call
dataGridView.Rows[0].Cells[assessmentLayerOneIndex].Value = assessmentLayerOneState;
// Assert
var rows = dataGridView.Rows;
var cells = rows[0].Cells;
Assert.AreEqual(4, cells.Count);
Assert.AreEqual("Section 1", cells[nameColumnIndex].FormattedValue);
var cellAssessmentLayerTwoA = cells[assessmentLayerTwoAIndex];
var cellAssessmentLayerThree = cells[assessmentLayerThreeIndex];
DataGridViewCell dataGridViewCell = cells[assessmentLayerOneIndex];
Assert.AreEqual(assessmentLayerOneState, dataGridViewCell.Value);
Assert.AreEqual("-", cellAssessmentLayerTwoA.FormattedValue);
Assert.AreEqual("-", cellAssessmentLayerThree.FormattedValue);
Assert.IsEmpty(dataGridViewCell.ErrorText);
if (assessmentLayerOneState == AssessmentLayerOneState.Sufficient)
{
DataGridViewCellTestHelper.AssertCellIsDisabled(cellAssessmentLayerTwoA);
DataGridViewCellTestHelper.AssertCellIsDisabled(cellAssessmentLayerThree);
Assert.IsTrue(cellAssessmentLayerThree.ReadOnly);
}
else
{
DataGridViewCellTestHelper.AssertCellIsEnabled(cellAssessmentLayerTwoA, true);
DataGridViewCellTestHelper.AssertCellIsEnabled(cellAssessmentLayerThree);
Assert.IsFalse(cellAssessmentLayerThree.ReadOnly);
}
}
}
[Test]
[TestCase("test", assessmentLayerThreeIndex)]
[TestCase(";/[].,~!@#$%^&*()_-+={}|?", assessmentLayerThreeIndex)]
public void FailureMechanismResultView_EditValueInvalid_ShowsErrorTooltip(string newValue, int cellIndex)
{
// Setup
using (ShowFullyConfiguredFailureMechanismResultsView())
{
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
// Call
dataGridView.Rows[0].Cells[cellIndex].Value = newValue;
// Assert
Assert.AreEqual("De tekst moet een getal zijn.", dataGridView.Rows[0].ErrorText);
}
}
[Test]
[TestCase("1", assessmentLayerThreeIndex, "AssessmentLayerThree")]
[TestCase("1e-6", assessmentLayerThreeIndex, "AssessmentLayerThree")]
[TestCase("1e+6", assessmentLayerThreeIndex, "AssessmentLayerThree")]
[TestCase("14.3", assessmentLayerThreeIndex, "AssessmentLayerThree")]
public void FailureMechanismResultView_EditValueValid_DoNotShowErrorToolTipAndEditValue(string newValue, int cellIndex, string propertyName)
{
// Setup
using (var view = ShowFullyConfiguredFailureMechanismResultsView())
{
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
// Call
dataGridView.Rows[0].Cells[cellIndex].Value = newValue;
// Assert
Assert.IsEmpty(dataGridView.Rows[0].ErrorText);
var dataObject = view.Data as List;
Assert.IsNotNull(dataObject);
var row = dataObject.First();
var propertyValue = row.GetType().GetProperty(propertyName).GetValue(row, null);
Assert.AreEqual((RoundedDouble) double.Parse(newValue), propertyValue);
}
}
[Test]
public void GivenSectionResultWithoutCalculation_ThenLayerTwoAErrorTooltip()
{
// Given
using (var view = ShowFullyConfiguredFailureMechanismResultsView())
{
FailureMechanismSection section = CreateSimpleFailureMechanismSection();
var sectionResult = new GrassCoverErosionInwardsFailureMechanismSectionResult(section);
view.Data = new[]
{
sectionResult
};
var gridTester = new ControlTester("dataGridView");
var dataGridView = (DataGridView) gridTester.TheObject;
DataGridViewCell dataGridViewCell = dataGridView.Rows[0].Cells[assessmentLayerTwoAIndex];
// When
var formattedValue = dataGridViewCell.FormattedValue; // Need to do this to fire the CellFormatting event.
// Then
Assert.AreEqual("-", formattedValue);
Assert.AreEqual("Er moet een maatgevende berekening voor dit vak worden geselecteerd.", dataGridViewCell.ErrorText);
}
}
[Test]
public void GivenSectionResultAndCalculationNotCalculated_ThenLayerTwoAErrorTooltip()
{
// Given
using (var view = ShowFullyConfiguredFailureMechanismResultsView())
{
var calculation = new GrassCoverErosionInwardsCalculation();
FailureMechanismSection section = CreateSimpleFailureMechanismSection();
var sectionResult = new GrassCoverErosionInwardsFailureMechanismSectionResult(section)
{
Calculation = calculation
};
view.Data = new[]
{
sectionResult
};
var gridTester = new ControlTester("dataGridView");
var dataGridView = (DataGridView) gridTester.TheObject;
DataGridViewCell dataGridViewCell = dataGridView.Rows[0].Cells[assessmentLayerTwoAIndex];
// When
var formattedValue = dataGridViewCell.FormattedValue; // Need to do this to fire the CellFormatting event.
// Then
Assert.AreEqual("-", formattedValue);
Assert.AreEqual("De maatgevende berekening voor dit vak moet nog worden uitgevoerd.", dataGridViewCell.ErrorText);
}
}
[Test]
public void GivenSectionResultAndFailedCalculation_ThenLayerTwoAErrorTooltip()
{
// Given
using (var view = ShowFullyConfiguredFailureMechanismResultsView())
{
var probabilityAssessmentOutput = new ProbabilityAssessmentOutput(1.0, 1.0, double.NaN, 1.0, 1.0);
var calculation = new GrassCoverErosionInwardsCalculation
{
Output = new GrassCoverErosionInwardsOutput(1.0, false, probabilityAssessmentOutput, 0.0)
};
FailureMechanismSection section = CreateSimpleFailureMechanismSection();
var sectionResult = new GrassCoverErosionInwardsFailureMechanismSectionResult(section)
{
Calculation = calculation
};
view.Data = new[]
{
sectionResult
};
var gridTester = new ControlTester("dataGridView");
var dataGridView = (DataGridView) gridTester.TheObject;
DataGridViewCell dataGridViewCell = dataGridView.Rows[0].Cells[assessmentLayerTwoAIndex];
// When
var formattedValue = dataGridViewCell.FormattedValue; // Need to do this to fire the CellFormatting event.
// Then
Assert.AreEqual("-", formattedValue);
Assert.AreEqual("De maatgevende berekening voor dit vak moet een geldige uitkomst hebben.", dataGridViewCell.ErrorText);
}
}
[Test]
public void GivenSectionResultAndSuccessfulCalculation_ThenLayerTwoANoError()
{
// Given
using (var view = ShowFullyConfiguredFailureMechanismResultsView())
{
const double probability = 0.56789;
var probabilityAssessmentOutput = new ProbabilityAssessmentOutput(1.0, 1.0, probability, 1.0, 1.0);
var calculation = new GrassCoverErosionInwardsCalculation
{
Output = new GrassCoverErosionInwardsOutput(1.1, true, probabilityAssessmentOutput, 0.0)
};
FailureMechanismSection section = CreateSimpleFailureMechanismSection();
var sectionResult = new GrassCoverErosionInwardsFailureMechanismSectionResult(section)
{
Calculation = calculation
};
view.Data = new[]
{
sectionResult
};
var gridTester = new ControlTester("dataGridView");
var dataGridView = (DataGridView) gridTester.TheObject;
DataGridViewCell dataGridViewCell = dataGridView.Rows[0].Cells[assessmentLayerTwoAIndex];
// When
var formattedValue = dataGridViewCell.FormattedValue; // Need to do this to fire the CellFormatting event.
// Then
Assert.AreEqual(ProbabilityFormattingHelper.Format(probability), formattedValue);
Assert.IsEmpty(dataGridViewCell.ErrorText);
}
}
[Test]
public void GivenSectionResultAndSuccessfulCalculation_WhenChangingCalculationToFailed_ThenLayerTwoAHasError()
{
// Given
using (var view = ShowFullyConfiguredFailureMechanismResultsView())
{
const double probability = 0.56789;
var successfulCalculationOutput = new ProbabilityAssessmentOutput(1.0, 1.0, probability, 1.0, 1.0);
var successfulCalculation = new GrassCoverErosionInwardsCalculation
{
Output = new GrassCoverErosionInwardsOutput(1.1, true, successfulCalculationOutput, 0.0)
};
var failedCalculationOutput = new ProbabilityAssessmentOutput(1.0, 1.0, double.NaN, 1.0, 1.0);
var failedCalculation = new GrassCoverErosionInwardsCalculation
{
Output = new GrassCoverErosionInwardsOutput(1.1, true, failedCalculationOutput, 0.0)
};
FailureMechanismSection section = CreateSimpleFailureMechanismSection();
var sectionResult = new GrassCoverErosionInwardsFailureMechanismSectionResult(section)
{
Calculation = successfulCalculation
};
view.Data = new[]
{
sectionResult
};
var gridTester = new ControlTester("dataGridView");
var dataGridView = (DataGridView) gridTester.TheObject;
DataGridViewCell dataGridViewCell = dataGridView.Rows[0].Cells[assessmentLayerTwoAIndex];
// Precondition
var formattedValue = dataGridViewCell.FormattedValue; // Need to do this to fire the CellFormatting event.
Assert.AreEqual(ProbabilityFormattingHelper.Format(probability), formattedValue);
Assert.IsEmpty(dataGridViewCell.ErrorText);
// When
sectionResult.Calculation = failedCalculation;
formattedValue = dataGridViewCell.FormattedValue; // Need to do this to fire the CellFormatting event.
// Then
Assert.AreEqual("-", formattedValue);
Assert.AreEqual("De maatgevende berekening voor dit vak moet een geldige uitkomst hebben.", dataGridViewCell.ErrorText);
}
}
private static FailureMechanismSection CreateSimpleFailureMechanismSection()
{
var section = new FailureMechanismSection("A",
new[]
{
new Point2D(1, 2),
new Point2D(3, 4)
});
return section;
}
private GrassCoverErosionInwardsFailureMechanismResultView ShowFullyConfiguredFailureMechanismResultsView()
{
var failureMechanism = new GrassCoverErosionInwardsFailureMechanism();
failureMechanism.AddSection(new FailureMechanismSection("Section 1", new List
{
new Point2D(0.0, 0.0),
new Point2D(5.0, 0.0)
}));
failureMechanism.AddSection(new FailureMechanismSection("Section 2", new List
{
new Point2D(5.0, 0.0),
new Point2D(10.0, 0.0)
}));
var failureMechanismResultView = ShowFailureMechanismResultsView();
failureMechanismResultView.Data = failureMechanism.SectionResults;
failureMechanismResultView.FailureMechanism = failureMechanism;
return failureMechanismResultView;
}
private GrassCoverErosionInwardsFailureMechanismResultView ShowFailureMechanismResultsView()
{
var failureMechanismResultView = new GrassCoverErosionInwardsFailureMechanismResultView();
testForm.Controls.Add(failureMechanismResultView);
testForm.Show();
return failureMechanismResultView;
}
}
}